From 3ce1bccf02869aaa4f61e6326aded42389d2513c Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Tue, 24 Mar 2026 19:56:51 +0800 Subject: [PATCH] Impl: Read config and print --- includes/JuiceAgent/JuiceAgent.h | 19 -------- includes/JuiceAgent/JuiceAgent.hpp | 14 ++++++ src/libloader/common/libloader.cpp | 5 +- src/libloader/includes/config.hpp | 76 +++++++++++++++++++++++++++++- 4 files changed, 91 insertions(+), 23 deletions(-) delete mode 100644 includes/JuiceAgent/JuiceAgent.h create mode 100644 includes/JuiceAgent/JuiceAgent.hpp diff --git a/includes/JuiceAgent/JuiceAgent.h b/includes/JuiceAgent/JuiceAgent.h deleted file mode 100644 index 5d94747..0000000 --- a/includes/JuiceAgent/JuiceAgent.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef JUICEAGENT_H -#define JUICEAGENT_H - -#define INJECT_PATH_MAX 260 - -typedef struct _InjectionInfo { - char JuiceAgentAPIJarPath[INJECT_PATH_MAX]; - char JuiceAgentNativePath[INJECT_PATH_MAX]; - - char EntryJarPath[INJECT_PATH_MAX]; - char EntryClass[INJECT_PATH_MAX]; - char EntryMethod[INJECT_PATH_MAX]; - - char InjectionDir[INJECT_PATH_MAX]; -} InjectionInfoType; - -extern InjectionInfoType InjectionInfo; - -#endif // JUICEAGENT_H \ No newline at end of file diff --git a/includes/JuiceAgent/JuiceAgent.hpp b/includes/JuiceAgent/JuiceAgent.hpp new file mode 100644 index 0000000..690e685 --- /dev/null +++ b/includes/JuiceAgent/JuiceAgent.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include + +struct InjectionInfo { + std::string JuiceAgentAPIJarPath; + std::string JuiceAgentNativePath; + + std::string EntryJarPath; + std::string EntryClass; + std::string EntryMethod; + + std::string InjectionDir; +}; \ No newline at end of file diff --git a/src/libloader/common/libloader.cpp b/src/libloader/common/libloader.cpp index a9062d0..0346598 100644 --- a/src/libloader/common/libloader.cpp +++ b/src/libloader/common/libloader.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -7,9 +8,9 @@ namespace libloader config::Config cfg(runtime_dir); if (!cfg.is_valid()) { PLOGE << "Invalid config"; - return; } - + InjectionInfo info = cfg.get_injection_info(); + config::print_injection_info(info); } } // namespace libloader diff --git a/src/libloader/includes/config.hpp b/src/libloader/includes/config.hpp index 68fdf30..82d8e34 100644 --- a/src/libloader/includes/config.hpp +++ b/src/libloader/includes/config.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -9,13 +10,15 @@ namespace config { class Config { private: + std::string runtime_dir; toml::Value config{}; bool valid = false; public: - explicit Config(const char* c_runtime_dir) { + explicit Config(const char* c_runtime_dir) + : runtime_dir(c_runtime_dir) { std::filesystem::path path = std::filesystem::path(c_runtime_dir) / "config.toml"; - + std::ifstream ifs(path); if (!ifs.is_open()) { PLOGE << "Failed to open config file: " << path; @@ -37,8 +40,77 @@ namespace config } } + template + T get_or_default(const std::string& key, const T& default_value) const { + if (!valid) return default_value; + + const toml::Value* current = &config; + + size_t start = 0; + while (true) { + size_t dot = key.find('.', start); + std::string part = key.substr(start, dot - start); + + if (!current->is()) { + return default_value; + } + + auto it = current->as().find(part); + if (it == current->as().end()) { + return default_value; + } + + current = &it->second; + + if (dot == std::string::npos) break; + start = dot + 1; + } + + try { + return current->as(); + } catch (...) { + return default_value; + } + } + bool is_valid() const { return valid; } + + InjectionInfo get_injection_info() { + InjectionInfo info; + auto get_default_path = [&](const std::string& name) -> std::string { + return (std::filesystem::path(runtime_dir) / name).string(); + }; + + // [JuiceAgent] + // -- APIJarPath = "runtime_dir/JuiceAgent.jar" + info.JuiceAgentAPIJarPath = get_or_default("JuiceAgent.APIJarPath", get_default_path("JuiceAgent.jar")); + // -- JuiceAgentNativePath = "runtime_dir/libagent.dll/.so" + info.JuiceAgentNativePath = get_or_default("JuiceAgent.JuiceAgentNativePath", get_default_path("libagent.dll")); + + // [Entry] + // -- EntryJarPath = "runtime_dir/entry.jar" + info.EntryJarPath = get_or_default("Entry.EntryJarPath", get_default_path("entry.jar")); + // -- EntryClass = "com.example.Entry" + info.EntryClass = get_or_default("Entry.EntryClass", "com.example.Entry"); + // -- EntryMethod = "start" + info.EntryMethod = get_or_default("Entry.EntryMethod", "start"); + + // [Runtime] + // -- InjectionDir = "runtime_dir/injection" + info.InjectionDir = get_or_default("Runtime.InjectionDir", get_default_path("injection")); + + return info; + } }; + + void print_injection_info(const InjectionInfo& info) { + PLOGI << "JuiceAgent Injection Info:"; + PLOGI << " JuiceAgentNativePath: " << info.JuiceAgentNativePath; + PLOGI << " EntryJarPath: " << info.EntryJarPath; + PLOGI << " EntryClass: " << info.EntryClass; + PLOGI << " EntryMethod: " << info.EntryMethod; + PLOGI << " InjectionDir: " << info.InjectionDir; + } } \ No newline at end of file