diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/installationmanager.cpp | 32 | ||||
| -rw-r--r-- | src/installationmanager.h | 17 | ||||
| -rw-r--r-- | src/iuserinterface.h | 2 | ||||
| -rw-r--r-- | src/mainwindow.cpp | 155 | ||||
| -rw-r--r-- | src/mainwindow.h | 14 | ||||
| -rw-r--r-- | src/mainwindow.ui | 22 | ||||
| -rw-r--r-- | src/organizercore.cpp | 18 | ||||
| -rw-r--r-- | src/organizercore.h | 5 | ||||
| -rw-r--r-- | src/organizerproxy.cpp | 40 | ||||
| -rw-r--r-- | src/organizerproxy.h | 12 | ||||
| -rw-r--r-- | src/plugincontainer.cpp | 549 | ||||
| -rw-r--r-- | src/plugincontainer.h | 249 | ||||
| -rw-r--r-- | src/previewgenerator.cpp | 11 | ||||
| -rw-r--r-- | src/previewgenerator.h | 6 | ||||
| -rw-r--r-- | src/problemsdialog.cpp | 15 | ||||
| -rw-r--r-- | src/problemsdialog.h | 5 | ||||
| -rw-r--r-- | src/proxyutils.h | 2 | ||||
| -rw-r--r-- | src/settings.cpp | 23 | ||||
| -rw-r--r-- | src/settingsdialog.ui | 10 | ||||
| -rw-r--r-- | src/settingsdialogplugins.cpp | 162 | ||||
| -rw-r--r-- | src/settingsdialogplugins.h | 7 |
21 files changed, 1112 insertions, 244 deletions
diff --git a/src/installationmanager.cpp b/src/installationmanager.cpp index e9773a95..06909994 100644 --- a/src/installationmanager.cpp +++ b/src/installationmanager.cpp @@ -78,7 +78,8 @@ static T resolveFunction(QLibrary &lib, const char *name) } InstallationManager::InstallationManager() - : m_ParentWidget(nullptr), + : + m_ParentWidget(nullptr), m_SupportedExtensions({"zip", "rar", "7z", "fomod", "001"}), m_IsRunning(false) { m_ArchiveHandler = CreateArchive(); @@ -106,7 +107,7 @@ InstallationManager::InstallationManager() // Connect the query password slot - This is the only way I found to be able to query user // from a separate thread. We use a BlockingQueuedConnection so that calling passwordRequested() // will block until the end of the slot. - connect(this, &InstallationManager::passwordRequested, + connect(this, &InstallationManager::passwordRequested, this, &InstallationManager::queryPassword, Qt::BlockingQueuedConnection); } @@ -122,6 +123,11 @@ void InstallationManager::setParentWidget(QWidget *widget) } } +void InstallationManager::setPluginContainer(const PluginContainer* pluginContainer) +{ + m_PluginContainer = pluginContainer; +} + void InstallationManager::queryPassword() { m_Password = QInputDialog::getText(m_ParentWidget, tr("Password required"), tr("Password"), QLineEdit::Password); @@ -284,7 +290,7 @@ QStringList InstallationManager::extractFiles(std::vector<std::shared_ptr<const } -QString InstallationManager::createFile(std::shared_ptr<const MOBase::FileTreeEntry> entry) +QString InstallationManager::createFile(std::shared_ptr<const MOBase::FileTreeEntry> entry) { // Use QTemporaryFile to create the temporary file with the given template: QTemporaryFile tempFile(QDir::cleanPath(QDir::tempPath() + QDir::separator() + "mo2-install")); @@ -308,11 +314,11 @@ QString InstallationManager::createFile(std::shared_ptr<const MOBase::FileTreeEn return QDir::toNativeSeparators(absPath); } -void InstallationManager::cleanCreatedFiles(std::shared_ptr<const MOBase::IFileTree> fileTree) +void InstallationManager::cleanCreatedFiles(std::shared_ptr<const MOBase::IFileTree> fileTree) { // We simply have to check if all the entries have fileTree as a parent: for (auto it = std::begin(m_CreatedFiles); it != std::end(m_CreatedFiles); ) { - + // Find the parent - Could this be in FileTreeEntry? bool found = false; { @@ -495,12 +501,12 @@ IPluginInstaller::EInstallResult InstallationManager::doInstall(GuessedValue<QSt if (QFile::exists(destPath)) { QFile::remove(destPath); } - + QDir dir = QFileInfo(destPath).absoluteDir(); if (!dir.exists()) { dir.mkpath("."); } - + QFile::copy(p.second, destPath); } @@ -691,7 +697,7 @@ IPluginInstaller::EInstallResult InstallationManager::install(const QString &fil } ON_BLOCK_EXIT(std::bind(&InstallationManager::postInstallCleanup, this)); - std::shared_ptr<IFileTree> filesTree = + std::shared_ptr<IFileTree> filesTree = archiveOpen ? ArchiveFileTree::makeTree(*m_ArchiveHandler) : nullptr; IPluginInstaller::EInstallResult installResult = IPluginInstaller::RESULT_NOTATTEMPTED; @@ -701,7 +707,7 @@ IPluginInstaller::EInstallResult InstallationManager::install(const QString &fil for (IPluginInstaller *installer : m_Installers) { // don't use inactive installers (installer can't be null here but vc static code analysis thinks it could) - if ((installer == nullptr) || !installer->isActive()) { + if ((installer == nullptr) || !m_PluginContainer->isEnabled(installer)) { continue; } @@ -731,7 +737,7 @@ IPluginInstaller::EInstallResult InstallationManager::install(const QString &fil if (p == nullptr) { throw IncompatibilityException(tr("Invalid file tree returned by plugin.")); } - + // Detach the file tree (this ensure the parent is null and call to path() // stops at this root): p->detach(); @@ -786,7 +792,7 @@ IPluginInstaller::EInstallResult InstallationManager::install(const QString &fil case IPluginInstaller::RESULT_SUCCESSCANCEL: { if (filesTree != nullptr) { auto iniTweakEntry = filesTree->find("INI Tweaks", FileTreeEntry::DIRECTORY); - hasIniTweaks = iniTweakEntry != nullptr + hasIniTweaks = iniTweakEntry != nullptr && !iniTweakEntry->astree()->empty(); } return IPluginInstaller::RESULT_SUCCESS; @@ -860,7 +866,7 @@ QStringList InstallationManager::getSupportedExtensions() const void InstallationManager::notifyInstallationStart(QString const& archive, bool reinstallation, ModInfo::Ptr currentMod) { for (auto* installer : m_Installers) { - if (installer->isActive()) { + if (m_PluginContainer->isEnabled(installer)) { installer->onInstallationStart(archive, reinstallation, currentMod.get()); } } @@ -871,7 +877,7 @@ void InstallationManager::notifyInstallationEnd( ModInfo::Ptr newMod) { for (auto* installer : m_Installers) { - if (installer->isActive()) { + if (m_PluginContainer->isEnabled(installer)) { installer->onInstallationEnd(result, newMod.get()); } } diff --git a/src/installationmanager.h b/src/installationmanager.h index 66f11eae..e1db5e1f 100644 --- a/src/installationmanager.h +++ b/src/installationmanager.h @@ -35,6 +35,7 @@ along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>. #include <errorcodes.h> #include "modinfo.h" +#include "plugincontainer.h" /** @@ -88,6 +89,11 @@ public: void setModsDirectory(const QString &modsDirectory) { m_ModsDirectory = modsDirectory; } /** + * + */ + void setPluginContainer(const PluginContainer* pluginContainer); + + /** * @brief update the directory where downloads are stored * @param downloadDirectory the download directory */ @@ -126,8 +132,8 @@ public: * @brief register an installer-plugin * @param the installer to register */ - void registerInstaller(MOBase::IPluginInstaller *installer); - + void registerInstaller(MOBase::IPluginInstaller *installer); + /** * @return the extensions of archives supported by this installation manager. */ @@ -167,7 +173,7 @@ public: * * The flatten argument is not present here while it is present in the deprecated QStringList * version for multiple reasons: 1) it was never used, 2) it is kind of fishy because there - * is no way to know if a file is going to be overriden, 3) it is quite easy to flatten a + * is no way to know if a file is going to be overriden, 3) it is quite easy to flatten a * IFileTree and thus to given a list of entries flattened (this was not possible with the * QStringList version since these were based on the name of the file inside the archive). */ @@ -185,7 +191,7 @@ public: * @return the path to the created file. */ virtual QString createFile(std::shared_ptr<const MOBase::FileTreeEntry> entry) override; - + /** * @brief Installs the given archive. * @@ -279,6 +285,9 @@ private: private: + // The plugin container, mostly to check if installer are enabled or not: + const PluginContainer *m_PluginContainer; + bool m_IsRunning; QWidget *m_ParentWidget; diff --git a/src/iuserinterface.h b/src/iuserinterface.h index cce89070..a2a91e62 100644 --- a/src/iuserinterface.h +++ b/src/iuserinterface.h @@ -13,8 +13,6 @@ class IUserInterface
{
public:
- virtual void registerPluginTool(MOBase::IPluginTool *tool, QString name = QString(), QMenu *menu = nullptr) = 0;
- virtual void registerPluginTools(std::vector<MOBase::IPluginTool *> toolPlugins) = 0;
virtual void registerModPage(MOBase::IPluginModPage *modPage) = 0;
virtual void installTranslator(const QString &name) = 0;
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 94bc5800..c927a691 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -260,7 +260,6 @@ MainWindow::MainWindow(Settings &settings , m_ContextItem(nullptr) , m_ContextAction(nullptr) , m_ContextRow(-1) - , m_browseModPage(nullptr) , m_CurrentSaveView(nullptr) , m_OrganizerCore(organizerCore) , m_PluginContainer(pluginContainer) @@ -467,6 +466,12 @@ MainWindow::MainWindow(Settings &settings connect(ui->toolBar, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(toolBar_customContextMenuRequested(QPoint))); connect(ui->menuToolbars, &QMenu::aboutToShow, [&]{ updateToolbarMenu(); }); connect(ui->menuView, &QMenu::aboutToShow, [&]{ updateViewMenu(); }); + connect(ui->actionTool->menu(), &QMenu::aboutToShow, [&] { updateToolMenu(); }); + connect(&m_PluginContainer, &PluginContainer::pluginEnabled, this, [this](IPlugin* plugin) { + if (m_PluginContainer.implementInterface<IPluginModPage>(plugin)) { updateModPageMenu(); } }); + connect(&m_PluginContainer, &PluginContainer::pluginDisabled, this, [this](IPlugin* plugin) { + if (m_PluginContainer.implementInterface<IPluginModPage>(plugin)) { updateModPageMenu(); } }); + connect(&m_OrganizerCore, &OrganizerCore::modInstalled, this, &MainWindow::modInstalled); connect(&m_OrganizerCore, &OrganizerCore::close, this, &QMainWindow::close); @@ -508,11 +513,7 @@ MainWindow::MainWindow(Settings &settings installTranslator(QFileInfo(fileName).baseName()); } - registerPluginTools(m_PluginContainer.plugins<IPluginTool>()); - - for (IPluginModPage *modPagePlugin : m_PluginContainer.plugins<IPluginModPage>()) { - registerModPage(modPagePlugin); - } + updateModPageMenu(); // refresh profiles so the current profile can be activated refreshProfiles(false); @@ -812,6 +813,7 @@ static QModelIndex mapToModel(const QAbstractItemModel *targetModel, QModelIndex void MainWindow::setupToolbar() { + setupActionMenu(ui->actionModPage); setupActionMenu(ui->actionTool); setupActionMenu(ui->actionHelp); setupActionMenu(ui->actionEndorseMO); @@ -1132,7 +1134,7 @@ void MainWindow::checkForProblemsImpl() size_t numProblems = 0; for (QObject *pluginObj : m_PluginContainer.plugins<QObject>()) { IPlugin *plugin = qobject_cast<IPlugin*>(pluginObj); - if (plugin == nullptr || plugin->isActive()) { + if (plugin == nullptr || m_PluginContainer.isEnabled(plugin)) { IPluginDiagnose *diagnose = qobject_cast<IPluginDiagnose*>(pluginObj); if (diagnose != nullptr) numProblems += diagnose->activeProblems().size(); @@ -1597,45 +1599,6 @@ bool MainWindow::eventFilter(QObject *object, QEvent *event) return false; } - -void MainWindow::toolPluginInvoke() -{ - QAction *triggeredAction = qobject_cast<QAction*>(sender()); - IPluginTool *plugin = qobject_cast<IPluginTool*>(triggeredAction->data().value<QObject*>()); - if (plugin != nullptr) { - try { - plugin->display(); - } catch (const std::exception &e) { - reportError(tr("Plugin \"%1\" failed: %2").arg(plugin->name()).arg(e.what())); - } catch (...) { - reportError(tr("Plugin \"%1\" failed").arg(plugin->name())); - } - } -} - -void MainWindow::modPagePluginInvoke() -{ - QAction *triggeredAction = qobject_cast<QAction*>(sender()); - IPluginModPage *plugin = qobject_cast<IPluginModPage*>(triggeredAction->data().value<QObject*>()); - if (plugin != nullptr) { - if (plugin->useIntegratedBrowser()) { - - if (!m_IntegratedBrowser) { - m_IntegratedBrowser.reset(new BrowserDialog); - - connect( - m_IntegratedBrowser.get(), SIGNAL(requestDownload(QUrl,QNetworkReply*)), - &m_OrganizerCore, SLOT(requestDownload(QUrl,QNetworkReply*))); - } - - m_IntegratedBrowser->setWindowTitle(plugin->displayName()); - m_IntegratedBrowser->openUrl(plugin->pageURL()); - } else { - QDesktopServices::openUrl(QUrl(plugin->pageURL())); - } - } -} - void MainWindow::registerPluginTool(IPluginTool *tool, QString name, QMenu *menu) { if (!menu) { @@ -1648,26 +1611,41 @@ void MainWindow::registerPluginTool(IPluginTool *tool, QString name, QMenu *menu QAction *action = new QAction(tool->icon(), name, menu); action->setToolTip(tool->tooltip()); tool->setParentWidget(this); - action->setData(QVariant::fromValue((QObject*)tool)); - connect(action, SIGNAL(triggered()), this, SLOT(toolPluginInvoke()), Qt::QueuedConnection); + connect(action, &QAction::triggered, this, [this, tool]() { + try { + tool->display(); + } + catch (const std::exception& e) { + reportError(tr("Plugin \"%1\" failed: %2").arg(tool->localizedName()).arg(e.what())); + } + catch (...) { + reportError(tr("Plugin \"%1\" failed").arg(tool->localizedName())); + } + }, Qt::QueuedConnection); menu->addAction(action); } -void MainWindow::registerPluginTools(std::vector<IPluginTool *> toolPlugins) +void MainWindow::updateToolMenu() { + // Clear the menu: + ui->actionTool->menu()->clear(); + + std::vector<IPluginTool*> toolPlugins = m_PluginContainer.plugins<IPluginTool>(); + // Sort the plugins by display name - std::sort(toolPlugins.begin(), toolPlugins.end(), + std::sort(std::begin(toolPlugins), std::end(toolPlugins), [](IPluginTool *left, IPluginTool *right) { return left->displayName().toLower() < right->displayName().toLower(); } ); - // Remove inactive plugins + // Remove disabled plugins: toolPlugins.erase( - std::remove_if(toolPlugins.begin(), toolPlugins.end(), [](IPluginTool *plugin) -> bool { return !plugin->isActive(); }), - toolPlugins.end() - ); + std::remove_if(std::begin(toolPlugins), std::end(toolPlugins), [&](auto* tool) { + return !m_PluginContainer.isEnabled(tool); + }), + toolPlugins.end()); // Group the plugins into submenus QMap<QString, QList<QPair<QString, IPluginTool *>>> submenuMap; @@ -1695,26 +1673,65 @@ void MainWindow::registerPluginTools(std::vector<IPluginTool *> toolPlugins) void MainWindow::registerModPage(IPluginModPage *modPage) { - // turn the browser action into a drop-down menu if necessary - if (!m_browseModPage) { - m_browseModPage = new QAction(ui->actionNexus->icon(), tr("Browse Mod Page"), this); - setupActionMenu(m_browseModPage); - - m_browseModPage->menu()->addAction(ui->actionNexus); - - ui->toolBar->insertAction(ui->actionNexus, m_browseModPage); - ui->toolBar->removeAction(ui->actionNexus); - } - QAction *action = new QAction(modPage->icon(), modPage->displayName(), this); modPage->setParentWidget(this); - action->setData(QVariant::fromValue(reinterpret_cast<QObject*>(modPage))); + connect(action, &QAction::triggered, this, [this, modPage]() { + if (modPage->useIntegratedBrowser()) { - connect(action, SIGNAL(triggered()), this, SLOT(modPagePluginInvoke()), Qt::QueuedConnection); + if (!m_IntegratedBrowser) { + m_IntegratedBrowser.reset(new BrowserDialog); - m_browseModPage->menu()->addAction(action); + connect( + m_IntegratedBrowser.get(), SIGNAL(requestDownload(QUrl, QNetworkReply*)), + &m_OrganizerCore, SLOT(requestDownload(QUrl, QNetworkReply*))); + } + + m_IntegratedBrowser->setWindowTitle(modPage->displayName()); + m_IntegratedBrowser->openUrl(modPage->pageURL()); + } + else { + QDesktopServices::openUrl(QUrl(modPage->pageURL())); + } + }, Qt::QueuedConnection); + + ui->actionModPage->menu()->addAction(action); } +void MainWindow::updateModPageMenu() +{ + // Clear the menu: + ui->actionModPage->menu()->clear(); + ui->actionModPage->menu()->addAction(ui->actionNexus); + + std::vector<IPluginModPage*> modPagePlugins = m_PluginContainer.plugins<IPluginModPage>(); + + // Sort the plugins by display name + std::sort(std::begin(modPagePlugins), std::end(modPagePlugins), + [](IPluginModPage* left, IPluginModPage* right) { + return left->displayName().toLower() < right->displayName().toLower(); + } + ); + + // Remove disabled plugins: + modPagePlugins.erase( + std::remove_if(std::begin(modPagePlugins), std::end(modPagePlugins), [&](auto* tool) { + return !m_PluginContainer.isEnabled(tool); + }), + modPagePlugins.end()); + + for (auto* modPagePlugin : modPagePlugins) { + registerModPage(modPagePlugin); + } + + // No mod page plugin and the menu was visible: + if (modPagePlugins.empty()) { + ui->toolBar->insertAction(ui->actionAdd_Profile, ui->actionNexus); + } + else { + ui->toolBar->removeAction(ui->actionNexus); + } + ui->actionModPage->setVisible(!modPagePlugins.empty()); +} void MainWindow::startExeAction() { @@ -5972,7 +5989,7 @@ void MainWindow::on_actionNotifications_triggered() future.waitForFinished(); - ProblemsDialog problems(m_PluginContainer.plugins<QObject>(), this); + ProblemsDialog problems(m_PluginContainer, this); problems.exec(); scheduleCheckForProblems(); diff --git a/src/mainwindow.h b/src/mainwindow.h index 8b2188c8..e2805b25 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -129,10 +129,6 @@ public: void saveArchiveList(); - void registerPluginTool(MOBase::IPluginTool *tool, QString name = QString(), QMenu *menu = nullptr); - void registerPluginTools(std::vector<MOBase::IPluginTool *> toolPlugins); - void registerModPage(MOBase::IPluginModPage *modPage); - void addPrimaryCategoryCandidates(QMenu *primaryCategoryMenu, ModInfo::Ptr info); void installTranslator(const QString &name); @@ -160,9 +156,6 @@ public slots: void directory_refreshed(); - void toolPluginInvoke(); - void modPagePluginInvoke(); - signals: /** @@ -212,7 +205,12 @@ private: void setToolbarSize(const QSize& s); void setToolbarButtonStyle(Qt::ToolButtonStyle s); + void registerModPage(MOBase::IPluginModPage* modPage); + void registerPluginTool(MOBase::IPluginTool* tool, QString name = QString(), QMenu* menu = nullptr); + void updateToolbarMenu(); + void updateToolMenu(); + void updateModPageMenu(); void updateViewMenu(); QMenu* createPopupMenu() override; @@ -326,8 +324,6 @@ private: QTreeWidgetItem *m_ContextItem; QAction *m_ContextAction; - QAction* m_browseModPage; - CategoryFactory &m_CategoryFactory; QTimer m_CheckBSATimer; diff --git a/src/mainwindow.ui b/src/mainwindow.ui index ace0dfeb..dbc6013d 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -1411,6 +1411,7 @@ p, li { white-space: pre-wrap; } <addaction name="actionChange_Game"/> <addaction name="actionInstallMod"/> <addaction name="actionNexus"/> + <addaction name="actionModPage"/> <addaction name="actionAdd_Profile"/> <addaction name="actionModify_Executables"/> <addaction name="actionTool"/> @@ -1684,6 +1685,27 @@ p, li { white-space: pre-wrap; } <string>Ctrl+N</string> </property> </action> + <action name="actionModPage"> + <property name="icon"> + <iconset resource="resources.qrc"> + <normaloff>:/MO/gui/resources/internet-web-browser.png</normaloff>:/MO/gui/resources/internet-web-browser.png</iconset> + </property> + <property name="text"> + <string>Browse Mod Page</string> + </property> + <property name="iconText"> + <string>Browse Mod Page</string> + </property> + <property name="toolTip"> + <string>Browse Mod Page</string> + </property> + <property name="statusTip"> + <string>Browse Mod Page</string> + </property> + <property name="visible"> + <bool>false</bool> + </property> + </action> <action name="actionUpdate"> <property name="enabled"> <bool>false</bool> diff --git a/src/organizercore.cpp b/src/organizercore.cpp index f57903e2..e004ba78 100644 --- a/src/organizercore.cpp +++ b/src/organizercore.cpp @@ -287,6 +287,7 @@ void OrganizerCore::connectPlugins(PluginContainer *container) m_InstallationManager.getSupportedExtensions()); m_PluginContainer = container; m_Updater.setPluginContainer(m_PluginContainer); + m_InstallationManager.setPluginContainer(m_PluginContainer); m_DownloadManager.setPluginContainer(m_PluginContainer); m_ModList.setPluginContainer(m_PluginContainer); @@ -294,6 +295,11 @@ void OrganizerCore::connectPlugins(PluginContainer *container) m_GamePlugin = m_PluginContainer->managedGame(m_GameName); emit managedGameChanged(m_GamePlugin); } + + connect(m_PluginContainer, &PluginContainer::pluginEnabled, + [&](IPlugin* plugin) { m_PluginEnabled(plugin); }); + connect(m_PluginContainer, &PluginContainer::pluginDisabled, + [&](IPlugin* plugin) { m_PluginDisabled(plugin); }); } void OrganizerCore::disconnectPlugins() @@ -1224,6 +1230,16 @@ bool OrganizerCore::onPluginSettingChanged(std::function<void(QString const&, co return m_PluginSettingChanged.connect(func).connected(); } +bool OrganizerCore::onPluginEnabled(std::function<void(const IPlugin*)> const& func) +{ + return m_PluginEnabled.connect(func).connected(); +} + +bool OrganizerCore::onPluginDisabled(std::function<void(const IPlugin*)> const& func) +{ + return m_PluginDisabled.connect(func).connected(); +} + void OrganizerCore::refresh(bool saveChanges) { // don't lose changes! @@ -2040,7 +2056,7 @@ std::vector<Mapping> OrganizerCore::fileMapping(const QString &profileName, for (MOBase::IPluginFileMapper *mapper : m_PluginContainer->plugins<MOBase::IPluginFileMapper>()) { IPlugin *plugin = dynamic_cast<IPlugin *>(mapper); - if (plugin->isActive()) { + if (m_PluginContainer->isEnabled(plugin)) { MappingType pluginMap = mapper->mappings(); result.reserve(result.size() + pluginMap.size()); result.insert(result.end(), pluginMap.begin(), pluginMap.end()); diff --git a/src/organizercore.h b/src/organizercore.h index b566e626..23b624e8 100644 --- a/src/organizercore.h +++ b/src/organizercore.h @@ -87,6 +87,7 @@ private: using SignalProfileRemoved = boost::signals2::signal<void(QString const&)>;
using SignalProfileChanged = boost::signals2::signal<void (MOBase::IProfile *, MOBase::IProfile *)>;
using SignalPluginSettingChanged = boost::signals2::signal<void (QString const&, const QString& key, const QVariant&, const QVariant&)>;
+ using SignalPluginEnabled = boost::signals2::signal<void(MOBase::IPlugin*)>;
public:
@@ -335,6 +336,8 @@ public: bool onProfileRemoved(std::function<void(QString const&)> const& func);
bool onProfileChanged(std::function<void(MOBase::IProfile*, MOBase::IProfile*)> const& func);
bool onPluginSettingChanged(std::function<void(QString const&, const QString& key, const QVariant&, const QVariant&)> const& func);
+ bool onPluginEnabled(std::function<void(const MOBase::IPlugin*)> const& func);
+ bool onPluginDisabled(std::function<void(const MOBase::IPlugin*)> const& func);
bool getArchiveParsing() const
{
@@ -457,6 +460,8 @@ private: SignalProfileRemoved m_ProfileRemoved;
SignalProfileChanged m_ProfileChanged;
SignalPluginSettingChanged m_PluginSettingChanged;
+ SignalPluginEnabled m_PluginEnabled;
+ SignalPluginEnabled m_PluginDisabled;
ModList m_ModList;
PluginList m_PluginList;
diff --git a/src/organizerproxy.cpp b/src/organizerproxy.cpp index a988ba9f..c2283acc 100644 --- a/src/organizerproxy.cpp +++ b/src/organizerproxy.cpp @@ -83,6 +83,16 @@ void OrganizerProxy::modDataChanged(IModInterface *mod) m_Proxied->modDataChanged(mod);
}
+bool OrganizerProxy::isPluginEnabled(QString const& pluginName) const
+{
+ return m_PluginContainer->isEnabled(pluginName);
+}
+
+bool OrganizerProxy::isPluginEnabled(IPlugin* plugin) const
+{
+ return m_PluginContainer->isEnabled(plugin);
+}
+
QVariant OrganizerProxy::pluginSetting(const QString &pluginName, const QString &key) const
{
return m_Proxied->pluginSetting(pluginName, key);
@@ -283,8 +293,36 @@ bool OrganizerProxy::onProfileChanged(std::function<void(MOBase::IProfile*, MOBa return m_Proxied->onProfileChanged(MOShared::callIfPluginActive(this, func));
}
+// Always call these one, otherwise plugin cannot detect they are being enabled / disabled:
bool OrganizerProxy::onPluginSettingChanged(std::function<void(QString const&, const QString& key, const QVariant&, const QVariant&)> const& func)
{
- // Always call this one, otherwise plugin cannot detect they are being enabled / disabled:
return m_Proxied->onPluginSettingChanged(func);
}
+
+bool OrganizerProxy::onPluginEnabled(std::function<void(const IPlugin*)> const& func)
+{
+ return m_Proxied->onPluginEnabled(func);
+}
+
+bool OrganizerProxy::onPluginEnabled(const QString& pluginName, std::function<void()> const& func)
+{
+ return m_Proxied->onPluginEnabled([=](const IPlugin* plugin) {
+ if (plugin->name().compare(pluginName, Qt::CaseInsensitive) == 0) {
+ func();
+ }
+ });
+}
+
+bool OrganizerProxy::onPluginDisabled(std::function<void(const IPlugin*)> const& func)
+{
+ return m_Proxied->onPluginDisabled(func);
+}
+
+bool OrganizerProxy::onPluginDisabled(const QString& pluginName, std::function<void()> const& func)
+{
+ return m_Proxied->onPluginDisabled([=](const IPlugin* plugin) {
+ if (plugin->name().compare(pluginName, Qt::CaseInsensitive) == 0) {
+ func();
+ }
+ });
+}
diff --git a/src/organizerproxy.h b/src/organizerproxy.h index 6690d612..8664c64f 100644 --- a/src/organizerproxy.h +++ b/src/organizerproxy.h @@ -35,8 +35,6 @@ public: virtual MOBase::IPluginGame *getGame(const QString &gameName) const;
virtual MOBase::IModInterface *createMod(MOBase::GuessedValue<QString> &name);
virtual void modDataChanged(MOBase::IModInterface *mod);
- virtual QVariant pluginSetting(const QString &pluginName, const QString &key) const;
- virtual void setPluginSetting(const QString &pluginName, const QString &key, const QVariant &value);
virtual QVariant persistent(const QString &pluginName, const QString &key, const QVariant &def = QVariant()) const;
virtual void setPersistent(const QString &pluginName, const QString &key, const QVariant &value, bool sync = true);
virtual QString pluginDataPath() const;
@@ -64,7 +62,17 @@ public: virtual bool onProfileRenamed(std::function<void(MOBase::IProfile*, QString const&, QString const&)> const& func) override;
virtual bool onProfileRemoved(std::function<void(QString const&)> const& func) override;
virtual bool onProfileChanged(std::function<void(MOBase::IProfile*, MOBase::IProfile*)> const& func) override;
+
+ // Plugin related:
+ virtual bool isPluginEnabled(QString const& pluginName) const override;
+ virtual bool isPluginEnabled(MOBase::IPlugin* plugin) const override;
+ virtual QVariant pluginSetting(const QString& pluginName, const QString& key) const override;
+ virtual void setPluginSetting(const QString& pluginName, const QString& key, const QVariant& value) override;
virtual bool onPluginSettingChanged(std::function<void(QString const&, const QString& key, const QVariant&, const QVariant&)> const& func) override;
+ virtual bool onPluginEnabled(std::function<void(const MOBase::IPlugin*)> const& func) override;
+ virtual bool onPluginEnabled(const QString& pluginName, std::function<void()> const& func) override;
+ virtual bool onPluginDisabled(std::function<void(const MOBase::IPlugin*)> const& func) override;
+ virtual bool onPluginDisabled(const QString& pluginName, std::function<void()> const& func) override;
virtual MOBase::IPluginGame const *managedGame() const;
diff --git a/src/plugincontainer.cpp b/src/plugincontainer.cpp index 5992f28d..03950413 100644 --- a/src/plugincontainer.cpp +++ b/src/plugincontainer.cpp @@ -21,6 +21,54 @@ using namespace MOShared; namespace bf = boost::fusion;
+// Welcome to the wonderful world of MO2 plugin management!
+//
+// We'll start by the C++ side.
+//
+// There are 9 types of MO2 plugins, two of which cannot be standalone: IPluginDiagnose
+// and IPluginFileMapper. This means that you can have a class implementing IPluginGame,
+// IPluginDiagnose and IPluginFileMapper. It is not possible for a class to implement
+// two full plugin types (e.g. IPluginPreview and IPluginTool).
+//
+// Plugins are fetch as QObject initially and must be "qobject-casted" to the right type.
+//
+// Plugins are stored in the PluginContainer class in various C++ containers: there is a vector
+// that stores all the plugin as QObject, multiple vectors that stores the plugin of each types,
+// a map to find IPlugin object from their names or from IPluginDiagnose or IFileMapper (since
+// these do not inherit IPlugin, they cannot be downcasted).
+//
+// Requirements for plugins are stored in m_Requirements:
+// - IPluginGame cannot be enabled by user. A game plugin is considered enable only if it is
+// the one corresponding to the currently managed games.
+// - If a plugin has a master plugin (IPlugin::master()), it cannot be enabled/disabled by users,
+// and will follow the enabled/disabled state of its parent.
+// - Each plugin has an "enabled" setting stored in persistence. A plugin is considered disabled
+// if the setting is false.
+// - If the setting is true or does not exist, a plugin is considered disabled if one of its
+// requirements is not met.
+// - Users cannot enable a plugin if one of its requirements is not met.
+//
+// Now let's move to the Proxy side... Or the as of now, the Python side.
+//
+// Proxied plugins are much more annoying because they can implement all interfaces, and are
+// given to MO2 as separate plugins... A Python class implementing IPluginGame and IPluginDiagnose
+// will be seen by MO2 as two separate QObject, and they will all have the same name.
+//
+// When a proxied plugin is registered, a few things must be taken care of:
+// - There can only be one plugin mapped to a name in the PluginContainer class, so we keep the
+// plugin corresponding to the most relevant class (see PluginTypeOrder), e.g. if the class
+// inherits both IPluginGame and IPluginFileMapper, we map the name to the C++ QObject corresponding
+// to the IPluginGame.
+// - When a proxied plugin implements multiple interfaces, the IPlugin corresponding to the most
+// important interface is set as the parent (hidden) of the other IPlugin through PluginRequirements.
+// This way, the plugin are managed together (enabled/disabled state). The "fake" children plugins
+// will not be returned by PluginRequirements::children().
+// - Since each interface corresponds to a different QObject, we need to take care not to call
+// IPlugin::init() on each QObject, but only on the first one.
+//
+// All the proxied plugins are linked to the proxy plugin by PluginRequirements. If the proxy plugin
+// is disabled, the proxied plugins are not even loaded so not visible in the plugin management tab.
+
template <class T>
struct PluginTypeName;
@@ -51,9 +99,176 @@ QStringList PluginContainer::pluginInterfaces() }
+// PluginRequirementProxy
+
+PluginRequirements::PluginRequirements(
+ PluginContainer* pluginContainer, MOBase::IPlugin* plugin, IOrganizer* proxy,
+ MOBase::IPluginProxy* pluginProxy)
+ : m_PluginContainer(pluginContainer)
+ , m_Plugin(plugin)
+ , m_PluginProxy(pluginProxy)
+ , m_Master(nullptr)
+ , m_Organizer(proxy)
+{
+ // There are a lots of things we cannot set here (e.g. m_Master) because we do not
+ // know the order plugins are loaded.
+}
+
+void PluginRequirements::fetchRequirements() {
+ for (auto* requirement : m_Plugin->requirements()) {
+ m_Requirements.emplace_back(requirement);
+ }
+}
+
+IPluginProxy* PluginRequirements::proxy() const
+{
+ return m_PluginProxy;
+}
+
+std::vector<IPlugin*> PluginRequirements::proxied() const
+{
+ std::vector<IPlugin*> children;
+ if (dynamic_cast<IPluginProxy*>(m_Plugin)) {
+ for (auto* obj : m_PluginContainer->plugins<QObject>()) {
+ auto* plugin = qobject_cast<IPlugin*>(obj);
+ if (plugin && m_PluginContainer->requirements(plugin).proxy() == m_Plugin) {
+ children.push_back(plugin);
+ }
+ }
+ }
+ return children;
+}
+
+IPlugin* PluginRequirements::master() const
+{
+ // If we have a m_Master, it was forced and thus override the default master().
+ if (m_Master) {
+ return m_Master;
+ }
+
+ if (m_Plugin->master().isEmpty()) {
+ return nullptr;
+ }
+
+ return m_PluginContainer->plugin(m_Plugin->master());
+}
+
+void PluginRequirements::setMaster(IPlugin* master)
+{
+ m_Master = master;
+}
+
+std::vector<IPlugin*> PluginRequirements::children() const
+{
+ std::vector<IPlugin*> children;
+ for (auto* obj : m_PluginContainer->plugins<QObject>()) {
+ auto* plugin = qobject_cast<IPlugin*>(obj);
+
+ // Not checking master() but requirements().master() due to "hidden"
+ // masters.
+ // If the master has the same name as the plugin, this is a "hidden"
+ // master, we do not had it here.
+ if (plugin
+ && m_PluginContainer->requirements(plugin).master() == m_Plugin
+ && plugin->name() != m_Plugin->name()) {
+ children.push_back(plugin);
+ }
+ }
+ return children;
+}
+
+std::vector<IPluginRequirement::Problem> PluginRequirements::problems() const
+{
+ std::vector<IPluginRequirement::Problem> result;
+ for (auto& requirement : m_Requirements) {
+ if (auto p = requirement->check(m_Organizer)) {
+ result.push_back(*p);
+ }
+ }
+ return result;
+}
+
+bool PluginRequirements::canEnable() const
+{
+ return problems().empty();
+}
+
+bool PluginRequirements::hasRequirements() const
+{
+ return !m_Requirements.empty();
+}
+
+QStringList PluginRequirements::requiredGames() const
+{
+ // We look for a "GameDependencyRequirement" - There can be only one since otherwise
+ // it'd mean that the plugin requires two games at once.
+ for (auto& requirement : m_Requirements) {
+ if (auto* gdep = dynamic_cast<const GameDependencyRequirement*>(requirement.get())) {
+ return gdep->gameNames();
+ }
+ }
+
+ return {};
+}
+
+std::vector<MOBase::IPlugin*> PluginRequirements::requiredFor() const
+{
+ std::vector<MOBase::IPlugin*> required;
+ std::set<MOBase::IPlugin*> visited;
+ requiredFor(required, visited);
+ return required;
+}
+
+
+void PluginRequirements::requiredFor(std::vector<MOBase::IPlugin*> &required, std::set<MOBase::IPlugin*>& visited) const
+{
+ // Handle cyclic dependencies.
+ if (visited.contains(m_Plugin)) {
+ return;
+ }
+ visited.insert(m_Plugin);
+
+
+ for (auto& [plugin, requirements] : m_PluginContainer->m_Requirements) {
+
+ // If the plugin is not enabled, discard:
+ if (!m_PluginContainer->isEnabled(plugin)) {
+ continue;
+ }
+
+ // Check the requirements:
+ for (auto& requirement : requirements.m_Requirements) {
+
+ // We check for plugin dependency. Game dependency are not checked this way.
+ if (auto* pdep = dynamic_cast<const PluginDependencyRequirement*>(requirement.get())) {
+
+ // Check if at least one of the plugin in the requirements is enabled (except this
+ // one):
+ bool oneEnabled = false;
+ for (auto& pluginName : pdep->pluginNames()) {
+ if (pluginName != m_Plugin->name() && m_PluginContainer->isEnabled(pluginName)) {
+ oneEnabled = true;
+ break;
+ }
+ }
+
+ // No plugin enabled found, so the plugin requires this plugin:
+ if (!oneEnabled) {
+ required.push_back(plugin);
+ requirements.requiredFor(required, visited);
+ break;
+ }
+ }
+ }
+ }
+}
+
+// PluginContainer
+
PluginContainer::PluginContainer(OrganizerCore *organizer)
: m_Organizer(organizer)
, m_UserInterface(nullptr)
+ , m_PreviewGenerator(this)
{
}
@@ -62,7 +277,6 @@ PluginContainer::~PluginContainer() { unloadPlugins();
}
-
void PluginContainer::setUserInterface(IUserInterface *userInterface, QWidget *widget)
{
for (IPluginProxy *proxy : bf::at_key<IPluginProxy>(m_Plugins)) {
@@ -73,16 +287,11 @@ void PluginContainer::setUserInterface(IUserInterface *userInterface, QWidget *w for (IPluginModPage *modPage : bf::at_key<IPluginModPage>(m_Plugins)) {
userInterface->registerModPage(modPage);
}
-
- for (IPluginTool *tool : bf::at_key<IPluginTool>(m_Plugins)) {
- userInterface->registerPluginTool(tool);
- }
}
m_UserInterface = userInterface;
}
-
QStringList PluginContainer::implementedInterfaces(IPlugin* plugin) const
{
// We need a QObject to be able to qobject_cast<> to the plugin types:
@@ -92,6 +301,11 @@ QStringList PluginContainer::implementedInterfaces(IPlugin* plugin) const return {};
}
+ return implementedInterfaces(oPlugin);
+}
+
+QStringList PluginContainer::implementedInterfaces(QObject * oPlugin) const
+{
// Find all the names:
QStringList names;
boost::mp11::mp_for_each<PluginTypeOrder>([oPlugin, &names](const auto* p) {
@@ -112,32 +326,28 @@ QStringList PluginContainer::implementedInterfaces(IPlugin* plugin) const return names;
}
-
QString PluginContainer::topImplementedInterface(IPlugin* plugin) const
{
- // We need a QObject to be able to qobject_cast<> to the plugin types:
- QObject* oPlugin = as_qobject(plugin);
-
- if (!oPlugin) {
- return {};
- }
+ auto interfaces = implementedInterfaces(plugin);
+ return interfaces.isEmpty() ? "" : interfaces[0];
+}
- // Find all the names:
- QString name;
- boost::mp11::mp_for_each<PluginTypeOrder>([oPlugin, &name](auto* p) {
+bool PluginContainer::isBetterInterface(QObject* lhs, QObject* rhs) const
+{
+ int count = 0, lhsIdx = -1, rhsIdx = -1;
+ boost::mp11::mp_for_each<PluginTypeOrder>([&](const auto* p) {
using plugin_type = std::decay_t<decltype(*p)>;
- if (name.isEmpty() && qobject_cast<plugin_type*>(oPlugin)) {
- auto tname = PluginTypeName<plugin_type>::value();
- if (!tname.isEmpty()) {
- name = tname;
- }
+ if (lhsIdx < 0 && qobject_cast<plugin_type*>(lhs)) {
+ lhsIdx = count;
}
- });
-
- return name;
+ if (rhsIdx < 0 && qobject_cast<plugin_type*>(rhs)) {
+ rhsIdx = count;
+ }
+ ++count;
+ });
+ return lhsIdx < rhsIdx;
}
-
QStringList PluginContainer::pluginFileNames() const
{
QStringList result;
@@ -153,14 +363,13 @@ QStringList PluginContainer::pluginFileNames() const return result;
}
-
QObject* PluginContainer::as_qobject(MOBase::IPlugin* plugin) const
{
// Find the correspond QObject - Can this be done safely with a cast?
auto& objects = bf::at_key<QObject>(m_Plugins);
auto it = std::find_if(std::begin(objects), std::end(objects), [plugin](QObject* obj) {
return qobject_cast<IPlugin*>(obj) == plugin;
- });
+ });
if (it == std::end(objects)) {
return nullptr;
@@ -169,7 +378,7 @@ QObject* PluginContainer::as_qobject(MOBase::IPlugin* plugin) const return *it;
}
-bool PluginContainer::initPlugin(IPlugin *plugin)
+bool PluginContainer::initPlugin(IPlugin *plugin, IPluginProxy *pluginProxy, bool skipInit)
{
// when MO has no instance loaded, init() is not called on plugins, except
// for proxy plugins, where init() is called with a null IOrganizer
@@ -182,69 +391,92 @@ bool PluginContainer::initPlugin(IPlugin *plugin) return false;
}
+ OrganizerProxy* proxy = nullptr;
if (m_Organizer) {
- auto* proxy = new OrganizerProxy(m_Organizer, this, plugin);
-
- if (!plugin->init(proxy)) {
- log::warn("plugin failed to initialize");
- return false;
- }
+ proxy = new OrganizerProxy(m_Organizer, this, plugin);
}
- return true;
-}
-
-bool PluginContainer::initProxyPlugin(IPlugin *plugin)
-{
- // see initPlugin() above for info
+ // Check if it is a proxy plugin:
+ bool isProxy = dynamic_cast<IPluginProxy*>(plugin);
- if (plugin == nullptr) {
- return false;
+ if (!m_Organizer && !isProxy) {
+ return true;
}
- IOrganizer* proxy = nullptr;
- if (m_Organizer) {
- proxy = new OrganizerProxy(m_Organizer, this, plugin);
- }
+ auto [it, bl] = m_Requirements.emplace(plugin, PluginRequirements(this, plugin, proxy, pluginProxy));
- if (!plugin->init(proxy)) {
- log::warn("proxy plugin failed to initialize");
+ if (!skipInit && !plugin->init(proxy)) {
+ log::warn("plugin failed to initialize");
return false;
}
+ // Update requirements:
+ it->second.fetchRequirements();
+
return true;
}
-
void PluginContainer::registerGame(IPluginGame *game)
{
m_SupportedGames.insert({ game->gameName(), game });
}
-bool PluginContainer::registerPlugin(QObject *plugin, const QString &fileName)
+IPlugin* PluginContainer::registerPlugin(QObject *plugin, const QString &fileName, MOBase::IPluginProxy* pluginProxy)
{
+
+ // generic treatment for all plugins
+ IPlugin *pluginObj = qobject_cast<IPlugin*>(plugin);
+ if (pluginObj == nullptr) {
+ log::debug("PluginContainer::registerPlugin() called with a non IPlugin QObject.");
+ return nullptr;
+ }
+
+ // If we already a plugin with this name:
+ bool skipInit = false;
+ auto& mapNames = bf::at_key<QString>(m_AccessPlugins);
+ if (mapNames.contains(pluginObj->name())) {
+
+ IPlugin* other = mapNames[pluginObj->name()];
+
+ // If both plugins are from the same proxy and the same file, this is usually
+ // ok (in theory some one could write two different classes from the same Python file/module):
+ if (pluginProxy && m_Requirements.at(other).proxy() == pluginProxy
+ && as_qobject(other)->property("filename") == fileName) {
+
+ // Plugin has already been initialized:
+ skipInit = true;
+
+ if (isBetterInterface(plugin, as_qobject(other))) {
+ log::debug("replacing plugin '{}' with interfaces [{}] by one with interfaces [{}]",
+ pluginObj->name(), implementedInterfaces(other).join(", "), implementedInterfaces(plugin).join(", "));
+ bf::at_key<QString>(m_AccessPlugins)[pluginObj->name()] = pluginObj;
+ }
+ }
+ else {
+ log::warn("Trying to register two plugins with the name '{}', the second one will not be registered.",
+ pluginObj->name());
+ return nullptr;
+ }
+ }
+ else {
+ bf::at_key<QString>(m_AccessPlugins)[pluginObj->name()] = pluginObj;
+ }
+
// Storing the original QObject* is a bit of a hack as I couldn't figure out any
// way to cast directly between IPlugin* and IPluginDiagnose*
bf::at_key<QObject>(m_Plugins).push_back(plugin);
- { // generic treatment for all plugins
- IPlugin *pluginObj = qobject_cast<IPlugin*>(plugin);
- if (pluginObj == nullptr) {
- log::debug("not an IPlugin");
- return false;
- }
+ plugin->setProperty("filename", fileName);
- plugin->setProperty("filename", fileName);
-
- if (m_Organizer) {
- m_Organizer->settings().plugins().registerPlugin(pluginObj);
- }
+ if (m_Organizer) {
+ m_Organizer->settings().plugins().registerPlugin(pluginObj);
}
{ // diagnosis plugin
IPluginDiagnose *diagnose = qobject_cast<IPluginDiagnose*>(plugin);
if (diagnose != nullptr) {
bf::at_key<IPluginDiagnose>(m_Plugins).push_back(diagnose);
+ bf::at_key<IPluginDiagnose>(m_AccessPlugins)[diagnose] = pluginObj;
m_DiagnosisConnections.push_back(
diagnose->onInvalidated([&] () { emit diagnosisUpdate(); })
);
@@ -254,67 +486,75 @@ bool PluginContainer::registerPlugin(QObject *plugin, const QString &fileName) IPluginFileMapper *mapper = qobject_cast<IPluginFileMapper*>(plugin);
if (mapper != nullptr) {
bf::at_key<IPluginFileMapper>(m_Plugins).push_back(mapper);
+ bf::at_key<IPluginFileMapper>(m_AccessPlugins)[mapper] = pluginObj;
}
}
{ // mod page plugin
IPluginModPage *modPage = qobject_cast<IPluginModPage*>(plugin);
- if (initPlugin(modPage)) {
+ if (initPlugin(modPage, pluginProxy, skipInit)) {
bf::at_key<IPluginModPage>(m_Plugins).push_back(modPage);
- return true;
+ return modPage;
}
}
{ // game plugin
IPluginGame *game = qobject_cast<IPluginGame*>(plugin);
-
if (game) {
game->detectGame();
- }
-
- if (initPlugin(game)) {
- bf::at_key<IPluginGame>(m_Plugins).push_back(game);
- registerGame(game);
- return true;
+ if (initPlugin(game, pluginProxy, skipInit)) {
+ bf::at_key<IPluginGame>(m_Plugins).push_back(game);
+ registerGame(game);
+ return game;
+ }
}
}
{ // tool plugins
IPluginTool *tool = qobject_cast<IPluginTool*>(plugin);
- if (initPlugin(tool)) {
+ if (initPlugin(tool, pluginProxy, skipInit)) {
bf::at_key<IPluginTool>(m_Plugins).push_back(tool);
- return true;
+ return tool;
}
}
{ // installer plugins
IPluginInstaller *installer = qobject_cast<IPluginInstaller*>(plugin);
- if (initPlugin(installer)) {
+ if (initPlugin(installer, pluginProxy, skipInit)) {
bf::at_key<IPluginInstaller>(m_Plugins).push_back(installer);
if (m_Organizer) {
m_Organizer->installationManager()->registerInstaller(installer);
}
- return true;
+ return installer;
}
}
{ // preview plugins
IPluginPreview *preview = qobject_cast<IPluginPreview*>(plugin);
- if (initPlugin(preview)) {
+ if (initPlugin(preview, pluginProxy, skipInit)) {
bf::at_key<IPluginPreview>(m_Plugins).push_back(preview);
m_PreviewGenerator.registerPlugin(preview);
- return true;
+ return preview;
}
}
{ // proxy plugins
IPluginProxy *proxy = qobject_cast<IPluginProxy*>(plugin);
- if (initProxyPlugin(proxy)) {
+ if (initPlugin(proxy, pluginProxy, skipInit)) {
bf::at_key<IPluginProxy>(m_Plugins).push_back(proxy);
QStringList pluginNames = proxy->pluginList(
QCoreApplication::applicationDirPath() + "/" + ToQString(AppConfig::pluginPath()));
for (const QString &pluginName : pluginNames) {
try {
- // we get a list of matching plugins as proxies don't necessarily have a good way of supporting multiple inheritance
+ // We get a list of matching plugins as proxies can return multiple plugins
+ // per file and do not have a good way of supporting multiple inheritance.
QList<QObject*> matchingPlugins = proxy->instantiate(pluginName);
+
+ // We are going to group plugin by names and "fix" them later:
+ std::map<QString, std::vector<IPlugin*>> proxiedByNames;
+
for (QObject *proxiedPlugin : matchingPlugins) {
if (proxiedPlugin != nullptr) {
- if (registerPlugin(proxiedPlugin, pluginName)) {
- log::debug("loaded plugin \"{}\"", QFileInfo(pluginName).fileName());
+ if (IPlugin* proxied = registerPlugin(proxiedPlugin, pluginName, proxy); proxied) {
+ log::debug("loaded plugin '{}' from '{}' - [{}]",
+ proxied->name(), QFileInfo(pluginName).fileName(), implementedInterfaces(proxied).join(", "));
+
+ // Store the plugin for later:
+ proxiedByNames[proxied->name()].push_back(proxied);
}
else {
log::warn(
@@ -324,37 +564,45 @@ bool PluginContainer::registerPlugin(QObject *plugin, const QString &fileName) }
}
}
+
+ // Fake masters:
+ for (auto& [name, proxiedPlugins] : proxiedByNames) {
+ if (proxiedPlugins.size() > 1) {
+ auto it = std::min_element(std::begin(proxiedPlugins), std::end(proxiedPlugins),
+ [&](auto const& lhs, auto const& rhs) {
+ return isBetterInterface(as_qobject(lhs), as_qobject(rhs));
+ });
+
+ for (auto& proxiedPlugin : proxiedPlugins) {
+ if (proxiedPlugin != *it) {
+ m_Requirements.at(proxiedPlugin).setMaster(*it);
+ }
+ }
+ }
+ }
+
} catch (const std::exception &e) {
reportError(QObject::tr("failed to initialize plugin %1: %2").arg(pluginName).arg(e.what()));
}
}
- return true;
+ return proxy;
}
}
{ // dummy plugins
// only initialize these, no processing otherwise
IPlugin *dummy = qobject_cast<IPlugin*>(plugin);
- if (initPlugin(dummy)) {
+ if (initPlugin(dummy, pluginProxy, skipInit)) {
bf::at_key<IPlugin>(m_Plugins).push_back(dummy);
- return true;
+ return dummy;
}
}
log::debug("no matching plugin interface");
- return false;
+ return nullptr;
}
-struct clearPlugins
-{
- template<typename T>
- void operator()(T& t) const
- {
- t.second.clear();
- }
-};
-
void PluginContainer::unloadPlugins()
{
if (m_UserInterface != nullptr) {
@@ -366,7 +614,9 @@ void PluginContainer::unloadPlugins() m_Organizer->disconnectPlugins();
}
- bf::for_each(m_Plugins, clearPlugins());
+ bf::for_each(m_Plugins, [](auto& t) { t.second.clear(); });
+ bf::for_each(m_AccessPlugins, [](auto& t) { t.second.clear(); });
+ m_Requirements.clear();
for (const boost::signals2::connection &connection : m_DiagnosisConnections) {
connection.disconnect();
@@ -383,6 +633,110 @@ void PluginContainer::unloadPlugins() }
}
+IPlugin* PluginContainer::managedGame() const
+{
+ // TODO: This const_cast is safe but ugly. Most methods require a IPlugin*, so
+ // returning a const-version if painful. This should be fixed by making methods accept
+ // a const IPlugin* instead, but there are a few tricks with qobject_cast and const.
+ return const_cast<IPluginGame*>(m_Organizer->managedGame());
+}
+
+bool PluginContainer::isEnabled(IPlugin* plugin) const
+{
+ // Check if it's a game plugin:
+ if (implementInterface<IPluginGame>(plugin)) {
+ return plugin == m_Organizer->managedGame();
+ }
+
+ // Check the master, if any:
+ auto& requirements = m_Requirements.at(plugin);
+
+ if (requirements.master()) {
+ return isEnabled(requirements.master());
+ }
+
+ // Check if the plugin is enabled:
+ if (!m_Organizer->persistent(plugin->name(), "enabled", true).toBool()) {
+ return false;
+ }
+
+ // Check the requirements:
+ return m_Requirements.at(plugin).canEnable();
+}
+
+void PluginContainer::setEnabled(MOBase::IPlugin* plugin, bool enable, bool dependencies)
+{
+ // If required, disable dependencies:
+ if (!enable && dependencies) {
+ for (auto* p : requirements(plugin).requiredFor()) {
+ setEnabled(p, false, false); // No need to "recurse" here since requiredFor already does it.
+ }
+ }
+
+ // Always disable/enable child plugins:
+ for (auto* p : requirements(plugin).children()) {
+ // "Child" plugin should have no dependencies.
+ setEnabled(p, enable, false);
+ }
+
+ m_Organizer->setPersistent(plugin->name(), "enabled", enable, true);
+
+ if (enable) {
+ emit pluginEnabled(plugin);
+ }
+ else {
+ emit pluginDisabled(plugin);
+ }
+}
+
+MOBase::IPlugin* PluginContainer::plugin(QString const& pluginName) const
+{
+ auto& map = bf::at_key<QString>(m_AccessPlugins);
+ auto it = map.find(pluginName);
+ if (it == std::end(map)) {
+ return nullptr;
+ }
+ return it->second;
+}
+
+MOBase::IPlugin* PluginContainer::plugin(MOBase::IPluginDiagnose* diagnose) const
+{
+ auto& map = bf::at_key<IPluginDiagnose>(m_AccessPlugins);
+ auto it = map.find(diagnose);
+ if (it == std::end(map)) {
+ return nullptr;
+ }
+ return it->second;
+}
+
+MOBase::IPlugin* PluginContainer::plugin(MOBase::IPluginFileMapper* mapper) const
+{
+ auto& map = bf::at_key<IPluginFileMapper>(m_AccessPlugins);
+ auto it = map.find(mapper);
+ if (it == std::end(map)) {
+ return nullptr;
+ }
+ return it->second;
+}
+
+bool PluginContainer::isEnabled(QString const& pluginName) const {
+ IPlugin* p = plugin(pluginName);
+ return p ? isEnabled(p) : false;
+}
+bool PluginContainer::isEnabled(MOBase::IPluginDiagnose* diagnose) const {
+ IPlugin* p = plugin(diagnose);
+ return p ? isEnabled(p) : false;
+}
+bool PluginContainer::isEnabled(MOBase::IPluginFileMapper* mapper) const {
+ IPlugin* p = plugin(mapper);
+ return p ? isEnabled(p) : false;
+}
+
+const PluginRequirements& PluginContainer::requirements(IPlugin* plugin) const
+{
+ return m_Requirements.at(plugin);
+}
+
IPluginGame *PluginContainer::managedGame(const QString &name) const
{
auto iter = m_SupportedGames.find(name);
@@ -405,7 +759,7 @@ void PluginContainer::loadPlugins() unloadPlugins();
for (QObject *plugin : QPluginLoader::staticInstances()) {
- registerPlugin(plugin, "");
+ registerPlugin(plugin, "", nullptr);
}
QFile loadCheck;
@@ -501,8 +855,9 @@ void PluginContainer::loadPlugins() "failed to load plugin {}: {}",
pluginName, pluginLoader->errorString());
} else {
- if (registerPlugin(pluginLoader->instance(), pluginName)) {
- log::debug("loaded plugin \"{}\"", QFileInfo(pluginName).fileName());
+ if (IPlugin* plugin = registerPlugin(pluginLoader->instance(), pluginName, nullptr); plugin) {
+ log::debug("loaded plugin '{}' from '{}' - [{}]",
+ plugin->name(), QFileInfo(pluginName).fileName(), implementedInterfaces(plugin).join(", "));
m_PluginLoaders.push_back(pluginLoader.release());
} else {
m_FailedPlugins.push_back(pluginName);
diff --git a/src/plugincontainer.h b/src/plugincontainer.h index 2b39726b..e9f0f453 100644 --- a/src/plugincontainer.h +++ b/src/plugincontainer.h @@ -22,8 +22,98 @@ class IUserInterface; #include <boost/mp11.hpp>
#endif // Q_MOC_RUN
#include <vector>
+#include <memory>
+class OrganizerProxy;
+
+/**
+ * @brief Class that wrap multiple requirements for a plugin together. THis
+ * class owns the requirements.
+ */
+class PluginRequirements {
+public:
+
+ /**
+ * @return true if the plugin can be enabled (all requirements are met).
+ */
+ bool canEnable() const;
+
+ /**
+ * @return true if this plugin has requirements (satisfied or not).
+ */
+ bool hasRequirements() const;
+
+ /**
+ * @return the proxy that created this plugin, if any.
+ */
+ MOBase::IPluginProxy* proxy() const;
+
+ /**
+ * @return the list of plugins this plugin proxies (if it's a proxy plugin).
+ */
+ std::vector<MOBase::IPlugin*> proxied() const;
+
+ /**
+ * @return the master of this plugin, if any.
+ */
+ MOBase::IPlugin* master() const;
+
+ /**
+ * @return the plugins this plugin is master of.
+ */
+ std::vector<MOBase::IPlugin*> children() const;
+
+ /**
+ * @return the list of problems to be resolved before enabling the plugin.
+ */
+ std::vector<MOBase::IPluginRequirement::Problem> problems() const;
+
+ /**
+ * @return the name of the games (gameName()) this plugin can be used with, or an empty
+ * list if this plugin does not require particular games.
+ */
+ QStringList requiredGames() const;
+
+ /**
+ * @return the list of plugins currently enabled that would have to be disabled
+ * if this plugin was disabled.
+ */
+ std::vector<MOBase::IPlugin*> requiredFor() const;
+
+private:
+
+ // Accumulator version for requiredFor() to avoid infinite recursion.
+ void requiredFor(std::vector<MOBase::IPlugin*>& required, std::set<MOBase::IPlugin*>& visited) const;
+
+ // Retrieve the requirements from the underlying plugin, take ownership on them
+ // and store them. We cannot do this in the constructor because we want to have a
+ // constructed object before calling init().
+ void fetchRequirements();
+
+ // Set the master for this plugin. This is required to "fake" masters for proxied plugins.
+ void setMaster(MOBase::IPlugin* master);
+
+ friend class PluginContainer;
+
+ PluginContainer* m_PluginContainer;
+ MOBase::IPlugin* m_Plugin;
+ MOBase::IPluginProxy* m_PluginProxy;
+ MOBase::IPlugin* m_Master;
+ std::vector<std::unique_ptr<const MOBase::IPluginRequirement>> m_Requirements;
+ MOBase::IOrganizer* m_Organizer;
+ std::vector<MOBase::IPlugin*> m_RequiredFor;
+
+ PluginRequirements(
+ PluginContainer* pluginContainer, MOBase::IPlugin* plugin,
+ MOBase::IOrganizer* proxy, MOBase::IPluginProxy* pluginProxy);
+
+};
+
+
+/**
+ *
+ */
class PluginContainer : public QObject, public MOBase::IPluginDiagnose
{
@@ -32,18 +122,24 @@ class PluginContainer : public QObject, public MOBase::IPluginDiagnose private:
- typedef boost::fusion::map<
- boost::fusion::pair<QObject, std::vector<QObject*>>,
- boost::fusion::pair<MOBase::IPlugin, std::vector<MOBase::IPlugin*>>,
- boost::fusion::pair<MOBase::IPluginDiagnose, std::vector<MOBase::IPluginDiagnose*>>,
- boost::fusion::pair<MOBase::IPluginGame, std::vector<MOBase::IPluginGame*>>,
- boost::fusion::pair<MOBase::IPluginInstaller, std::vector<MOBase::IPluginInstaller*>>,
- boost::fusion::pair<MOBase::IPluginModPage, std::vector<MOBase::IPluginModPage*>>,
- boost::fusion::pair<MOBase::IPluginPreview, std::vector<MOBase::IPluginPreview*>>,
- boost::fusion::pair<MOBase::IPluginTool, std::vector<MOBase::IPluginTool*>>,
- boost::fusion::pair<MOBase::IPluginProxy, std::vector<MOBase::IPluginProxy*>>,
- boost::fusion::pair<MOBase::IPluginFileMapper, std::vector<MOBase::IPluginFileMapper*>>
- > PluginMap;
+ using PluginMap = boost::fusion::map<
+ boost::fusion::pair<QObject, std::vector<QObject*>>,
+ boost::fusion::pair<MOBase::IPlugin, std::vector<MOBase::IPlugin*>>,
+ boost::fusion::pair<MOBase::IPluginDiagnose, std::vector<MOBase::IPluginDiagnose*>>,
+ boost::fusion::pair<MOBase::IPluginGame, std::vector<MOBase::IPluginGame*>>,
+ boost::fusion::pair<MOBase::IPluginInstaller, std::vector<MOBase::IPluginInstaller*>>,
+ boost::fusion::pair<MOBase::IPluginModPage, std::vector<MOBase::IPluginModPage*>>,
+ boost::fusion::pair<MOBase::IPluginPreview, std::vector<MOBase::IPluginPreview*>>,
+ boost::fusion::pair<MOBase::IPluginTool, std::vector<MOBase::IPluginTool*>>,
+ boost::fusion::pair<MOBase::IPluginProxy, std::vector<MOBase::IPluginProxy*>>,
+ boost::fusion::pair<MOBase::IPluginFileMapper, std::vector<MOBase::IPluginFileMapper*>>
+ >;
+
+ using AccessPluginMap = boost::fusion::map<
+ boost::fusion::pair<MOBase::IPluginDiagnose, std::map<MOBase::IPluginDiagnose*, MOBase::IPlugin*>>,
+ boost::fusion::pair<MOBase::IPluginFileMapper, std::map<MOBase::IPluginFileMapper*, MOBase::IPlugin*>>,
+ boost::fusion::pair<QString, std::map<QString, MOBase::IPlugin*>>
+ >;
static const unsigned int PROBLEM_PLUGINSNOTLOADED = 1;
@@ -113,6 +209,81 @@ public: }
/**
+ * @brief Check if a plugin implement a given interface.
+ *
+ * @param plugin The plugin to check.
+ *
+ * @return true if the plugin implements the interface, false otherwise.
+ *
+ * @tparam The interface type.
+ */
+ template <typename T>
+ bool implementInterface(MOBase::IPlugin* plugin) const {
+ // We need a QObject to be able to qobject_cast<> to the plugin types:
+ QObject* oPlugin = as_qobject(plugin);
+
+ if (!oPlugin) {
+ return false;
+ }
+
+ return qobject_cast<T*>(oPlugin);
+ }
+
+ /**
+ * @brief Retrieve a plugin from its name or a corresponding non-IPlugin
+ * interface.
+ *
+ * @param t Name of the plugin to retrieve, or non-IPlugin interface.
+ *
+ * @return the corresponding plugin, or a null pointer.
+ *
+ * @note It is possible to have multiple plugins for the same name when
+ * dealing with proxied plugins (e.g. Python), in which case the
+ * most important one will be returned, as specified in PluginTypeOrder.
+ */
+ MOBase::IPlugin* plugin(QString const& pluginName) const;
+ MOBase::IPlugin* plugin(MOBase::IPluginDiagnose* diagnose) const;
+ MOBase::IPlugin* plugin(MOBase::IPluginFileMapper* mapper) const;
+
+ /**
+ * @return the IPlugin interface to the currently managed game.
+ */
+ MOBase::IPlugin* managedGame() const;
+
+ /**
+ * @brief Check if the given plugin is enabled.
+ *
+ * @param plugin The plugin to check.
+ *
+ * @return true if the plugin is enabled, false otherwise.
+ */
+ bool isEnabled(MOBase::IPlugin* plugin) const;
+
+ // These are friendly methods that called isEnabled(plugin(arg)).
+ bool isEnabled(QString const& pluginName) const;
+ bool isEnabled(MOBase::IPluginDiagnose* diagnose) const;
+ bool isEnabled(MOBase::IPluginFileMapper* mapper) const;
+
+ /**
+ * @brief Enable or disable a plugin.
+ *
+ * @param plugin The plugin to enable or disable.
+ * @param enable true to enable, false to disable.
+ * @param dependencies If true and enable is false, dependencies will also
+ * be disabled (see PluginRequirements::requiredFor).
+ */
+ void setEnabled(MOBase::IPlugin* plugin, bool enable, bool dependencies = true);
+
+ /**
+ * @brief Retrieve the requirements for the given plugin.
+ *
+ * @param plugin The plugin to retrieve the requirements for.
+ *
+ * @return the requirements (as proxy) for the given plugin.
+ */
+ const PluginRequirements& requirements(MOBase::IPlugin* plugin) const;
+
+ /**
* @brief Retrieved the (localized) names of interfaces implemented by the given
* plugin.
*
@@ -154,10 +325,43 @@ public: // IPluginDiagnose interface signals:
+ /**
+ * @brief Emitted plugins are enabled or disabled.
+ */
+ void pluginEnabled(MOBase::IPlugin*);
+ void pluginDisabled(MOBase::IPlugin*);
+
void diagnosisUpdate();
private:
+ friend class PluginRequirements;
+
+
+ /**
+ * @brief Retrieved the (localized) names of interfaces implemented by the given
+ * plugin.
+ *
+ * @param plugin The plugin to retrieve interface for.
+ *
+ * @return the (localized) names of interfaces implemented by this plugin.
+ *
+ * @note This function can be used to get implemented interfaces before registering
+ * a plugin.
+ */
+ QStringList implementedInterfaces(QObject* plugin) const;
+
+ /**
+ * @brief Check if a plugin implements a "better" interface than another
+ * one, as specified by PluginTypeOrder.
+ *
+ * @param lhs, rhs The plugin to compare.
+ *
+ * @return true if the left plugin implements a better interface than the right
+ * one, false otherwise (or if both implements the same interface).
+ */
+ bool isBetterInterface(QObject* lhs, QObject* rhs) const;
+
/**
* @brief Find the QObject* corresponding to the given plugin.
*
@@ -167,11 +371,21 @@ private: */
QObject* as_qobject(MOBase::IPlugin* plugin) const;
+ /**
+ * @brief Initialize a plugin.
+ *
+ * @param plugin The plugin to initialize.
+ * @param proxy The proxy that created this plugin (can be null).
+ * @param skipInit If true, IPlugin::init() will not be called, regardless
+ * of the state of the container.
+ *
+ * @return true if the plugin was initialized correctly, false otherwise.
+ */
+ bool initPlugin(MOBase::IPlugin *plugin, MOBase::IPluginProxy* proxy, bool skipInit);
- bool initPlugin(MOBase::IPlugin *plugin);
- bool initProxyPlugin(MOBase::IPlugin *plugin);
void registerGame(MOBase::IPluginGame *game);
- bool registerPlugin(QObject *pluginObj, const QString &fileName);
+
+ MOBase::IPlugin* registerPlugin(QObject *pluginObj, const QString &fileName, MOBase::IPluginProxy *proxy);
OrganizerCore *m_Organizer;
@@ -179,6 +393,11 @@ private: PluginMap m_Plugins;
+ // This maps allow access to IPlugin* from name or diagnose/mapper object.
+ AccessPluginMap m_AccessPlugins;
+
+ std::map<MOBase::IPlugin*, PluginRequirements> m_Requirements;
+
std::map<QString, MOBase::IPluginGame*> m_SupportedGames;
std::vector<boost::signals2::connection> m_DiagnosisConnections;
QStringList m_FailedPlugins;
diff --git a/src/previewgenerator.cpp b/src/previewgenerator.cpp index b406b7bc..c7df5a25 100644 --- a/src/previewgenerator.cpp +++ b/src/previewgenerator.cpp @@ -18,14 +18,17 @@ along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>. */
#include "previewgenerator.h"
+
#include <QFileInfo>
#include <QLabel>
#include <QImageReader>
#include <QTextEdit>
#include <utility.h>
-PreviewGenerator::PreviewGenerator()
-{
+#include "plugincontainer.h"
+
+PreviewGenerator::PreviewGenerator(const PluginContainer* pluginContainer) :
+ m_PluginContainer(pluginContainer) {
m_MaxSize = QGuiApplication::primaryScreen()->size() * 0.8;
}
@@ -42,13 +45,13 @@ bool PreviewGenerator::previewSupported(const QString &fileExtension) const if (it == m_PreviewPlugins.end()) {
return false;
}
- return it->second->isActive();
+ return m_PluginContainer->isEnabled(it->second);
}
QWidget *PreviewGenerator::genPreview(const QString &fileName) const
{
auto iter = m_PreviewPlugins.find(QFileInfo(fileName).suffix().toLower());
- if (iter != m_PreviewPlugins.end() && iter->second->isActive()) {
+ if (iter != m_PreviewPlugins.end() && m_PluginContainer->isEnabled(iter->second)) {
return iter->second->genFilePreview(fileName, m_MaxSize);
} else {
return nullptr;
diff --git a/src/previewgenerator.h b/src/previewgenerator.h index e872b06b..0d8f0781 100644 --- a/src/previewgenerator.h +++ b/src/previewgenerator.h @@ -26,10 +26,12 @@ along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>. #include <functional>
#include <ipluginpreview.h>
+class PluginContainer;
+
class PreviewGenerator
{
public:
- PreviewGenerator();
+ PreviewGenerator(const PluginContainer* pluginContainer);
void registerPlugin(MOBase::IPluginPreview *plugin);
@@ -44,8 +46,8 @@ private: private:
+ const PluginContainer* m_PluginContainer;
std::map<QString, MOBase::IPluginPreview*> m_PreviewPlugins;
-
QSize m_MaxSize;
};
diff --git a/src/problemsdialog.cpp b/src/problemsdialog.cpp index 4b944ed2..d72f8c20 100644 --- a/src/problemsdialog.cpp +++ b/src/problemsdialog.cpp @@ -7,12 +7,13 @@ #include <QPushButton>
#include <Shellapi.h>
+#include "plugincontainer.h"
using namespace MOBase;
-ProblemsDialog::ProblemsDialog(std::vector<QObject *> pluginObjects, QWidget *parent) :
- QDialog(parent), ui(new Ui::ProblemsDialog), m_PluginObjects(pluginObjects),
+ProblemsDialog::ProblemsDialog(const PluginContainer& pluginContainer, QWidget *parent) :
+ QDialog(parent), ui(new Ui::ProblemsDialog), m_PluginContainer(pluginContainer),
m_hasProblems(false)
{
ui->setupUi(this);
@@ -40,14 +41,10 @@ void ProblemsDialog::runDiagnosis() m_hasProblems = false;
ui->problemsWidget->clear();
- for(QObject *pluginObj : m_PluginObjects) {
- IPlugin *plugin = qobject_cast<IPlugin*>(pluginObj);
- if (plugin != nullptr && !plugin->isActive())
- continue;
-
- IPluginDiagnose *diagnose = qobject_cast<IPluginDiagnose*>(pluginObj);
- if (diagnose == nullptr)
+ for (IPluginDiagnose *diagnose : m_PluginContainer.plugins<IPluginDiagnose>()) {
+ if (!m_PluginContainer.isEnabled(diagnose)) {
continue;
+ }
std::vector<unsigned int> activeProblems = diagnose->activeProblems();
foreach (unsigned int key, activeProblems) {
diff --git a/src/problemsdialog.h b/src/problemsdialog.h index a30c8d48..504a0a10 100644 --- a/src/problemsdialog.h +++ b/src/problemsdialog.h @@ -11,13 +11,14 @@ namespace Ui { class ProblemsDialog;
}
+class PluginContainer;
class ProblemsDialog : public QDialog
{
Q_OBJECT
public:
- explicit ProblemsDialog(std::vector<QObject*> pluginObjects, QWidget *parent = 0);
+ explicit ProblemsDialog(PluginContainer const& pluginContainer, QWidget *parent = 0);
~ProblemsDialog();
// also saves and restores geometry
@@ -37,7 +38,7 @@ private slots: private:
Ui::ProblemsDialog *ui;
- std::vector<QObject *> m_PluginObjects;
+ const PluginContainer& m_PluginContainer;
bool m_hasProblems;
};
diff --git a/src/proxyutils.h b/src/proxyutils.h index 4c1717d8..4f26c070 100644 --- a/src/proxyutils.h +++ b/src/proxyutils.h @@ -10,7 +10,7 @@ namespace MOShared { template <class Fn, class T = int> auto callIfPluginActive(OrganizerProxy* proxy, Fn&& callback, T defaultReturn = T{}) { return [fn = std::forward<Fn>(callback), proxy, defaultReturn](auto&& ...args) { - if (proxy->plugin()->isActive()) { + if (proxy->isPluginEnabled(proxy->plugin())) { return fn(std::forward<decltype(args)>(args)...); } else { diff --git a/src/settings.cpp b/src/settings.cpp index 54d32786..db48cfd2 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -1280,9 +1280,17 @@ void PluginSettings::registerPlugin(IPlugin *plugin) const QString settingName = plugin->name() + "/" + setting.key; QVariant temp = get<QVariant>( - m_Settings, "Plugins", settingName, setting.defaultValue); + m_Settings, "Plugins", settingName, QVariant()); - if (!temp.convert(setting.defaultValue.type())) { + // No previous enabled? Skip. + if (setting.key == "enabled" && (!temp.isValid() || !temp.canConvert<bool>())) { + continue; + } + + if (!temp.isValid()) { + temp = setting.defaultValue; + } + else if (!temp.convert(setting.defaultValue.type())) { log::warn( "failed to interpret \"{}\" as correct type for \"{}\" in plugin \"{}\", using default", temp.toString(), setting.key, plugin->name()); @@ -1296,6 +1304,17 @@ void PluginSettings::registerPlugin(IPlugin *plugin) .arg(setting.description) .arg(setting.defaultValue.toString()); } + + // Handle previous "enabled" settings: + if (m_PluginSettings[plugin->name()].contains("enabled")) { + setPersistent(plugin->name(), "enabled", m_PluginSettings[plugin->name()]["enabled"].toBool(), true); + m_PluginSettings[plugin->name()].remove("enabled"); + m_PluginDescriptions[plugin->name()].remove("enabled"); + + // We need to drop it manually in Settings since it is not possible to remove plugin + // settings: + remove(m_Settings, "Plugins", plugin->name() + "/enabled"); + } } std::vector<MOBase::IPlugin*> PluginSettings::plugins() const diff --git a/src/settingsdialog.ui b/src/settingsdialog.ui index 85c5a4e4..4fd34b21 100644 --- a/src/settingsdialog.ui +++ b/src/settingsdialog.ui @@ -1127,6 +1127,9 @@ </property> <item> <layout class="QFormLayout" name="formLayout_2"> + <property name="leftMargin"> + <number>6</number> + </property> <item row="0" column="0"> <widget class="QLabel" name="label_13"> <property name="text"> @@ -1172,6 +1175,13 @@ </property> </widget> </item> + <item row="3" column="0"> + <widget class="QCheckBox" name="enabledCheckbox"> + <property name="text"> + <string>Enabled</string> + </property> + </widget> + </item> </layout> </item> <item> diff --git a/src/settingsdialogplugins.cpp b/src/settingsdialogplugins.cpp index 5faa0dc9..f019cd2f 100644 --- a/src/settingsdialogplugins.cpp +++ b/src/settingsdialogplugins.cpp @@ -3,9 +3,10 @@ #include "noeditdelegate.h" #include <iplugin.h> +#include "organizercore.h" #include "plugincontainer.h" -using MOBase::IPlugin; +using namespace MOBase; PluginsSettingsTab::PluginsSettingsTab(Settings& s, PluginContainer* pluginContainer, SettingsDialog& d) : SettingsTab(s, d), m_pluginContainer(pluginContainer) @@ -30,14 +31,10 @@ PluginsSettingsTab::PluginsSettingsTab(Settings& s, PluginContainer* pluginConta // display plugin settings QSet<QString> handledNames; for (IPlugin* plugin : settings().plugins().plugins()) { - if (handledNames.contains(plugin->name())) { - continue; - } - if (!m_filter.matches([plugin](const QRegularExpression& regex) { - return regex.match(plugin->localizedName()).hasMatch(); - })) { + if (handledNames.contains(plugin->name()) || m_pluginContainer->requirements(plugin).master()) { continue; } + QTreeWidgetItem* listItem = new QTreeWidgetItem( topItems.at(m_pluginContainer->topImplementedInterface(plugin))); listItem->setData(0, Qt::DisplayRole, plugin->localizedName()); @@ -45,6 +42,18 @@ PluginsSettingsTab::PluginsSettingsTab(Settings& s, PluginContainer* pluginConta listItem->setData(0, ROLE_SETTINGS, settings().plugins().settings(plugin->name())); listItem->setData(0, ROLE_DESCRIPTIONS, settings().plugins().descriptions(plugin->name())); + // Handle child item: + auto children = m_pluginContainer->requirements(plugin).children(); + for (auto* child : children) { + QTreeWidgetItem* childItem = new QTreeWidgetItem(listItem); + childItem->setData(0, Qt::DisplayRole, child->localizedName()); + childItem->setData(0, ROLE_PLUGIN, QVariant::fromValue((void*)child)); + childItem->setData(0, ROLE_SETTINGS, settings().plugins().settings(child->name())); + childItem->setData(0, ROLE_DESCRIPTIONS, settings().plugins().descriptions(child->name())); + + handledNames.insert(child->name()); + } + handledNames.insert(plugin->name()); } @@ -66,15 +75,41 @@ PluginsSettingsTab::PluginsSettingsTab(Settings& s, PluginContainer* pluginConta QObject::connect( ui->pluginsList, &QTreeWidget::currentItemChanged, [&](auto* current, auto* previous) { on_pluginsList_currentItemChanged(current, previous); }); + QObject::connect( + ui->enabledCheckbox, &QCheckBox::clicked, + [&](bool checked) { on_checkboxEnabled_clicked(checked); }); QShortcut *delShortcut = new QShortcut( QKeySequence(Qt::Key_Delete), ui->pluginBlacklist); QObject::connect(delShortcut, &QShortcut::activated, &dialog(), [&] { deleteBlacklistItem(); }); - QObject::connect(&m_filter, &MOBase::FilterWidget::changed, [&] { filterPluginList(); }); + QObject::connect(&m_filter, &FilterWidget::changed, [&] { filterPluginList(); }); + updateListItems(); filterPluginList(); } +void PluginsSettingsTab::updateListItems() +{ + for (auto i = 0; i < ui->pluginsList->topLevelItemCount(); ++i) { + auto* topLevelItem = ui->pluginsList->topLevelItem(i); + for (auto j = 0; j < topLevelItem->childCount(); ++j) { + auto* item = topLevelItem->child(j); + auto* plugin = this->plugin(item); + + bool inactive = !m_pluginContainer->implementInterface<IPluginGame>(plugin) + && !m_pluginContainer->isEnabled(plugin); + + auto font = item->font(0); + font.setItalic(inactive); + item->setFont(0, font); + for (auto k = 0; k < item->childCount(); ++k) { + item->child(k)->setFont(0, font); + } + } + } + +} + void PluginsSettingsTab::filterPluginList() { QTreeWidgetItem* firstNotHidden = nullptr; @@ -87,9 +122,18 @@ void PluginsSettingsTab::filterPluginList() auto* item = topLevelItem->child(j); auto* plugin = this->plugin(item); - if (m_filter.matches([plugin](const QRegularExpression& regex) { + // Check the item or the child - If any match (item or child), the whole + // group is displayed. + bool match = m_filter.matches([plugin](const QRegularExpression& regex) { return regex.match(plugin->localizedName()).hasMatch(); - })) { + }); + for (auto* child : m_pluginContainer->requirements(plugin).children()) { + m_filter.matches([child](const QRegularExpression& regex) { + return regex.match(child->localizedName()).hasMatch(); + }); + } + + if (match) { found = true; item->setHidden(false); @@ -109,7 +153,7 @@ void PluginsSettingsTab::filterPluginList() auto selectedItems = ui->pluginsList->selectedItems(); if (!selectedItems.isEmpty() && selectedItems[0]->isHidden()) { selectedItems[0]->setSelected(false); - + if (firstNotHidden) { firstNotHidden->setSelected(true); } @@ -150,6 +194,97 @@ void PluginsSettingsTab::closing() storeSettings(ui->pluginsList->currentItem()); } +void PluginsSettingsTab::on_checkboxEnabled_clicked(bool checked) +{ + // Retrieve the plugin: + auto *item = ui->pluginsList->currentItem(); + if (!item || !item->data(0, ROLE_PLUGIN).isValid()) { + return; + } + IPlugin* plugin = this->plugin(item); + const auto& requirements = m_pluginContainer->requirements(plugin); + + // User wants to enable: + if (checked) { + auto problems = requirements.problems(); + if (!problems.empty()) { + QStringList descriptions; + for (auto& problem : problems) { + descriptions.append(problem.shortDescription()); + } + QMessageBox::warning( + parentWidget(), QObject::tr("Cannot enable plugin"), + QObject::tr("<p>This plugin cannot be enabled:<p><ul>%1</ul>") + .arg("<li>" + descriptions.join("</li><li>") + "</li>"), QMessageBox::Ok); + ui->enabledCheckbox->setChecked(false); + return; + } + + m_pluginContainer->setEnabled(plugin, true, false); + } + else { + // Custom check for proxy + current game: + if (m_pluginContainer->implementInterface<IPluginProxy>(plugin)) { + + // Current game: + auto* game = m_pluginContainer->managedGame(); + if (m_pluginContainer->requirements(game).proxy() == plugin) { + QMessageBox::warning( + parentWidget(), QObject::tr("Cannot disable plugin"), + QObject::tr("This plugin is used by the current game plugin and cannot disabled."), QMessageBox::Ok); + ui->enabledCheckbox->setChecked(true); + return; + } + + // Check the proxied plugins: + auto proxied = requirements.proxied(); + QStringList pluginNames; + for (auto& p : proxied) { + pluginNames.append(p->localizedName()); + } + pluginNames.sort(); + QString message = QObject::tr( + "<p>Disabling this plugin will prevent the following plugins from working:</p><ul>%1</ul>" + "<p>Do you want to continue? You will need to restart ModOrganizer2 for the change to take effect.</p>") + .arg("<li>" + pluginNames.join("</li><li>") + "</li>"); + if (QMessageBox::critical( + parentWidget(), QObject::tr("Really disable plugin?"), message, + QMessageBox::Yes | QMessageBox::No) == QMessageBox::No) { + ui->enabledCheckbox->setChecked(true); + return; + } + + } + + // Check if the plugins is required for other plugins: + auto requiredFor = requirements.requiredFor(); + if (!requiredFor.empty()) { + QStringList pluginNames; + for (auto& p : requiredFor) { + pluginNames.append(p->localizedName()); + } + pluginNames.sort(); + QString message = QObject::tr( + "<p>Disabling this plugin will also disable the following plugins:</p><ul>%1</ul><p>Do you want to continue?</p>") + .arg("<li>" + pluginNames.join("</li><li>") + "</li>"); + if (QMessageBox::warning( + parentWidget(), QObject::tr("Really disable plugin?"), message, + QMessageBox::Yes | QMessageBox::No) == QMessageBox::No) { + ui->enabledCheckbox->setChecked(true); + return; + } + } + m_pluginContainer->setEnabled(plugin, false, true); + } + + // Proxy was disabled / enabled, need restart: + if (m_pluginContainer->implementInterface<IPluginProxy>(plugin)) { + dialog().setExitNeeded(Exit::Restart); + } + + updateListItems(); +} + void PluginsSettingsTab::on_pluginsList_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous) { storeSettings(previous); @@ -164,6 +299,11 @@ void PluginsSettingsTab::on_pluginsList_currentItemChanged(QTreeWidgetItem *curr ui->versionLabel->setText(plugin->version().canonicalString()); ui->descriptionLabel->setText(plugin->description()); + ui->enabledCheckbox->setVisible( + !m_pluginContainer->implementInterface<MOBase::IPluginGame>(plugin) + && plugin->master().isEmpty()); + ui->enabledCheckbox->setChecked(m_pluginContainer->isEnabled(plugin)); + QVariantMap settings = current->data(0, ROLE_SETTINGS).toMap(); QVariantMap descriptions = current->data(0, ROLE_DESCRIPTIONS).toMap(); ui->pluginSettingsList->setEnabled(settings.count() != 0); diff --git a/src/settingsdialogplugins.h b/src/settingsdialogplugins.h index add8fede..3de0d7dc 100644 --- a/src/settingsdialogplugins.h +++ b/src/settingsdialogplugins.h @@ -16,10 +16,17 @@ public: private: void on_pluginsList_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous); + void on_checkboxEnabled_clicked(bool checked); void deleteBlacklistItem(); void storeSettings(QTreeWidgetItem *pluginItem); private slots: + + /** + * @brief Update the list item to display inactive plugins. + */ + void updateListItems(); + /** * @brief Filter the plugin list according to the filter widget. * |
