-
Notifications
You must be signed in to change notification settings - Fork 13
/
launcherapp.cpp
129 lines (122 loc) · 4.62 KB
/
launcherapp.cpp
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
#include "launcherapp.h"
#include <QIcon>
#include <QFileOpenEvent>
#include <QDebug>
#include "profilemanager.h"
#include "versionmanager.h"
#include "gamelauncher.h"
#include "googleversionchannel.h"
LauncherApp::LauncherApp(int &argc, char **argv) : QApplication(argc, argv) {
auto appdir = getenv("APPDIR");
if(appdir != nullptr)
setWindowIcon(QIcon(QString::fromUtf8(appdir) + "/mcpelauncher-ui-qt.png"));
}
bool LauncherApp::event(QEvent *event) {
if (event->type() == QEvent::Close) {
AppCloseEvent qmlEvent;
emit closing(&qmlEvent);
if (!qmlEvent.isAccepted()) {
event->setAccepted(false);
return true;
}
} else if (event->type() == QEvent::FileOpen) {
QFileOpenEvent *openEvent = static_cast<QFileOpenEvent *>(event);
auto url = openEvent->url();
qDebug() << "Open Url " << url;
if(url.isLocalFile()) {
launchProfileFile("", url.toLocalFile(), false);
} else if(url.isValid()) {
launchProfileFile("", url.toString(), false);
} else {
launchProfileFile("", openEvent->file(), false);
}
}
return QApplication::event(event);
}
int LauncherApp::launchProfileFile(QString profileName, QString filePath, bool startEventLoop) {
VersionManager vmanager;
ProfileManager manager;
ProfileInfo * profile = nullptr;
if(profileName.length() > 0) {
for(auto&& pro : manager.profiles()) {
if(((ProfileInfo *)pro)->name == profileName) {
profile = (ProfileInfo *)pro;
}
}
if(profile == nullptr) {
printf("Profile not found: %s\n", profileName.toStdString().data());
return 1;
}
} else {
profile = manager.activeProfile();
}
GameLauncher launcher;
launcher.logAttached();
QObject::connect(&launcher, &GameLauncher::logAppended, [](QString str) {
printf("%s", str.toStdString().data());
});
int exitCode = -1;
bool exited = false;
auto shouldExit = [&](int code) {
exitCode = code;
exited = true;
this->exit(code);
};
QObject::connect(&launcher, &GameLauncher::stateChanged, [&]() {
if(!launcher.running() && startEventLoop) {
this->exit(launcher.crashed() ? 1 : 0);
}
});
QObject::connect(&launcher, &GameLauncher::launchFailed, [&]() {
if(startEventLoop) {
shouldExit(1);
}
});
QSettings m_settings;
auto trialMode = m_settings.value("trialMode", false).toBool();
m_settings.beginGroup("googleversionchannel");
auto m_latestVersion = m_settings.value("latest_version").toString();
auto m_latestVersionCode = m_settings.value("latest_version_code").toInt();
auto m_latestVersionIsBeta = m_settings.value("latest_version_isbeta").toBool();
if(!trialMode && m_settings.value("latest_version_id").toString() != (m_latestVersion + QChar((char)m_latestVersionCode) + QChar(m_latestVersionIsBeta))) {
printf("Something went wrong\n");
shouldExit(1);
}
QObject::connect(&launcher, &GameLauncher::fileStarted, [&](bool success) {
if(success) {
if(startEventLoop) {
shouldExit(success ? 0 : 1);
}
} else {
launcher.start(false, profile->arch, !trialMode, filePath);
}
});
launcher.setProfile(profile);
if(profile->versionType == ProfileInfo::LATEST_GOOGLE_PLAY) {
GoogleVersionChannel playChannel;
auto versionInfo = vmanager.versionList()->get(playChannel.latestVersionCode());
if(versionInfo == nullptr) {
printf("Couldn't find Google Play Latest version %d\n", playChannel.latestVersionCode());
versionInfo = vmanager.versionList()->latestDownloadedVersion();
}
if(versionInfo == nullptr) {
printf("Couldn't find any Latest Downloaded version!\n");
}
launcher.setGameDir(vmanager.getDirectoryFor(versionInfo));
} else if(profile->versionType == ProfileInfo::LOCKED_NAME) {
launcher.setGameDir(vmanager.getDirectoryFor(profile->versionDirName));
} else if(profile->versionType == ProfileInfo::LOCKED_CODE && profile->versionCode) {
launcher.setGameDir(vmanager.getDirectoryFor(vmanager.versionList()->get(profile->versionCode)));
}
if(filePath.length() > 0) {
launcher.startFile(filePath);
} else {
launcher.start(false, profile->arch, !trialMode);
}
return exited ? exitCode : startEventLoop ? this->exec() : 0;
}
#ifndef __APPLE__
void LauncherApp::setVisibleInDock(bool) {
// stub
}
#endif