summaryrefslogtreecommitdiff
path: root/src/organizerproxy.cpp
blob: 22a8a0234efaedcbef40fc168279cfb7c7bbed97 (plain)
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#include "organizerproxy.h"

#include "downloadmanagerproxy.h"
#include "gamefeaturesproxy.h"
#include "glob_matching.h"
#include "modlistproxy.h"
#include "organizercore.h"
#include "plugincontainer.h"
#include "pluginlistproxy.h"
#include "proxyutils.h"
#include "settings.h"
#include "shared/appconfig.h"
#include "shared/util.h"

#include <QApplication>
#include <QObject>

using namespace MOBase;
using namespace MOShared;

OrganizerProxy::OrganizerProxy(OrganizerCore* organizer,
                               PluginContainer* pluginContainer,
                               MOBase::IPlugin* plugin)
    : m_Proxied(organizer), m_PluginContainer(pluginContainer), m_Plugin(plugin),
      m_DownloadManagerProxy(
          std::make_unique<DownloadManagerProxy>(this, organizer->downloadManager())),
      m_ModListProxy(std::make_unique<ModListProxy>(this, organizer->modList())),
      m_PluginListProxy(
          std::make_unique<PluginListProxy>(this, organizer->pluginList())),
      m_GameFeaturesProxy(
          std::make_unique<GameFeaturesProxy>(this, pluginContainer->gameFeatures()))
{}

OrganizerProxy::~OrganizerProxy()
{
  disconnectSignals();
}

void OrganizerProxy::connectSignals()
{
  m_Connections.push_back(
      m_Proxied->onAboutToRun(callSignalIfPluginActive(this, m_AboutToRun, true)));
  m_Connections.push_back(
      m_Proxied->onFinishedRun(callSignalIfPluginActive(this, m_FinishedRun)));
  m_Connections.push_back(
      m_Proxied->onProfileCreated(callSignalIfPluginActive(this, m_ProfileCreated)));
  m_Connections.push_back(
      m_Proxied->onProfileRenamed(callSignalIfPluginActive(this, m_ProfileRenamed)));
  m_Connections.push_back(
      m_Proxied->onProfileRemoved(callSignalIfPluginActive(this, m_ProfileRemoved)));
  m_Connections.push_back(
      m_Proxied->onProfileChanged(callSignalIfPluginActive(this, m_ProfileChanged)));

  m_Connections.push_back(m_Proxied->onUserInterfaceInitialized(
      callSignalAlways(m_UserInterfaceInitialized)));
  m_Connections.push_back(
      m_Proxied->onPluginSettingChanged(callSignalAlways(m_PluginSettingChanged)));
  m_Connections.push_back(
      m_Proxied->onPluginEnabled(callSignalAlways(m_PluginEnabled)));
  m_Connections.push_back(
      m_Proxied->onPluginDisabled(callSignalAlways(m_PluginDisabled)));

  // Connect the child proxies.
  m_DownloadManagerProxy->connectSignals();
  m_ModListProxy->connectSignals();
  m_PluginListProxy->connectSignals();
}

void OrganizerProxy::disconnectSignals()
{
  // Disconnect the child proxies.
  m_DownloadManagerProxy->disconnectSignals();
  m_ModListProxy->disconnectSignals();
  m_PluginListProxy->disconnectSignals();

  for (auto& conn : m_Connections) {
    conn.disconnect();
  }
  m_Connections.clear();
}

IModRepositoryBridge* OrganizerProxy::createNexusBridge() const
{
  return new NexusBridge(m_PluginContainer, m_Plugin->name());
}

QString OrganizerProxy::profileName() const
{
  return m_Proxied->profileName();
}

QString OrganizerProxy::profilePath() const
{
  return m_Proxied->profilePath();
}

QString OrganizerProxy::downloadsPath() const
{
  return m_Proxied->downloadsPath();
}

QString OrganizerProxy::overwritePath() const
{
  return m_Proxied->overwritePath();
}

QString OrganizerProxy::basePath() const
{
  return m_Proxied->basePath();
}

QString OrganizerProxy::modsPath() const
{
  return m_Proxied->modsPath();
}

Version OrganizerProxy::version() const
{
  return m_Proxied->version();
}

