#pragma once #include #include #include #include #include namespace config { class Config { private: std::string runtime_dir; toml::Value config{}; bool valid = false; public: 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; return; } try { auto pr = toml::parse(ifs); if (!pr.valid()) { PLOGE << "Invalid TOML file: " << pr.errorReason; return; } config = pr.value; valid = true; } catch (const std::exception& e) { PLOGE << "TOML parse error: " << e.what(); } } 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; } }