summaryrefslogtreecommitdiff
path: root/src/organizerproxy.cpp
blob: 5e720ea8c74bc64dc3b1cf27e0d817b1fd6e887b (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
#include "organizerproxy.h"

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

#include <QObject>
#include <QApplication>

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()))
{
}

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

VersionInfo OrganizerProxy::appVersion() const
{
  return m_Proxied->appVersion();
}

IModInterface *OrganizerProxy::getMod(const QString &name) const
{
  return m_Proxied->getMod(name);
}

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

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

bool OrganizerProxy::removeMod(IModInterface *mod)
{
  return m_Proxied->removeMod(mod);
}

void OrganizerProxy::modDataChanged(IModInterface *mod)
{
  m_Proxied->modDataChanged(mod);
}

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 m_Proxied->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, 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();

  const auto r = runner
    .setWaitForCompletion(ProcessRunner::ForceWait, 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, 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);
}

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::IProfile *OrganizerProxy::profile() const
{
  return m_Proxied->currentProfile();
}

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

QStringList OrganizerProxy::modsSortedByProfilePriority() const
{
  return m_Proxied->modsSortedByProfilePriority();
}

// CALLBACKS

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

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

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_Proxied->onUserInterfaceInitialized(func);
}

bool OrganizerProxy::onProfileChanged(std::function<void(MOBase::IProfile*, MOBase::IProfile*)> const& func)
{
  return m_Proxied->onProfileChanged(MOShared::callIfPluginActive(this, func));
}

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