mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 13:05:53 +08:00
[Feat] Serializer
This commit is contained in:
1 parent
9a2dd217f1
commit
ccb332cb42
3 files changed
+225
-11
No files matched your search
@@ -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实现
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <toml.hpp>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include <JuiceAgent/Utils.hpp>
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,9 +3,13 @@
|
||||
#include <jni.h>
|
||||
#include <jvmti.h>
|
||||
#include <JuiceAgent/Logger.hpp>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <type_traits>
|
||||
#include <cstdlib>
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
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<std::string, std::string> 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<typename T>
|
||||
static T convert(const std::string& v, T def) {
|
||||
if constexpr (std::is_same_v<T, std::string>) {
|
||||
return v;
|
||||
}
|
||||
else if constexpr (std::is_same_v<T, bool>) {
|
||||
return to_bool(v, def);
|
||||
}
|
||||
else if constexpr (std::is_integral_v<T>) {
|
||||
try {
|
||||
long long n = std::stoll(v);
|
||||
return static_cast<T>(n);
|
||||
} catch (...) {
|
||||
return def;
|
||||
}
|
||||
}
|
||||
else if constexpr (std::is_floating_point_v<T>) {
|
||||
try {
|
||||
long double n = std::stold(v);
|
||||
return static_cast<T>(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<typename T>
|
||||
T get(const std::string& key, T def) const {
|
||||
auto it = data.find(key);
|
||||
if (it == data.end()) {
|
||||
return def;
|
||||
}
|
||||
|
||||
return convert<T>(it->second, def);
|
||||
}
|
||||
|
||||
void clear() {
|
||||
data.clear();
|
||||
}
|
||||
|
||||
bool empty() const {
|
||||
return data.empty();
|
||||
}
|
||||
|
||||
std::size_t size() const {
|
||||
return data.size();
|
||||
}
|
||||
};
|
||||
} // namespace JuiceAgent::Utils
|
||||
|
||||
Reference in new issue
Block a user