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

@@ -4,7 +4,7 @@
# Variables: ${PREFIX}, ${SYSTEM_FOLDER} # Variables: ${PREFIX}, ${SYSTEM_FOLDER}
# Archivos de configuración del sistema (absolutos y opcionales) # Archivos de configuración del sistema (absolutos y opcionales)
DATA|${SYSTEM_FOLDER}/config.txt|optional,absolute DATA|${SYSTEM_FOLDER}/config_v2.txt|optional,absolute
DATA|${SYSTEM_FOLDER}/controllers.json|optional,absolute DATA|${SYSTEM_FOLDER}/controllers.json|optional,absolute
DATA|${SYSTEM_FOLDER}/score.bin|optional,absolute DATA|${SYSTEM_FOLDER}/score.bin|optional,absolute

View File

@@ -86,7 +86,7 @@ void Director::init() {
#endif #endif
loadAssets(); // Crea el índice de archivos loadAssets(); // Crea el índice de archivos
Input::init(Asset::get()->get("gamecontrollerdb.txt"), Asset::get()->get("controllers.json")); // Carga configuración de controles Input::init(Asset::get()->get("gamecontrollerdb.txt"), Asset::get()->get("controllers.json")); // Carga configuración de controles
Options::setConfigFile(Asset::get()->get("config.txt")); // Establece el fichero de configuración Options::setConfigFile(Asset::get()->get("config_v2.txt")); // Establece el fichero de configuración
Options::setControllersFile(Asset::get()->get("controllers.json")); // Establece el fichero de configuración de mandos Options::setControllersFile(Asset::get()->get("controllers.json")); // Establece el fichero de configuración de mandos
Options::loadFromFile(); // Carga el archivo de configuración Options::loadFromFile(); // Carga el archivo de configuración
loadParams(); // Carga los parámetros del programa loadParams(); // Carga los parámetros del programa

View File

@@ -6,6 +6,7 @@
#include <cstddef> // Para size_t #include <cstddef> // Para size_t
#include <fstream> // Para basic_ostream, operator<<, basic_ostream::operator<<, basic_ofstream, basic_istream, basic_ifstream, ifstream, ofstream #include <fstream> // Para basic_ostream, operator<<, basic_ostream::operator<<, basic_ofstream, basic_istream, basic_ifstream, ifstream, ofstream
#include <functional> // Para function #include <functional> // Para function
#include <sstream> // Para istringstream
#include <map> // Para map, operator==, _Rb_tree_const_iterator #include <map> // Para map, operator==, _Rb_tree_const_iterator
#include <ranges> // Para std::ranges::any_of #include <ranges> // Para std::ranges::any_of
#include <stdexcept> // Para invalid_argument, out_of_range #include <stdexcept> // Para invalid_argument, out_of_range
@@ -64,11 +65,27 @@ auto loadFromFile() -> bool {
// --- CASO: EL FICHERO EXISTE --- // --- CASO: EL FICHERO EXISTE ---
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\nReading file: %s", getFileName(settings.config_file).c_str()); SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\nReading file: %s", getFileName(settings.config_file).c_str());
std::string line; std::string line;
std::string param_name;
std::string param_value;
while (std::getline(file, line)) { while (std::getline(file, line)) {
if (line.substr(0, 1) != "#") { // Elimina comentarios
int pos = line.find('='); auto comment_pos = line.find('#');
if (!set(line.substr(0, pos), line.substr(pos + 1, line.length()))) { if (comment_pos != std::string::npos) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Unknown parameter: %s", line.substr(0, pos).c_str()); 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(); 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 // Opciones de ventana
file << "## WINDOW\n"; file << "\n# WINDOW\n";
file << "window.zoom=" << window.zoom << "\n"; file << "window.zoom " << window.zoom << "\n";
// Opciones de video // Opciones de video
file << "\n## VIDEO\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) << ": lineal]\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.fullscreen=" << boolToString(video.fullscreen) << "\n"; file << "video.scale_mode " << static_cast<int>(video.scale_mode) << "\n";
file << "video.scale_mode=" << static_cast<int>(video.scale_mode) << "\n"; file << "video.vsync " << boolToString(video.vsync) << "\n";
file << "video.vsync=" << boolToString(video.vsync) << "\n"; file << "video.integer_scale " << boolToString(video.integer_scale) << "\n";
file << "video.integer_scale=" << boolToString(video.integer_scale) << "\n"; file << "video.shaders " << boolToString(video.shaders) << "\n";
file << "video.shaders=" << boolToString(video.shaders) << "\n";
// Opciones de audio // Opciones de audio
file << "\n## AUDIO\n"; file << "\n# AUDIO\n";
file << "## volume [0 .. 100]\n"; file << "# volume range: [0 .. 100]\n";
file << "audio.enabled " << boolToString(audio.enabled) << "\n";
file << "audio.enabled=" << boolToString(audio.enabled) << "\n"; file << "audio.volume " << audio.volume << "\n";
file << "audio.volume=" << audio.volume << "\n"; file << "audio.music.enabled " << boolToString(audio.music.enabled) << "\n";
file << "audio.music.enabled=" << boolToString(audio.music.enabled) << "\n"; file << "audio.music.volume " << audio.music.volume << "\n";
file << "audio.music.volume=" << audio.music.volume << "\n"; file << "audio.sound.enabled " << boolToString(audio.sound.enabled) << "\n";
file << "audio.sound.enabled=" << boolToString(audio.sound.enabled) << "\n"; file << "audio.sound.volume " << audio.sound.volume << "\n";
file << "audio.sound.volume=" << audio.sound.volume << "\n";
// Opciones del juego // Opciones del juego
file << "\n## GAME\n"; file << "\n# GAME\n";
file << "## game.language [0: spanish, 1: valencian, 2: english]\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.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.language=" << static_cast<int>(settings.language) << "\n"; file << "game.difficulty " << static_cast<int>(settings.difficulty) << "\n";
file << "game.difficulty=" << static_cast<int>(settings.difficulty) << "\n"; file << "game.autofire " << boolToString(settings.autofire) << "\n";
file << "game.autofire=" << boolToString(settings.autofire) << "\n"; file << "game.shutdown_enabled " << boolToString(settings.shutdown_enabled) << "\n";
file << "game.shutdown_enabled=" << boolToString(settings.shutdown_enabled) << "\n"; file << "game.params_file " << settings.params_file << "\n";
file << "game.params_file=" << settings.params_file << "\n";
// Opciones de mandos // Opciones de mandos
file << "\n## CONTROLLERS\n"; file << "\n# CONTROLLERS\n";
gamepad_manager.saveToFile(file); gamepad_manager.saveToFile(file);
// Opciones de teclado // Opciones de teclado
file << "\n## KEYBOARD\n"; file << "\n# KEYBOARD\n";
file << "keyboard.player=" << static_cast<int>(keyboard.player_id) << "\n"; file << "keyboard.player " << static_cast<int>(keyboard.player_id) << "\n";
// Cierra el fichero // Cierra el fichero
file.close(); 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 // 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 = { 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 // Ventana
{"window.zoom", [](const auto& val) { window.zoom = std::stoi(val); }}, {"window.zoom", [](const auto& val) { window.zoom = std::stoi(val); }},
// Vídeo // Vídeo

View File

@@ -58,6 +58,7 @@ struct Audio {
}; };
struct Settings { struct Settings {
int config_version = 2; // Versión del archivo de configuración
Difficulty::Code difficulty = Difficulty::Code::NORMAL; // Dificultad del juego Difficulty::Code difficulty = Difficulty::Code::NORMAL; // Dificultad del juego
Lang::Code language = Lang::Code::VALENCIAN; // Idioma usado en el juego Lang::Code language = Lang::Code::VALENCIAN; // Idioma usado en el juego
bool autofire = GameDefaults::Options::SETTINGS_AUTOFIRE; // Indicador de autofire bool autofire = GameDefaults::Options::SETTINGS_AUTOFIRE; // Indicador de autofire
@@ -158,12 +159,12 @@ class GamepadManager {
const auto& gamepad = gamepads_[i]; const auto& gamepad = gamepads_[i];
// Guardar el nombre solo si hay path (mando real asignado) // Guardar el nombre solo si hay path (mando real asignado)
if (!gamepad.path.empty()) { if (!gamepad.path.empty()) {
file << "controller." << i << ".name=" << gamepad.name << "\n"; file << "controller." << i << ".name " << gamepad.name << "\n";
} else { } else {
file << "controller." << i << ".name=\n"; // vacío file << "controller." << i << ".name \n"; // vacío
} }
file << "controller." << i << ".path=" << gamepad.path << "\n"; file << "controller." << i << ".path " << gamepad.path << "\n";
file << "controller." << i << ".player=" << static_cast<int>(gamepad.player_id) << "\n"; file << "controller." << i << ".player " << static_cast<int>(gamepad.player_id) << "\n";
} }
} }