167 lines
4.8 KiB
C++
167 lines
4.8 KiB
C++
#include <Void/Log/Log.hpp>
|
|
#include "Types.hpp"
|
|
#include "echoion/core/ConfigLoader.hpp"
|
|
#include <AudioTypes.hpp>
|
|
#include <Void/Data/Json.hpp>
|
|
#include <Void/Data/Reflect.hpp>
|
|
#include <format>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <string>
|
|
|
|
namespace Echoion::Core
|
|
{
|
|
|
|
void ConfigLoader::normalize(Api::AppConfig& config)
|
|
{
|
|
// yt-dlp concurrency is hard-capped regardless of what the file says.
|
|
config.youtube.ytDlpMaxConcurrent =
|
|
std::min(config.youtube.ytDlpMaxConcurrent, size_t(8));
|
|
}
|
|
|
|
Api::Result<Api::AppConfig>
|
|
ConfigLoader::loadFromFile(const std::filesystem::path& path)
|
|
{
|
|
if (!std::filesystem::exists(path)) {
|
|
return Api::fail(Api::ErrorCode::NotFound,
|
|
std::format("Configuration file not found: {}",
|
|
path.string()));
|
|
}
|
|
|
|
std::ifstream file(path);
|
|
if (!file.is_open()) {
|
|
return Api::fail(Api::ErrorCode::PermissionDenied,
|
|
std::format("Failed to open configuration file: {}",
|
|
path.string()));
|
|
}
|
|
|
|
std::string content((std::istreambuf_iterator<char>(file)),
|
|
std::istreambuf_iterator<char>());
|
|
file.close();
|
|
|
|
auto parsed = Void::Json::parse(content);
|
|
if (!parsed) {
|
|
return Api::fail(
|
|
Api::ErrorCode::ParseError,
|
|
std::format("Failed to parse configuration ({}:{}): {}",
|
|
parsed.error().line, parsed.error().col,
|
|
parsed.error().message));
|
|
}
|
|
|
|
// Deserialize Api::AppConfig directly via reflection: unknown keys are
|
|
// ignored and absent fields keep their defaults. Durations are duration
|
|
// strings ("30000ms"), enums are names, the proxy is a nested object.
|
|
auto result = Void::Json::from_json<Api::AppConfig>(*parsed);
|
|
if (!result) {
|
|
return Api::fail(
|
|
Api::ErrorCode::ParseError,
|
|
std::format("Failed to parse configuration: field '{}': {}",
|
|
result.error().field, result.error().message));
|
|
}
|
|
|
|
Api::AppConfig config = std::move(*result);
|
|
normalize(config);
|
|
|
|
std::cout << std::format("[INFO] Configuration loaded from {}",
|
|
path.string())
|
|
<< '\n';
|
|
return config;
|
|
}
|
|
|
|
Api::Result<void>
|
|
ConfigLoader::saveToFile(const std::filesystem::path& path,
|
|
const Api::AppConfig& config)
|
|
{
|
|
const std::string json = Void::Json::serialize(
|
|
Void::Json::to_json(config), {.pretty = true});
|
|
std::ofstream file(path);
|
|
if (!file.is_open()) {
|
|
return Api::fail(Api::ErrorCode::PermissionDenied,
|
|
std::format("Failed to write configuration file: {}",
|
|
path.string()));
|
|
}
|
|
|
|
file << json;
|
|
file.close();
|
|
|
|
Void::Log::info("ConfigLoader", "{}",
|
|
std::format("Configuration saved to {}", path.string()));
|
|
return {};
|
|
}
|
|
|
|
std::filesystem::path ConfigLoader::getDefaultConfigPath()
|
|
{
|
|
// Try to get config path from environment or use default
|
|
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";
|
|
}
|
|
|
|
// Fallback to current directory
|
|
return "echoion_config.json";
|
|
}
|
|
|
|
Api::AppConfig ConfigLoader::createDefaultConfig()
|
|
{
|
|
Api::AppConfig config;
|
|
|
|
// Default bot config
|
|
config.bot.botName = "Echoion";
|
|
config.bot.commandPrefix = "!";
|
|
config.bot.defaultVolume = 0.5f;
|
|
config.bot.maxQueueSize = 100;
|
|
config.bot.trackTimeout = std::chrono::milliseconds(10000);
|
|
config.bot.allowPlaylistImports = true;
|
|
|
|
// Default YouTube config
|
|
config.youtube.ytDlpMaxConcurrent = 4;
|
|
config.youtube.ytDlpAsyncEnabled = true;
|
|
config.youtube.enableUrlShortening = true;
|
|
|
|
// Default network config
|
|
config.network.rpcEnabled = true;
|
|
config.network.rpcPort = 8765;
|
|
config.network.rpcBindAddress = "127.0.0.1";
|
|
|
|
// Default TeamSpeak config
|
|
config.teamspeak.address = "localhost";
|
|
config.teamspeak.port = 9987;
|
|
config.teamspeak.nickname = "EchoionBot";
|
|
|
|
// Default security config
|
|
config.security.requireAuthForAdmin = true;
|
|
config.security.maxLoginAttempts = 5;
|
|
config.security.lockoutDuration = std::chrono::milliseconds(300000);
|
|
config.security.sessionTimeout = std::chrono::milliseconds(3600000);
|
|
config.security.enableAuditLog = true;
|
|
config.security.allowCors = false;
|
|
|
|
// Default features config
|
|
config.features.enableHotReload = true;
|
|
config.features.enableAutoReload = false;
|
|
config.features.enableAutoUpdateCheck = true;
|
|
config.features.autoStartOnSubscribe = true;
|
|
config.features.pauseOnEmptySubscribers = true;
|
|
config.features.resumeOnSubscriberReturn = true;
|
|
config.features.shuffleMode = "off";
|
|
config.features.repeatMode = "off";
|
|
config.features.maxHistorySize = 50;
|
|
|
|
// Default advanced config
|
|
config.advanced.cacheEnabled = true;
|
|
config.advanced.cacheMaxSize = "1GB";
|
|
config.advanced.cacheTTL = 86400;
|
|
config.advanced.tempCleanupInterval = 3600;
|
|
|
|
return config;
|
|
}
|
|
|
|
} // namespace Echoion::Core
|