From 026d00fba057d1b1999059b6c49c324e629c57ed Mon Sep 17 00:00:00 2001 From: faith Date: Thu, 5 Jun 2025 17:38:02 +0800 Subject: [PATCH] wip: load action_set_layers in controller/layers --- dll/settings_parser.cpp | 110 +++++++++++++++++++++------------------- 1 file changed, 58 insertions(+), 52 deletions(-) diff --git a/dll/settings_parser.cpp b/dll/settings_parser.cpp index 57a8c883..8b7bda88 100644 --- a/dll/settings_parser.cpp +++ b/dll/settings_parser.cpp @@ -453,66 +453,72 @@ static void split_string(const std::string &s, char delim, Out result) { // folder "controller" static void load_gamecontroller_settings(Settings *settings) { - std::string path = Local_Storage::get_game_settings_path() + "controller"; - std::vector paths = Local_Storage::get_filenames_path(path); + auto process_paths = [&](std::string path, std::map, std::string>>> action_sets) { + std::vector paths = Local_Storage::get_filenames_path(path); + for (auto& p : paths) { + size_t length = p.length(); + if (length < 4) continue; + if (std::toupper(p.back()) != 'T') continue; + if (std::toupper(p[length - 2]) != 'X') continue; + if (std::toupper(p[length - 3]) != 'T') continue; + if (p[length - 4] != '.') continue; - for (auto & p: paths) { - size_t length = p.length(); - if (length < 4) continue; - if ( std::toupper(p.back()) != 'T') continue; - if ( std::toupper(p[length - 2]) != 'X') continue; - if ( std::toupper(p[length - 3]) != 'T') continue; - if (p[length - 4] != '.') continue; + PRINT_DEBUG("controller config %s", p.c_str()); + std::string action_set_name = p.substr(0, length - 4); + std::transform(action_set_name.begin(), action_set_name.end(), action_set_name.begin(), [](unsigned char c) { return std::toupper(c); }); - PRINT_DEBUG("controller config %s", p.c_str()); - std::string action_set_name = p.substr(0, length - 4); - std::transform(action_set_name.begin(), action_set_name.end(), action_set_name.begin(),[](unsigned char c){ return std::toupper(c); }); + std::string controller_config_path = path + PATH_SEPARATOR + p; + std::ifstream input(std::filesystem::u8path(controller_config_path)); + if (input.is_open()) { + common_helpers::consume_bom(input); + std::map, std::string>> button_pairs; - std::string controller_config_path = path + PATH_SEPARATOR + p; - std::ifstream input( std::filesystem::u8path(controller_config_path) ); - if (input.is_open()) { - common_helpers::consume_bom(input); - std::map, std::string>> button_pairs; - - for( std::string line; getline( input, line ); ) { - if (!line.empty() && line[line.length()-1] == '\n') { - line.pop_back(); - } - - if (!line.empty() && line[line.length()-1] == '\r') { - line.pop_back(); - } - - std::string action_name; - std::string button_name; - std::string source_mode; - - std::size_t deliminator = line.find("="); - if (deliminator != 0 && deliminator != std::string::npos && deliminator != line.size()) { - action_name = line.substr(0, deliminator); - std::size_t deliminator2 = line.find("=", deliminator + 1); - - if (deliminator2 != std::string::npos && deliminator2 != line.size()) { - button_name = line.substr(deliminator + 1, deliminator2 - (deliminator + 1)); - source_mode = line.substr(deliminator2 + 1); - } else { - button_name = line.substr(deliminator + 1); - source_mode = ""; + for (std::string line; getline(input, line); ) { + if (!line.empty() && line[line.length() - 1] == '\n') { + line.pop_back(); } + + if (!line.empty() && line[line.length() - 1] == '\r') { + line.pop_back(); + } + + std::string action_name; + std::string button_name; + std::string source_mode; + + std::size_t deliminator = line.find("="); + if (deliminator != 0 && deliminator != std::string::npos && deliminator != line.size()) { + action_name = line.substr(0, deliminator); + std::size_t deliminator2 = line.find("=", deliminator + 1); + + if (deliminator2 != std::string::npos && deliminator2 != line.size()) { + button_name = line.substr(deliminator + 1, deliminator2 - (deliminator + 1)); + source_mode = line.substr(deliminator2 + 1); + } + else { + button_name = line.substr(deliminator + 1); + source_mode = ""; + } + } + + std::transform(action_name.begin(), action_name.end(), action_name.begin(), [](unsigned char c) { return std::toupper(c); }); + std::transform(button_name.begin(), button_name.end(), button_name.begin(), [](unsigned char c) { return std::toupper(c); }); + std::pair, std::string> button_config = { {}, source_mode }; + split_string(button_name, ',', std::inserter(button_config.first, button_config.first.begin())); + button_pairs[action_name] = button_config; + PRINT_DEBUG("Added %s %s %s", action_name.c_str(), button_name.c_str(), source_mode.c_str()); } - std::transform(action_name.begin(), action_name.end(), action_name.begin(),[](unsigned char c){ return std::toupper(c); }); - std::transform(button_name.begin(), button_name.end(), button_name.begin(),[](unsigned char c){ return std::toupper(c); }); - std::pair, std::string> button_config = {{}, source_mode}; - split_string(button_name, ',', std::inserter(button_config.first, button_config.first.begin())); - button_pairs[action_name] = button_config; - PRINT_DEBUG("Added %s %s %s", action_name.c_str(), button_name.c_str(), source_mode.c_str()); + action_sets[action_set_name] = button_pairs; + PRINT_DEBUG("Added %zu action names to %s", button_pairs.size(), action_set_name.c_str()); } - - settings->controller_settings.action_sets[action_set_name] = button_pairs; - PRINT_DEBUG("Added %zu action names to %s", button_pairs.size(), action_set_name.c_str()); } - } + }; + + std::string path = Local_Storage::get_game_settings_path() + "controller"; + std::string layers_path = path + (PATH_SEPARATOR "layers" PATH_SEPARATOR); + process_paths(path, settings->controller_settings.action_sets); + process_paths(layers_path, settings->controller_settings.action_set_layers); settings->glyphs_directory = path + (PATH_SEPARATOR "glyphs" PATH_SEPARATOR); }