From 617a6005451aba1d45f13228f13f1cc150a78bb4 Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Fri, 7 Jun 2019 18:24:41 -0400 Subject: ExecutablesList: - merged addExecutable(), updateExecutable() and addExecutableInternal() into a new setExecutable() - setExecutable() adds the executable to the list if not found, or forwards to Executable::mergeFrom() - mergeFrom() handles merging from/to plugin executables --- src/editexecutablesdialog.cpp | 24 ++++---- src/executableslist.cpp | 125 ++++++++++++++++++------------------------ src/executableslist.h | 48 +++------------- src/mainwindow.cpp | 10 ++-- 4 files changed, 77 insertions(+), 130 deletions(-) (limited to 'src') diff --git a/src/editexecutablesdialog.cpp b/src/editexecutablesdialog.cpp index 9dbc6bae..83756ac8 100644 --- a/src/editexecutablesdialog.cpp +++ b/src/editexecutablesdialog.cpp @@ -69,7 +69,7 @@ ExecutablesList EditExecutablesDialog::getExecutablesList() const continue; } - newList.addExecutable(*itor); + newList.setExecutable(*itor); } return newList; @@ -138,17 +138,17 @@ void EditExecutablesDialog::resetInput() void EditExecutablesDialog::saveExecutable() { - m_ExecutablesList.updateExecutable( - ui->titleEdit->text(), - QDir::fromNativeSeparators(ui->binaryEdit->text()), - ui->argumentsEdit->text(), - QDir::fromNativeSeparators(ui->workingDirEdit->text()), - ui->overwriteAppIDBox->isChecked() ? - ui->appIDOverwriteEdit->text() : "", - Executable::UseApplicationIcon | Executable::CustomExecutable, - (ui->useAppIconCheckBox->isChecked() ? - Executable::UseApplicationIcon : Executable::Flags()) - | Executable::CustomExecutable); + Executable::Flags flags = Executable::CustomExecutable; + if (ui->useAppIconCheckBox->isChecked()) + flags |= Executable::UseApplicationIcon; + + m_ExecutablesList.setExecutable({ + ui->titleEdit->text(), + QDir::fromNativeSeparators(ui->binaryEdit->text()), + ui->argumentsEdit->text(), + QDir::fromNativeSeparators(ui->workingDirEdit->text()), + ui->overwriteAppIDBox->isChecked() ? ui->appIDOverwriteEdit->text() : "", + flags}); if (ui->newFilesModCheckBox->isChecked()) { m_Profile->storeSetting("custom_overwrites", ui->titleEdit->text(), diff --git a/src/executableslist.cpp b/src/executableslist.cpp index 43a30f02..062ee28e 100644 --- a/src/executableslist.cpp +++ b/src/executableslist.cpp @@ -81,11 +81,13 @@ void ExecutablesList::load(const MOBase::IPluginGame* game, QSettings& settings) if (settings.value("ownicon", false).toBool()) flags |= Executable::UseApplicationIcon; - addExecutable( - settings.value("title").toString(), settings.value("binary").toString(), + setExecutable({ + settings.value("title").toString(), + settings.value("binary").toString(), settings.value("arguments").toString(), settings.value("workingDirectory", "").toString(), - settings.value("steamAppID", "").toString(), flags); + settings.value("steamAppID", "").toString(), + flags}); } settings.endArray(); @@ -123,11 +125,13 @@ void ExecutablesList::addFromPlugin(IPluginGame const *game) for (const ExecutableInfo &info : game->executables()) { if (info.isValid()) { - addExecutableInternal(info.title(), - info.binary().absoluteFilePath(), - info.arguments().join(" "), - info.workingDirectory().absolutePath(), - info.steamAppID()); + setExecutable({ + info.title(), + info.binary().absoluteFilePath(), + info.arguments().join(" "), + info.workingDirectory().absolutePath(), + info.steamAppID(), + Executable::UseApplicationIcon}); } } @@ -135,11 +139,13 @@ void ExecutablesList::addFromPlugin(IPluginGame const *game) .withArgument(QString("\"%1\"").arg(QDir::toNativeSeparators(game->dataDirectory().absolutePath()))); if (explorerpp.isValid()) { - addExecutableInternal(explorerpp.title(), + setExecutable({ + explorerpp.title(), explorerpp.binary().absoluteFilePath(), explorerpp.arguments().join(" "), explorerpp.workingDirectory().absolutePath(), - explorerpp.steamAppID()); + explorerpp.steamAppID(), + Executable::UseApplicationIcon}); } } @@ -185,60 +191,17 @@ bool ExecutablesList::titleExists(const QString &title) const return std::find_if(m_Executables.begin(), m_Executables.end(), test) != m_Executables.end(); } -void ExecutablesList::addExecutable(const Executable &executable) +void ExecutablesList::setExecutable(const Executable &executable) { - auto existingExe = find(executable.title()); - if (existingExe != m_Executables.end()) { - *existingExe = executable; - } else { - m_Executables.push_back(executable); - } -} + auto itor = find(executable.title()); -void ExecutablesList::updateExecutable(const QString &title, - const QString &executableName, - const QString &arguments, - const QString &workingDirectory, - const QString &steamAppID, - Executable::Flags mask, - Executable::Flags flags) -{ - QFileInfo file(executableName); - QDir dir(workingDirectory); - auto existingExe = find(title); - flags &= mask; - - if (existingExe != m_Executables.end()) { - existingExe->setTitle(title); - - auto newFlags = existingExe->flags(); - newFlags &= ~mask; - newFlags |= flags; - - existingExe->setFlags(newFlags); - - // for pre-configured executables don't overwrite settings we didn't store - if (flags & Executable::CustomExecutable) { - if (file.exists()) { - // don't overwrite a valid binary with an invalid one - existingExe->setBinaryInfo(file); - } - - if (dir.exists()) { - // don't overwrite a valid working directory with an invalid one - existingExe->setWorkingDirectory(workingDirectory); - } - existingExe->setArguments(arguments); - existingExe->setSteamAppID(steamAppID); - } + if (itor == m_Executables.end()) { + m_Executables.push_back(executable); } else { - m_Executables.push_back({ - title, file, arguments, workingDirectory, steamAppID, - Executable::CustomExecutable | flags}); + itor->mergeFrom(executable); } } - void ExecutablesList::remove(const QString &title) { for (std::vector::iterator iter = m_Executables.begin(); iter != m_Executables.end(); ++iter) { @@ -250,19 +213,6 @@ void ExecutablesList::remove(const QString &title) } -void ExecutablesList::addExecutableInternal(const QString &title, const QString &executableName, - const QString &arguments, const QString &workingDirectory, - const QString &steamAppID) -{ - QFileInfo file(executableName); - if (file.exists()) { - m_Executables.push_back({ - title, file, arguments, steamAppID, - workingDirectory, Executable::UseApplicationIcon}); - } -} - - Executable::Executable( QString title, QFileInfo binaryInfo, QString arguments, QString steamAppID, QString workingDirectory, Flags flags) : @@ -358,3 +308,36 @@ bool Executable::usesOwnIcon() const { return m_flags.testFlag(UseApplicationIcon); } + +void Executable::mergeFrom(const Executable& other) +{ + if (!isCustom()) { + // this happens when the user is trying to modify a plugin executable + + // only change some of the flags + const auto allow = ShowInToolbar; + + m_flags |= (other.flags() & allow); + } else { + // this happens after executables are loaded from settings and plugin + // executables are being added, or when users are modifying executables + + m_title = other.title(); + m_arguments = other.arguments(); + m_steamAppID = other.steamAppID(); + m_workingDirectory = other.workingDirectory(); + + // don't overwrite a valid binary with an invalid one + if (other.binaryInfo().exists()) { + m_binaryInfo = other.binaryInfo(); + } + + if (!other.isCustom()) { + // overwriting a custom executable with a plugin, merge all the flags + m_flags |= other.flags(); + } else { + // overwriting a custom with another custom, just replace the flags + m_flags = other.flags(); + } + } +} diff --git a/src/executableslist.h b/src/executableslist.h index 136f70bf..4965dbf9 100644 --- a/src/executableslist.h +++ b/src/executableslist.h @@ -73,6 +73,8 @@ public: void setShownOnToolbar(bool state); bool usesOwnIcon() const; + void mergeFrom(const Executable& other); + private: QString m_title; QFileInfo m_binaryInfo; @@ -155,57 +157,21 @@ public: * @brief add a new executable to the list * @param executable */ - void addExecutable(const Executable &executable); - - /** - * @brief add a new executable to the list - * - * @param title name displayed in the UI - * @param executableName the actual filename to execute - * @param arguments arguments to pass to the executable - **/ - void addExecutable(const QString &title, - const QString &executableName, - const QString &arguments, - const QString &workingDirectory, - const QString &steamAppID, - Executable::Flags flags) - { - updateExecutable(title, executableName, arguments, workingDirectory, - steamAppID, Executable::AllFlags, flags); - } - - /** - * @brief Update an executable to the list - * - * @param title name displayed in the UI - * @param executableName the actual filename to execute - * @param arguments arguments to pass to the executable - * @param closeMO if true, MO will be closed when the binary is started - **/ - void updateExecutable(const QString &title, - const QString &executableName, - const QString &arguments, - const QString &workingDirectory, - const QString &steamAppID, - Executable::Flags mask, - Executable::Flags flags); + void setExecutable(const Executable &executable); /** - * @brief remove the executable with the specified file name. This needs to be an absolute file path + * @brief remove the executable with the specified file name. This needs to + * be an absolute file path * * @param title title of the executable to remove - * @note if the executable name is invalid, nothing happens. There is no way to determine if this was successful + * @note if the executable name is invalid, nothing happens. There is no way + * to determine if this was successful **/ void remove(const QString &title); private: std::vector m_Executables; - void addExecutableInternal(const QString &title, const QString &executableName, const QString &arguments, - const QString &workingDirectory, - const QString &steamAppID); - /** * @brief add the executables preconfigured for this game **/ diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 2fbdbec7..926ba43c 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -5507,12 +5507,10 @@ void MainWindow::addAsExecutable() if (!name.isEmpty()) { //Note: If this already exists, you'll lose custom settings - m_OrganizerCore.executablesList()->addExecutable(name, - binaryInfo.absoluteFilePath(), - arguments, - targetInfo.absolutePath(), - QString(), - Executable::CustomExecutable); + m_OrganizerCore.executablesList()->setExecutable({ + name, binaryInfo, arguments, targetInfo.absolutePath(), QString(), + Executable::CustomExecutable}); + refreshExecutablesList(); } -- cgit v1.3.1