112 lines
3.6 KiB
C++
112 lines
3.6 KiB
C++
#pragma once
|
|
#ifndef PLUGIN
|
|
#include "ConfigTypes.hpp"
|
|
#include <atomic>
|
|
#include <filesystem>
|
|
#include <functional>
|
|
#include <mutex>
|
|
#include <optional>
|
|
#include <shared_mutex>
|
|
|
|
namespace Echoion::Api
|
|
{
|
|
|
|
class ConfigManager
|
|
{
|
|
public:
|
|
|
|
using ConfigChangedCallback = std::function<void(const AppConfig&)>;
|
|
|
|
// Get singleton instance
|
|
static ConfigManager& instance();
|
|
|
|
// Delete copy/move
|
|
ConfigManager(const ConfigManager&) = delete;
|
|
ConfigManager& operator=(const ConfigManager&) = delete;
|
|
|
|
// Initialize with config file path (call once at startup)
|
|
[[nodiscard]] bool
|
|
initialize(const std::filesystem::path& configPath);
|
|
|
|
// Initialize with default path (~/.config/echoion/config.json)
|
|
[[nodiscard]] bool initialize();
|
|
|
|
// Get current config (thread-safe)
|
|
[[nodiscard]] AppConfig getConfig() const;
|
|
|
|
// Get config by reference (faster, but hold lock briefly)
|
|
template <typename Func>
|
|
auto withConfig(Func&& func) const
|
|
-> decltype(func(std::declval<const AppConfig&>()))
|
|
{
|
|
std::shared_lock lock(mutex_);
|
|
return func(config_);
|
|
}
|
|
|
|
// Direct accessors for config sections (convenience methods)
|
|
[[nodiscard]] const BotConfig& getBot() const;
|
|
[[nodiscard]] const YouTubeConfig& getYouTube() const;
|
|
[[nodiscard]] const NetworkConfig& getNetwork() const;
|
|
[[nodiscard]] const TeamSpeakConfig& getTeamSpeak() const;
|
|
[[nodiscard]] const SecurityConfig& getSecurity() const;
|
|
[[nodiscard]] const FeaturesConfig& getFeatures() const;
|
|
[[nodiscard]] const AdvancedConfig& getAdvanced() const;
|
|
|
|
// Reload configuration from file
|
|
[[nodiscard]] bool reload();
|
|
|
|
// Save current configuration to file
|
|
[[nodiscard]] bool save();
|
|
|
|
// Update config (and optionally save)
|
|
void updateConfig(const AppConfig& config, bool saveToFile = true);
|
|
|
|
// Check if initialized
|
|
[[nodiscard]] bool isInitialized() const;
|
|
|
|
// Get config file path
|
|
[[nodiscard]] std::filesystem::path getConfigPath() const;
|
|
|
|
// Get default config path (~/.config/echoion/config.json)
|
|
[[nodiscard]] static std::filesystem::path getDefaultConfigPath();
|
|
|
|
// Subscribe to config changes
|
|
void onConfigChanged(ConfigChangedCallback callback);
|
|
|
|
// Create default config file if it doesn't exist
|
|
[[nodiscard]] bool createDefaultConfigIfNeeded();
|
|
|
|
private:
|
|
|
|
ConfigManager() = default;
|
|
~ConfigManager() = default;
|
|
|
|
mutable std::shared_mutex mutex_;
|
|
AppConfig config_;
|
|
std::filesystem::path configPath_;
|
|
std::atomic<bool> initialized_{false};
|
|
std::vector<ConfigChangedCallback> callbacks_;
|
|
mutable std::shared_mutex callbackMutex_;
|
|
|
|
void notifyCallbacks();
|
|
[[nodiscard]] bool loadFromFileInternal();
|
|
[[nodiscard]] bool saveToFileInternal();
|
|
};
|
|
|
|
} // namespace Echoion::Api
|
|
|
|
// Convenience macros for accessing config
|
|
#define ECHOION_CONFIG (::Echoion::Api::ConfigManager::instance().getConfig())
|
|
#define ECHOION_BOT (::Echoion::Api::ConfigManager::instance().getBot())
|
|
#define ECHOION_YOUTUBE (::Echoion::Api::ConfigManager::instance().getYouTube())
|
|
#define ECHOION_NETWORK (::Echoion::Api::ConfigManager::instance().getNetwork())
|
|
#define ECHOION_TEAMSPEAK \
|
|
(::Echoion::Api::ConfigManager::instance().getTeamSpeak())
|
|
#define ECHOION_SECURITY \
|
|
(::Echoion::Api::ConfigManager::instance().getSecurity())
|
|
#define ECHOION_FEATURES \
|
|
(::Echoion::Api::ConfigManager::instance().getFeatures())
|
|
#define ECHOION_ADVANCED \
|
|
(::Echoion::Api::ConfigManager::instance().getAdvanced())
|
|
#endif
|