Impl: Read config and print

This commit is contained in:
xiaozhou233 committed 2026-03-24 19:56:51 +08:00
1 parent 205e608d60
commit 3ce1bccf02
4 files changed
+91 -23

No files matched your search

-19
View File
@@ -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
+14
View File
@@ -0,0 +1,14 @@
#pragma once
#include <string>
struct InjectionInfo {
std::string JuiceAgentAPIJarPath;
std::string JuiceAgentNativePath;
std::string EntryJarPath;
std::string EntryClass;
std::string EntryMethod;
std::string InjectionDir;
};
+3 -2
View File
@@ -1,3 +1,4 @@
#include <JuiceAgent/Logger.hpp>
#include <libloader.hpp>
#include <config.hpp>
@@ -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
+74 -2
View File
@@ -1,6 +1,7 @@
#pragma once
#include <JuiceAgent/Logger.hpp>
#include <JuiceAgent/JuiceAgent.hpp>
#include <tinytoml/toml.h>
#include <filesystem>
#include <fstream>
@@ -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<typename T>
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<toml::Table>()) {
return default_value;
}
auto it = current->as<toml::Table>().find(part);
if (it == current->as<toml::Table>().end()) {
return default_value;
}
current = &it->second;
if (dot == std::string::npos) break;
start = dot + 1;
}
try {
return current->as<T>();
} 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<std::string>("JuiceAgent.APIJarPath", get_default_path("JuiceAgent.jar"));
// -- JuiceAgentNativePath = "runtime_dir/libagent.dll/.so"
info.JuiceAgentNativePath = get_or_default<std::string>("JuiceAgent.JuiceAgentNativePath", get_default_path("libagent.dll"));
// [Entry]
// -- EntryJarPath = "runtime_dir/entry.jar"
info.EntryJarPath = get_or_default<std::string>("Entry.EntryJarPath", get_default_path("entry.jar"));
// -- EntryClass = "com.example.Entry"
info.EntryClass = get_or_default<std::string>("Entry.EntryClass", "com.example.Entry");
// -- EntryMethod = "start"
info.EntryMethod = get_or_default<std::string>("Entry.EntryMethod", "start");
// [Runtime]
// -- InjectionDir = "runtime_dir/injection"
info.InjectionDir = get_or_default<std::string>("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;
}
}