diff --git a/src/libagent/includes/modules/ModuleManager.hpp b/src/libagent/includes/modules/ModuleManager.hpp index ecc5b54..2f33b77 100644 --- a/src/libagent/includes/modules/ModuleManager.hpp +++ b/src/libagent/includes/modules/ModuleManager.hpp @@ -12,7 +12,7 @@ public: ModuleManager() = default; ~ModuleManager() = default; - void register_module(std::unique_ptr module); + void load_all(); bool init_all(); bool start_all(); diff --git a/src/libagent/includes/modules/ModuleRegistry.hpp b/src/libagent/includes/modules/ModuleRegistry.hpp new file mode 100644 index 0000000..a75881a --- /dev/null +++ b/src/libagent/includes/modules/ModuleRegistry.hpp @@ -0,0 +1,56 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace JuiceAgent::Core::Modules { + +class ModuleRegistry { +public: + using Factory = std::function()>; + + struct Entry { + std::string name; + Factory factory; + }; + + static ModuleRegistry& instance() { + static ModuleRegistry inst; + return inst; + } + + bool add(const std::string& name, Factory factory) { + for (const auto& item : _entries) { + if (item.name == name) { + return false; + } + } + + _entries.push_back({name, std::move(factory)}); + return true; + } + + const std::vector& entries() const { + return _entries; + } + +private: + std::vector _entries; +}; + +} + +#define JUICEAGENT_REGISTER_MODULE(TYPE, NAME) \ +namespace { \ + const bool registered_##NAME = \ + JuiceAgent::Core::Modules::ModuleRegistry::instance().add( \ + #NAME, \ + []() -> std::unique_ptr { \ + return std::make_unique(); \ + } \ + ); \ +} + diff --git a/src/libagent/includes/modules/Modules.hpp b/src/libagent/includes/modules/Modules.hpp deleted file mode 100644 index f69f79e..0000000 --- a/src/libagent/includes/modules/Modules.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -#include - -namespace JuiceAgent::Core::Modules { - -class JarLoaderModule : public IModule { -public: - JarLoaderModule() = default; - ~JarLoaderModule() override = default; - - std::string name() const override { - return "JarLoader"; - } - - bool init() override; - bool start() override; - void stop() override; - -private: - bool _initialized = false; - bool _running = false; -}; - -} \ No newline at end of file diff --git a/src/libagent/libagent.cpp b/src/libagent/libagent.cpp index afd6d0b..5a53c7e 100644 --- a/src/libagent/libagent.cpp +++ b/src/libagent/libagent.cpp @@ -4,7 +4,6 @@ #include #include #include -#include namespace JuiceAgent { // ===== Singleton ===== @@ -42,8 +41,9 @@ namespace JuiceAgent { set_config(cfg); PLOGD << "env: " << env << ", jvmti: " << jvmti; - // Register Modules - module_manager.register_module(std::make_unique()); + // Load and Initialize Modules + module_manager.load_all(); + module_manager.init_all(); // Initialize Modules module_manager.init_all(); diff --git a/src/libagent/modules/JarLoader.cpp b/src/libagent/modules/JarLoader.cpp index 309e68a..fd8b35e 100644 --- a/src/libagent/modules/JarLoader.cpp +++ b/src/libagent/modules/JarLoader.cpp @@ -1,87 +1,117 @@ -#include +#include #include #include #include namespace JuiceAgent::Core::Modules { -struct _Config { - bool Enabled = true; - std::string InjectionDir; - std::string JarPath; - std::string EntryClass; - std::string EntryMethod; -} Config; +struct JarLoaderConfig { + bool enabled = true; + std::string injection_dir; + std::string jar_path; + std::string entry_class; + std::string entry_method; +}; +static JarLoaderConfig config; static JuiceAgent::Agent& agent = JuiceAgent::Agent::instance(); static const char* MODULE_CLASS = "cn/xiaozhou233/juiceagent/api/modules/JarLoader"; static const char* MODULE_METHOD = "loadJar"; -bool JarLoaderModule::init() { - if (_initialized) return true; - - PLOGI << "JarLoader init"; - - JuiceAgent::Config::Config& cfg = agent.get_config(); - - Config.Enabled = cfg.get("JuiceAgent.Modules.JarLoader.Enabled", false); - Config.InjectionDir = cfg.get("JuiceAgent.Modules.JarLoader.InjectionDir", (agent.get_config().runtime_dir() / "injection").string(), true); - Config.JarPath = cfg.get("JuiceAgent.Modules.JarLoader.JarPath", "./Entry.jar", true); - Config.EntryClass = cfg.get("JuiceAgent.Modules.JarLoader.EntryClass", "Example.Main", false); - Config.EntryMethod = cfg.get("JuiceAgent.Modules.JarLoader.EntryMethod", "start", false); - - PLOGD << ">>> JarLoader Config <<<"; - PLOGD << "Enabled: " << Config.Enabled; - PLOGD << "InjectionDir: " << Config.InjectionDir; - PLOGD << "JarPath: " << Config.JarPath; - PLOGD << "EntryClass: " << Config.EntryClass; - PLOGD << "EntryMethod: " << Config.EntryMethod; - PLOGD << ">>> JarLoader Config <<<"; - _initialized = true; - return true; -} - -bool JarLoaderModule::start() { - if (!_initialized) return false; - if (_running) return true; - - PLOGI << "JarLoader start"; - - if (!Config.Enabled) { - PLOGI << "JarLoader is disabled"; - return false; +class JarLoaderModule : public IModule { +public: + std::string name() const override { + return "JarLoader"; } - if (Config.InjectionDir.empty() || Config.JarPath.empty() || Config.EntryClass.empty() || Config.EntryMethod.empty()) { - PLOGE << "JarLoader config is invalid"; - return false; + bool init() override { + if (_initialized) { + return true; + } + + auto& cfg = agent.get_config(); + + config.enabled = cfg.get( + "JuiceAgent.Modules.JarLoader.Enabled", + false + ); + + config.injection_dir = cfg.get( + "JuiceAgent.Modules.JarLoader.InjectionDir", + (cfg.runtime_dir() / "injection").string(), + true + ); + + config.jar_path = cfg.get( + "JuiceAgent.Modules.JarLoader.JarPath", + "./Entry.jar", + true + ); + + config.entry_class = cfg.get( + "JuiceAgent.Modules.JarLoader.EntryClass", + "Example.Main", + false + ); + + config.entry_method = cfg.get( + "JuiceAgent.Modules.JarLoader.EntryMethod", + "start", + false + ); + + PLOGI << "JarLoader init"; + _initialized = true; + return true; } - JuiceAgent::Utils::Serializer ser; + bool start() override { + if (!_initialized) { + return false; + } - ser.add_kv("InjectionDir", Config.InjectionDir); - ser.add_kv("JarPath", Config.JarPath); - ser.add_kv("EntryClass", Config.EntryClass); - ser.add_kv("EntryMethod", Config.EntryMethod); + if (_running || !config.enabled) { + return true; + } - std::string serialized = ser.serialize(); + JuiceAgent::Utils::Serializer ser; + ser.add_kv("InjectionDir", config.injection_dir); + ser.add_kv("JarPath", config.jar_path); + ser.add_kv("EntryClass", config.entry_class); + ser.add_kv("EntryMethod", config.entry_method); - PLOGD << "Serialized config: " << serialized; + auto data = ser.serialize(); - JNIEnv* env = agent.get_env(); - JuiceAgent::Utils::call_java_impl(env, MODULE_CLASS, MODULE_METHOD, serialized.c_str()); + JuiceAgent::Utils::call_java_impl( + agent.get_env(), + MODULE_CLASS, + MODULE_METHOD, + data.c_str() + ); + + PLOGI << "JarLoader start"; + _running = true; + return true; + } + + void stop() override { + if (!_running) { + return; + } + + PLOGI << "JarLoader stop"; + _running = false; + } + +private: + bool _initialized = false; + bool _running = false; +}; - PLOGI << "JarLoader is running"; - _running = true; - return true; } -void JarLoaderModule::stop() { - if (!_running) return; - - PLOGI << "JarLoader stop"; - _running = false; -} - -} \ No newline at end of file +JUICEAGENT_REGISTER_MODULE( + JuiceAgent::Core::Modules::JarLoaderModule, + JarLoader +) \ No newline at end of file diff --git a/src/libagent/modules/ModuleManager.cpp b/src/libagent/modules/ModuleManager.cpp index 75490fd..f60585c 100644 --- a/src/libagent/modules/ModuleManager.cpp +++ b/src/libagent/modules/ModuleManager.cpp @@ -1,13 +1,21 @@ #include +#include #include namespace JuiceAgent::Core::Modules { -void ModuleManager::register_module(std::unique_ptr module) { - if (!module) return; +void ModuleManager::load_all() { + for (const auto& entry : ModuleRegistry::instance().entries()) { + auto module = entry.factory(); - PLOGI << "Register module: " << module->name(); - _modules.push_back(std::move(module)); + if (!module) { + PLOGE << "Failed to create module: " << entry.name; + continue; + } + + PLOGI << "Register module: " << module->name(); + _modules.push_back(std::move(module)); + } } bool ModuleManager::init_all() { @@ -19,6 +27,7 @@ bool ModuleManager::init_all() { return false; } } + return true; } @@ -31,11 +40,13 @@ bool ModuleManager::start_all() { return false; } } + return true; } void ModuleManager::stop_all() { for (auto it = _modules.rbegin(); it != _modules.rend(); ++it) { + PLOGI << "Stop module: " << (*it)->name(); (*it)->stop(); } } @@ -46,6 +57,7 @@ IModule* ModuleManager::find_module(const std::string& name) { return module.get(); } } + return nullptr; }