82 lines
3.2 KiB
C++
82 lines
3.2 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* env, jclass clazz, jobjectArray args, jobject instrumentation);
|
|
extern "C" JNIEXPORT jint JNICALL Java_nativeAgentStart(JNIEnv* env, jclass clazz);
|
|
extern "C" JNIEXPORT jint JNICALL Java_ru_faumaray_VoidLoaderAgent_nativeAgentStart(JNIEnv* env, jclass clazz);
|
|
extern "C" JNIEXPORT jint JNICALL Java_ru_faumaray_VoidLoaderAgent_nativeAgentInit(JNIEnv* env, jclass jclass, jobject instrumentation, jobjectArray args);
|
|
|
|
extern "C" JNIEXPORT void JNICALL Java_ru_faumaray_VoidLoaderAgent_nativeCleanup(JNIEnv* env, jclass clazz);
|
|
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 int JNICALL ::Java_nativeAgentInit(JNIEnv* env, jclass clazz, jobjectArray args, jobject instrumentation);
|
|
friend int JNICALL ::Java_nativeAgentInit(JNIEnv* env, jclass clazz, jobjectArray args, jobject instrumentation);
|
|
friend int JNICALL ::Java_nativeAgentStart(JNIEnv* env, jclass clazz);
|
|
friend int JNICALL ::Java_ru_faumaray_VoidLoaderAgent_nativeAgentStart(JNIEnv* env, jclass clazz);
|
|
friend int JNICALL ::Java_ru_faumaray_VoidLoaderAgent_nativeAgentInit(JNIEnv* env, jclass jclass, jobject instrumentation, jobjectArray args);
|
|
friend void JNICALL ::Java_ru_faumaray_VoidLoaderAgent_nativeCleanup(JNIEnv* env, jclass clazz);
|
|
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;
|
|
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_;
|
|
} |