From 0fa1220eca95514812682140221917af1b8e4932 Mon Sep 17 00:00:00 2001 From: DanielSvoboda Date: Sat, 22 Mar 2025 19:27:42 -0300 Subject: [PATCH] Add option to enable/disable game folders individually (#2662) * Add option to enable/disable game folders individually A checkbox (button to select) game folders has been added to the menu, allowing you to Enable/Disable them without having to remove the folder. config.toml is now saved in alphabetical order * ordering is separation in a function * remove my comment in portuguese :) --- src/common/config.cpp | 137 +++++++++++++++++++++++++++++---- src/common/config.h | 14 +++- src/qt_gui/settings_dialog.cpp | 48 +++++++++--- 3 files changed, 170 insertions(+), 29 deletions(-) diff --git a/src/common/config.cpp b/src/common/config.cpp index 514024c30..0b5fb8be1 100644 --- a/src/common/config.cpp +++ b/src/common/config.cpp @@ -81,7 +81,7 @@ static std::string trophyKey; // Gui static bool load_game_size = true; -std::vector settings_install_dirs = {}; +static std::vector settings_install_dirs = {}; std::filesystem::path settings_addon_install_dir = {}; std::filesystem::path save_data_path = {}; u32 main_window_geometry_x = 400; @@ -519,22 +519,34 @@ void setMainWindowGeometry(u32 x, u32 y, u32 w, u32 h) { main_window_geometry_h = h; } -bool addGameInstallDir(const std::filesystem::path& dir) { - if (std::find(settings_install_dirs.begin(), settings_install_dirs.end(), dir) == - settings_install_dirs.end()) { - settings_install_dirs.push_back(dir); - return true; +bool addGameInstallDir(const std::filesystem::path& dir, bool enabled) { + for (const auto& install_dir : settings_install_dirs) { + if (install_dir.path == dir) { + return false; + } } - return false; + settings_install_dirs.push_back({dir, enabled}); + return true; } void removeGameInstallDir(const std::filesystem::path& dir) { - auto iterator = std::find(settings_install_dirs.begin(), settings_install_dirs.end(), dir); + auto iterator = + std::find_if(settings_install_dirs.begin(), settings_install_dirs.end(), + [&dir](const GameInstallDir& install_dir) { return install_dir.path == dir; }); if (iterator != settings_install_dirs.end()) { settings_install_dirs.erase(iterator); } } +void setGameInstallDirEnabled(const std::filesystem::path& dir, bool enabled) { + auto iterator = + std::find_if(settings_install_dirs.begin(), settings_install_dirs.end(), + [&dir](const GameInstallDir& install_dir) { return install_dir.path == dir; }); + if (iterator != settings_install_dirs.end()) { + iterator->enabled = enabled; + } +} + void setAddonInstallDir(const std::filesystem::path& dir) { settings_addon_install_dir = dir; } @@ -590,8 +602,15 @@ void setEmulatorLanguage(std::string language) { emulator_language = language; } -void setGameInstallDirs(const std::vector& settings_install_dirs_config) { - settings_install_dirs = settings_install_dirs_config; +void setGameInstallDirs(const std::vector& dirs_config) { + settings_install_dirs.clear(); + for (const auto& dir : dirs_config) { + settings_install_dirs.push_back({dir, true}); + } +} + +void setAllGameInstallDirs(const std::vector& dirs_config) { + settings_install_dirs = dirs_config; } void setSaveDataPath(const std::filesystem::path& path) { @@ -614,7 +633,17 @@ u32 getMainWindowGeometryH() { return main_window_geometry_h; } -const std::vector& getGameInstallDirs() { +const std::vector getGameInstallDirs() { + std::vector enabled_dirs; + for (const auto& dir : settings_install_dirs) { + if (dir.enabled) { + enabled_dirs.push_back(dir.path); + } + } + return enabled_dirs; +} + +const std::vector& getAllGameInstallDirs() { return settings_install_dirs; } @@ -809,8 +838,23 @@ void load(const std::filesystem::path& path) { const auto install_dir_array = toml::find_or>(gui, "installDirs", {}); - for (const auto& dir : install_dir_array) { - addGameInstallDir(std::filesystem::path{dir}); + + std::vector install_dirs_enabled; + try { + install_dirs_enabled = toml::find>(gui, "installDirsEnabled"); + } catch (...) { + // If it does not exist, assume that all are enabled. + install_dirs_enabled.resize(install_dir_array.size(), true); + } + + if (install_dirs_enabled.size() < install_dir_array.size()) { + install_dirs_enabled.resize(install_dir_array.size(), true); + } + + settings_install_dirs.clear(); + for (size_t i = 0; i < install_dir_array.size(); i++) { + settings_install_dirs.push_back( + {std::filesystem::path{install_dir_array[i]}, install_dirs_enabled[i]}); } save_data_path = toml::find_fs_path_or(gui, "saveDataPath", {}); @@ -853,6 +897,37 @@ void load(const std::filesystem::path& path) { } } +void sortTomlSections(toml::ordered_value& data) { + toml::ordered_value ordered_data; + std::vector section_order = {"General", "Input", "GPU", "Vulkan", + "Debug", "Keys", "GUI", "Settings"}; + + for (const auto& section : section_order) { + if (data.contains(section)) { + std::vector keys; + for (const auto& item : data.at(section).as_table()) { + keys.push_back(item.first); + } + + std::sort(keys.begin(), keys.end(), [](const std::string& a, const std::string& b) { + return std::lexicographical_compare( + a.begin(), a.end(), b.begin(), b.end(), [](char a_char, char b_char) { + return std::tolower(a_char) < std::tolower(b_char); + }); + }); + + toml::ordered_value ordered_section; + for (const auto& key : keys) { + ordered_section[key] = data.at(section).at(key); + } + + ordered_data[section] = ordered_section; + } + } + + data = ordered_data; +} + void save(const std::filesystem::path& path) { toml::ordered_value data; @@ -922,14 +997,37 @@ void save(const std::filesystem::path& path) { data["Debug"]["CollectShader"] = isShaderDebug; data["Debug"]["isSeparateLogFilesEnabled"] = isSeparateLogFilesEnabled; data["Debug"]["FPSColor"] = isFpsColor; - data["Keys"]["TrophyKey"] = trophyKey; std::vector install_dirs; - for (const auto& dirString : settings_install_dirs) { - install_dirs.emplace_back(std::string{fmt::UTF(dirString.u8string()).data}); + std::vector install_dirs_enabled; + + // temporary structure for ordering + struct DirEntry { + std::string path_str; + bool enabled; + }; + + std::vector sorted_dirs; + for (const auto& dirInfo : settings_install_dirs) { + sorted_dirs.push_back( + {std::string{fmt::UTF(dirInfo.path.u8string()).data}, dirInfo.enabled}); } + + // Sort directories alphabetically + std::sort(sorted_dirs.begin(), sorted_dirs.end(), [](const DirEntry& a, const DirEntry& b) { + return std::lexicographical_compare( + a.path_str.begin(), a.path_str.end(), b.path_str.begin(), b.path_str.end(), + [](char a_char, char b_char) { return std::tolower(a_char) < std::tolower(b_char); }); + }); + + for (const auto& entry : sorted_dirs) { + install_dirs.push_back(entry.path_str); + install_dirs_enabled.push_back(entry.enabled); + } + data["GUI"]["installDirs"] = install_dirs; + data["GUI"]["installDirsEnabled"] = install_dirs_enabled; data["GUI"]["saveDataPath"] = std::string{fmt::UTF(save_data_path.u8string()).data}; data["GUI"]["loadGameSizeEnabled"] = load_game_size; @@ -940,9 +1038,13 @@ void save(const std::filesystem::path& path) { data["GUI"]["showBackgroundImage"] = showBackgroundImage; data["Settings"]["consoleLanguage"] = m_language; + // Sorting of TOML sections + sortTomlSections(data); + std::ofstream file(path, std::ios::binary); file << data; file.close(); + saveMainWindow(path); } @@ -984,6 +1086,9 @@ void saveMainWindow(const std::filesystem::path& path) { data["GUI"]["elfDirs"] = m_elf_viewer; data["GUI"]["recentFiles"] = m_recent_files; + // Sorting of TOML sections + sortTomlSections(data); + std::ofstream file(path, std::ios::binary); file << data; file.close(); diff --git a/src/common/config.h b/src/common/config.h index 82d65d30e..e495be52a 100644 --- a/src/common/config.h +++ b/src/common/config.h @@ -9,6 +9,11 @@ namespace Config { +struct GameInstallDir { + std::filesystem::path path; + bool enabled; +}; + enum HideCursorState : s16 { Never, Idle, Always }; void load(const std::filesystem::path& path); @@ -98,7 +103,8 @@ void setUserName(const std::string& type); void setUpdateChannel(const std::string& type); void setChooseHomeTab(const std::string& type); void setSeparateUpdateEnabled(bool use); -void setGameInstallDirs(const std::vector& settings_install_dirs_config); +void setGameInstallDirs(const std::vector& dirs_config); +void setAllGameInstallDirs(const std::vector& dirs_config); void setSaveDataPath(const std::filesystem::path& path); void setCompatibilityEnabled(bool use); void setCheckCompatibilityOnStartup(bool use); @@ -133,8 +139,9 @@ void setVkGuestMarkersEnabled(bool enable); // Gui void setMainWindowGeometry(u32 x, u32 y, u32 w, u32 h); -bool addGameInstallDir(const std::filesystem::path& dir); +bool addGameInstallDir(const std::filesystem::path& dir, bool enabled = true); void removeGameInstallDir(const std::filesystem::path& dir); +void setGameInstallDirEnabled(const std::filesystem::path& dir, bool enabled); void setAddonInstallDir(const std::filesystem::path& dir); void setMainWindowTheme(u32 theme); void setIconSize(u32 size); @@ -153,7 +160,8 @@ u32 getMainWindowGeometryX(); u32 getMainWindowGeometryY(); u32 getMainWindowGeometryW(); u32 getMainWindowGeometryH(); -const std::vector& getGameInstallDirs(); +const std::vector getGameInstallDirs(); +const std::vector& getAllGameInstallDirs(); std::filesystem::path getAddonInstallDir(); u32 getMainWindowTheme(); u32 getIconSize(); diff --git a/src/qt_gui/settings_dialog.cpp b/src/qt_gui/settings_dialog.cpp index 41caccec9..a8beef8da 100644 --- a/src/qt_gui/settings_dialog.cpp +++ b/src/qt_gui/settings_dialog.cpp @@ -246,12 +246,13 @@ SettingsDialog::SettingsDialog(std::span physical_devices, // PATH TAB { connect(ui->addFolderButton, &QPushButton::clicked, this, [this]() { - const auto config_dir = Config::getGameInstallDirs(); QString file_path_string = QFileDialog::getExistingDirectory(this, tr("Directory to install games")); auto file_path = Common::FS::PathFromQString(file_path_string); - if (!file_path.empty() && Config::addGameInstallDir(file_path)) { + if (!file_path.empty() && Config::addGameInstallDir(file_path, true)) { QListWidgetItem* item = new QListWidgetItem(file_path_string); + item->setFlags(item->flags() | Qt::ItemIsUserCheckable); + item->setCheckState(Qt::Checked); ui->gameFoldersListWidget->addItem(item); } }); @@ -783,6 +784,17 @@ void SettingsDialog::UpdateSettings() { emit BackgroundOpacityChanged(ui->backgroundImageOpacitySlider->value()); Config::setShowBackgroundImage(ui->showBackgroundImageCheckBox->isChecked()); + std::vector dirs_with_states; + for (int i = 0; i < ui->gameFoldersListWidget->count(); i++) { + QListWidgetItem* item = ui->gameFoldersListWidget->item(i); + QString path_string = item->text(); + auto path = Common::FS::PathFromQString(path_string); + bool enabled = (item->checkState() == Qt::Checked); + + dirs_with_states.push_back({path, enabled}); + } + Config::setAllGameInstallDirs(dirs_with_states); + #ifdef ENABLE_DISCORD_RPC auto* rpc = Common::Singleton::Instance(); if (Config::getEnableDiscordRPC()) { @@ -797,6 +809,7 @@ void SettingsDialog::UpdateSettings() { } void SettingsDialog::ResetInstallFolders() { + ui->gameFoldersListWidget->clear(); std::filesystem::path userdir = Common::FS::GetUserPath(Common::FS::PathType::UserDir); const toml::value data = toml::parse(userdir / "config.toml"); @@ -805,21 +818,36 @@ void SettingsDialog::ResetInstallFolders() { const toml::value& gui = data.at("GUI"); const auto install_dir_array = toml::find_or>(gui, "installDirs", {}); - std::vector settings_install_dirs_config = {}; - for (const auto& dir : install_dir_array) { - if (std::find(settings_install_dirs_config.begin(), settings_install_dirs_config.end(), - dir) == settings_install_dirs_config.end()) { - settings_install_dirs_config.push_back(dir); - } + std::vector install_dirs_enabled; + try { + install_dirs_enabled = toml::find>(gui, "installDirsEnabled"); + } catch (...) { + // If it does not exist, assume that all are enabled. + install_dirs_enabled.resize(install_dir_array.size(), true); } - for (const auto& dir : settings_install_dirs_config) { + if (install_dirs_enabled.size() < install_dir_array.size()) { + install_dirs_enabled.resize(install_dir_array.size(), true); + } + + std::vector settings_install_dirs_config; + + for (size_t i = 0; i < install_dir_array.size(); i++) { + std::filesystem::path dir = install_dir_array[i]; + bool enabled = install_dirs_enabled[i]; + + settings_install_dirs_config.push_back({dir, enabled}); + QString path_string; Common::FS::PathToQString(path_string, dir); + QListWidgetItem* item = new QListWidgetItem(path_string); + item->setFlags(item->flags() | Qt::ItemIsUserCheckable); + item->setCheckState(enabled ? Qt::Checked : Qt::Unchecked); ui->gameFoldersListWidget->addItem(item); } - Config::setGameInstallDirs(settings_install_dirs_config); + + Config::setAllGameInstallDirs(settings_install_dirs_config); } }