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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
|
/*
Copyright (C) 2012 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/>.
*/
#ifdef LEAK_CHECK_WITH_VLD
#include <wchar.h>
#include <vld.h>
#endif // LEAK_CHECK_WITH_VLD
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <DbgHelp.h>
#include <appconfig.h>
#include <utility.h>
#include <scopeguard.h>
#include "mainwindow.h"
#include <report.h>
#include "modlist.h"
#include "profile.h"
#include "spawn.h"
#include "executableslist.h"
#include "singleinstance.h"
#include "utility.h"
#include "helper.h"
#include "logbuffer.h"
#include "selectiondialog.h"
#include "moapplication.h"
#include "tutorialmanager.h"
#include "nxmaccessmanager.h"
#include "instancemanager.h"
#include "moshortcut.h"
#include "organizercore.h"
#include <eh.h>
#include <windows_error.h>
#include <usvfs.h>
#include <QApplication>
#include <QPushButton>
#include <QListWidget>
#include <QComboBox>
#include <QCheckBox>
#include <QDir>
#include <QFileInfo>
#include <QSettings>
#include <QWhatsThis>
#include <QToolBar>
#include <QFileDialog>
#include <QDesktopServices>
#include <QMessageBox>
#include <QSharedMemory>
#include <QBuffer>
#include <QSplashScreen>
#include <QDirIterator>
#include <QDesktopServices>
#include <QLibraryInfo>
#include <QSslSocket>
#include <boost/scoped_array.hpp>
#include <ShellAPI.h>
#include <cstdarg>
#include <iostream>
#include <sstream>
#include <stdexcept>
#pragma comment(linker, "/manifestDependency:\"name='dlls' processorArchitecture='x86' version='1.0.0.0' type='win32' \"")
using namespace MOBase;
using namespace MOShared;
bool createAndMakeWritable(const std::wstring &subPath) {
QString const dataPath = qApp->property("dataPath").toString();
QString fullPath = dataPath + "/" + QString::fromStdWString(subPath);
if (!QDir(fullPath).exists() && !QDir().mkdir(fullPath)) {
QMessageBox::critical(nullptr, QObject::tr("Error"),
QObject::tr("Failed to create \"%1\". Your user "
"account probably lacks permission.")
.arg(fullPath));
return false;
} else {
return true;
}
}
bool bootstrap()
{
// remove the temporary backup directory in case we're restarting after an update
QString backupDirectory = qApp->applicationDirPath() + "/update_backup";
if (QDir(backupDirectory).exists()) {
shellDelete(QStringList(backupDirectory));
}
// cycle logfile
removeOldFiles(qApp->property("dataPath").toString() + "/" + QString::fromStdWString(AppConfig::logPath()),
"usvfs*.log", 5, QDir::Name);
if (!createAndMakeWritable(AppConfig::logPath())) {
return false;
}
return true;
}
LPTOP_LEVEL_EXCEPTION_FILTER prevUnhandledExceptionFilter = nullptr;
static LONG WINAPI MyUnhandledExceptionFilter(struct _EXCEPTION_POINTERS *exceptionPtrs)
{
const std::wstring& dumpPath = OrganizerCore::crashDumpsPath();
int dumpRes =
CreateMiniDump(exceptionPtrs, OrganizerCore::getGlobalCrashDumpsType(), dumpPath.c_str());
if (!dumpRes)
qCritical("ModOrganizer has crashed, crash dump created.");
else
qCritical("ModOrganizer has crashed, CreateMiniDump failed (%d, error %lu).", dumpRes, GetLastError());
if (prevUnhandledExceptionFilter)
return prevUnhandledExceptionFilter(exceptionPtrs);
else
return EXCEPTION_CONTINUE_SEARCH;
}
// Parses the first parseArgCount arguments of the current process command line and returns
// them in parsedArgs, the rest of the command line is returned untouched.
LPCWSTR UntouchedCommandLineArguments(int parseArgCount, std::vector<std::wstring>& parsedArgs)
{
LPCWSTR cmd = GetCommandLineW();
LPCWSTR arg = nullptr; // to skip executable name
for (; parseArgCount >= 0 && *cmd; ++cmd)
{
if (*cmd == '"') {
int escaped = 0;
for (++cmd; *cmd && (*cmd != '"' || escaped % 2 != 0); ++cmd)
escaped = *cmd == '\\' ? escaped + 1 : 0;
}
if (*cmd == ' ') {
if (arg)
if (cmd-1 > arg && *arg == '"' && *(cmd-1) == '"')
parsedArgs.push_back(std::wstring(arg+1, cmd-1));
else
parsedArgs.push_back(std::wstring(arg, cmd));
arg = cmd + 1;
--parseArgCount;
}
}
return cmd;
}
static int SpawnWaitProcess(LPCWSTR workingDirectory, LPCWSTR commandLine) {
PROCESS_INFORMATION pi{ 0 };
STARTUPINFO si{ 0 };
si.cb = sizeof(si);
std::wstring commandLineCopy = commandLine;
if (!CreateProcessW(NULL, &commandLineCopy[0], NULL, NULL, FALSE, 0, NULL, workingDirectory, &si, &pi)) {
// A bit of a problem where to log the error message here, at least this way you can get the message
// using a either DebugView or a live debugger:
std::wostringstream ost;
ost << L"CreateProcess failed: " << commandLine << ", " << GetLastError();
OutputDebugStringW(ost.str().c_str());
return -1;
}
WaitForSingleObject(pi.hProcess, INFINITE);
DWORD exitCode = (DWORD)-1;
::GetExitCodeProcess(pi.hProcess, &exitCode);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
return static_cast<int>(exitCode);
}
static DWORD WaitForProcess() {
}
static bool HaveWriteAccess(const std::wstring &path)
{
bool writable = false;
const static SECURITY_INFORMATION requestedFileInformation = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION;
DWORD length = 0;
if (!::GetFileSecurityW(path.c_str(), requestedFileInformation, nullptr, 0UL, &length)
&& (::GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
std::string tempBuffer;
tempBuffer.reserve(length);
PSECURITY_DESCRIPTOR security = (PSECURITY_DESCRIPTOR)tempBuffer.data();
if (security
&& ::GetFileSecurity(path.c_str(), requestedFileInformation, security, length, &length)) {
HANDLE token = nullptr;
const static DWORD tokenDesiredAccess = TOKEN_IMPERSONATE | TOKEN_QUERY | TOKEN_DUPLICATE | STANDARD_RIGHTS_READ;
if (!::OpenThreadToken(::GetCurrentThread(), tokenDesiredAccess, TRUE, &token)) {
if (!::OpenProcessToken(::GetCurrentProcess(), tokenDesiredAccess, &token)) {
throw std::runtime_error("Unable to get any thread or process token");
}
}
HANDLE impersonatedToken = nullptr;
if (::DuplicateToken(token, SecurityImpersonation, &impersonatedToken)) {
GENERIC_MAPPING mapping = { 0xFFFFFFFF };
mapping.GenericRead = FILE_GENERIC_READ;
mapping.GenericWrite = FILE_GENERIC_WRITE;
mapping.GenericExecute = FILE_GENERIC_EXECUTE;
mapping.GenericAll = FILE_ALL_ACCESS;
DWORD genericAccessRights = FILE_GENERIC_WRITE;
::MapGenericMask(&genericAccessRights, &mapping);
PRIVILEGE_SET privileges = { 0 };
DWORD grantedAccess = 0;
DWORD privilegesLength = sizeof(privileges);
BOOL result = 0;
if (::AccessCheck(security, impersonatedToken, genericAccessRights, &mapping, &privileges, &privilegesLength, &grantedAccess, &result)) {
writable = result != 0;
}
::CloseHandle(impersonatedToken);
}
::CloseHandle(token);
}
}
return writable;
}
QString determineProfile(QStringList &arguments, const QSettings &settings)
{
QString selectedProfileName = QString::fromUtf8(settings.value("selected_profile", "").toByteArray());
{ // see if there is a profile on the command line
int profileIndex = arguments.indexOf("-p", 1);
if ((profileIndex != -1) && (profileIndex < arguments.size() - 1)) {
qDebug("profile overwritten on command line");
selectedProfileName = arguments.at(profileIndex + 1);
}
arguments.removeAt(profileIndex);
arguments.removeAt(profileIndex);
}
if (selectedProfileName.isEmpty()) {
qDebug("no configured profile");
selectedProfileName = "Default";
} else {
qDebug("configured profile: %s", qPrintable(selectedProfileName));
}
return selectedProfileName;
}
MOBase::IPluginGame *selectGame(QSettings &settings, QDir const &gamePath, MOBase::IPluginGame *game)
{
settings.setValue("gameName", game->gameName());
//Sadly, hookdll needs gamePath in order to run. So following code block is
//commented out
/*if (gamePath == game->gameDirectory()) {
settings.remove("gamePath");
} else*/ {
QString gameDir = gamePath.absolutePath();
game->setGamePath(gameDir);
settings.setValue("gamePath", QDir::toNativeSeparators(gameDir).toUtf8().constData());
}
return game; //Woot
}
MOBase::IPluginGame *determineCurrentGame(QString const &moPath, QSettings &settings, PluginContainer const &plugins)
{
//Determine what game we are running where. Be very paranoid in case the
//user has done something odd.
//If the game name has been set up, use that.
QString gameName = settings.value("gameName", "").toString();
if (!gameName.isEmpty()) {
MOBase::IPluginGame *game = plugins.managedGame(gameName);
if (game == nullptr) {
reportError(QObject::tr("Plugin to handle %1 no longer installed").arg(gameName));
return nullptr;
}
QString gamePath = QString::fromUtf8(settings.value("gamePath", "").toByteArray());
if (gamePath == "") {
gamePath = game->gameDirectory().absolutePath();
}
QDir gameDir(gamePath);
if (game->looksValid(gameDir)) {
return selectGame(settings, gameDir, game);
}
}
//gameName wasn't set, or otherwise can't be found. Try looking through all
//the plugins using the gamePath
QString gamePath = QString::fromUtf8(settings.value("gamePath", "").toByteArray());
if (!gamePath.isEmpty()) {
QDir gameDir(gamePath);
//Look to see if one of the installed games binary file exists in the current
//game directory.
for (IPluginGame * const game : plugins.plugins<IPluginGame>()) {
if (game->looksValid(gameDir)) {
return selectGame(settings, gameDir, game);
}
}
}
//The following code would try to determine the right game to mange but it would usually find the wrong one
//so it was commented out.
/*
//OK, we are in a new setup or existing info is useless.
//See if MO has been installed inside a game directory
for (IPluginGame * const game : plugins.plugins<IPluginGame>()) {
if (game->isInstalled() && moPath.startsWith(game->gameDirectory().absolutePath())) {
//Found it.
return selectGame(settings, game->gameDirectory(), game);
}
}
//Try walking up the directory tree to see if MO has been installed inside a game
{
QDir gameDir(moPath);
do {
//Look to see if one of the installed games binary file exists in the current
//directory.
for (IPluginGame * const game : plugins.plugins<IPluginGame>()) {
if (game->looksValid(gameDir)) {
return selectGame(settings, gameDir, game);
}
}
//OK, chop off the last directory and try again
} while (gameDir.cdUp());
}
*/
//Then try a selection dialogue.
if (!gamePath.isEmpty() || !gameName.isEmpty()) {
reportError(QObject::tr("Could not use configuration settings for game \"%1\", path \"%2\".").
arg(gameName).arg(gamePath));
}
SelectionDialog selection(QObject::tr("Please select the game to manage"), nullptr, QSize(32, 32));
for (IPluginGame *game : plugins.plugins<IPluginGame>()) {
if (game->isInstalled()) {
QString path = game->gameDirectory().absolutePath();
selection.addChoice(game->gameIcon(), game->gameName(), path, QVariant::fromValue(game));
}
}
selection.addChoice(QString("Browse..."), QString(), QVariant::fromValue(static_cast<IPluginGame *>(nullptr)));
while (selection.exec() != QDialog::Rejected) {
IPluginGame * game = selection.getChoiceData().value<IPluginGame *>();
if (game != nullptr) {
return selectGame(settings, game->gameDirectory(), game);
}
gamePath = QFileDialog::getExistingDirectory(
nullptr, QObject::tr("Please select the game to manage"), QString(),
QFileDialog::ShowDirsOnly);
if (!gamePath.isEmpty()) {
QDir gameDir(gamePath);
for (IPluginGame * const game : plugins.plugins<IPluginGame>()) {
if (game->looksValid(gameDir)) {
return selectGame(settings, gameDir, game);
}
}
reportError(QObject::tr("No game identified in \"%1\". The directory is required to contain "
"the game binary and its launcher.").arg(gamePath));
}
}
return nullptr;
}
// extend path to include dll directory so plugins don't need a manifest
// (using AddDllDirectory would be an alternative to this but it seems fairly
// complicated esp.
// since it isn't easily accessible on Windows < 8
// SetDllDirectory replaces other search directories and this seems to
// propagate to child processes)
void setupPath()
{
static const int BUFSIZE = 4096;
qDebug("MO at: %s", qPrintable(QDir::toNativeSeparators(
QCoreApplication::applicationDirPath())));
QCoreApplication::setLibraryPaths(QStringList(QCoreApplication::applicationDirPath() + "/dlls") + QCoreApplication::libraryPaths());
boost::scoped_array<TCHAR> oldPath(new TCHAR[BUFSIZE]);
DWORD offset = ::GetEnvironmentVariable(TEXT("PATH"), oldPath.get(), BUFSIZE);
if (offset > BUFSIZE) {
oldPath.reset(new TCHAR[offset]);
::GetEnvironmentVariable(TEXT("PATH"), oldPath.get(), offset);
}
std::wstring newPath(ToWString(QDir::toNativeSeparators(
QCoreApplication::applicationDirPath())) + L"\\dlls");
newPath += L";";
newPath += oldPath.get();
::SetEnvironmentVariableW(L"PATH", newPath.c_str());
}
static void preloadSsl()
{
QString appPath = QDir::toNativeSeparators(QCoreApplication::applicationDirPath());
if (GetModuleHandleA("libeay32.dll"))
qWarning("libeay32.dll already loaded?!");
else {
QString libeay32 = appPath + "\\libeay32.dll";
if (!QFile::exists(libeay32))
qWarning("libeay32.dll not found: %s", qPrintable(libeay32));
else if (!LoadLibraryW(libeay32.toStdWString().c_str()))
qWarning("failed to load: %s, %d", qPrintable(libeay32), GetLastError());
}
if (GetModuleHandleA("ssleay32.dll"))
qWarning("ssleay32.dll already loaded?!");
else {
QString ssleay32 = appPath + "\\ssleay32.dll";
if (!QFile::exists(ssleay32))
qWarning("ssleay32.dll not found: %s", qPrintable(ssleay32));
else if (!LoadLibraryW(ssleay32.toStdWString().c_str()))
qWarning("failed to load: %s, %d", qPrintable(ssleay32), GetLastError());
}
}
static QString getVersionDisplayString()
{
VS_FIXEDFILEINFO version = GetFileVersion(ToWString(QApplication::applicationFilePath()));
return VersionInfo(version.dwFileVersionMS >> 16,
version.dwFileVersionMS & 0xFFFF,
version.dwFileVersionLS >> 16,
version.dwFileVersionLS & 0xFFFF).displayString();
}
int runApplication(MOApplication &application, SingleInstance &instance,
const QString &splashPath)
{
qDebug("Starting Mod Organizer version %s revision %s", qPrintable(getVersionDisplayString()),
#if defined(HGID)
HGID
#elif defined(GITID)
GITID
#else
"unknown"
#endif
);
#if !defined(QT_NO_SSL)
preloadSsl();
qDebug("ssl support: %d", QSslSocket::supportsSsl());
#else
qDebug("non-ssl build");
#endif
QString dataPath = application.property("dataPath").toString();
qDebug("data path: %s", qPrintable(dataPath));
if (!bootstrap()) {
reportError("failed to set up data paths");
return 1;
}
QStringList arguments = application.arguments();
try {
qDebug("Working directory: %s", qPrintable(QDir::toNativeSeparators(QDir::currentPath())));
QSettings settings(dataPath + "/"
+ QString::fromStdWString(AppConfig::iniFileName()),
QSettings::IniFormat);
// global crashDumpType sits in OrganizerCore to make a bit less ugly to update it when the settings are changed during runtime
OrganizerCore::setGlobalCrashDumpsType(settings.value("Settings/crash_dumps_type", static_cast<int>(CrashDumpsType::Mini)).toInt());
qDebug("Loaded settings:");
settings.beginGroup("Settings");
for (auto k : settings.allKeys())
if (!k.contains("username") && !k.contains("password"))
qDebug(" %s=%s", k.toUtf8().data(), settings.value(k).toString().toUtf8().data());
settings.endGroup();
qDebug("initializing core");
OrganizerCore organizer(settings);
if (!organizer.bootstrap()) {
reportError("failed to set up data paths");
return 1;
}
qDebug("initialize plugins");
PluginContainer pluginContainer(&organizer);
pluginContainer.loadPlugins();
MOBase::IPluginGame *game = determineCurrentGame(
application.applicationDirPath(), settings, pluginContainer);
if (game == nullptr) {
return 1;
}
if (splashPath.startsWith(':')) {
// currently using MO splash, see if the plugin contains one
QString pluginSplash
= QString(":/%1/splash").arg(game->gameShortName());
QImage image(pluginSplash);
if (!image.isNull()) {
image.save(dataPath + "/splash.png");
} else {
qDebug("no plugin splash");
}
}
organizer.setManagedGame(game);
organizer.createDefaultProfile();
if (!settings.contains("game_edition")) {
QStringList editions = game->gameVariants();
if (editions.size() > 1) {
SelectionDialog selection(
QObject::tr("Please select the game edition you have (MO can't "
"start the game correctly if this is set "
"incorrectly!)"),
nullptr);
int index = 0;
for (const QString &edition : editions) {
selection.addChoice(edition, "", index++);
}
if (selection.exec() == QDialog::Rejected) {
return 1;
} else {
settings.setValue("game_edition", selection.getChoiceString());
}
}
}
game->setGameVariant(settings.value("game_edition").toString());
qDebug("managing game at %s", qPrintable(QDir::toNativeSeparators(
game->gameDirectory().absolutePath())));
organizer.updateExecutablesList(settings);
QString selectedProfileName = determineProfile(arguments, settings);
organizer.setCurrentProfile(selectedProfileName);
// if we have a command line parameter, it is either a nxm link or
// a binary to start
if (arguments.size() > 1) {
if (MOShortcut shortcut{ arguments.at(1) }) {
if (shortcut.hasExecutable()) {
try {
organizer.runShortcut(shortcut);
return 0;
}
catch (const std::exception &e) {
reportError(
QObject::tr("failed to start shortcut: %1").arg(e.what()));
return 1;
}
}
}
else if (OrganizerCore::isNxmLink(arguments.at(1))) {
qDebug("starting download from command line: %s",
qPrintable(arguments.at(1)));
organizer.externalMessage(arguments.at(1));
}
else {
QString exeName = arguments.at(1);
qDebug("starting %s from command line", qPrintable(exeName));
arguments.removeFirst(); // remove application name (ModOrganizer.exe)
arguments.removeFirst(); // remove binary name
// pass the remaining parameters to the binary
try {
organizer.startApplication(exeName, arguments, QString(), QString());
return 0;
}
catch (const std::exception &e) {
reportError(
QObject::tr("failed to start application: %1").arg(e.what()));
return 1;
}
}
}
QPixmap pixmap(splashPath);
QSplashScreen splash(pixmap);
splash.show();
NexusInterface::instance(&pluginContainer)->getAccessManager()->startLoginCheck();
qDebug("initializing tutorials");
TutorialManager::init(
qApp->applicationDirPath() + "/"
+ QString::fromStdWString(AppConfig::tutorialsPath()) + "/",
&organizer);
if (!application.setStyleFile(settings.value("Settings/style", "").toString())) {
// disable invalid stylesheet
settings.setValue("Settings/style", "");
}
int res = 1;
{ // scope to control lifetime of mainwindow
// set up main window and its data structures
MainWindow mainWindow(settings, organizer, pluginContainer);
QObject::connect(&mainWindow, SIGNAL(styleChanged(QString)), &application,
SLOT(setStyleFile(QString)));
QObject::connect(&instance, SIGNAL(messageSent(QString)), &organizer,
SLOT(externalMessage(QString)));
mainWindow.readSettings();
qDebug("displaying main window");
mainWindow.show();
splash.finish(&mainWindow);
return application.exec();
}
} catch (const std::exception &e) {
reportError(e.what());
return 1;
}
}
int main(int argc, char *argv[])
{
//Should allow for better scaling of ui with higher resolution displays
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
if (argc >= 4) {
std::vector<std::wstring> arg;
auto args = UntouchedCommandLineArguments(2, arg);
if (arg[0] == L"launch")
return SpawnWaitProcess(arg[1].c_str(), args);
}
MOApplication application(argc, argv);
QStringList arguments = application.arguments();
setupPath();
bool forcePrimary = false;
if (arguments.contains("update")) {
arguments.removeAll("update");
forcePrimary = true;
}
MOShortcut moshortcut{ arguments.size() > 1 ? arguments.at(1) : "" };
SingleInstance instance(forcePrimary);
if (!instance.primaryInstance()) {
if (moshortcut ||
arguments.size() > 1 && OrganizerCore::isNxmLink(arguments.at(1)))
{
qDebug("not primary instance, sending shortcut/download message");
instance.sendMessage(arguments.at(1));
return 0;
} else if (arguments.size() == 1) {
QMessageBox::information(
nullptr, QObject::tr("Mod Organizer"),
QObject::tr("An instance of Mod Organizer is already running"));
return 0;
}
} // we continue for the primary instance OR if MO was called with parameters
do {
QString dataPath;
try {
InstanceManager& instanceManager = InstanceManager::instance();
if (moshortcut && moshortcut.hasInstance())
instanceManager.overrideInstance(moshortcut.instance());
dataPath = instanceManager.determineDataPath();
} catch (const std::exception &e) {
if (strcmp(e.what(),"Canceled"))
QMessageBox::critical(nullptr, QObject::tr("Failed to set up instance"), e.what());
return 1;
}
application.setProperty("dataPath", dataPath);
// initialize dump collection only after "dataPath" since the crashes are stored under it
prevUnhandledExceptionFilter = SetUnhandledExceptionFilter(MyUnhandledExceptionFilter);
LogBuffer::init(1000, QtDebugMsg, qApp->property("dataPath").toString() + "/logs/mo_interface.log");
QString splash = dataPath + "/splash.png";
if (!QFile::exists(dataPath + "/splash.png")) {
splash = ":/MO/gui/splash";
}
int result = runApplication(application, instance, splash);
if (result != INT_MAX) {
return result;
}
argc = 1;
} while (true);
}
|