store config in executable

This commit is contained in:
PancakeTAS 2025-07-18 02:34:47 +02:00 committed by Pancake
parent 8b29b952fd
commit a457ef5aff
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.
///
/// @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.

View file

@ -1,3 +1,8 @@
#pragma once
#include <string>
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
)";

View file

@ -1,6 +1,8 @@
#include "config/config.hpp"
#include "common/exception.hpp"
#include "config/default_conf.hpp"
#include <vulkan/vulkan_core.h>
#include <linux/limits.h>
#include <sys/inotify.h>
@ -19,6 +21,7 @@
#include <stdexcept>
#include <iostream>
#include <optional>
#include <fstream>
#include <cstddef>
#include <utility>
#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);
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<toml::value> 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) {