179 lines
3.7 KiB
C++
179 lines
3.7 KiB
C++
#include "ConfigManager.hpp"
|
|
#include "echoion/core/ConfigLoader.hpp"
|
|
#include <format>
|
|
#include <iostream>
|
|
|
|
namespace Echoion::Api
|
|
{
|
|
|
|
ConfigManager& ConfigManager::instance()
|
|
{
|
|
static ConfigManager instance;
|
|
return instance;
|
|
}
|
|
|
|
bool ConfigManager::initialize(const std::filesystem::path& configPath)
|
|
{
|
|
std::unique_lock lock(mutex_);
|
|
configPath_ = configPath;
|
|
bool success = loadFromFileInternal();
|
|
initialized_.store(success);
|
|
return success;
|
|
}
|
|
|
|
bool ConfigManager::initialize()
|
|
{
|
|
return initialize(getDefaultConfigPath());
|
|
}
|
|
|
|
AppConfig ConfigManager::getConfig() const
|
|
{
|
|
std::shared_lock lock(mutex_);
|
|
return config_;
|
|
}
|
|
|
|
const BotConfig& ConfigManager::getBot() const
|
|
{
|
|
std::shared_lock lock(mutex_);
|
|
return config_.bot;
|
|
}
|
|
|
|
const YouTubeConfig& ConfigManager::getYouTube() const
|
|
{
|
|
std::shared_lock lock(mutex_);
|
|
return config_.youtube;
|
|
}
|
|
|
|
const NetworkConfig& ConfigManager::getNetwork() const
|
|
{
|
|
std::shared_lock lock(mutex_);
|
|
return config_.network;
|
|
}
|
|
|
|
const TeamSpeakConfig& ConfigManager::getTeamSpeak() const
|
|
{
|
|
std::shared_lock lock(mutex_);
|
|
return config_.teamspeak;
|
|
}
|
|
|
|
const SecurityConfig& ConfigManager::getSecurity() const
|
|
{
|
|
std::shared_lock lock(mutex_);
|
|
return config_.security;
|
|
}
|
|
|
|
const FeaturesConfig& ConfigManager::getFeatures() const
|
|
{
|
|
std::shared_lock lock(mutex_);
|
|
return config_.features;
|
|
}
|
|
|
|
const AdvancedConfig& ConfigManager::getAdvanced() const
|
|
{
|
|
std::shared_lock lock(mutex_);
|
|
return config_.advanced;
|
|
}
|
|
|
|
bool ConfigManager::reload()
|
|
{
|
|
std::unique_lock lock(mutex_);
|
|
return loadFromFileInternal();
|
|
}
|
|
|
|
bool ConfigManager::save()
|
|
{
|
|
std::shared_lock lock(mutex_);
|
|
return saveToFileInternal();
|
|
}
|
|
|
|
void ConfigManager::updateConfig(const AppConfig& config, bool saveToFile)
|
|
{
|
|
{
|
|
std::unique_lock lock(mutex_);
|
|
config_ = config;
|
|
}
|
|
|
|
if (saveToFile) {
|
|
save();
|
|
}
|
|
|
|
notifyCallbacks();
|
|
}
|
|
|
|
bool ConfigManager::isInitialized() const { return initialized_.load(); }
|
|
|
|
std::filesystem::path ConfigManager::getConfigPath() const
|
|
{
|
|
std::shared_lock lock(mutex_);
|
|
return configPath_;
|
|
}
|
|
|
|
std::filesystem::path ConfigManager::getDefaultConfigPath()
|
|
{
|
|
const char* configHome = std::getenv("XDG_CONFIG_HOME");
|
|
if (configHome) {
|
|
return std::filesystem::path(configHome) / "echoion" /
|
|
"config.json";
|
|
}
|
|
|
|
const char* home = std::getenv("HOME");
|
|
if (home) {
|
|
return std::filesystem::path(home) / ".config" / "echoion" /
|
|
"config.json";
|
|
}
|
|
|
|
return "echoion_config.json";
|
|
}
|
|
|
|
void ConfigManager::onConfigChanged(ConfigChangedCallback callback)
|
|
{
|
|
std::unique_lock lock(callbackMutex_);
|
|
callbacks_.push_back(std::move(callback));
|
|
}
|
|
|
|
bool ConfigManager::createDefaultConfigIfNeeded()
|
|
{
|
|
if (!std::filesystem::exists(configPath_)) {
|
|
std::filesystem::create_directories(configPath_.parent_path());
|
|
return save();
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void ConfigManager::notifyCallbacks()
|
|
{
|
|
std::shared_lock lock(mutex_);
|
|
std::shared_lock cbLock(callbackMutex_);
|
|
|
|
for (const auto& callback : callbacks_) {
|
|
callback(config_);
|
|
}
|
|
}
|
|
|
|
bool ConfigManager::loadFromFileInternal()
|
|
{
|
|
// Delegate to Core::ConfigLoader, which performs the JSON <-> AppConfig
|
|
// mapping (via the *Json mirror structs and Void::Json reflection).
|
|
auto result = Core::ConfigLoader::loadFromFile(configPath_);
|
|
if (!result) {
|
|
std::cerr << std::format("Failed to load config: {}\n",
|
|
result.error().toString());
|
|
return false;
|
|
}
|
|
config_ = std::move(*result);
|
|
return true;
|
|
}
|
|
|
|
bool ConfigManager::saveToFileInternal()
|
|
{
|
|
auto result = Core::ConfigLoader::saveToFile(configPath_, config_);
|
|
if (!result) {
|
|
std::cerr << std::format("Failed to save config: {}\n",
|
|
result.error().toString());
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
} // namespace Echoion::Api
|