store config in executable

This commit is contained in:
PancakeTAS 2025-07-18 02:34:47 +02:00
parent 6e86945dc8
commit 0b4ac2c2de
No known key found for this signature in database
3 changed files with 22 additions and 15 deletions

View file

@ -45,22 +45,20 @@ namespace Config {
/// Load the config file and create a file watcher. /// Load the config file and create a file watcher.
/// ///
/// @param file The path to the configuration file. /// @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. /// @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 /// Reread the configuration file while preserving the old configuration
/// in case of an error. /// in case of an error.
/// ///
/// @param file The path to the configuration file. /// @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. /// @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. /// Get the configuration for a game.

View file

@ -1,3 +1,8 @@
#pragma once
#include <string>
const std::string DEFAULT_CONFIG = R"(
[global] [global]
# enable/disable lsfg on every game # enable/disable lsfg on every game
# enable = true # enable = true
@ -47,3 +52,4 @@ enable = true
multiplier = 4 multiplier = 4
performance_mode = true performance_mode = true
)";

View file

@ -1,6 +1,8 @@
#include "config/config.hpp" #include "config/config.hpp"
#include "common/exception.hpp" #include "common/exception.hpp"
#include "config/default_conf.hpp"
#include <vulkan/vulkan_core.h> #include <vulkan/vulkan_core.h>
#include <linux/limits.h> #include <linux/limits.h>
#include <sys/inotify.h> #include <sys/inotify.h>
@ -19,6 +21,7 @@
#include <stdexcept> #include <stdexcept>
#include <iostream> #include <iostream>
#include <optional> #include <optional>
#include <fstream>
#include <cstddef> #include <cstddef>
#include <utility> #include <utility>
#include <cstring> #include <cstring>
@ -119,12 +122,9 @@ namespace {
} }
} }
bool Config::loadAndWatchConfig(const std::string& file) { void Config::loadAndWatchConfig(const std::string& file) {
globalConf.valid = std::make_shared<std::atomic_bool>(true); globalConf.valid = std::make_shared<std::atomic_bool>(true);
updateConfig(file);
auto res = updateConfig(file);
if (!res)
return false;
// prepare config watcher // prepare config watcher
std::thread([file = file, valid = globalConf.valid]() { std::thread([file = file, valid = globalConf.valid]() {
@ -135,8 +135,6 @@ bool Config::loadAndWatchConfig(const std::string& file) {
std::cerr << "- " << e.what() << '\n'; std::cerr << "- " << e.what() << '\n';
} }
}).detach(); }).detach();
return true;
} }
namespace { 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); globalConf.valid->store(true, std::memory_order_relaxed);
if (!std::filesystem::exists(file)) if (!std::filesystem::exists(file)) {
return false; 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 // parse config file
std::optional<toml::value> parsed; std::optional<toml::value> parsed;
@ -277,7 +281,6 @@ bool Config::updateConfig(const std::string& file) {
// store configurations // store configurations
globalConf = global; globalConf = global;
gameConfs = std::move(games); gameConfs = std::move(games);
return true;
} }
Configuration Config::getConfig(std::string_view name) { Configuration Config::getConfig(std::string_view name) {