diff --git a/src/libloader/common/libloader.cpp b/src/libloader/common/libloader.cpp new file mode 100644 index 0000000..a9062d0 --- /dev/null +++ b/src/libloader/common/libloader.cpp @@ -0,0 +1,15 @@ +#include +#include + +namespace libloader +{ + void entrypoint(const char* runtime_dir) { + config::Config cfg(runtime_dir); + if (!cfg.is_valid()) { + PLOGE << "Invalid config"; + return; + } + + + } +} // namespace libloader diff --git a/src/libloader/includes/config.hpp b/src/libloader/includes/config.hpp index 0ac8f1a..68fdf30 100644 --- a/src/libloader/includes/config.hpp +++ b/src/libloader/includes/config.hpp @@ -1,86 +1,44 @@ -#include -#include -#include -#include +#pragma once + +#include #include -#include +#include +#include namespace config { class Config { private: - std::string config_path; - toml::Value config; + toml::Value config{}; + bool valid = false; public: - Config(const std::string& config_path) - : config_path(config_path) - { - PLOGD << "[Config] Loading config file: " << config_path; + explicit Config(const char* c_runtime_dir) { + std::filesystem::path path = std::filesystem::path(c_runtime_dir) / "config.toml"; - 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); + std::ifstream ifs(path); + if (!ifs.is_open()) { + PLOGE << "Failed to open config file: " << path; + return; } - toml::ParseResult pr = toml::parse(file); + try { + auto pr = toml::parse(ifs); - if (!pr.valid()) { - PLOGE << "[Config] Failed to parse config file: " << config_path - << " | Reason: " << pr.errorReason; + if (!pr.valid()) { + PLOGE << "Invalid TOML file: " << pr.errorReason; + return; + } - throw std::runtime_error( - "Failed to parse config file: " + config_path + "\n" + pr.errorReason - ); + config = pr.value; + 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 - 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; + bool is_valid() const { + return valid; } }; } \ No newline at end of file