-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremglkprocess.cpp
496 lines (424 loc) · 18.7 KB
/
remglkprocess.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
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
/* Thunderquake Qt
* Copyright (C) 2017 Stephen A Gutknecht. All rights reserved.
*
* References
* =============
* QProcess on Android
* http://www.qtcentre.org/threads/56859-Starting-Qt-console-application-on-Android-using-QProcess
* */
#include "remglkprocess.h"
#include <QDebug>
#include <QColor>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>
#include <QMessageBox>
#include <QFileInfo>
#include "glk.h"
#include "remglkcompletertextedit.h"
RemGlkProcess::RemGlkProcess(QObject *parent, RemGlkCompleterTextEdit *textEditOutput) : QObject(parent)
{
qDebug() << "---! RemGlkProcess constructor";
textEdit = textEditOutput;
}
void RemGlkProcess::setOutputTextEdit(RemGlkCompleterTextEdit *textEditOutput)
{
textEdit = textEditOutput;
// outputLayout.textEdit = textEditOutput;
}
bool setupOnceA = false;
void RemGlkProcess::launchRemGlkEngine() {
storyEngineState = 1;
// assistant/remotecontrol.cpp
if (remGlkDebug_process_steps) {
textEdit->append("\n:::: creating QProcess object");
}
// Beyond the very first launch since app start, skip some steps.
if (!setupOnceA) {
setupOnceA = true;
dataTempStream.setCodec("UTF-8");
dataTempStream.setString(&dataTempBuffer,QIODevice::ReadWrite | QIODevice::Text);
}
process = new QProcess(this);
connect(process, SIGNAL(finished(int,QProcess::ExitStatus)),
this, SLOT(engineProcessClosed()));
connect(process, SIGNAL(readyReadStandardOutput()),
this, SLOT(processStandardOutputFromRemGlk()));
connect(process, SIGNAL(stateChanged(QProcess::ProcessState)),
this, SLOT(processStateChange(const QProcess::ProcessState)));
if (process->state() != QProcess::Running) {
launchRemGlkEngineGo();
} else {
qDebug() << "CONFUSED QProcess state " << process->state();
textEdit->append("\n<b>:::: CONFUSED process state in launchRemGlkEngine.</b>");
}
}
void RemGlkProcess::resetForNewLaunch()
{
engineInputMode = -1;
engineInputCharGen = -1;
engineInputLineGen = -1;
engineInputLineMaxLen = -1;
engineInputLineWindowId = -1;
engineInputCharWindowId = -1;
processCycle = 0;
incomingRemGlkStanzaCount = 0;
rawIncomingPileJSON = "";
rawIncomingPileAdditions = 0;
updateGeneration = -1;
stanzaIncomingFlag = false;
engineInputLineGenPreviousSent = -1;
engineInputCharGenPreviousSent = -1;
}
void RemGlkProcess::stopRunningEngine()
{
if (storyEngineState > 0) {
textEdit->append("\n::::: stopping running engine.");
process->close();
process->kill();
resetForNewLaunch();
textEdit->append(QString(" ::::: ready for new launch. storyEngineState: %1\n").arg(storyEngineState));
}
storyEngineState = 0;
}
void RemGlkProcess::setDataFilePath(QString dataFilePath)
{
this->dataFilePath = dataFilePath;
}
void RemGlkProcess::setRemGlkEngine(int engineCode)
{
switch (enginePickA) {
case 0:
appRemGlkBinaryPath = "./remglk_glulxe";
break;
case 1:
// if inside this APK use:
appRemGlkBinaryPath = "../lib/lib_app_git.so";
// if inside the Thunderfall APK, use this (or modify to be -2 APK variation)
// NOTE: this needs to distinguish x86 vs arm CPU, currently does not
appRemGlkBinaryPath = "/data/app/com.wakereality.thunderfall-1/lib/x86/lib_app_git.so";
break;
case 2:
// if inside this APK use:
appRemGlkBinaryPath = "../lib/lib_app_glulxe.so";
// if inside the Thunderfall APK, use this (or modify to be -2 APK variation)
// NOTE: this needs to distinguish x86 vs arm CPU, currently does not
appRemGlkBinaryPath = "/data/app/com.wakereality.thunderfall-1/lib/x86/lib_app_glulxe.so";
break;
}
}
/*
* Start the child RemGlk executable process
* */
void RemGlkProcess::launchRemGlkEngineGo()
{
if (remGlkDebug_process_steps) {
textEdit->append("\r\n:::: QProcess launchButton_clicked");
}
if (process->state() == QProcess::Running)
return;
QStringList args;
args << dataFilePath;
if (! QFileInfo(appRemGlkBinaryPath).exists()) {
qDebug() << "Process target binary does not exist. appBinary path: " << appRemGlkBinaryPath;
QMessageBox::critical(textEdit, tr("Thunderquake Engine Launch"), tr("Could not locate IF Interpreter engine at path: %1 pwd: %2").arg(appRemGlkBinaryPath).arg(QDir::currentPath()));
textEdit->append(QString("\nERROR PEL-A00. PROBLEM: Thunderquake Engine Launch. Could not locate IF Interpreter engine at path: %1 pwd: %2").arg(appRemGlkBinaryPath).arg(QDir::currentPath()));
return;
}
if (! QFileInfo(dataFilePath).exists()) {
qDebug() << "IF story data file path does not exist: " << dataFilePath;
QMessageBox::critical(textEdit, tr("Thunderquake Engine Launch"), tr("Could not locate IF story data file path: %1").arg(dataFilePath));
textEdit->append(QString("\nERROR PEL-A10. PROBLEM: Thunderquake Engine Launch. Could not locate IF story data file at path: %1").arg(dataFilePath));
return;
}
process->start(appRemGlkBinaryPath, args);
if (remGlkDebug_process_steps) {
textEdit->append("\n:::: QProcess start issued");
}
if (!process->waitForStarted()) {
qDebug() << "Process start failed, show MessageBox to user";
QMessageBox::critical(textEdit, tr("Thunderquake Engine Launch"), tr("Could not start RemGlk engine executable %1.").arg(appRemGlkBinaryPath));
textEdit->append(QString("\nERROR PEL-A20. PROBLEM: Thunderquake Engine Launch. Could not start RemGlk engine executable %1.").arg(appRemGlkBinaryPath));
return;
}
if (remGlkDebug_process_steps) {
textEdit->append("\n:::: QProcess started");
}
// RemGlk Initialize the screen layout and features support.
// ToDo: create a class to hold screen choice parameters. Create user interface to allow fixed-size selection. Measure dynamic size of window and offer as an option.
sendCommand(QLatin1String("{ \"type\": \"init\", \"gen\": 0, \"metrics\": { \"width\":720, \"height\":400, \"charwidth\":9, \"charheight\":16 }, \"support\": [ \"timer\", \"hyperlinks\", \"graphics\", \"graphicswin\" ] }"));
}
void RemGlkProcess::sendCommand(const QString &cmd)
{
if (process->state() != QProcess::Running)
return;
qDebug() << "[DataToEngine] " << cmd;
process->write(cmd.toLocal8Bit() + '\n');
if (remGlkDebug_process_steps) {
textEdit->append("\n:::: command sent");
}
}
void RemGlkProcess::storyRespondTimer() {
sendCommand(QString("{\"type\":\"timer\", \"gen\":%1}").arg(updateGeneration));
}
void RemGlkProcess::storyRespond(const QString storyInput) {
storyRespond_returnPressed(storyInput);
}
void RemGlkProcess::storyRespond_returnPressed(const QString storyInput) {
if (!stanzaIncomingFlag) {
// Holding [enter] key down on the input combo box results in generation errors after 80 to 200. This logic improves that problem.
// It seemingly eliminate the problem - 5000 RemGlk generations were tested as fast as possible.
if (engineInputLineGen > engineInputLineGenPreviousSent) {
int engineInputLineGenCopy = engineInputLineGen;
engineInputLineGenPreviousSent = engineInputLineGenCopy;
sendCommand(QString("{\"type\":\"line\", \"gen\":%2, \"window\":%1, \"value\":\"%3\"}").arg(engineInputLineWindowId).arg(engineInputLineGenCopy).arg(storyInput));
} else {
qDebug() << "input ignored, stanzaIncomingFlag is false, however, generation is the same. " << engineInputLineGen << " line " << engineInputLineGenPreviousSent;
}
} else {
qDebug() << "input ignored, stanzaIncomingFlag is true";
}
}
void RemGlkProcess::storyRespondChar(const QString storyInput) {
if (!stanzaIncomingFlag) {
// Holding [enter] key down on the input combo box results in generation errors after 80 to 200. This logic improves that problem.
// It seemingly eliminate the problem - 5000 RemGlk generations were tested as fast as possible.
if (engineInputCharGen > engineInputCharGenPreviousSent) {
int engineInputCharGenCopy = engineInputCharGen;
engineInputCharGenPreviousSent = engineInputCharGenCopy;
// sendCommand(QString("{\"type\":\"char\", \"gen\":%2, \"window\":%1, \"value\":\"%3\"}").arg(engineInputCharWindowId).arg(engineInputCharGenCopy).arg(storyInput));
sendCommand(QString("{\"type\":\"char\", \"gen\":%2, \"window\":%1, \"value\":\"%3\"}").arg(engineInputCharWindowId).arg(updateGeneration).arg(storyInput));
} else {
qDebug() << "input ignored, stanzaIncomingFlag is false, however, generation is the same. " << engineInputCharGen << " char " << engineInputCharGenPreviousSent;
}
} else {
qDebug() << "input ignored, stanzaIncomingFlag is true";
}
}
void RemGlkProcess::processStateChange(const QProcess::ProcessState newState)
{
qDebug() << "processStateChange" << newState;
if (remGlkDebug_process_steps) {
textEdit->append("\n:::: QProcess processStateChange");
}
if (newState == QProcess::NotRunning)
{
// ToDo: some variable to track if disconnected from the remote RemGlk service. Imaginge playing on a cloud/ssh/telnet session.
// setOnline(false);
}
}
void RemGlkProcess::engineProcessClosed()
{
textEdit->append(QString("\n:::: QProcess <b>closed</b> %1 %2").arg(QString::number(process->exitCode())).arg(QString::number(process->exitStatus())));
storyEngineState = 4;
}
// ##################################################################################
// ## RemGlk JSON stanza processing
// ##################################################################################
QString RemGlkProcess::toStringJsonObject(const QJsonObject jsonObj) {
QJsonDocument doc(jsonObj);
QString strJson(doc.toJson(QJsonDocument::Compact));
return strJson;
}
void RemGlkProcess::inlineOutputRemGlkDebug(int format, QString debugOutput) {
if (remGlkDebug_JSON_parsing) {
switch (format) {
case 0:
textEdit->insertPlainText(debugOutput);
break;
case 1:
textEdit->insertHtml(debugOutput);
break;
}
}
}
void RemGlkProcess::processRemGlkStanza(const QJsonObject jsonObject) {
stanzaIncomingFlag = true;
if (storyEngineState == 1) {
storyEngineState = 2;
// Keep the initial document or clear it?
switch (1) {
case 0:
textEdit->moveCursor(QTextCursor::Down);
textEdit->moveCursor(QTextCursor::End);
break;
case 1:
// This is first stanza of a newly laoded story.
textEdit->clear();
break;
}
}
inlineOutputRemGlkDebug(0, "--::---------- BEGIN\n");
if (jsonObject.contains("type")) {
QString stanzaType = jsonObject["type"].toString();
if (remGlkDebug_JSON_parsing) {
textEdit->insertPlainText("type ");
textEdit->insertPlainText(stanzaType);
}
if (stanzaType == "update") {
if (jsonObject.contains("gen")) {
updateGeneration = jsonObject["gen"].toVariant().toLongLong();
if (remGlkDebug_JSON_parsing) {
textEdit->insertPlainText(" gen ");
textEdit->insertPlainText(QString::number(updateGeneration));
}
}
if (jsonObject.contains("windows")) {
outputLayout.stanzaRemGlkWindows(jsonObject["windows"].toArray());
}
if (jsonObject.contains("content")) {
outputLayout.stanzaRemGlkContent(jsonObject["content"].toArray());
}
if (jsonObject.contains("input")) {
stanzaRemGlkInput(jsonObject["input"].toArray());
}
if (jsonObject.contains("timer")) {
stanzaRemGlkTimer(jsonObject["timer"]);
}
} else {
textEdit->insertHtml(" <b>::: NOT UPDATE :::</b> ");
textEdit->insertPlainText(toStringJsonObject(jsonObject));
}
}
inlineOutputRemGlkDebug(0, "\n--::---------- END\n");
// Position cursor to end of newly inserted text.
textEdit->moveCursor(QTextCursor::End);
stanzaIncomingFlag = false;
}
void RemGlkProcess::stanzaRemGlkInput(QJsonArray input) {
engineInputMode = -1;
int inputLineDetected = 0;
int inputCharDetected = 0;
int inputHyperLinkDetected = 0;
if (! input.isEmpty()) {
inlineOutputRemGlkDebug(1, " <font color=\"DeepPink\">input</font> [");
for(int i=0; i< input.count(); ++i){
if (input.at(i).isObject()) {
inlineOutputRemGlkDebug(1, " <font color=\"Brown\">!!</font> ");
QJsonObject inputElement = input.at(i).toObject();
if (inputElement.contains("type")) {
if (inputElement["type"] == "line") {
inputLineDetected++;
engineInputLineGen = inputElement["gen"].toInt();
engineInputLineMaxLen = inputElement["maxlen"].toInt();
engineInputLineWindowId = inputElement["id"].toInt();
} else if (inputElement["type"] == "char") {
inputCharDetected++;
engineInputCharGen = inputElement["gen"].toInt();
engineInputCharWindowId = inputElement["id"].toInt();
}
}
if (inputElement.contains("hyperlink")) {
inputHyperLinkDetected++;
}
inlineOutputRemGlkDebug(0, toStringJsonObject(inputElement));
qDebug() << "input " << toStringJsonObject(inputElement);
}
}
inlineOutputRemGlkDebug(0, "] ");
}
int newInputMode = 0;
// multiple windows may have line input detected, we only need to know if one or more.
if (inputLineDetected > 0)
newInputMode += 1;
if (inputCharDetected > 0)
newInputMode += 2;
if (inputHyperLinkDetected > 0)
newInputMode += 4;
if (newInputMode > 0) {
engineInputMode = newInputMode;
}
textEdit->setInputMode(engineInputMode);
}
/*
* ToDo: everything to do with the Glk Timer. This is incomplete.
* Glk only has a single timer. Suggest creating a single thread and using sleep. However, keep in mind that
* any char or line input activity from the player cancels the timer cycle. Only one RemGlk response per generation
* be it a hyperlink click, line input, char input, or timer tock.
* */
void RemGlkProcess::stanzaRemGlkTimer(QJsonValue timer) {
qDebug() << "[DataFromEngine][GlkTimer] :::::::::--- GLK Timer incoming value " << timer;
// Glk timer value is tricky, review the GlkOte code comments - they are verbose and explinatiory ;)
// is it null?
if (timer == QJsonValue::Null) {
qDebug() << "[DataFromEngine][GlkTimer] :::::::::--- GLK Timer value null";
} else {
long timerValue = timer.toInt(-1);
if (timerValue >= 0 ) {
qDebug() << "[DataFromEngine][GlkTimer] :::::::::--- GLK Timer value int " << timerValue << " engineInputMode " << engineInputMode;
// Glulx story Six opening has a 500ms timer delay with no other input. Test with virgin run, delete data files sixdata0/sixdata1.
if (engineInputMode == -1) {
storyRespondTimer();
}
} else {
qDebug() << "[DataFromEngine][GlkTimer] :::::::::--- GLK Timer value confused " << timer;
}
}
}
void RemGlkProcess::processStandardOutputFromRemGlk()
{
if (remGlkDebug_process_steps) {
textEdit->append("\n:::: QProcess processStandardOutput ");
}
int onLine = 0;
QString line;
QString assembledText = "";
while (process->canReadLine())
{
dataTempStream << process->readLine();
if (dataTempStream.readLineInto(&line))
{
onLine++;
rawIncomingPileJSON.append(line);
rawIncomingPileJSON.append("\n");
rawIncomingPileAdditions++;
if (line == "") {
// End of RemGlk stanza, blank line
assembledText = rawIncomingPileJSON;
// WARNING: "This is extremely inefficient. When you assign readAll() to a QString you're causing a byte array to be converted from utf8 bytes to a utf16 QString. And then when you call fromJson you're causing the utf16 string to be converted back into a new utf8 byte array. Change strReply to a QByteArray and you avoid all this pointless conversion."
QJsonDocument jsonResponse = QJsonDocument::fromJson(assembledText.toUtf8());
if(jsonResponse.isObject()){
QJsonObject jsonObject = jsonResponse.object();
QString strJson(jsonResponse.toJson(QJsonDocument::Compact));
if (remGlkDebug_JSON_in_raw) {
qDebug() << "[DataFromEngine] incoming JSON: " << strJson;
assembledText = "got JSON~~~ ";
assembledText.append(strJson);
assembledText.append(" ~~~");
textEdit->append(assembledText);
}
incomingRemGlkStanzaCount++;
processRemGlkStanza(jsonObject);
} else {
qDebug() << "ERROR: not JSONObject";
textEdit->append("\n::: JSON failure spot A");
}
/*
* LOGIC CONCERN WARNING
* What if two stanzas are together in one stdin batch, waiting to read. This loop
* will keep reading until there is no more stdin input - even if we found a blank line ending stanza.
* */
// clear pile for start of next RemGlk stanza
rawIncomingPileJSON = "";
rawIncomingPileAdditions = 0;
}
}
}
processCycle++;
if (remGlkDebug_process_steps) {
textEdit->append("\n:::: QProcess processStandardOutput loop finished,");
textEdit->append(QString::number(onLine));
}
/*
// autoplay Story: Bronze. This has hard-coded response to the first couple turns of the story Bronze.
switch (updateGeneration) {
case 1:
sendCommand(QString("{\"type\":\"line\", \"gen\":1, \"window\":%1, \"value\":\"yes\"}").arg(engineInputLineWindowId));
break;
case 2:
sendCommand(QString("{\"type\":\"line\", \"gen\":%2, \"window\":%1, \"value\":\"north\"}").arg(engineInputLineWindowId).arg(engineInputLineGen));
break;
}
*/
}