options: readability-function-cognitive-complexity

This commit is contained in:
2025-07-23 18:46:34 +02:00
parent 5f170ee44e
commit e21839e478

View File

@@ -12,6 +12,11 @@
#include "lang.h" // Para Code
#include "utils.h" // Para boolToString, stringToBool, getFileName
#include <string>
#include <functional>
#include <map>
#include <stdexcept> // 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<InputDevice>(std::stoi(value));
} else if (setting_key == "button.fire_left") {
controller.buttons.at(0) = static_cast<SDL_GamepadButton>(std::stoi(value));
} else if (setting_key == "button.fire_center") {
controller.buttons.at(1) = static_cast<SDL_GamepadButton>(std::stoi(value));
} else if (setting_key == "button.fire_right") {
controller.buttons.at(2) = static_cast<SDL_GamepadButton>(std::stoi(value));
} else if (setting_key == "button.start") {
controller.buttons.at(3) = static_cast<SDL_GamepadButton>(std::stoi(value));
} else if (setting_key == "button.service") {
controller.buttons.at(4) = static_cast<SDL_GamepadButton>(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);
}
// Opciones de video
if (var == "video.fullscreen") {
video.fullscreen = stringToBool(value);
} else if (var == "video.scale_mode") {
video.scale_mode = static_cast<SDL_ScaleMode>(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);
}
// 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<Lang::Code>(std::stoi(value));
// 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 = {
// 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<SDL_ScaleMode>(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<Lang::Code>(std::stoi(val));
pending_changes.new_language = settings.language;
} else if (var == "game.difficulty") {
settings.difficulty = static_cast<DifficultyCode>(std::stoi(value));
}},
{"game.difficulty", [](const auto& val){
settings.difficulty = static_cast<DifficultyCode>(std::stoi(val));
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<InputDevice>(std::stoi(value));
} else if (var == "controller.0.button.fire_left") {
controllers.at(0).buttons.at(0) = static_cast<SDL_GamepadButton>(std::stoi(value));
} else if (var == "controller.0.button.fire_center") {
controllers.at(0).buttons.at(1) = static_cast<SDL_GamepadButton>(std::stoi(value));
} else if (var == "controller.0.button.fire_right") {
controllers.at(0).buttons.at(2) = static_cast<SDL_GamepadButton>(std::stoi(value));
} else if (var == "controller.0.button.start") {
controllers.at(0).buttons.at(3) = static_cast<SDL_GamepadButton>(std::stoi(value));
} else if (var == "controller.0.button.service") {
controllers.at(0).buttons.at(4) = static_cast<SDL_GamepadButton>(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<InputDevice>(std::stoi(value));
} else if (var == "controller.1.button.fire_left") {
controllers.at(1).buttons.at(0) = static_cast<SDL_GamepadButton>(std::stoi(value));
} else if (var == "controller.1.button.fire_center") {
controllers.at(1).buttons.at(1) = static_cast<SDL_GamepadButton>(std::stoi(value));
} else if (var == "controller.1.button.fire_right") {
controllers.at(1).buttons.at(2) = static_cast<SDL_GamepadButton>(std::stoi(value));
} else if (var == "controller.1.button.start") {
controllers.at(1).buttons.at(3) = static_cast<SDL_GamepadButton>(std::stoi(value));
} else if (var == "controller.1.button.service") {
controllers.at(1).buttons.at(4) = static_cast<SDL_GamepadButton>(std::stoi(value));
}
}},
{"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;
}
}
// 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;
}
}
// 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) {