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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
|
/*
Copyright (C) 2016 Sebastian Herbord. All rights reserved.
This file is part of Mod Organizer.
Mod Organizer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Mod Organizer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>.
*/
#include "instancemanager.h"
#include "selectiondialog.h"
#include "settings.h"
#include "shared/appconfig.h"
#include "plugincontainer.h"
#include "shared/util.h"
#include <report.h>
#include <iplugingame.h>
#include <utility.h>
#include <log.h>
#include <QCoreApplication>
#include <QDir>
#include <QStandardPaths>
#include <QInputDialog>
#include <QMessageBox>
#include <cstdint>
using namespace MOBase;
Instance::Instance(QDir dir, bool portable, QString profileName) :
m_dir(std::move(dir)), m_portable(portable), m_plugin(nullptr),
m_profile(std::move(profileName))
{
}
QString Instance::name() const
{
if (isPortable())
return QObject::tr("Portable");
else
return m_dir.dirName();
}
QString Instance::gameName() const
{
return m_gameName;
}
QString Instance::gameDirectory() const
{
return m_gameDir;
}
QDir Instance::directory() const
{
return m_dir;
}
MOBase::IPluginGame* Instance::gamePlugin() const
{
return m_plugin;
}
QString Instance::profileName() const
{
return m_profile;
}
QString Instance::iniPath() const
{
return InstanceManager::iniPath(m_dir);
}
bool Instance::isPortable() const
{
return m_portable;
}
Instance::SetupResults Instance::setup(PluginContainer& plugins)
{
Settings s(iniPath());
if (s.iniStatus() != QSettings::NoError) {
log::error("can't read ini {}", iniPath());
return SetupResults::BadIni;
}
if (m_gameName.isEmpty()) {
if (auto v=s.game().name())
m_gameName = *v;
}
if (m_gameDir.isEmpty()) {
if (auto v=s.game().directory())
m_gameDir = *v;
}
const auto r = getGamePlugin(plugins);
if (r != SetupResults::Ok) {
return r;
}
if (m_gameVariant.isEmpty()) {
if (auto v=s.game().edition()) {
m_gameVariant = *v;
}
}
if (m_gameVariant.isEmpty() && m_plugin->gameVariants().size() > 1) {
return SetupResults::MissingVariant;
} else {
m_plugin->setGameVariant(m_gameVariant);
}
getProfile(s);
s.game().setName(m_gameName);
s.game().setDirectory(m_gameDir);
s.game().setSelectedProfileName(m_profile);
if (!m_gameVariant.isEmpty())
s.game().setEdition(m_gameVariant);
m_plugin->setGamePath(m_gameDir);
return SetupResults::Ok;
}
void Instance::setGame(const QString& name, const QString& dir)
{
m_gameName = name;
m_gameDir = dir;
}
void Instance::setVariant(const QString& name)
{
m_gameVariant = name;
}
Instance::SetupResults Instance::getGamePlugin(PluginContainer& plugins)
{
if (!m_gameName.isEmpty() && !m_gameDir.isEmpty())
{
// normal case: both the name and dir are in the ini
// find the plugin by name
for (IPluginGame* game : plugins.plugins<IPluginGame>()) {
if (m_gameName.compare(game->gameName(), Qt::CaseInsensitive) == 0) {
// plugin found, check if the game directory is valid
if (!game->looksValid(m_gameDir)) {
// the directory from the ini is not valid anymore
log::warn(
"game plugin {} says dir {} from ini {} is not valid",
game->gameName(), m_gameDir, iniPath());
// note that some plugins return true for isInstalled() if a path
// is found in the registry, but without actually checking if it's
// valid
if (game->isInstalled() && game->looksValid(game->gameDirectory())) {
// bad game directory but the plugin reports there's a valid one
// somewhere; take it instead
log::warn(
"game plugin {} found a game at {}, taking it",
game->gameName(), game->gameDirectory().absolutePath());
m_gameDir = game->gameDirectory().absolutePath();
} else {
// game seems to be gone completely
log::warn("game plugin {} found no game installation at all", game->gameName());
return SetupResults::GameGone;
}
}
m_plugin = game;
return SetupResults::Ok;
}
}
log::warn("game plugin {} not found", m_gameName);
return SetupResults::PluginGone;
}
else if (m_gameName.isEmpty() && !m_gameDir.isEmpty())
{
// the name is missing, but there's a directory; find a plugin that can
// handle it
log::warn(
"game name is missing from ini {} but dir {} is available",
iniPath(), m_gameDir);
for (IPluginGame* game : plugins.plugins<IPluginGame>()) {
if (game->looksValid(m_gameDir)) {
// take it
log::warn("found plugin {} that can use dir {}", game->gameName(), m_gameDir);
m_plugin = game;
m_gameName = game->gameName();
return SetupResults::Ok;
}
}
log::error("no plugins can use dir {}", m_gameDir);
return SetupResults::GameGone;
}
else if (!m_gameName.isEmpty() && m_gameDir.isEmpty())
{
// dir is missing, find a plugin with the correct name and use the install
// dir it detected
log::warn(
"game dir is missing from ini {} but name {} is available",
iniPath(), m_gameName);
for (IPluginGame* game : plugins.plugins<IPluginGame>()) {
if (m_gameName.compare(game->gameName(), Qt::CaseInsensitive) == 0) {
// plugin found, use its detected installation dir
if (game->isInstalled()) {
log::warn(
"found plugin {} that matches name in ini {}, using auto detected "
"game dir {}",
game->gameName(), iniPath(), game->gameDirectory().absolutePath());
m_plugin = game;
m_gameDir = game->gameDirectory().absolutePath();
return SetupResults::Ok;
} else {
log::warn(
"found plugin {} that matches name in ini {}, but no game install "
"detected by plugin",
game->gameName(), iniPath());
return SetupResults::GameGone;
}
}
}
// plugin seems to be gone
log::error("no plugin matches name {}", m_gameName);
return SetupResults::PluginGone;
}
else
{
// can't do anything with these two missing
log::error("both game name and dir are missing from ini {}", iniPath());
return SetupResults::IniMissingGame;
}
}
void Instance::getProfile(const Settings& s)
{
if (!m_profile.isEmpty()) {
// there's already a profile set up, probably an override
return;
}
if (auto name=s.game().selectedProfileName()) {
// use last profile
m_profile = *name;
return;
}
// profile missing from ini, use the default
m_profile = QString::fromStdWString(AppConfig::defaultProfileName());
log::warn(
"no profile found in ini {}, using default '{}'",
iniPath(), m_profile);
}
InstanceManager::InstanceManager()
{
GlobalSettings::updateRegistryKey();
}
InstanceManager &InstanceManager::instance()
{
static InstanceManager s_Instance;
return s_Instance;
}
void InstanceManager::overrideInstance(const QString& instanceName)
{
m_overrideInstanceName = instanceName;
m_overrideInstance = true;
}
void InstanceManager::overrideProfile(const QString& profileName)
{
m_overrideProfileName = profileName;
m_overrideProfile = true;
}
std::optional<Instance> InstanceManager::currentInstance() const
{
const QString profile = m_overrideProfile ? m_overrideProfileName : "";
if (portableInstallIsLocked()) {
// force portable instance
return Instance(QDir(portablePath()), true, profile);
}
QString name;
if (m_overrideInstance)
name = m_overrideInstanceName;
else
name = GlobalSettings::currentInstance();
if (name.isEmpty()) {
if (portableInstanceExists()) {
// use portable
return Instance(QDir(portablePath()), true, profile);
} else {
// no instance set
return {};
}
}
return Instance(QDir(instancePath(name)), false, profile);
}
void InstanceManager::clearCurrentInstance()
{
setCurrentInstance("");
m_overrideInstance = false;
}
void InstanceManager::setCurrentInstance(const QString &name)
{
GlobalSettings::setCurrentInstance(name);
}
QString InstanceManager::instancePath(const QString& instanceName) const
{
return QDir::fromNativeSeparators(instancesPath() + "/" + instanceName);
}
QString InstanceManager::instancesPath() const
{
return QDir::fromNativeSeparators(
QStandardPaths::writableLocation(QStandardPaths::DataLocation));
}
QString InstanceManager::iniPath(const QDir& instanceDir)
{
return instanceDir.filePath(QString::fromStdWString(AppConfig::iniFileName()));
}
std::vector<QDir> InstanceManager::instancePaths() const
{
const std::set<QString> ignore = {
"cache", "qtwebengine",
};
const QDir root(instancesPath());
const auto dirs = root.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
std::vector<QDir> list;
for (auto&& d : dirs) {
if (!ignore.contains(QFileInfo(d).fileName().toLower())) {
list.push_back(root.filePath(d));
}
}
return list;
}
QStringList InstanceManager::instanceNames() const
{
QStringList list;
for (auto&& d : instancePaths()) {
list.push_back(d.dirName());
}
return list;
}
bool InstanceManager::isPortablePath(const QString& dataPath)
{
return (dataPath == portablePath());
}
QString InstanceManager::portablePath()
{
return qApp->applicationDirPath();
}
bool InstanceManager::portableInstanceExists() const
{
return QFile::exists(qApp->applicationDirPath() + "/" +
QString::fromStdWString(AppConfig::iniFileName()));
}
bool InstanceManager::portableInstallIsLocked() const
{
return QFile::exists(qApp->applicationDirPath() + "/" +
QString::fromStdWString(AppConfig::portableLockFileName()));
}
bool InstanceManager::allowedToChangeInstance() const
{
return !portableInstallIsLocked();
}
const MOBase::IPluginGame* InstanceManager::gamePluginForDirectory(
const QDir& instanceDir, const PluginContainer& plugins) const
{
const QString ini = iniPath(instanceDir);
Settings s(ini);
if (s.iniStatus() != QSettings::NoError)
{
log::error("failed to load settings from {}", ini);
return nullptr;
}
const auto instanceGameName = s.game().name();
if (instanceGameName && !instanceGameName->isEmpty())
{
for (const IPluginGame* game : plugins.plugins<IPluginGame>()) {
if (instanceGameName->compare(game->gameName(), Qt::CaseInsensitive) == 0) {
return game;
}
}
log::error(
"no plugin reports game name '{}' found in ini {}",
*instanceGameName, ini);
}
else
{
log::error("no game name found in ini {}", ini);
}
log::error("falling back on looksValid check");
const auto gameDir = s.game().directory();
if (gameDir && !gameDir->isEmpty())
{
for (const IPluginGame* game : plugins.plugins<IPluginGame>()) {
if (game->looksValid(*gameDir)) {
return game;
}
}
log::error(
"no plugins appear to support game directory '{}' from ini {}",
*gameDir, ini);
}
else
{
log::error("no game directory found in ini {}", ini);
}
return nullptr;
}
QString InstanceManager::makeUniqueName(const QString& instanceName) const
{
const QString sanitized = sanitizeInstanceName(instanceName);
QString name = sanitized;
for (int i=2; i<100; ++i) {
if (!instanceExists(name)) {
return name;
}
name = QString("%1 (%2)").arg(sanitized).arg(i);
}
return {};
}
bool InstanceManager::instanceExists(const QString& instanceName) const
{
const QDir root = instancesPath();
return root.exists(instanceName);
}
QString InstanceManager::sanitizeInstanceName(const QString &name) const
{
QString new_name = name;
// Restrict the allowed characters
new_name = new_name.remove(QRegExp("[^A-Za-z0-9 _=+;!@#$%^'\\-\\.\\[\\]\\{\\}\\(\\)]"));
// Don't end in spaces and periods
new_name = new_name.remove(QRegExp("\\.*$"));
new_name = new_name.remove(QRegExp(" *$"));
// Recurse until stuff stops changing
if (new_name != name) {
return sanitizeInstanceName(new_name);
}
return new_name;
}
bool InstanceManager::validInstanceName(const QString& instanceName) const
{
if (instanceName.isEmpty()) {
return false;
}
return (instanceName == sanitizeInstanceName(instanceName));
}
|