From dc99d3ebfc5ce69ed08a1b83129e99305851c1aa Mon Sep 17 00:00:00 2001 From: KrisCris <38860226+KrisCris@users.noreply.github.com> Date: Fri, 11 Oct 2024 14:27:28 +0800 Subject: [PATCH] Slightly refactor the game install dirs code (#1329) * fix: game dir dupes on each launch * fix copy around settings_addon_install_dir vector * do not show the path if it is not added * remove installDir from config file * format * moved migration code to save, and added notice * move ui->removeFolderButton->setEnabled to LoadValuesFromConfig * avoid checking duplicates for gameDirs loaded from config * use else condition to switch to the installDirs --- src/common/config.cpp | 38 ++++++++++++++++++------------ src/common/config.h | 5 ++-- src/qt_gui/game_install_dialog.cpp | 5 +--- src/qt_gui/settings_dialog.cpp | 24 ++++--------------- 4 files changed, 32 insertions(+), 40 deletions(-) diff --git a/src/common/config.cpp b/src/common/config.cpp index 37e51c65..78da372e 100644 --- a/src/common/config.cpp +++ b/src/common/config.cpp @@ -325,9 +325,19 @@ void setMainWindowGeometry(u32 x, u32 y, u32 w, u32 h) { main_window_geometry_w = w; main_window_geometry_h = h; } -void setGameInstallDirs(const std::vector& dir) { - settings_install_dirs.resize(dir.size()); - settings_install_dirs = dir; +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; + } + return false; +} +void removeGameInstallDir(const std::filesystem::path& dir) { + auto iterator = std::find(settings_install_dirs.begin(), settings_install_dirs.end(), dir); + if (iterator != settings_install_dirs.end()) { + settings_install_dirs.erase(iterator); + } } void setAddonInstallDir(const std::filesystem::path& dir) { settings_addon_install_dir = dir; @@ -385,7 +395,7 @@ u32 getMainWindowGeometryW() { u32 getMainWindowGeometryH() { return main_window_geometry_h; } -std::vector getGameInstallDirs() { +const std::vector& getGameInstallDirs() { return settings_install_dirs; } std::filesystem::path getAddonInstallDir() { @@ -525,19 +535,14 @@ void load(const std::filesystem::path& path) { m_window_size_W = toml::find_or(gui, "mw_width", 0); m_window_size_H = toml::find_or(gui, "mw_height", 0); + // TODO Migration code, after a major release this should be removed. auto old_game_install_dir = toml::find_fs_path_or(gui, "installDir", {}); if (!old_game_install_dir.empty()) { - settings_install_dirs.push_back(old_game_install_dir); - data.as_table().erase("installDir"); - } - - const auto install_dir_array = - toml::find_or>(gui, "installDirs", {}); - for (const auto& dir : install_dir_array) { - bool not_already_included = - std::find(settings_install_dirs.begin(), settings_install_dirs.end(), dir) == - settings_install_dirs.end(); - if (not_already_included) { + settings_install_dirs.emplace_back(std::filesystem::path{old_game_install_dir}); + } else { + const auto install_dir_array = + toml::find_or>(gui, "installDirs", {}); + for (const auto& dir : install_dir_array) { settings_install_dirs.emplace_back(std::filesystem::path{dir}); } } @@ -639,6 +644,9 @@ void save(const std::filesystem::path& path) { data["Settings"]["consoleLanguage"] = m_language; + // TODO Migration code, after a major release this should be removed. + data.at("GUI").as_table().erase("installDir"); + std::ofstream file(path, std::ios::out); file << data; file.close(); diff --git a/src/common/config.h b/src/common/config.h index 8e799b55..591d6dce 100644 --- a/src/common/config.h +++ b/src/common/config.h @@ -84,7 +84,8 @@ bool vkCrashDiagnosticEnabled(); // Gui void setMainWindowGeometry(u32 x, u32 y, u32 w, u32 h); -void setGameInstallDirs(const std::vector& dir); +bool addGameInstallDir(const std::filesystem::path& dir); +void removeGameInstallDir(const std::filesystem::path& dir); void setAddonInstallDir(const std::filesystem::path& dir); void setMainWindowTheme(u32 theme); void setIconSize(u32 size); @@ -103,7 +104,7 @@ u32 getMainWindowGeometryX(); u32 getMainWindowGeometryY(); u32 getMainWindowGeometryW(); u32 getMainWindowGeometryH(); -std::vector getGameInstallDirs(); +const std::vector& getGameInstallDirs(); std::filesystem::path getAddonInstallDir(); u32 getMainWindowTheme(); u32 getIconSize(); diff --git a/src/qt_gui/game_install_dialog.cpp b/src/qt_gui/game_install_dialog.cpp index 4418d9a4..e53c5831 100644 --- a/src/qt_gui/game_install_dialog.cpp +++ b/src/qt_gui/game_install_dialog.cpp @@ -126,10 +126,7 @@ void GameInstallDialog::Save() { return; } } - - std::vector install_dirs; - install_dirs.emplace_back(Common::FS::PathFromQString(gamesDirectory)); - Config::setGameInstallDirs(install_dirs); + Config::addGameInstallDir(Common::FS::PathFromQString(gamesDirectory)); Config::setAddonInstallDir(Common::FS::PathFromQString(addonsDirectory)); const auto config_dir = Common::FS::GetUserPath(Common::FS::PathType::UserDir); Config::save(config_dir / "config.toml"); diff --git a/src/qt_gui/settings_dialog.cpp b/src/qt_gui/settings_dialog.cpp index 1cc5a85e..efc2cd3e 100644 --- a/src/qt_gui/settings_dialog.cpp +++ b/src/qt_gui/settings_dialog.cpp @@ -218,19 +218,12 @@ SettingsDialog::SettingsDialog(std::span physical_devices, QWidge // PATH TAB { - ui->removeFolderButton->setEnabled(false); - 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); - bool not_already_included = - std::find(config_dir.begin(), config_dir.end(), file_path) == config_dir.end(); - if (!file_path.empty() && not_already_included) { - std::vector install_dirs = config_dir; - install_dirs.push_back(file_path); - Config::setGameInstallDirs(install_dirs); + if (!file_path.empty() && Config::addGameInstallDir(file_path)) { QListWidgetItem* item = new QListWidgetItem(file_path_string); ui->gameFoldersListWidget->addItem(item); } @@ -246,17 +239,8 @@ SettingsDialog::SettingsDialog(std::span physical_devices, QWidge QString item_path_string = selected_item ? selected_item->text() : QString(); if (!item_path_string.isEmpty()) { auto file_path = Common::FS::PathFromQString(item_path_string); - std::vector install_dirs = Config::getGameInstallDirs(); - - auto iterator = std::remove_if( - install_dirs.begin(), install_dirs.end(), - [&file_path](const std::filesystem::path& dir) { return file_path == dir; }); - - if (iterator != install_dirs.end()) { - install_dirs.erase(iterator, install_dirs.end()); - delete selected_item; - } - Config::setGameInstallDirs(install_dirs); + Config::removeGameInstallDir(file_path); + delete selected_item; } }); } @@ -370,6 +354,8 @@ void SettingsDialog::LoadValuesFromConfig() { QString backButtonBehavior = QString::fromStdString(Config::getBackButtonBehavior()); int index = ui->backButtonBehaviorComboBox->findData(backButtonBehavior); ui->backButtonBehaviorComboBox->setCurrentIndex(index != -1 ? index : 0); + + ui->removeFolderButton->setEnabled(!ui->gameFoldersListWidget->selectedItems().isEmpty()); } void SettingsDialog::InitializeEmulatorLanguages() {