Files
VoidLoaderCore/include/Agent.hpp
T
faumaray 29a095639f
VoidLoader/VoidLoaderCore/pipeline/head There was a failure building this commit
fixing mingw errors
Signed-off-by: Faumaray <faumaray@gmail.com>
2025-10-21 00:09:24 +03:00

82 lines
2.8 KiB
C++

#pragma once
#include <class_transformer.h>
#include <Logging/Logger.hpp>
#include <Mod/Context.hpp>
#include <Mods/Loader.hpp>
#include <atomic>
#include <filesystem>
#include <jvmti.h>
#include <jni.h>
#include <memory>
#include <mutex>
#include <string>
#include <unordered_map>
extern "C" {
JNIEXPORT jint JNICALL Java_nativeAgentInit(JNIEnv*, jclass, jobjectArray, jobject);
JNIEXPORT jint JNICALL Java_nativeAgentStart(JNIEnv*, jclass);
JNIEXPORT jint JNICALL Java_ru_faumaray_VoidLoaderAgent_nativeAgentStart(JNIEnv*, jclass);
JNIEXPORT jint JNICALL Java_ru_faumaray_VoidLoaderAgent_nativeAgentInit(JNIEnv*, jclass, jobject, jobjectArray);
JNIEXPORT void JNICALL Java_ru_faumaray_VoidLoaderAgent_nativeCleanup(JNIEnv*, jclass);
}
namespace vl::Agent {
class Agent {
public:
static void JNICALL vmInit(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread);
static void JNICALL vmDeath(jvmtiEnv* jvmti, JNIEnv* jni);
static Agent& getInstance(const std::unordered_map<std::string, std::string>& args) {
std::call_once(initFlag_, [&] {
instance_.reset(new Agent(args));
});
return *instance_;
}
// Accessor that just retrieves the existing instance
static Agent& getInstance() {
if (!instance_) {
throw std::runtime_error("Agent not initialized — call getInstance(args) first.");
}
return *instance_;
}
explicit Agent(std::unordered_map<std::string, std::string> args);
Agent(const Agent&) = delete;
Agent& operator=(const Agent&) = delete;
Agent(Agent&&) = delete;
Agent& operator=(Agent&&) = delete;
bool isValid() const noexcept
{
return m_isValid;
}
private:
std::unordered_map<std::string, std::filesystem::path> findMods(const std::string& dir);
friend jint ::Java_nativeAgentInit(JNIEnv*, jclass, jobjectArray, jobject);
friend jint ::Java_nativeAgentStart(JNIEnv*, jclass);
friend jint ::Java_ru_faumaray_VoidLoaderAgent_nativeAgentStart(JNIEnv*, jclass);
friend jint ::Java_ru_faumaray_VoidLoaderAgent_nativeAgentInit(JNIEnv*, jclass, jobject, jobjectArray);
friend void ::Java_ru_faumaray_VoidLoaderAgent_nativeCleanup(JNIEnv*, jclass);
friend class ClassTransformer;
friend class vl::mod::ModLoader;
private:
std::unordered_map<std::string, std::filesystem::path> m_mods;
jvmtiEnv* m_jmti = nullptr;
JavaVM* m_jvm = nullptr;
JNIEnv* m_jni = nullptr;
std::unique_ptr<vl::mod::ModLoader> m_modloader;
std::unique_ptr<ClassTransformer> m_transformer;
vl::mod::Context m_context;
std::atomic<bool> m_isValid{false};
std::shared_ptr<vl::logging::Logger> m_logger;
static std::unique_ptr<Agent> instance_;
static std::once_flag initFlag_;
};
inline std::unique_ptr<Agent> Agent::instance_ = nullptr;
inline std::once_flag Agent::initFlag_;
}