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
|
/*
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 <utility.h>
#include <log.h>
#include "shared/appconfig.h"
#include <QCoreApplication>
#include <QDir>
#include <QStandardPaths>
#include <QInputDialog>
#include <QMessageBox>
#include <cstdint>
using namespace MOBase;
static const char COMPANY_NAME[] = "Tannin";
static const char APPLICATION_NAME[] = "Mod Organizer";
static const char INSTANCE_KEY[] = "CurrentInstance";
InstanceManager::InstanceManager()
: m_AppSettings(COMPANY_NAME, APPLICATION_NAME)
{
}
InstanceManager &InstanceManager::instance()
{
static InstanceManager s_Instance;
return s_Instance;
}
void InstanceManager::overrideInstance(const QString& instanceName)
{
m_overrideInstanceName = instanceName;
m_overrideInstance = true;
}
QString InstanceManager::currentInstance() const
{
if (m_overrideInstance)
return m_overrideInstanceName;
else
return m_AppSettings.value(INSTANCE_KEY, "").toString();
}
void InstanceManager::clearCurrentInstance()
{
setCurrentInstance("");
m_Reset = true;
m_overrideInstance = false;
}
void InstanceManager::setCurrentInstance(const QString &name)
{
m_AppSettings.setValue(INSTANCE_KEY, name);
}
bool InstanceManager::deleteLocalInstance(const QString &instanceId) const
{
bool result = true;
QString instancePath = QDir::fromNativeSeparators(QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/" + instanceId);
if (QMessageBox::warning(nullptr, QObject::tr("Deleting folder"),
QObject::tr("I'm about to delete the following folder: \"%1\". Proceed?").arg(instancePath), QMessageBox::No | QMessageBox::Yes, QMessageBox::No) == QMessageBox::No ){
return false;
}
if (!MOBase::shellDelete(QStringList(instancePath),true))
{
log::warn(
"Failed to shell-delete \"{}\" (errorcode {}), trying regular delete",
instancePath, ::GetLastError());
if (!MOBase::removeDir(instancePath))
{
log::warn("regular delete failed too");
result = false;
}
}
return result;
}
QString InstanceManager::manageInstances(const QStringList &instanceList) const
{
SelectionDialog selection(
QString("<h3>%1</h3><br>%2")
.arg(QObject::tr("Choose Instance to Delete"))
.arg(QObject::tr("Be Careful! Deleting an Instance will remove all your files for that Instance (mods, downloads, profiles, configuration, ...). Custom paths outside of the instance folder for downloads, mods, etc. will be left untoched.")),
nullptr);
for (const QString &instance : instanceList)
{
selection.addChoice(QIcon(":/MO/gui/multiply_red"), instance, "", instance);
}
if (selection.exec() == QDialog::Rejected) {
return(chooseInstance(instances()));
}
else {
QString choice = selection.getChoiceData().toString();
{
if (QMessageBox::warning(nullptr, QObject::tr("Are you sure?"),
QObject::tr("Are you really sure you want to delete the Instance \"%1\" with all its files?").arg(choice), QMessageBox::No | QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes)
{
if (!deleteLocalInstance(choice))
{
QMessageBox::warning(nullptr, QObject::tr("Failed to delete Instance"),
QObject::tr("Could not delete Instance \"%1\". \nIf the folder was still in use, restart MO and try again.").arg(choice), QMessageBox::Ok);
}
}
}
}
return(manageInstances(instances()));
}
QString InstanceManager::queryInstanceName(const QStringList &instanceList) const
{
QString instanceId;
QString dialogText;
while (instanceId.isEmpty()) {
QInputDialog dialog;
dialog.setWindowTitle(QObject::tr("Enter a Name for the new Instance"));
dialog.setLabelText(QObject::tr("Enter a new name or select one from the suggested list: \n"
"(This is just a name for the Instance and can be whatever you wish,\n"
" the actual game selection will happen on the next screen regardless of chosen name)"));
// would be neat if we could take the names from the game plugins but
// the required initialization order requires the ini file to be
// available *before* we load plugins
dialog.setComboBoxItems({ "NewName", "Fallout 4", "SkyrimSE", "Skyrim", "SkyrimVR", "Fallout 3",
"Fallout NV", "TTW", "FO4VR", "Oblivion", "Morrowind", "Enderal" });
dialog.setComboBoxEditable(true);
if (dialog.exec() == QDialog::Rejected) {
throw MOBase::MyException(QObject::tr("Canceled"));
}
dialogText = dialog.textValue();
instanceId = sanitizeInstanceName(dialogText);
if (instanceId != dialogText) {
if (QMessageBox::question( nullptr,
QObject::tr("Invalid instance name"),
QObject::tr("The instance name \"%1\" is invalid. Use the name \"%2\" instead?").arg(dialogText,instanceId),
QMessageBox::Yes | QMessageBox::No) == QMessageBox::No) {
instanceId="";
continue;
}
}
bool alreadyExists=false;
for (const QString &instance : instanceList) {
if(instanceId==instance)
alreadyExists=true;
}
if(alreadyExists)
{
QMessageBox msgBox;
msgBox.setText( QObject::tr("The instance \"%1\" already exists.").arg(instanceId) );
msgBox.setInformativeText(QObject::tr("Please choose a different instance name, like: \"%1 1\" .").arg(instanceId));
msgBox.exec();
instanceId="";
}
}
return instanceId;
}
QString InstanceManager::chooseInstance(const QStringList &instanceList) const
{
if (portableInstallIsLocked()) {
return QString();
}
enum class Special : uint8_t {
NewInstance,
Portable,
Manage
};
SelectionDialog selection(
QString("<h3>%1</h3><br>%2")
.arg(QObject::tr("Choose Instance"))
.arg(QObject::tr(
"Each Instance is a full set of MO data files (mods, "
"downloads, profiles, configuration, ...). You can use multiple "
"instances for different games. Instances are stored in Appdata and can be accessed by all MO installations. "
"If your MO folder is writable, you can also store a single instance locally (called "
"a Portable install, and all the MO data files will be inside the installation folder).")),
nullptr);
selection.disableCancel();
for (const QString &instance : instanceList) {
selection.addChoice(instance, "", instance);
}
selection.addChoice(QIcon(":/MO/gui/add"), QObject::tr("New"),
QObject::tr("Create a new instance."),
static_cast<uint8_t>(Special::NewInstance));
if (QFileInfo(qApp->applicationDirPath()).isWritable()) {
selection.addChoice(QIcon(":/MO/gui/package"), QObject::tr("Portable"),
QObject::tr("Use MO folder for data."),
static_cast<uint8_t>(Special::Portable));
}
selection.addChoice(QIcon(":/MO/gui/remove"), QObject::tr("Manage Instances"),
QObject::tr("Delete an Instance."),
static_cast<uint8_t>(Special::Manage));
selection.setWindowFlags(selection.windowFlags() | Qt::WindowStaysOnTopHint);
if (selection.exec() == QDialog::Rejected) {
log::debug("rejected");
throw MOBase::MyException(QObject::tr("Canceled"));
}
QVariant choice = selection.getChoiceData();
if (choice.type() == QVariant::String) {
return choice.toString();
} else {
switch (static_cast<Special>(choice.value<uint8_t>())) {
case Special::NewInstance: return queryInstanceName(instanceList);
case Special::Portable: return QString();
case Special::Manage: {
return(manageInstances(instances()));
}
default: throw std::runtime_error("invalid selection");
}
}
}
QString InstanceManager::instancePath() const
{
return QDir::fromNativeSeparators(
QStandardPaths::writableLocation(QStandardPaths::DataLocation));
}
QStringList InstanceManager::instances() const
{
const std::set<QString> ignore = {
"cache", "qtwebengine",
};
const auto dirs = QDir(instancePath())
.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
QStringList list;
for (auto&& d : dirs) {
if (!ignore.contains(QFileInfo(d).fileName().toLower())) {
list.push_back(d);
}
}
return list;
}
bool InstanceManager::isPortablePath(const QString& dataPath)
{
return (dataPath == qApp->applicationDirPath());
}
bool InstanceManager::portableInstall() 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();
}
void InstanceManager::createDataPath(const QString &dataPath) const
{
if (!QDir(dataPath).exists()) {
if (!QDir().mkpath(dataPath)) {
throw MOBase::MyException(
QObject::tr("failed to create %1").arg(dataPath));
} else {
QMessageBox::information(
nullptr, QObject::tr("Data directory created"),
QObject::tr("New data directory created at %1. If you don't want to "
"store a lot of data there, reconfigure the storage "
"directories via settings.").arg(dataPath));
}
}
}
QString InstanceManager::determineDataPath()
{
QString instanceId = currentInstance();
if (portableInstallIsLocked())
{
instanceId.clear();
}
if (instanceId.isEmpty() && !m_Reset && (m_overrideInstance || portableInstall()))
{
// startup, apparently using portable mode before
return qApp->applicationDirPath();
}
QString dataPath = QDir::fromNativeSeparators(
QStandardPaths::writableLocation(QStandardPaths::DataLocation)
+ "/" + instanceId);
if (!m_overrideInstance && (instanceId.isEmpty() || !QFileInfo::exists(dataPath))) {
instanceId = chooseInstance(instances());
setCurrentInstance(instanceId);
if (!instanceId.isEmpty()) {
dataPath = QDir::fromNativeSeparators(
QStandardPaths::writableLocation(QStandardPaths::DataLocation)
+ "/" + instanceId);
}
}
if (instanceId.isEmpty()) {
return qApp->applicationDirPath();
} else {
createDataPath(dataPath);
return dataPath;
}
}
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;
}
|