From 9a2dd217f1e75f46042f2126c48d373e37578909 Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Sun, 19 Apr 2026 22:26:51 +0800 Subject: [PATCH] [Refactor] Update configuration handling in JarLoader and enhance Config class --- Todo.md | 8 +++- includes/JuiceAgent/Config.hpp | 30 +++++++++++++- src/libagent/includes/libagent.hpp | 3 ++ src/libagent/libagent.cpp | 1 + src/libagent/modules/JarLoader.cpp | 64 ++++++++++++++++++++++++++++++ 5 files changed, 102 insertions(+), 4 deletions(-) diff --git a/Todo.md b/Todo.md index 91a08a2..9f1dba3 100644 --- a/Todo.md +++ b/Todo.md @@ -11,5 +11,9 @@ ## 2026-04-17 - [ ] 重构项目为模块化项目 - - [ ] `libloader` 模块只负责加载`JuiceAgent-API`和`JuiceAgent`, Jar文件由`JuiceAgent`接管 - - [ ] `libloader` 读取配置文件并传参`JuiceAgentNativePath`和`ConfigDir`到 `JuiceAgent` 中,由`JuiceAgent`读取配置文件继续进行操作 \ No newline at end of file + - [x] `libloader` 模块只负责加载`JuiceAgent-API`和`JuiceAgent`, Jar文件由`JuiceAgent`接管 + - [x] `libloader` 读取配置文件并传参`JuiceAgentNativePath`和`ConfigDir`到 `JuiceAgent` 中,由`JuiceAgent`读取配置文件继续进行操作 + +## 2026-04-19 + - [ ] 在 `JuiceAgent/Utils.hpp`做序列化操作 + - [ ] 统一调用 module的Java实现 \ No newline at end of file diff --git a/includes/JuiceAgent/Config.hpp b/includes/JuiceAgent/Config.hpp index ab3be60..983db37 100644 --- a/includes/JuiceAgent/Config.hpp +++ b/includes/JuiceAgent/Config.hpp @@ -64,7 +64,7 @@ public: } template - T get(const std::string& path, const T& default_value) const { + T get(const std::string& path, const T& default_value, bool is_path = false) const { if (!_valid) { return default_value; } @@ -89,7 +89,33 @@ public: start = end + 1; } - return _read_value(*current); + T value = _read_value(*current); + + if constexpr (std::is_same_v) { + if (value.empty()) { + return default_value; + } + + // Handle relative paths + if (is_path) { + // Check if starts with "." + if (value.starts_with(".")) { + // Remove "." + value = value.substr(1); + // Check if starts with "/" + if (value.starts_with("/")) { + // Remove "/" + value = value.substr(1); + } + + // runtime_dir + value + value = (_runtime_dir / value).string(); + } + } + + } + + return value; } catch (const std::exception& e) { PLOGW << "Config get failed: " << path << ", using default value"; diff --git a/src/libagent/includes/libagent.hpp b/src/libagent/includes/libagent.hpp index 302afb5..3b26ca7 100644 --- a/src/libagent/includes/libagent.hpp +++ b/src/libagent/includes/libagent.hpp @@ -12,6 +12,7 @@ namespace JuiceAgent { private: JavaVM* jvm; jvmtiEnv* jvmti; + JNIEnv* env; EventBus eventbus; JuiceAgent::Config::Config config; JuiceAgent::Core::Modules::ModuleManager module_manager; @@ -33,12 +34,14 @@ namespace JuiceAgent { // getters JavaVM* get_jvm() const { return jvm; } jvmtiEnv* get_jvmti() const { return jvmti; } + JNIEnv* get_env() const { return env; } EventBus& get_eventbus() { return eventbus; } JuiceAgent::Config::Config& get_config() { return config; } // setters void set_jvm(JavaVM* jvm) { this->jvm = jvm; } void set_jvmti(jvmtiEnv* jvmti) { this->jvmti = jvmti; } + void set_env(JNIEnv* env) { this->env = env; } void set_config(JuiceAgent::Config::Config& config) { this->config = config; } }; } \ No newline at end of file diff --git a/src/libagent/libagent.cpp b/src/libagent/libagent.cpp index 4673ae4..afd6d0b 100644 --- a/src/libagent/libagent.cpp +++ b/src/libagent/libagent.cpp @@ -38,6 +38,7 @@ namespace JuiceAgent { set_jvm(jvm); set_jvmti(jvmti); + set_env(env); set_config(cfg); PLOGD << "env: " << env << ", jvmti: " << jvmti; diff --git a/src/libagent/modules/JarLoader.cpp b/src/libagent/modules/JarLoader.cpp index d4e6af7..63a940a 100644 --- a/src/libagent/modules/JarLoader.cpp +++ b/src/libagent/modules/JarLoader.cpp @@ -1,12 +1,44 @@ #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; + +static JuiceAgent::Agent& agent = JuiceAgent::Agent::instance(); + +static const char* MODULE_CLASS = "cn/xiaozhou233/juiceagent/api/modules/JarLoader"; +static const char* MODULE_METHOD = "loadJar"; +static const char* MODULE_METHOD_DESC = "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"; + 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()); + 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; } @@ -16,6 +48,38 @@ bool JarLoaderModule::start() { if (_running) return true; PLOGI << "JarLoader start"; + + if (!Config.Enabled) { + PLOGI << "JarLoader is disabled"; + return false; + } + + if (Config.InjectionDir.empty() || Config.JarPath.empty() || Config.EntryClass.empty() || Config.EntryMethod.empty()) { + PLOGE << "JarLoader config is invalid"; + return false; + } + + JNIEnv* env = agent.get_env(); + jclass cls = env->FindClass(MODULE_CLASS); + if (!cls) { + PLOGE << "JarLoader class not found"; + return false; + } + + jmethodID mid = env->GetStaticMethodID(cls, MODULE_METHOD, MODULE_METHOD_DESC); + if (!mid) { + PLOGE << "JarLoader method not found"; + return false; + } + + env->CallStaticVoidMethod(cls, mid, + env->NewStringUTF(Config.InjectionDir.c_str()), + env->NewStringUTF(Config.JarPath.c_str()), + env->NewStringUTF(Config.EntryClass.c_str()), + env->NewStringUTF(Config.EntryMethod.c_str())); + check_and_clear_exception(env, "Invoke JarLoader"); + + PLOGI << "JarLoader is running"; _running = true; return true; }