From 0b4ac2c2deae9416de06ffaebfcb738e4ec886ef Mon Sep 17 00:00:00 2001 From: PancakeTAS Date: Fri, 18 Jul 2025 02:34:47 +0200 Subject: [PATCH] store config in executable --- include/config/config.hpp | 6 ++--- conf.toml => include/config/default_conf.hpp | 6 +++++ src/config/config.cpp | 25 +++++++++++--------- 3 files changed, 22 insertions(+), 15 deletions(-) rename conf.toml => include/config/default_conf.hpp (92%) diff --git a/include/config/config.hpp b/include/config/config.hpp index 2ffa67a..4aaeef9 100644 --- a/include/config/config.hpp +++ b/include/config/config.hpp @@ -45,22 +45,20 @@ namespace Config { /// Load the config file and create a file watcher. /// /// @param file The path to the configuration file. - /// @return Whether a configuration exists or not. /// /// @throws std::runtime_error if an error occurs while loading the configuration file. /// - bool loadAndWatchConfig(const std::string& file); + void loadAndWatchConfig(const std::string& file); /// /// Reread the configuration file while preserving the old configuration /// in case of an error. /// /// @param file The path to the configuration file. - /// @return Whether a configuration exists or not. /// /// @throws std::runtime_error if an error occurs while loading the configuration file. /// - bool updateConfig(const std::string& file); + void updateConfig(const std::string& file); /// /// Get the configuration for a game. diff --git a/conf.toml b/include/config/default_conf.hpp similarity index 92% rename from conf.toml rename to include/config/default_conf.hpp index 8768984..624b7ba 100644 --- a/conf.toml +++ b/include/config/default_conf.hpp @@ -1,3 +1,8 @@ +#pragma once + +#include + +const std::string DEFAULT_CONFIG = R"( [global] # enable/disable lsfg on every game # enable = true @@ -47,3 +52,4 @@ enable = true multiplier = 4 performance_mode = true +)"; diff --git a/src/config/config.cpp b/src/config/config.cpp index 589850f..11b1efe 100644 --- a/src/config/config.cpp +++ b/src/config/config.cpp @@ -1,6 +1,8 @@ #include "config/config.hpp" #include "common/exception.hpp" +#include "config/default_conf.hpp" + #include #include #include @@ -19,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -119,12 +122,9 @@ namespace { } } -bool Config::loadAndWatchConfig(const std::string& file) { +void Config::loadAndWatchConfig(const std::string& file) { globalConf.valid = std::make_shared(true); - - auto res = updateConfig(file); - if (!res) - return false; + updateConfig(file); // prepare config watcher std::thread([file = file, valid = globalConf.valid]() { @@ -135,8 +135,6 @@ bool Config::loadAndWatchConfig(const std::string& file) { std::cerr << "- " << e.what() << '\n'; } }).detach(); - - return true; } namespace { @@ -194,10 +192,16 @@ namespace { } } -bool Config::updateConfig(const std::string& file) { +void Config::updateConfig(const std::string& file) { globalConf.valid->store(true, std::memory_order_relaxed); - if (!std::filesystem::exists(file)) - return false; + if (!std::filesystem::exists(file)) { + std::cerr << "lsfg-vk: Placing default configuration file at " << file << '\n'; + std::ofstream out(file); + if (!out.is_open()) + throw std::runtime_error("Unable to create configuration file at " + file); + out << DEFAULT_CONFIG; + out.close(); + } // parse config file std::optional parsed; @@ -277,7 +281,6 @@ bool Config::updateConfig(const std::string& file) { // store configurations globalConf = global; gameConfs = std::move(games); - return true; } Configuration Config::getConfig(std::string_view name) {