From e21839e478481feb13dccb460043b76359029a70 Mon Sep 17 00:00:00 2001 From: Sergio Date: Wed, 23 Jul 2025 18:46:34 +0200 Subject: [PATCH] options: readability-function-cognitive-complexity --- source/options.cpp | 174 ++++++++++++++++++++++++--------------------- 1 file changed, 92 insertions(+), 82 deletions(-) diff --git a/source/options.cpp b/source/options.cpp index 610613d..36d394e 100644 --- a/source/options.cpp +++ b/source/options.cpp @@ -12,6 +12,11 @@ #include "lang.h" // Para Code #include "utils.h" // Para boolToString, stringToBool, getFileName +#include +#include +#include +#include // Para excepciones de std::stoi + namespace Options { // --- Variables globales --- WindowOptions window; // Opciones de la ventana @@ -171,98 +176,103 @@ auto saveToFile() -> bool { return true; } -// Asigna variables a partir de dos cadenas +// Función auxiliar para analizar la configuración del mando y reducir duplicación +void parseAndSetController(const std::string &var, const std::string &value) { + // Lógica básica de análisis (puede hacerse más robusta) + size_t first_dot = var.find('.'); + size_t second_dot = var.find('.', first_dot + 1); + + int controller_index = std::stoi(var.substr(first_dot + 1, second_dot - first_dot - 1)); + std::string setting_key = var.substr(second_dot + 1); + + auto& controller = controllers.at(controller_index); + + if (setting_key == "name") { + controller.name = value; + } else if (setting_key == "player") { + controller.player_id = std::clamp(std::stoi(value), 1, 2); + } else if (setting_key == "type") { + controller.type = static_cast(std::stoi(value)); + } else if (setting_key == "button.fire_left") { + controller.buttons.at(0) = static_cast(std::stoi(value)); + } else if (setting_key == "button.fire_center") { + controller.buttons.at(1) = static_cast(std::stoi(value)); + } else if (setting_key == "button.fire_right") { + controller.buttons.at(2) = static_cast(std::stoi(value)); + } else if (setting_key == "button.start") { + controller.buttons.at(3) = static_cast(std::stoi(value)); + } else if (setting_key == "button.service") { + controller.buttons.at(4) = static_cast(std::stoi(value)); + } +} + auto set(const std::string &var, const std::string &value) -> bool { - // Indicador de éxito en la asignación - // Lineas vacias o que empiezan por comentario + // Clausula de protección: ignora líneas vacías o comentarios if (var.empty() || var.starts_with("#")) { return false; } - // Opciones de ventana - if (var == "window.zoom") { - window.zoom = std::stoi(value); + // Un mapa estático asegura que se inicializa solo una vez + static const std::map> settings_map = { + // Ventana + {"window.zoom", [](const auto& val){ window.zoom = std::stoi(val); }}, + // Vídeo + {"video.fullscreen", [](const auto& val){ video.fullscreen = stringToBool(val); }}, + {"video.scale_mode", [](const auto& val){ video.scale_mode = static_cast(std::stoi(val)); }}, + {"video.shaders", [](const auto& val){ video.shaders = stringToBool(val); }}, + {"video.integer_scale", [](const auto& val){ video.integer_scale = stringToBool(val); }}, + {"video.vsync", [](const auto& val){ video.vsync = stringToBool(val); }}, + // Audio + {"audio.enabled", [](const auto& val){ audio.enabled = stringToBool(val); }}, + {"audio.volume", [](const auto& val){ audio.volume = std::clamp(std::stoi(val), 0, 100); }}, + {"audio.music.enabled", [](const auto& val){ audio.music.enabled = stringToBool(val); }}, + {"audio.music.volume", [](const auto& val){ audio.music.volume = std::clamp(std::stoi(val), 0, 100); }}, + {"audio.sound.enabled", [](const auto& val){ audio.sound.enabled = stringToBool(val); }}, + {"audio.sound.volume", [](const auto& val){ audio.sound.volume = std::clamp(std::stoi(val), 0, 100); }}, + // Juego + {"game.language", [](const auto& val){ + settings.language = static_cast(std::stoi(val)); + pending_changes.new_language = settings.language; + }}, + {"game.difficulty", [](const auto& val){ + settings.difficulty = static_cast(std::stoi(val)); + pending_changes.new_difficulty = settings.difficulty; + }}, + {"game.autofire", [](const auto& val){ settings.autofire = stringToBool(val); }}, + {"game.shutdown_enabled", [](const auto& val){ settings.shutdown_enabled = stringToBool(val); }} + }; + + // Maneja por separado la configuración general de los mandos + if (var.starts_with("controller.")) { + try { + parseAndSetController(var, value); + return true; + } catch (const std::out_of_range& e) { + // Error: por ejemplo, índice de mando fuera de rango + return false; + } catch (const std::invalid_argument& e) { + // Error: por ejemplo, fallo en std::stoi + return false; + } } - // Opciones de video - if (var == "video.fullscreen") { - video.fullscreen = stringToBool(value); - } else if (var == "video.scale_mode") { - video.scale_mode = static_cast(std::stoi(value)); - } else if (var == "video.shaders") { - video.shaders = stringToBool(value); - } else if (var == "video.integer_scale") { - video.integer_scale = stringToBool(value); - } else if (var == "video.vsync") { - video.vsync = stringToBool(value); + // Busca el nombre de la variable en el mapa + if (auto it = settings_map.find(var); it != settings_map.end()) { + try { + // Ejecuta la función lambda asociada + it->second(value); + return true; + } catch (const std::invalid_argument& e) { + // Maneja casos donde std::stoi falla por entrada inválida + return false; + } } - - // Opciones de audio - else if (var == "audio.enabled") { - audio.enabled = stringToBool(value); - } else if (var == "audio.volume") { - audio.volume = std::clamp(std::stoi(value), 0, 100); - } else if (var == "audio.music.enabled") { - audio.music.enabled = stringToBool(value); - } else if (var == "audio.music.volume") { - audio.music.volume = std::clamp(std::stoi(value), 0, 100); - } else if (var == "audio.sound.enabled") { - audio.sound.enabled = stringToBool(value); - } else if (var == "audio.sound.volume") { - audio.sound.volume = std::clamp(std::stoi(value), 0, 100); - } - - // Opciones de juego - else if (var == "game.language") { - settings.language = static_cast(std::stoi(value)); - pending_changes.new_language = settings.language; - } else if (var == "game.difficulty") { - settings.difficulty = static_cast(std::stoi(value)); - pending_changes.new_difficulty = settings.difficulty; - } else if (var == "game.autofire") { - settings.autofire = stringToBool(value); - } else if (var == "game.shutdown_enabled") { - settings.shutdown_enabled = stringToBool(value); - } - - // Opciones de mandos - else if (var == "controller.0.name") { - controllers.at(0).name = value; - } else if (var == "controller.0.player") { - controllers.at(0).player_id = std::clamp(std::stoi(value), 1, 2); - } else if (var == "controller.0.type") { - controllers.at(0).type = static_cast(std::stoi(value)); - } else if (var == "controller.0.button.fire_left") { - controllers.at(0).buttons.at(0) = static_cast(std::stoi(value)); - } else if (var == "controller.0.button.fire_center") { - controllers.at(0).buttons.at(1) = static_cast(std::stoi(value)); - } else if (var == "controller.0.button.fire_right") { - controllers.at(0).buttons.at(2) = static_cast(std::stoi(value)); - } else if (var == "controller.0.button.start") { - controllers.at(0).buttons.at(3) = static_cast(std::stoi(value)); - } else if (var == "controller.0.button.service") { - controllers.at(0).buttons.at(4) = static_cast(std::stoi(value)); - } else if (var == "controller.1.name") { - controllers.at(1).name = value; - } else if (var == "controller.1.player") { - controllers.at(1).player_id = std::clamp(std::stoi(value), 1, 2); - } else if (var == "controller.1.type") { - controllers.at(1).type = static_cast(std::stoi(value)); - } else if (var == "controller.1.button.fire_left") { - controllers.at(1).buttons.at(0) = static_cast(std::stoi(value)); - } else if (var == "controller.1.button.fire_center") { - controllers.at(1).buttons.at(1) = static_cast(std::stoi(value)); - } else if (var == "controller.1.button.fire_right") { - controllers.at(1).buttons.at(2) = static_cast(std::stoi(value)); - } else if (var == "controller.1.button.start") { - controllers.at(1).buttons.at(3) = static_cast(std::stoi(value)); - } else if (var == "controller.1.button.service") { - controllers.at(1).buttons.at(4) = static_cast(std::stoi(value)); - } - - return true; + + // Si la clave no se encontró en el mapa ni en la lógica de mandos + return false; } + // Asigna el teclado al jugador void setKeyboardToPlayer(int player_id) { for (auto &controller : controllers) {