From 9dab75b5fd5a968381c7448cd923cb3f2e911cf7 Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Tue, 24 Mar 2026 13:53:54 +0800 Subject: [PATCH] [Refactor] Config System --- src/libloader/includes/config.hpp | 86 +++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/libloader/includes/config.hpp diff --git a/src/libloader/includes/config.hpp b/src/libloader/includes/config.hpp new file mode 100644 index 0000000..0ac8f1a --- /dev/null +++ b/src/libloader/includes/config.hpp @@ -0,0 +1,86 @@ +#include +#include +#include +#include +#include +#include + +namespace config +{ + class Config { + private: + std::string config_path; + toml::Value config; + + public: + Config(const std::string& config_path) + : config_path(config_path) + { + PLOGD << "[Config] Loading config file: " << config_path; + + std::ifstream file(config_path); + if (!file.is_open()) { + PLOGE << "[Config] Failed to open config file: " << config_path; + throw std::runtime_error("Failed to open config file: " + config_path); + } + + toml::ParseResult pr = toml::parse(file); + + if (!pr.valid()) { + PLOGE << "[Config] Failed to parse config file: " << config_path + << " | Reason: " << pr.errorReason; + + throw std::runtime_error( + "Failed to parse config file: " + config_path + "\n" + pr.errorReason + ); + } + + config = pr.value; + + PLOGI << "[Config] Successfully loaded config file: " << config_path; + } + + template + T get_or_default(const std::string& key, const T& default_value) const + { + const toml::Value* value = config.find(key); + + if (!value) { + PLOGW << "[Config] Key not found: " << key + << " | Using default value"; + return default_value; + } + + if (!value->is()) { + 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(); + } + + std::string get_path_or_default(const std::string& key, const std::string& def) const + { + std::string result = get_or_default(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; + } + }; +} \ No newline at end of file