1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
#include "plugincontainer.h"
#include "organizercore.h"
#include "organizerproxy.h"
#include "report.h"
#include <ipluginproxy.h>
#include "iuserinterface.h"
#include <idownloadmanager.h>
#include "shared/appconfig.h"
#include <QAction>
#include <QToolButton>
#include <QCoreApplication>
#include <QMessageBox>
#include <QDirIterator>
#include <boost/fusion/sequence/intrinsic/at_key.hpp>
#include <boost/fusion/include/at_key.hpp>
#include <boost/fusion/algorithm/iteration/for_each.hpp>
#include <boost/fusion/include/for_each.hpp>
using namespace MOBase;
using namespace MOShared;
namespace bf = boost::fusion;
PluginContainer::PluginContainer(OrganizerCore *organizer)
: m_Organizer(organizer)
, m_UserInterface(nullptr)
{
}
void PluginContainer::setUserInterface(IUserInterface *userInterface, QWidget *widget)
{
for (IPluginProxy *proxy : bf::at_key<IPluginProxy>(m_Plugins)) {
proxy->setParentWidget(widget);
}
if (userInterface != nullptr) {
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::pluginFileNames() const
{
QStringList result;
for (QPluginLoader *loader : m_PluginLoaders) {
result.append(loader->fileName());
}
std::vector<IPluginProxy *> proxyList = bf::at_key<IPluginProxy>(m_Plugins);
for (IPluginProxy *proxy : proxyList) {
QStringList proxiedPlugins = proxy->pluginList(
QCoreApplication::applicationDirPath() + "/" + ToQString(AppConfig::pluginPath()));
result.append(proxiedPlugins);
}
return result;
}
bool PluginContainer::verifyPlugin(IPlugin *plugin)
{
if (plugin == nullptr) {
return false;
} else if (!plugin->init(new OrganizerProxy(m_Organizer, this, plugin->name()))) {
log::warn("plugin failed to initialize");
return false;
}
return true;
}
void PluginContainer::registerGame(IPluginGame *game)
{
m_SupportedGames.insert({ game->gameName(), game });
}
bool PluginContainer::registerPlugin(QObject *plugin, const QString &fileName)
{
// 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);
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);
m_DiagnosisConnections.push_back(
diagnose->onInvalidated([&] () { emit diagnosisUpdate(); })
);
}
}
{ // file mapper plugin
IPluginFileMapper *mapper = qobject_cast<IPluginFileMapper*>(plugin);
if (mapper != nullptr) {
bf::at_key<IPluginFileMapper>(m_Plugins).push_back(mapper);
}
}
{ // mod page plugin
IPluginModPage *modPage = qobject_cast<IPluginModPage*>(plugin);
if (verifyPlugin(modPage)) {
bf::at_key<IPluginModPage>(m_Plugins).push_back(modPage);
return true;
}
}
{ // game plugin
IPluginGame *game = qobject_cast<IPluginGame*>(plugin);
if (verifyPlugin(game)) {
bf::at_key<IPluginGame>(m_Plugins).push_back(game);
registerGame(game);
return true;
}
}
{ // tool plugins
IPluginTool *tool = qobject_cast<IPluginTool*>(plugin);
if (verifyPlugin(tool)) {
bf::at_key<IPluginTool>(m_Plugins).push_back(tool);
return true;
}
}
{ // installer plugins
IPluginInstaller *installer = qobject_cast<IPluginInstaller*>(plugin);
if (verifyPlugin(installer)) {
bf::at_key<IPluginInstaller>(m_Plugins).push_back(installer);
m_Organizer->installationManager()->registerInstaller(installer);
return true;
}
}
{ // preview plugins
IPluginPreview *preview = qobject_cast<IPluginPreview*>(plugin);
if (verifyPlugin(preview)) {
bf::at_key<IPluginPreview>(m_Plugins).push_back(preview);
m_PreviewGenerator.registerPlugin(preview);
return true;
}
}
{ // proxy plugins
IPluginProxy *proxy = qobject_cast<IPluginProxy*>(plugin);
if (verifyPlugin(proxy)) {
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
QList<QObject*> matchingPlugins = proxy->instantiate(pluginName);
for (QObject *proxiedPlugin : matchingPlugins) {
if (proxiedPlugin != nullptr) {
if (registerPlugin(proxiedPlugin, pluginName)) {
log::debug("loaded plugin \"{}\"", QFileInfo(pluginName).fileName());
}
else {
log::warn(
"plugin \"{}\" failed to load. If this plugin is for an older version of MO "
"you have to update it or delete it if no update exists.",
pluginName);
}
}
}
} catch (const std::exception &e) {
reportError(QObject::tr("failed to initialize plugin %1: %2").arg(pluginName).arg(e.what()));
}
}
return true;
}
}
{ // dummy plugins
// only initialize these, no processing otherwise
IPlugin *dummy = qobject_cast<IPlugin*>(plugin);
if (verifyPlugin(dummy)) {
bf::at_key<IPlugin>(m_Plugins).push_back(dummy);
return true;
}
}
log::debug("no matching plugin interface");
return false;
}
struct clearPlugins
{
template<typename T>
void operator()(T& t) const
{
t.second.clear();
}
};
void PluginContainer::unloadPlugins()
{
if (m_UserInterface != nullptr) {
m_UserInterface->disconnectPlugins();
}
// disconnect all slots before unloading plugins so plugins don't have to take care of that
m_Organizer->disconnectPlugins();
bf::for_each(m_Plugins, clearPlugins());
for (const boost::signals2::connection &connection : m_DiagnosisConnections) {
connection.disconnect();
}
m_DiagnosisConnections.clear();
while (!m_PluginLoaders.empty()) {
QPluginLoader *loader = m_PluginLoaders.back();
m_PluginLoaders.pop_back();
if ((loader != nullptr) && !loader->unload()) {
log::debug("failed to unload {}: {}", loader->fileName(), loader->errorString());
}
delete loader;
}
}
IPluginGame *PluginContainer::managedGame(const QString &name) const
{
auto iter = m_SupportedGames.find(name);
if (iter != m_SupportedGames.end()) {
return iter->second;
} else {
return nullptr;
}
}
const PreviewGenerator &PluginContainer::previewGenerator() const
{
return m_PreviewGenerator;
}
void PluginContainer::loadPlugins()
{
TimeThis tt("PluginContainer::loadPlugins()");
unloadPlugins();
for (QObject *plugin : QPluginLoader::staticInstances()) {
registerPlugin(plugin, "");
}
QFile loadCheck(qApp->property("dataPath").toString() + "/plugin_loadcheck.tmp");
if (loadCheck.exists() && loadCheck.open(QIODevice::ReadOnly)) {
// oh, there was a failed plugin load last time. Find out which plugin was loaded last
QString fileName;
while (!loadCheck.atEnd()) {
fileName = QString::fromUtf8(loadCheck.readLine().constData()).trimmed();
}
if (QMessageBox::question(nullptr, QObject::tr("Plugin error"),
QObject::tr("It appears the plugin \"%1\" failed to load last startup and caused MO to crash. Do you want to disable it?\n"
"(Please note: If this is the first time you see this message for this plugin you may want to give it another try. "
"The plugin may be able to recover from the problem)").arg(fileName),
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes) == QMessageBox::Yes) {
m_Organizer->settings().plugins().addBlacklist(fileName);
}
loadCheck.close();
}
loadCheck.open(QIODevice::WriteOnly);
QString pluginPath = qApp->applicationDirPath() + "/" + ToQString(AppConfig::pluginPath());
log::debug("looking for plugins in {}", QDir::toNativeSeparators(pluginPath));
QDirIterator iter(pluginPath, QDir::Files | QDir::NoDotAndDotDot);
while (iter.hasNext()) {
iter.next();
if (m_Organizer->settings().plugins().blacklisted(iter.fileName())) {
log::debug("plugin \"{}\" blacklisted", iter.fileName());
continue;
}
loadCheck.write(iter.fileName().toUtf8());
loadCheck.write("\n");
loadCheck.flush();
QString pluginName = iter.filePath();
if (QLibrary::isLibrary(pluginName)) {
std::unique_ptr<QPluginLoader> pluginLoader(new QPluginLoader(pluginName, this));
if (pluginLoader->instance() == nullptr) {
m_FailedPlugins.push_back(pluginName);
log::error(
"failed to load plugin {}: {}",
pluginName, pluginLoader->errorString());
} else {
if (registerPlugin(pluginLoader->instance(), pluginName)) {
log::debug("loaded plugin \"{}\"", QFileInfo(pluginName).fileName());
m_PluginLoaders.push_back(pluginLoader.release());
} else {
m_FailedPlugins.push_back(pluginName);
log::warn("plugin \"{}\" failed to load (may be outdated)", pluginName);
}
}
}
}
// remove the load check file on success
loadCheck.remove();
bf::at_key<IPluginDiagnose>(m_Plugins).push_back(m_Organizer);
bf::at_key<IPluginDiagnose>(m_Plugins).push_back(this);
m_Organizer->connectPlugins(this);
}
std::vector<unsigned int> PluginContainer::activeProblems() const
{
std::vector<unsigned int> problems;
if (m_FailedPlugins.size()) {
problems.push_back(PROBLEM_PLUGINSNOTLOADED);
}
return problems;
}
QString PluginContainer::shortDescription(unsigned int key) const
{
switch (key) {
case PROBLEM_PLUGINSNOTLOADED: {
return tr("Some plugins could not be loaded");
} break;
default: {
return tr("Description missing");
} break;
}
}
QString PluginContainer::fullDescription(unsigned int key) const
{
switch (key) {
case PROBLEM_PLUGINSNOTLOADED: {
QString result = tr("The following plugins could not be loaded. The reason may be missing "
"dependencies (i.e. python) or an outdated version:") + "<ul>";
for (const QString &plugin : m_FailedPlugins) {
result += "<li>" + plugin + "</li>";
}
result += "<ul>";
return result;
} break;
default: {
return tr("Description missing");
} break;
}
}
bool PluginContainer::hasGuidedFix(unsigned int) const
{
return false;
}
void PluginContainer::startGuidedFix(unsigned int) const
{
}
|