fix copy around settings_addon_install_dir vector

This commit is contained in:
connlost 2024-10-10 22:39:11 +08:00
parent 85c48051f1
commit 420e4c04ad
4 changed files with 22 additions and 27 deletions

View File

@ -325,9 +325,17 @@ 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<std::filesystem::path>& dir) {
settings_install_dirs.resize(dir.size());
settings_install_dirs = dir;
void 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);
}
}
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 +393,7 @@ u32 getMainWindowGeometryW() {
u32 getMainWindowGeometryH() {
return main_window_geometry_h;
}
std::vector<std::filesystem::path> getGameInstallDirs() {
const std::vector<std::filesystem::path>& getGameInstallDirs() {
return settings_install_dirs;
}
std::filesystem::path getAddonInstallDir() {
@ -527,14 +535,14 @@ void load(const std::filesystem::path& path) {
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);
addGameInstallDir(old_game_install_dir);
gui.as_table().erase("installDir");
}
const auto install_dir_array =
toml::find_or<std::vector<std::string>>(gui, "installDirs", {});
for (const auto& dir : install_dir_array) {
settings_install_dirs.emplace_back(std::filesystem::path{dir});
addGameInstallDir(dir);
}
settings_addon_install_dir = toml::find_fs_path_or(gui, "addonInstallDir", {});

View File

@ -85,7 +85,8 @@ bool vkCrashDiagnosticEnabled();
// Gui
void setMainWindowGeometry(u32 x, u32 y, u32 w, u32 h);
void setGameInstallDirs(const std::vector<std::filesystem::path>& dir);
void 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);
@ -104,7 +105,7 @@ u32 getMainWindowGeometryX();
u32 getMainWindowGeometryY();
u32 getMainWindowGeometryW();
u32 getMainWindowGeometryH();
std::vector<std::filesystem::path> getGameInstallDirs();
const std::vector<std::filesystem::path>& getGameInstallDirs();
std::filesystem::path getAddonInstallDir();
u32 getMainWindowTheme();
u32 getIconSize();

View File

@ -126,10 +126,7 @@ void GameInstallDialog::Save() {
return;
}
}
std::vector<std::filesystem::path> 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");

View File

@ -236,9 +236,7 @@ SettingsDialog::SettingsDialog(std::span<const QString> physical_devices, QWidge
QFileDialog::getExistingDirectory(this, tr("Directory to install games"));
auto file_path = Common::FS::PathFromQString(file_path_string);
if (!file_path.empty()) {
std::vector<std::filesystem::path> install_dirs = Config::getGameInstallDirs();
install_dirs.push_back(file_path);
Config::setGameInstallDirs(install_dirs);
Config::addGameInstallDir(file_path);
QListWidgetItem* item = new QListWidgetItem(file_path_string);
ui->gameFoldersListWidget->addItem(item);
}
@ -254,17 +252,8 @@ SettingsDialog::SettingsDialog(std::span<const QString> 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<std::filesystem::path> 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;
}
});
}