-
Notifications
You must be signed in to change notification settings - Fork 7
/
gameprocess.cpp
245 lines (182 loc) · 7.39 KB
/
gameprocess.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
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
#define NOMINMAX
#include "gameprocess.h"
#include "ui_gameprocess.h"
#include <QDebug>
#include <QMessageBox>
#include <QSettings>
#ifdef Q_OS_WIN32
#include "windebugmonitor.h"
#endif
#include "configparser.h"
#include <QScrollBar>
#include <QDateTime>
#include <QFileDialog>
#include "utils.h"
#ifdef Q_OS_WIN32
WinDebugMonitor* GameProcess::debugMonitor = NULL;
#endif
GameProcess::GameProcess(ConfigParser* config, QWidget *parent) :
QDialog(parent),
ui(new Ui::GameProcess) {
ui->setupUi(this);
process = NULL;
configParser = config;
connect(ui->pushButton_config, SIGNAL(clicked()), this, SLOT(dumpConfigValues()));
connect(ui->pushButton_kill, SIGNAL(clicked()), this, SLOT(killProcess()));
connect(ui->pushButton_save, SIGNAL(clicked()), this, SLOT(saveLogToFile()));
connect(ui->pushButton_clean, SIGNAL(clicked()), this, SLOT(clearOutputLogScreen()));
}
GameProcess::~GameProcess() {
delete ui;
delete configParser;
}
void GameProcess::saveLogToFile() {
QString fileName = QFileDialog::getSaveFileName(this, tr("Save Log"),
"/home/log.txt",
tr("Text files (*.txt)"));
if (fileName.isEmpty())
return;
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::warning(this, "ERROR", "Could not save file!");
return;
}
QTextStream stream(&file);
stream << ui->textBrowser->toPlainText();
file.close();
}
bool GameProcess::start(const QString& folder, const QString& executable, const QStringList &arguments) {
process = new QProcess();
QSettings settings;
process->setWorkingDirectory(folder);
#ifdef Q_OS_WIN32
if (debugMonitor == NULL) {
debugMonitor = new WinDebugMonitor();
}
connect(debugMonitor, SIGNAL(outputDebugString(int, QString)), this, SLOT(outputDebugString(int, QString)));
if (!settings.value("close_after_start", false).toBool() && !debugMonitor->IsInitialized() && settings.value("capture_debug_output", false).toBool()) {
int ret = debugMonitor->Initialize();
if (ret != 0)
ui->textBrowser->append("Warning: Debug Monitor could not be initialized, if you want the debug output enabled please start the launchpad with administrator privileges");
} else if (!settings.value("capture_debug_output", false).toBool()) {
if (debugMonitor->IsInitialized()) {
ui->textBrowser->append("Please restart the launchpad to disable the debug output");
} else
ui->textBrowser->append("Capture debug output is disabled");
}
#endif
connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(startError(QProcess::ProcessError)));
connect(process, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(finished(int,QProcess::ExitStatus)));
connect(process, SIGNAL(started()), this, SLOT(started()));
QStringList env = QProcess::systemEnvironment();
//env.append("SWGCLIENT_MEMORY_SIZE_MB=4096");
//qDebug() << "env:" << env;
process->setEnvironment(env);
#ifdef Q_OS_WIN32
process->start(folder + "\\" + executable, arguments);
#else
QString wineBinary = settings.value("wine_binary").toString();
if (wineBinary.isEmpty())
wineBinary = "wine";
QString args = settings.value("wine_args").toString();
QStringList argsList;
if (!args.isEmpty())
argsList = Utils::getArgumentList(args);
argsList.append(folder + "/" + executable);
qDebug() << argsList;
QString envs = settings.value("wine_env").toString();
if (!envs.isEmpty()) {
env.append(Utils::getArgumentList(envs));
qDebug() << env;
process->setEnvironment(env);
}
if (!QProcess::startDetached(wineBinary, argsList, folder)) {
QMessageBox::warning(this, "ERROR", "Could not launch wine!");
}
#endif
return true;
}
void GameProcess::killProcess() {
if (!process)
return;
if (QMessageBox::question(this, "Warning", "Are you sure you want to attempt terminate this process?") != QMessageBox::Yes)
return;
//process->kill();
process->terminate();
}
void GameProcess::dumpConfigValues() {
QMap<QString, QVector<ConfigValue> >* values = configParser->getAllValues();
QMapIterator<QString, QVector<ConfigValue> > it(*values);
ui->textBrowser->append("------------------------------------------------------------------------------------------------------------------------------------");
ui->textBrowser->append("Starting the config value dump");
while (it.hasNext()) {
it.next();
QString className = it.key();
ui->textBrowser->append("[" + className + "]");
QVector<ConfigValue> configValues = it.value();
for (int i = 0; i < configValues.size(); ++i) {
const ConfigValue val = configValues.at(i);
ui->textBrowser->append("\t" + val.variableName + " = " + val.variableValue + " \t\tdefined in: " + val.fileName);
}
ui->textBrowser->append("\n");
}
ui->textBrowser->append("Config dump end");
ui->textBrowser->append("------------------------------------------------------------------------------------------------------------------------------------");
//ui->textBrowser->
QScrollBar *sb = ui->textBrowser->verticalScrollBar();
sb->setValue(sb->maximum());
}
void GameProcess::started() {
//qDebug() << "proces started with state: " << process->state();
if (process && process->state() != QProcess::Running)
return;
QSettings settings;
if (settings.value("close_after_start", false) == true) {
QApplication::quit();
}
}
void GameProcess::outputDebugString(QString str) {
ui->textBrowser->append(str);
}
void GameProcess::outputDebugString(int processId, QString str) {
//QDateTime currentStamp = ;
QString date = QDateTime::currentDateTime().toString("hh:mm:ss");
#ifdef Q_OS_WIN32
if (processId < 0 || (process && process->pid()->dwProcessId == processId)) {
ui->textBrowser->append(date + ": " + str);
}
#else
if (processId < 0 || (process && process->pid() == processId)) {
ui->textBrowser->append(date + ": " + str);
}
#endif
}
void GameProcess::finished(int exitCode, QProcess::ExitStatus ) {
process = NULL;
ui->textBrowser->append("Process exited with exit code " + QString::number(exitCode));
emit processFinished(this, exitCode);
}
void GameProcess::outputChannelDataReady() {
qDebug() << "data ready emitted";
//qDebug() << process->readAllStandardOutput();
}
void GameProcess::startError(QProcess::ProcessError err) {
qDebug() << "start swg error:" << err;
QString errorMessage;
switch (err) {
case QProcess::FailedToStart:
errorMessage = "File not found!";
break;
case QProcess::Crashed:
errorMessage = "Process crashed!";
break;
default:
errorMessage = "Unknown error!";
break;
}
//QMessageBox::critical( this, "ERROR", "Could not launch client! " + errorMessage);
ui->textBrowser->append(errorMessage);
}
void GameProcess::clearOutputLogScreen() {
ui->textBrowser->clear();
}