VersionInfo OrganizerProxy::appVersion() const
{
  const auto version = m_Proxied->version();
  const int major = version.major(), minor = version.minor(),
            subminor                       = version.patch();
  int subsubminor                          = 0;
  VersionInfo::ReleaseType infoReleaseType = VersionInfo::RELEASE_FINAL;

  // make a copy
  auto prereleases = version.preReleases();

  if (!prereleases.empty()) {
    // check if the first pre-release entry is a number
    if (prereleases.front().index() == 0) {
      subsubminor = std::get<int>(prereleases.front());
      prereleases.erase(prereleases.begin());
    }

    if (!prereleases.empty()) {
      const auto releaseType = std::get<Version::ReleaseType>(prereleases.front());
      switch (releaseType) {
      case Version::Development:
        infoReleaseType = VersionInfo::RELEASE_PREALPHA;
        break;
      case Version::Alpha:
        infoReleaseType = VersionInfo::RELEASE_ALPHA;
        break;
      case Version::Beta:
        infoReleaseType = VersionInfo::RELEASE_BETA;
        break;
      case Version::ReleaseCandidate:
        infoReleaseType = VersionInfo::RELEASE_CANDIDATE;
        break;
      default:
        infoReleaseType = VersionInfo::RELEASE_PREALPHA;
      }
    }

    // there is no way to differentiate two pre-releases?
  }

  return VersionInfo(major, minor, subminor, subsubminor, infoReleaseType);
}

IPluginGame* OrganizerProxy::getGame(const QString& gameName) const
{
  return m_Proxied->getGame(gameName);
}

IModInterface* OrganizerProxy::createMod(MOBase::GuessedValue<QString>& name)
{
  return m_Proxied->createMod(name);
}

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);
}

void OrganizerProxy::setPluginSetting(const QString& pluginName, const QString& key,
                                      const QVariant& value)
{
  m_Proxied->setPluginSetting(pluginName, key, value);
}

QVariant OrganizerProxy::persistent(const QString& pluginName, const QString& key,
                                    const QVariant& def) const
{
  return m_Proxied->persistent(pluginName, key, def);
}

void OrganizerProxy::setPersistent(const QString& pluginName, const QString& key,
                                   const QVariant& value, bool sync)
{
  m_Proxied->setPersistent(pluginName, key, value, sync);
}

QString OrganizerProxy::pluginDataPath() const
{
  return OrganizerCore::pluginDataPath();
}

HANDLE OrganizerProxy::startApplication(const QString& exe, const QStringList& args,
                                        const QString& cwd, const QString& profile,
                                        const QString& overwrite, bool ignoreOverwrite)
{
  log::debug("a plugin has requested to start an application:\n"
             " . executable: '{}'\n"
             " . args: '{}'\n"
             " . cwd: '{}'\n"
             " . profile: '{}'\n"
             " . overwrite: '{}'\n"
             " . ignore overwrite: {}",
             exe, args.join(" "), cwd, profile, overwrite, ignoreOverwrite);

  auto runner = m_Proxied->processRunner();

  // don't wait for completion
  runner.setFromFileOrExecutable(exe, args, cwd, profile, overwrite, ignoreOverwrite)
      .run();

  // the plugin is in charge of closing the handle, unless waitForApplication()
  // is called on it
  return runner.stealProcessHandle().release();
}

bool OrganizerProxy::waitForApplication(HANDLE handle, bool refresh,
                                        LPDWORD exitCode) const
{
  const auto pid = ::GetProcessId(handle);

  log::debug("a plugin wants to wait for an application to complete, pid {}{}", pid,
             (pid == 0 ? "unknown (probably already completed)" : ""));

  auto runner = m_Proxied->processRunner();

  ProcessRunner::WaitFlags waitFlags = ProcessRunner::ForceWait;

  if (refresh) {
    waitFlags |= ProcessRunner::TriggerRefresh | ProcessRunner::WaitForRefresh;
  }

  const auto r = runner.setWaitForCompletion(waitFlags, UILocker::OutputRequired)
                     .attachToProcess(handle);

  if (exitCode) {
    *exitCode = runner.exitCode();
  }

  switch (r) {
  case ProcessRunner::Completed:
    return true;

  case ProcessRunner::Cancelled:  // fall-through
  case ProcessRunner::ForceUnlocked:
    // this is always an error because the application should have run to
    // completion
    return false;

  case ProcessRunner::Error:  // fall-through
  default:
    return false;
  }
}

void OrganizerProxy::refresh(bool saveChanges)
{
  m_Proxied->refresh(saveChanges);
}

IModInterface* OrganizerProxy::installMod(const QString& fileName,
                                          const QString& nameSuggestion)
{
  return m_Proxied->installMod(fileName, -1, false, nullptr, nameSuggestion);
}

QString OrganizerProxy::resolvePath(const QString& fileName) const
{
  return m_Proxied->resolvePath(fileName);
}

QStringList OrganizerProxy::listDirectories(const QString& directoryName) const
{
  return m_Proxied->listDirectories(directoryName);
}

QStringList
OrganizerProxy::findFiles(const QString& path,
                          const std::function<bool(const QString&)>& filter) const
{
  return m_Proxied->findFiles(path, filter);
}

