-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
147 lines (132 loc) · 5.97 KB
/
main.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <QGuiApplication>
#include <singleapplication.h>
#include <QQmlApplicationEngine>
#include <QCommandLineParser>
#include <QUrl>
#include <QUrlQuery>
#include <QSettings>
#include <QIcon>
#include <QQuickStyle>
#include <QWindow>
#include "assetsmanager.h"
#include "rvgllauncher.h"
#ifdef Q_OS_WIN
#include <QSysInfo>
#else
#include <QFile>
#include <QTextStream>
#include <QProcess>
#endif
int main(int argc, char *argv[])
{
SingleApplication app(argc, argv, true);
app.setApplicationName("RVGL Companion");
app.setOrganizationName("Re-Volt.io");
app.setAttribute(Qt::AA_EnableHighDpiScaling);
app.setApplicationVersion("1.1");
app.setWindowIcon(QIcon("qrc:/icon.ico"));
QSettings settings;
#ifdef Q_OS_WIN
if (QSysInfo::currentCpuArchitecture() == "x86_64" || QSysInfo::currentCpuArchitecture() == "ia64") {
QSettings classes("HKEY_CURRENT_USER\\Software\\Classes\\rvgl",
QSettings::Registry64Format);
classes.setValue(".", "rvgl scheme handler");
classes.setValue("URL Protocol", "");
QSettings shell("HKEY_CURRENT_USER\\Software\\Classes\\rvgl\\shell",
QSettings::Registry64Format);
shell.setValue(".", "open");
QSettings command("HKEY_CURRENT_USER\\Software\\Classes\\rvgl\\shell\\open\\command",
QSettings::Registry64Format);
command.setValue(".", "\""+QGuiApplication::applicationFilePath().replace('/', '\\')+"\" \"%1\"");
}
QSettings classes("HKEY_CURRENT_USER\\Software\\Classes\\rvgl",
QSettings::Registry32Format);
classes.setValue(".", "rvgl scheme handler");
classes.setValue("URL Protocol", "");
QSettings shell("HKEY_CURRENT_USER\\Software\\Classes\\rvgl\\shell",
QSettings::Registry32Format);
shell.setValue(".", "open");
QSettings command("HKEY_CURRENT_USER\\Software\\Classes\\rvgl\\shell\\open\\command",
QSettings::Registry32Format);
command.setValue(".", "\""+QGuiApplication::applicationFilePath().replace('/', '\\')+"\" \"%1\"");
#else
QFile desktopFile(QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation)+"/rvgl_companion.desktop");
desktopFile.open(QIODevice::WriteOnly);
QTextStream desktopTextFile(&desktopFile);
desktopTextFile << "[Desktop Entry]\n"
"Version=1.0\n"
"Type=Application\n"
"Exec="+QGuiApplication::applicationFilePath()+" %u\n"
"Icon="+QGuiApplication::applicationDirPath()+"/icon.png\n"
"StartupNotify=true\n"
"Terminal=false\n"
"Categories=Utility;\n"
"MimeType=x-scheme-handler/rvgl\n"
"Name=RVGL Companion\n"
"Comment=Launch RVGL";
desktopFile.close();
QProcess desktopUpdater;
desktopUpdater.start("update-desktop-database ~/.local/share/applications");
#endif
QCommandLineParser parser;
parser.setApplicationDescription("RVGL companion app");
parser.addHelpOption();
parser.addVersionOption();
parser.addPositionalArgument("uri", QCoreApplication::translate("main", "rvgl: URI indicating what operation to execute."), "[uri]");
parser.process(app);
const QStringList args = parser.positionalArguments();
qmlRegisterType<AssetsManager>("AssetsManager", 1, 0, "AssetsManager");
qmlRegisterType<RVGLLauncher>("RVGLLauncher", 1, 0, "RVGLLauncher");
QString func;
QString option;
if (args.length() > 0) {
QUrl url(args.at(0));
func = url.fileName();
QUrlQuery query(url);
if (func == "run" || func == "join") {
RVGLLauncher* launcher = new RVGLLauncher();
QString dir = settings.value("installs").toList()[0].toMap()["dir"].toString();
QStringList launchOptions = settings.value("installs").toList()[0].toMap()["options"].toString().split(' ');
QPair<QString, QString> optionPair;
foreach (optionPair, query.queryItems()) {
launchOptions << "-"+optionPair.first << optionPair.second;
}
if (func == "join") {
launchOptions << "-lobby "+query.queryItemValue("IP");
}
QProcess* rvgl = launcher->launchRaw(dir, launchOptions);
QObject::connect(rvgl, SIGNAL (destroyed()), &app, SLOT (quit()));
return app.exec();
} else if (func == "install_asset") {
option = query.queryItemValue("URL");
}
}
if (app.isSecondary()){
app.sendMessage(func.toLatin1()+'\\'+option.toLatin1());
return 0;
}
QString style = QQuickStyle::name();
if (!style.isEmpty())
settings.setValue("style", style);
else
QQuickStyle::setStyle(settings.value("style").toString());
QQmlApplicationEngine engine;
engine.load("qrc:/main.qml");
engine.rootObjects().at(0)->setProperty("availableStyles", QStringList() << "Default" << "Material" << "Universal");
engine.rootObjects().at(0)->setProperty("func", func);
engine.rootObjects().at(0)->setProperty("option", option);
QObject::connect(
&app, SIGNAL (instanceStarted()),
engine.rootObjects().at(0), SLOT (raise())
);
QObject::connect(
&app, &SingleApplication::receivedMessage, [&](qint32 id, QByteArray message) {
qDebug() << "Secondary instance" << id << "sent us" << message;
engine.rootObjects().at(0)->setProperty("func", "");
engine.rootObjects().at(0)->setProperty("func", message.split('\\')[0]);
engine.rootObjects().at(0)->setProperty("option", "");
engine.rootObjects().at(0)->setProperty("option", message.split('\\')[1]);
}
);
return app.exec();
}