#pragma once #include #include #include #include #include #include #include #include #include #include #include #include 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& 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 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 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 m_mods; jvmtiEnv* m_jmti = nullptr; JavaVM* m_jvm = nullptr; JNIEnv* m_jni = nullptr; std::unique_ptr m_modloader; std::unique_ptr m_transformer; vl::mod::Context m_context; std::atomic m_isValid{false}; std::shared_ptr m_logger; static std::unique_ptr instance_; static std::once_flag initFlag_; }; inline std::unique_ptr Agent::instance_ = nullptr; inline std::once_flag Agent::initFlag_; }