QStringList OrganizerProxy::findFiles(const QString& path,
                                      const QStringList& globFilters) const
{
  QList<GlobPattern<QChar>> patterns;
  for (auto& gfilter : globFilters) {
    patterns.append(GlobPattern(gfilter));
  }
  return findFiles(path, [&patterns](const QString& filename) {
    for (auto& p : patterns) {
      if (p.match(filename)) {
        return true;
      }
    }
    return false;
  });
}

QStringList OrganizerProxy::getFileOrigins(const QString& fileName) const
{
  return m_Proxied->getFileOrigins(fileName);
}

QList<MOBase::IOrganizer::FileInfo> OrganizerProxy::findFileInfos(
    const QString& path,
    const std::function<bool(const MOBase::IOrganizer::FileInfo&)>& filter) const
{
  return m_Proxied->findFileInfos(path, filter);
}

std::shared_ptr<const MOBase::IFileTree> OrganizerProxy::virtualFileTree() const
{
  return m_Proxied->m_VirtualFileTree.value();
}

MOBase::IDownloadManager* OrganizerProxy::downloadManager() const
{
  return m_DownloadManagerProxy.get();
}

MOBase::IPluginList* OrganizerProxy::pluginList() const
{
  return m_PluginListProxy.get();
}

MOBase::IModList* OrganizerProxy::modList() const
{
  return m_ModListProxy.get();
}

MOBase::IGameFeatures* OrganizerProxy::gameFeatures() const
{
  return m_GameFeaturesProxy.get();
}

MOBase::IProfile* OrganizerProxy::profile() const
{
  return m_Proxied->currentProfile();
}

MOBase::IPluginGame const* OrganizerProxy::managedGame() const
{
  return m_Proxied->managedGame();
}

// CALLBACKS

bool OrganizerProxy::onAboutToRun(const std::function<bool(const QString&)>& func)
{
  return m_Proxied
      ->onAboutToRun(MOShared::callIfPluginActive(
          this,
          [func](const QString& binary, const QDir&, const QString&) {
            return func(binary);
          },
          true))
      .connected();
}

bool OrganizerProxy::onAboutToRun(
    const std::function<bool(const QString&, const QDir&, const QString&)>& func)
{
  return m_Proxied->onAboutToRun(MOShared::callIfPluginActive(this, func, true))
      .connected();
}

bool OrganizerProxy::onFinishedRun(
    const std::function<void(const QString&, unsigned int)>& func)
{
  return m_Proxied->onFinishedRun(MOShared::callIfPluginActive(this, func)).connected();
}

bool OrganizerProxy::onUserInterfaceInitialized(
    std::function<void(QMainWindow*)> const& func)
{
  // Always call this one to allow plugin to initialize themselves even when not active:
  return m_UserInterfaceInitialized.connect(func).connected();
}

bool OrganizerProxy::onNextRefresh(const std::function<void()>& func,
                                   bool immediateIfPossible)
{
  using enum OrganizerCore::RefreshCallbackMode;
  return m_Proxied
      ->onNextRefresh(MOShared::callIfPluginActive(this, func),
                      OrganizerCore::RefreshCallbackGroup::EXTERNAL,
                      immediateIfPossible ? RUN_NOW_IF_POSSIBLE
                                          : FORCE_WAIT_FOR_REFRESH)
      .connected();
}

bool OrganizerProxy::onProfileCreated(std::function<void(IProfile*)> const& func)
{
  return m_ProfileCreated.connect(func).connected();
}

bool OrganizerProxy::onProfileRenamed(
    std::function<void(IProfile*, QString const&, QString const&)> const& func)
{
  return m_ProfileRenamed.connect(func).connected();
}

bool OrganizerProxy::onProfileRemoved(std::function<void(QString const&)> const& func)
{
  return m_ProfileRemoved.connect(func).connected();
}

bool OrganizerProxy::onProfileChanged(
    std::function<void(MOBase::IProfile*, MOBase::IProfile*)> const& func)
{
  return m_ProfileChanged.connect(func).connected();
}
// 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)
{
  return m_PluginSettingChanged.connect(func).connected();
}

bool OrganizerProxy::onPluginEnabled(std::function<void(const IPlugin*)> const& func)
{
  return m_PluginEnabled.connect(func).connected();
}

bool OrganizerProxy::onPluginEnabled(const QString& pluginName,
                                     std::function<void()> const& func)
{
  return 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_PluginDisabled.connect(func).connected();
}

bool OrganizerProxy::onPluginDisabled(const QString& pluginName,
                                      std::function<void()> const& func)
{
  return onPluginDisabled([=](const IPlugin* plugin) {
    if (plugin->name().compare(pluginName, Qt::CaseInsensitive) == 0) {
      func();
    }
  });
}