diff --git a/Todo.md b/Todo.md index 9f1dba3..5011520 100644 --- a/Todo.md +++ b/Todo.md @@ -10,10 +10,10 @@ - [ ] 重构 `libloader` 的配置系统 ## 2026-04-17 - - [ ] 重构项目为模块化项目 + - [x] 重构项目为模块化项目 - [x] `libloader` 模块只负责加载`JuiceAgent-API`和`JuiceAgent`, Jar文件由`JuiceAgent`接管 - [x] `libloader` 读取配置文件并传参`JuiceAgentNativePath`和`ConfigDir`到 `JuiceAgent` 中,由`JuiceAgent`读取配置文件继续进行操作 ## 2026-04-19 - - [ ] 在 `JuiceAgent/Utils.hpp`做序列化操作 + - [x] 在 `JuiceAgent/Utils.hpp`做序列化操作 - [ ] 统一调用 module的Java实现 \ No newline at end of file diff --git a/includes/JuiceAgent/Config.hpp b/includes/JuiceAgent/Config.hpp index 983db37..28710ad 100644 --- a/includes/JuiceAgent/Config.hpp +++ b/includes/JuiceAgent/Config.hpp @@ -5,6 +5,7 @@ #include #include #include +#include namespace JuiceAgent::Config { @@ -143,14 +144,20 @@ namespace Utils { } inline std::string serialize_loader_config(const LoaderConfig& config) { - std::string result = ""; + // std::string result = ""; - result += "Version=2;"; - result += "JuiceAgentAPIJarPath=" + config.JuiceAgentAPIJarPath + ";"; - result += "JuiceAgentNativeLibraryPath=" + config.JuiceAgentNativeLibraryPath + ";"; - result += "RuntimeDir=" + config.RuntimeDir + ";"; - - return result; + // result += "Version=2;"; + // result += "JuiceAgentAPIJarPath=" + config.JuiceAgentAPIJarPath + ";"; + // result += "JuiceAgentNativeLibraryPath=" + config.JuiceAgentNativeLibraryPath + ";"; + // result += "RuntimeDir=" + config.RuntimeDir + ";"; + + JuiceAgent::Utils::Serializer ser; + ser.add_kv("Version", 2); + ser.add_kv("JuiceAgentAPIJarPath", config.JuiceAgentAPIJarPath); + ser.add_kv("JuiceAgentNativeLibraryPath", config.JuiceAgentNativeLibraryPath); + ser.add_kv("RuntimeDir", config.RuntimeDir); + + return ser.serialize(); } } diff --git a/includes/JuiceAgent/Utils.hpp b/includes/JuiceAgent/Utils.hpp index 8b224c8..35811a3 100644 --- a/includes/JuiceAgent/Utils.hpp +++ b/includes/JuiceAgent/Utils.hpp @@ -3,9 +3,13 @@ #include #include #include +#include +#include +#include +#include // Check for JNI exceptions and clear them -bool check_and_clear_exception(JNIEnv* env, const char* context) { +inline bool check_and_clear_exception(JNIEnv* env, const char* context) { if (env->ExceptionCheck()) { PLOGE << "JNI Exception occurred at: " << context; env->ExceptionDescribe(); @@ -13,4 +17,207 @@ bool check_and_clear_exception(JNIEnv* env, const char* context) { return true; } return false; -} \ No newline at end of file +} + +namespace JuiceAgent::Utils +{ + class Serializer { + private: + std::string s; + + private: + static std::string escape(const std::string& input) { + std::string out; + out.reserve(input.size()); + + for (char c : input) { + if (c == '=' || c == ';' || c == '\\') { + out += '\\'; + } + out += c; + } + + return out; + } + + public: + Serializer() = default; + + Serializer& add_kv(const std::string& key, const std::string& value) { + s += escape(key); + s += '='; + s += escape(value); + s += ';'; + return *this; + } + + Serializer& add_kv(const std::string& key, const char* value) { + return add_kv(key, std::string(value)); + } + + Serializer& add_kv(const std::string& key, int value) { + return add_kv(key, std::to_string(value)); + } + + Serializer& add_kv(const std::string& key, bool value) { + return add_kv(key, value ? "true" : "false"); + } + + void clear() { + s.clear(); + } + + bool empty() const { + return s.empty(); + } + + const std::string& serialize() const { + return s; + } + }; + + + class Deserializer { + private: + std::unordered_map data; + + private: + static std::string unescape(const std::string& input) { + std::string out; + out.reserve(input.size()); + + bool escaped = false; + + for (char c : input) { + if (escaped) { + out += c; + escaped = false; + continue; + } + + if (c == '\\') { + escaped = true; + continue; + } + + out += c; + } + + return out; + } + + static bool to_bool(const std::string& v, bool def) { + if (v == "true" || v == "1") return true; + if (v == "false" || v == "0") return false; + return def; + } + + template + static T convert(const std::string& v, T def) { + if constexpr (std::is_same_v) { + return v; + } + else if constexpr (std::is_same_v) { + return to_bool(v, def); + } + else if constexpr (std::is_integral_v) { + try { + long long n = std::stoll(v); + return static_cast(n); + } catch (...) { + return def; + } + } + else if constexpr (std::is_floating_point_v) { + try { + long double n = std::stold(v); + return static_cast(n); + } catch (...) { + return def; + } + } + else { + return def; + } + } + + public: + Deserializer() = default; + + explicit Deserializer(const std::string& input) { + parse(input); + } + + void parse(const std::string& input) { + data.clear(); + + std::string key; + std::string value; + + bool reading_key = true; + bool escaped = false; + + for (char c : input) { + if (escaped) { + (reading_key ? key : value) += c; + escaped = false; + continue; + } + + if (c == '\\') { + escaped = true; + continue; + } + + if (reading_key && c == '=') { + reading_key = false; + continue; + } + + if (c == ';') { + if (!key.empty()) { + data[unescape(key)] = unescape(value); + } + + key.clear(); + value.clear(); + reading_key = true; + continue; + } + + (reading_key ? key : value) += c; + } + + if (!key.empty()) { + data[unescape(key)] = unescape(value); + } + } + + bool has(const std::string& key) const { + return data.find(key) != data.end(); + } + + template + T get(const std::string& key, T def) const { + auto it = data.find(key); + if (it == data.end()) { + return def; + } + + return convert(it->second, def); + } + + void clear() { + data.clear(); + } + + bool empty() const { + return data.empty(); + } + + std::size_t size() const { + return data.size(); + } + }; +} // namespace JuiceAgent::Utils +