mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 21:15:56 +08:00
Refactor: Config System
This commit is contained in:
1 parent
9dab75b5fd
commit
205e608d60
2 files changed
+40
-67
No files matched your search
@@ -0,0 +1,15 @@
|
|||||||
|
#include <libloader.hpp>
|
||||||
|
#include <config.hpp>
|
||||||
|
|
||||||
|
namespace libloader
|
||||||
|
{
|
||||||
|
void entrypoint(const char* runtime_dir) {
|
||||||
|
config::Config cfg(runtime_dir);
|
||||||
|
if (!cfg.is_valid()) {
|
||||||
|
PLOGE << "Invalid config";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
} // namespace libloader
|
||||||
@@ -1,86 +1,44 @@
|
|||||||
#include <string>
|
#pragma once
|
||||||
#include <fstream>
|
|
||||||
#include <stdexcept>
|
#include <JuiceAgent/Logger.hpp>
|
||||||
#include <filesystem>
|
|
||||||
#include <tinytoml/toml.h>
|
#include <tinytoml/toml.h>
|
||||||
#include <plog/Log.h>
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
namespace config
|
namespace config
|
||||||
{
|
{
|
||||||
class Config {
|
class Config {
|
||||||
private:
|
private:
|
||||||
std::string config_path;
|
toml::Value config{};
|
||||||
toml::Value config;
|
bool valid = false;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Config(const std::string& config_path)
|
explicit Config(const char* c_runtime_dir) {
|
||||||
: config_path(config_path)
|
std::filesystem::path path = std::filesystem::path(c_runtime_dir) / "config.toml";
|
||||||
{
|
|
||||||
PLOGD << "[Config] Loading config file: " << config_path;
|
|
||||||
|
|
||||||
std::ifstream file(config_path);
|
std::ifstream ifs(path);
|
||||||
if (!file.is_open()) {
|
if (!ifs.is_open()) {
|
||||||
PLOGE << "[Config] Failed to open config file: " << config_path;
|
PLOGE << "Failed to open config file: " << path;
|
||||||
throw std::runtime_error("Failed to open config file: " + config_path);
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
toml::ParseResult pr = toml::parse(file);
|
try {
|
||||||
|
auto pr = toml::parse(ifs);
|
||||||
|
|
||||||
if (!pr.valid()) {
|
if (!pr.valid()) {
|
||||||
PLOGE << "[Config] Failed to parse config file: " << config_path
|
PLOGE << "Invalid TOML file: " << pr.errorReason;
|
||||||
<< " | Reason: " << pr.errorReason;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
throw std::runtime_error(
|
config = pr.value;
|
||||||
"Failed to parse config file: " + config_path + "\n" + pr.errorReason
|
valid = true;
|
||||||
);
|
} catch (const std::exception& e) {
|
||||||
|
PLOGE << "TOML parse error: " << e.what();
|
||||||
}
|
}
|
||||||
|
|
||||||
config = pr.value;
|
|
||||||
|
|
||||||
PLOGI << "[Config] Successfully loaded config file: " << config_path;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
bool is_valid() const {
|
||||||
T get_or_default(const std::string& key, const T& default_value) const
|
return valid;
|
||||||
{
|
|
||||||
const toml::Value* value = config.find(key);
|
|
||||||
|
|
||||||
if (!value) {
|
|
||||||
PLOGW << "[Config] Key not found: " << key
|
|
||||||
<< " | Using default value";
|
|
||||||
return default_value;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!value->is<T>()) {
|
|
||||||
PLOGE << "[Config] Type mismatch for key: " << key
|
|
||||||
<< " | Expected type does not match | Using default value";
|
|
||||||
return default_value;
|
|
||||||
}
|
|
||||||
|
|
||||||
PLOGD << "[Config] Read key: " << key;
|
|
||||||
|
|
||||||
return value->as<T>();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string get_path_or_default(const std::string& key, const std::string& def) const
|
|
||||||
{
|
|
||||||
std::string result = get_or_default<std::string>(key, def);
|
|
||||||
|
|
||||||
std::filesystem::path p(result);
|
|
||||||
|
|
||||||
if (p.is_relative()) {
|
|
||||||
std::filesystem::path full =
|
|
||||||
std::filesystem::path(config_path).parent_path() / p;
|
|
||||||
|
|
||||||
PLOGD << "[Config] Resolved relative path: " << result
|
|
||||||
<< " -> " << full.string();
|
|
||||||
|
|
||||||
return full.string();
|
|
||||||
}
|
|
||||||
|
|
||||||
PLOGD << "[Config] Using absolute path: " << result;
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user