feat: Refactor module management and introduce ModuleRegistry for dynamic module loading

This commit is contained in:
xiaozhou233 committed 2026-04-22 13:19:39 +08:00
1 parent 08ec414094
commit 50eb73d4e8
6 files changed
+170 -97

No files matched your search

+94 -64
View File
@@ -1,87 +1,117 @@
#include <modules/Modules.hpp>
#include <modules/ModuleRegistry.hpp>
#include <JuiceAgent/Logger.hpp>
#include <libagent.hpp>
#include <JuiceAgent/Utils.hpp>
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<bool>("JuiceAgent.Modules.JarLoader.Enabled", false);
Config.InjectionDir = cfg.get<std::string>("JuiceAgent.Modules.JarLoader.InjectionDir", (agent.get_config().runtime_dir() / "injection").string(), true);
Config.JarPath = cfg.get<std::string>("JuiceAgent.Modules.JarLoader.JarPath", "./Entry.jar", true);
Config.EntryClass = cfg.get<std::string>("JuiceAgent.Modules.JarLoader.EntryClass", "Example.Main", false);
Config.EntryMethod = cfg.get<std::string>("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<bool>(
"JuiceAgent.Modules.JarLoader.Enabled",
false
);
config.injection_dir = cfg.get<std::string>(
"JuiceAgent.Modules.JarLoader.InjectionDir",
(cfg.runtime_dir() / "injection").string(),
true
);
config.jar_path = cfg.get<std::string>(
"JuiceAgent.Modules.JarLoader.JarPath",
"./Entry.jar",
true
);
config.entry_class = cfg.get<std::string>(
"JuiceAgent.Modules.JarLoader.EntryClass",
"Example.Main",
false
);
config.entry_method = cfg.get<std::string>(
"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;
}
}
JUICEAGENT_REGISTER_MODULE(
JuiceAgent::Core::Modules::JarLoaderModule,
JarLoader
)