migrat fitxer de config a v2

This commit is contained in:
2025-10-02 16:35:11 +02:00
parent 62b73d6f41
commit 79033346c0
4 changed files with 63 additions and 41 deletions

View File

@@ -6,6 +6,7 @@
#include <cstddef> // Para size_t
#include <fstream> // Para basic_ostream, operator<<, basic_ostream::operator<<, basic_ofstream, basic_istream, basic_ifstream, ifstream, ofstream
#include <functional> // Para function
#include <sstream> // Para istringstream
#include <map> // Para map, operator==, _Rb_tree_const_iterator
#include <ranges> // Para std::ranges::any_of
#include <stdexcept> // Para invalid_argument, out_of_range
@@ -64,11 +65,27 @@ auto loadFromFile() -> bool {
// --- CASO: EL FICHERO EXISTE ---
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\nReading file: %s", getFileName(settings.config_file).c_str());
std::string line;
std::string param_name;
std::string param_value;
while (std::getline(file, line)) {
if (line.substr(0, 1) != "#") {
int pos = line.find('=');
if (!set(line.substr(0, pos), line.substr(pos + 1, line.length()))) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Unknown parameter: %s", line.substr(0, pos).c_str());
// Elimina comentarios
auto comment_pos = line.find('#');
if (comment_pos != std::string::npos) {
line.resize(comment_pos);
}
// Si la línea contiene '=', lo reemplazamos por un espacio para compatibilidad
auto equals_pos = line.find('=');
if (equals_pos != std::string::npos) {
line[equals_pos] = ' ';
}
// Usa un stream para separar palabras (elimina automáticamente espacios extra)
std::istringstream iss(line);
if (iss >> param_name >> param_value) {
if (!set(param_name, param_value)) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Unknown parameter: %s", param_name.c_str());
}
}
}
@@ -100,49 +117,51 @@ auto saveToFile() -> bool {
applyPendingChanges();
// Versión del archivo
file << "# Coffee Crisis Arcade Edition - Configuration File\n";
file << "# Format: key value\n";
file << "config.version " << settings.config_version << "\n";
// Opciones de ventana
file << "## WINDOW\n";
file << "window.zoom=" << window.zoom << "\n";
file << "\n# WINDOW\n";
file << "window.zoom " << window.zoom << "\n";
// Opciones de video
file << "\n## VIDEO\n";
file << "## video.scale_mode [" << static_cast<int>(SDL_ScaleMode::SDL_SCALEMODE_NEAREST) << ": nearest, " << static_cast<int>(SDL_ScaleMode::SDL_SCALEMODE_LINEAR) << ": lineal]\n";
file << "video.fullscreen=" << boolToString(video.fullscreen) << "\n";
file << "video.scale_mode=" << static_cast<int>(video.scale_mode) << "\n";
file << "video.vsync=" << boolToString(video.vsync) << "\n";
file << "video.integer_scale=" << boolToString(video.integer_scale) << "\n";
file << "video.shaders=" << boolToString(video.shaders) << "\n";
file << "\n# VIDEO\n";
file << "# video.scale_mode [" << static_cast<int>(SDL_ScaleMode::SDL_SCALEMODE_NEAREST) << ": nearest, " << static_cast<int>(SDL_ScaleMode::SDL_SCALEMODE_LINEAR) << ": linear]\n";
file << "video.fullscreen " << boolToString(video.fullscreen) << "\n";
file << "video.scale_mode " << static_cast<int>(video.scale_mode) << "\n";
file << "video.vsync " << boolToString(video.vsync) << "\n";
file << "video.integer_scale " << boolToString(video.integer_scale) << "\n";
file << "video.shaders " << boolToString(video.shaders) << "\n";
// Opciones de audio
file << "\n## AUDIO\n";
file << "## volume [0 .. 100]\n";
file << "audio.enabled=" << boolToString(audio.enabled) << "\n";
file << "audio.volume=" << audio.volume << "\n";
file << "audio.music.enabled=" << boolToString(audio.music.enabled) << "\n";
file << "audio.music.volume=" << audio.music.volume << "\n";
file << "audio.sound.enabled=" << boolToString(audio.sound.enabled) << "\n";
file << "audio.sound.volume=" << audio.sound.volume << "\n";
file << "\n# AUDIO\n";
file << "# volume range: [0 .. 100]\n";
file << "audio.enabled " << boolToString(audio.enabled) << "\n";
file << "audio.volume " << audio.volume << "\n";
file << "audio.music.enabled " << boolToString(audio.music.enabled) << "\n";
file << "audio.music.volume " << audio.music.volume << "\n";
file << "audio.sound.enabled " << boolToString(audio.sound.enabled) << "\n";
file << "audio.sound.volume " << audio.sound.volume << "\n";
// Opciones del juego
file << "\n## GAME\n";
file << "## game.language [0: spanish, 1: valencian, 2: english]\n";
file << "## game.difficulty [" << static_cast<int>(Difficulty::Code::EASY) << ": easy, " << static_cast<int>(Difficulty::Code::NORMAL) << ": normal, " << static_cast<int>(Difficulty::Code::HARD) << ": hard]\n";
file << "game.language=" << static_cast<int>(settings.language) << "\n";
file << "game.difficulty=" << static_cast<int>(settings.difficulty) << "\n";
file << "game.autofire=" << boolToString(settings.autofire) << "\n";
file << "game.shutdown_enabled=" << boolToString(settings.shutdown_enabled) << "\n";
file << "game.params_file=" << settings.params_file << "\n";
file << "\n# GAME\n";
file << "# game.language [0: spanish, 1: valencian, 2: english]\n";
file << "# game.difficulty [" << static_cast<int>(Difficulty::Code::EASY) << ": easy, " << static_cast<int>(Difficulty::Code::NORMAL) << ": normal, " << static_cast<int>(Difficulty::Code::HARD) << ": hard]\n";
file << "game.language " << static_cast<int>(settings.language) << "\n";
file << "game.difficulty " << static_cast<int>(settings.difficulty) << "\n";
file << "game.autofire " << boolToString(settings.autofire) << "\n";
file << "game.shutdown_enabled " << boolToString(settings.shutdown_enabled) << "\n";
file << "game.params_file " << settings.params_file << "\n";
// Opciones de mandos
file << "\n## CONTROLLERS\n";
file << "\n# CONTROLLERS\n";
gamepad_manager.saveToFile(file);
// Opciones de teclado
file << "\n## KEYBOARD\n";
file << "keyboard.player=" << static_cast<int>(keyboard.player_id) << "\n";
file << "\n# KEYBOARD\n";
file << "keyboard.player " << static_cast<int>(keyboard.player_id) << "\n";
// Cierra el fichero
file.close();
@@ -177,6 +196,8 @@ auto set(const std::string& var, const std::string& value) -> bool {
// Un mapa estático asegura que se inicializa solo una vez
static const std::map<std::string, std::function<void(const std::string&)>> SETTINGS_MAP = {
// Configuración
{"config.version", [](const auto& val) { settings.config_version = std::stoi(val); }},
// Ventana
{"window.zoom", [](const auto& val) { window.zoom = std::stoi(val); }},
// Vídeo