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
|
#include "createinstancedialog.h"
#include "ui_createinstancedialog.h"
#include "instancemanager.h"
#include "plugincontainer.h"
#include <report.h>
#include <iplugingame.h>
namespace cid
{
class Page
{
public:
Page(CreateInstanceDialog& dlg, std::size_t i)
: ui(dlg.getUI()), m_dlg(dlg), m_pc(dlg.pluginContainer()), m_index(i)
{
}
virtual bool ready() const
{
return true;
}
void next()
{
m_dlg.next();
}
protected:
Ui::CreateInstanceDialog* ui;
CreateInstanceDialog& m_dlg;
const PluginContainer& m_pc;
private:
std::size_t m_index;
};
class TypePage : public Page
{
public:
TypePage(CreateInstanceDialog& dlg, std::size_t i)
: Page(dlg, i)
{
ui->createGlobal->setDescription(
ui->createGlobal->description()
.arg(InstanceManager::instance().instancesPath()));
ui->createPortable->setDescription(
ui->createPortable->description()
.arg(qApp->applicationDirPath()));
QObject::connect(
ui->createGlobal, &QAbstractButton::clicked, [&]{ global(); });
QObject::connect(
ui->createPortable, &QAbstractButton::clicked, [&]{ portable(); });
}
bool ready() const override
{
return m_global.has_value();
}
void global()
{
m_global = true;
ui->createGlobal->setChecked(true);
ui->createPortable->setChecked(false);
next();
}
void portable()
{
m_global = false;
ui->createGlobal->setChecked(false);
ui->createPortable->setChecked(true);
next();
}
private:
std::optional<bool> m_global;
};
class GamePage : public Page
{
public:
GamePage(CreateInstanceDialog& dlg, std::size_t i)
: Page(dlg, i)
{
createGames();
fillList();
QObject::connect(ui->showAllGames, &QCheckBox::clicked, [&]{ fillList(); });
}
void select(MOBase::IPluginGame* game)
{
Game* checked = findGame(game);
if (!checked) {
return;
}
if (!checked->installed) {
const auto path = QFileDialog::getExistingDirectory(
&m_dlg, QObject::tr("Find game installation"));
if (path.isEmpty()) {
checked = nullptr;
} else {
checked = checkInstallation(path, checked);
}
}
selectButton(checked);
}
private:
struct Game
{
MOBase::IPluginGame* game = nullptr;
QCommandLinkButton* button = nullptr;
QString dir;
bool installed = false;
Game(MOBase::IPluginGame* g)
: game(g), installed(g->isInstalled())
{
if (installed) {
dir = game->gameDirectory().path();
}
}
Game(const Game&) = delete;
Game& operator=(const Game&) = delete;
};
std::vector<std::unique_ptr<Game>> m_games;
Game* findGame(MOBase::IPluginGame* game)
{
for (auto& g : m_games) {
if (g->game == game) {
return g.get();
}
}
return nullptr;
}
void createGames()
{
m_games.clear();
for (auto* game : m_pc.plugins<MOBase::IPluginGame>()) {
m_games.push_back(std::make_unique<Game>(game));
}
}
void createButton(Game* g)
{
g->button = new QCommandLinkButton;
g->button->setCheckable(true);
updateButton(g);
QObject::connect(g->button, &QAbstractButton::clicked, [g, this] {
select(g->game);
});
}
void updateButton(Game* g)
{
if (!g->button) {
return;
}
g->button->setText(g->game->gameName());
if (g->installed) {
g->button->setDescription(g->dir);
} else {
g->button->setDescription(QObject::tr("No installation found"));
}
}
void selectButton(Game* g)
{
for (const auto& gg : m_games) {
if (!gg->button) {
continue;
}
if (g) {
gg->button->setChecked(gg->game == g->game);
} else {
gg->button->setChecked(false);
}
}
}
void fillList()
{
const bool showAll = ui->showAllGames->isChecked();
ui->games->clear();
for (auto& g : m_games) {
g->button = nullptr;
if (!showAll && !g->installed) {
// not installed
continue;
}
createButton(g.get());
ui->games->addButton(g->button, QDialogButtonBox::AcceptRole);
}
}
Game* checkInstallation(const QString& path, Game* g)
{
if (g->game->looksValid(path)) {
// okay
return g;
}
// the selected game can't use that folder, find another one
auto* otherGame = findAnotherGame(path);
if (otherGame == g->game) {
// shouldn't happen, but okay
return g;
}
if (otherGame) {
auto* confirmedGame = confirmOtherGame(path, g->game, otherGame);
if (!confirmedGame) {
// cancelled
return nullptr;
}
// make it look like the user clicked that button instead
g = findGame(confirmedGame);
if (!g) {
return nullptr;
}
} else {
// nothing can manage this, but the user can override
if (!confirmUnknown(path, g->game)) {
// cancelled
return nullptr;
}
}
// remember this path
g->dir = path;
g->installed = true;
updateButton(g);
return g;
}
MOBase::IPluginGame* findAnotherGame(const QString& path)
{
for (auto* otherGame : m_pc.plugins<MOBase::IPluginGame>()) {
if (otherGame->looksValid(path)) {
return otherGame;
}
}
return nullptr;
}
bool confirmUnknown(const QString& path, MOBase::IPluginGame* game)
{
const auto r = MOBase::TaskDialog(&m_dlg)
.title(QObject::tr("Unrecognized game"))
.main(QObject::tr("Unrecognized game"))
.content(QObject::tr(
"The folder %1 does not seem to contain installation for "
"<span style=\"white-space: nowrap; font-weight: bold;\">%2</span> or "
"any other game Mod Organizer can manage.")
.arg(path)
.arg(game->gameName()))
.button({
QObject::tr("Use this folder for %1").arg(game->gameName()),
QObject::tr("I know what I'm doing"),
QMessageBox::Ignore})
.button({
QObject::tr("Cancel"),
QMessageBox::Cancel})
.exec();
return (r == QMessageBox::Ignore);
}
MOBase::IPluginGame* confirmOtherGame(
const QString& path,
MOBase::IPluginGame* selectedGame, MOBase::IPluginGame* guessedGame)
{
const auto r = MOBase::TaskDialog(&m_dlg)
.title(QObject::tr("Incorrect game"))
.main(QObject::tr("Incorrect game"))
.content(QObject::tr(
"The folder %1 seems to contain an installation for "
"<span style=\"white-space: nowrap; font-weight: bold;\">%2</span>, "
"not "
"<span style=\"white-space: nowrap; font-weight: bold;\">%3</span>.")
.arg(path)
.arg(guessedGame->gameName())
.arg(selectedGame->gameName()))
.button({
QObject::tr("Manage %1 instead").arg(guessedGame->gameName()),
QMessageBox::Ok})
.button({
QObject::tr("Use this folder for %1").arg(selectedGame->gameName()),
QObject::tr("I know what I'm doing"),
QMessageBox::Ignore})
.button({
QObject::tr("Cancel"),
QMessageBox::Cancel})
.exec();
switch (r)
{
case QMessageBox::Ok:
return guessedGame;
case QMessageBox::Ignore:
return selectedGame;
case QMessageBox::Cancel:
default:
return nullptr;
}
}
};
class NamePage : public Page
{
public:
NamePage(CreateInstanceDialog& dlg, std::size_t i)
: Page(dlg, i)
{
}
};
class PathsPage : public Page
{
public:
PathsPage(CreateInstanceDialog& dlg, std::size_t i)
: Page(dlg, i)
{
}
};
} // namespace
CreateInstanceDialog::CreateInstanceDialog(
const PluginContainer& pc, QWidget *parent)
: QDialog(parent), ui(new Ui::CreateInstanceDialog), m_pc(pc)
{
using namespace cid;
ui->setupUi(this);
m_pages.push_back(std::make_unique<TypePage>(*this, 0));
m_pages.push_back(std::make_unique<GamePage>(*this, 1));
m_pages.push_back(std::make_unique<NamePage>(*this, 2));
m_pages.push_back(std::make_unique<PathsPage>(*this, 3));
ui->pages->setCurrentIndex(0);
updateNavigationButtons();
connect(ui->next, &QPushButton::clicked, [&]{ next(); });
connect(ui->back, &QPushButton::clicked, [&]{ back(); });
}
CreateInstanceDialog::~CreateInstanceDialog() = default;
Ui::CreateInstanceDialog* CreateInstanceDialog::getUI()
{
return ui.get();
}
const PluginContainer& CreateInstanceDialog::pluginContainer()
{
return m_pc;
}
void CreateInstanceDialog::next()
{
ui->pages->setCurrentIndex(ui->pages->currentIndex() + 1);
updateNavigationButtons();
}
void CreateInstanceDialog::back()
{
ui->pages->setCurrentIndex(ui->pages->currentIndex() - 1);
updateNavigationButtons();
}
void CreateInstanceDialog::updateNavigationButtons()
{
const auto i = ui->pages->currentIndex();
const auto last = (i == (ui->pages->count() - 1));
ui->next->setEnabled(m_pages[i]->ready() && !last);
ui->back->setEnabled(i > 0);
}
|