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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program 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 2
* of the License, or (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "backends/networking/curl/connectionmanager.h"
#include "scumm/he/intern_he.h"
#include "scumm/he/moonbase/moonbase.h"
#include "scumm/he/moonbase/net_main.h"
#include "scumm/he/moonbase/net_defines.h"
namespace Scumm {
Net::Net(ScummEngine_v100he *vm) : _latencyTime(1), _fakeLatency(false), _vm(vm) {
//some defaults for fields
_packbuffer = (byte *)malloc(MAX_PACKET_SIZE + DATA_HEADER_SIZE);
_tmpbuffer = (byte *)malloc(MAX_PACKET_SIZE);
_myUserId = -1;
_myPlayerKey = -1;
_lastResult = 0;
_sessionsBeingQueried = false;
_sessionid = -1;
_sessions = nullptr;
_packetdata = nullptr;
_serverprefix = "http://localhost/moonbase";
}
Net::~Net() {
free(_tmpbuffer);
free(_packbuffer);
delete _sessions;
delete _packetdata;
}
int Net::hostGame(char *sessionName, char *userName) {
if (createSession(sessionName)) {
if (addUser(userName, userName)) {
return 1;
} else {
_vm->displayMessage(0, "Error Adding User \"%s\" to Session \"%s\"", userName, sessionName);
endSession();
closeProvider();
}
} else {
_vm->displayMessage(0, "Error creating session \"%s\"", userName );
closeProvider();
}
return 0;
}
int Net::joinGame(char *IP, char *userName) {
warning("STUB: Net::joinGame(\"%s\", \"%s\")", IP, userName); // PN_JoinTCPIPGame
return 0;
}
int Net::addUser(char *shortName, char *longName) {
debug(1, "Net::addUser(\"%s\", \"%s\")", shortName, longName); // PN_AddUser
Networking::PostRequest rq(_serverprefix + "/adduser",
new Common::Callback<Net, Common::JSONValue *>(this, &Net::addUserCallback),
new Common::Callback<Net, Networking::ErrorResponse>(this, &Net::addUserErrorCallback));
char *buf = (char *)malloc(MAX_PACKET_SIZE);
snprintf(buf, MAX_PACKET_SIZE, "{\"shortname\":\"%s\",\"longname\":\"%s\",\"sessionid\":%d}", shortName, longName, _sessionid);
rq.setPostData((byte *)buf, strlen(buf));
rq.setContentType("application/json");
rq.start();
_myUserId = -1;
while(rq.state() == Networking::PROCESSING) {
g_system->delayMillis(5);
}
if (_myUserId == -1)
return 0;
return 1;
}
void Net::addUserCallback(Common::JSONValue *response) {
Common::JSONObject info = response->asObject();
if (info.contains("userid")) {
_myUserId = info["userid"]->asIntegerNumber();
_myPlayerKey = info["playerkey"]->asIntegerNumber();
}
debug(1, "addUserCallback: got: '%s' as %d", response->stringify().c_str(), _myUserId);
}
void Net::addUserErrorCallback(Networking::ErrorResponse error) {
warning("Error in addUser(): %ld %s", error.httpResponseCode, error.response.c_str());
}
int Net::removeUser() {
debug(1, "Net::removeUser()"); // PN_RemoveUser
if (_myUserId != -1)
destroyPlayer(_myUserId);
return 1;
}
int Net::whoSentThis() {
debug(1, "Net::whoSentThis()"); // PN_WhoSentThis
return _packetdata->child("from")->asIntegerNumber();
}
int Net::whoAmI() {
debug(1, "Net::whoAmI()"); // PN_WhoAmI
return _myUserId;
}
int Net::createSession(char *name) {
debug(1, "Net::createSession(\"%s\")", name); // PN_CreateSession
Networking::PostRequest rq(_serverprefix + "/createsession",
new Common::Callback<Net, Common::JSONValue *>(this, &Net::createSessionCallback),
new Common::Callback<Net, Networking::ErrorResponse>(this, &Net::createSessionErrorCallback));
char *buf = (char *)malloc(MAX_PACKET_SIZE);
snprintf(buf, MAX_PACKET_SIZE, "{\"name\":\"%s\"}", name);
rq.setPostData((byte *)buf, strlen(buf));
rq.setContentType("application/json");
rq.start();
_sessionid = -1;
while(rq.state() == Networking::PROCESSING) {
g_system->delayMillis(5);
}
if (_sessionid == -1)
return 0;
return 1;
}
void Net::createSessionCallback(Common::JSONValue *response) {
Common::JSONObject info = response->asObject();
if (info.contains("sessionid")) {
_sessionid = info["sessionid"]->asIntegerNumber();
}
debug(1, "createSessionCallback: got: '%s' as %d", response->stringify().c_str(), _sessionid);
}
void Net::createSessionErrorCallback(Networking::ErrorResponse error) {
warning("Error in createSession(): %ld %s", error.httpResponseCode, error.response.c_str());
}
int Net::joinSession(int sessionIndex) {
debug(1, "Net::joinSession(%d)", sessionIndex); // PN_JoinSession
if (!_sessions) {
warning("Net::joinSession(): no sessions");
return 0;
}
if (sessionIndex >= (int)_sessions->countChildren()) {
warning("Net::joinSession(): session number too big: %d >= %lu", sessionIndex, _sessions->countChildren());
return 0;
}
if (!_sessions->child(sessionIndex)->hasChild("sessionid")) {
warning("Net::joinSession(): no sessionid in session");
return 0;
}
_sessionid = _sessions->child(sessionIndex)->child("sessionid")->asIntegerNumber();
return 1;
}
int Net::endSession() {
debug(1, "Net::endSession()"); // PN_EndSession
Networking::PostRequest rq(_serverprefix + "/endsession",
new Common::Callback<Net, Common::JSONValue *>(this, &Net::endSessionCallback),
new Common::Callback<Net, Networking::ErrorResponse>(this, &Net::endSessionErrorCallback));
char *buf = (char *)malloc(MAX_PACKET_SIZE);
snprintf(buf, MAX_PACKET_SIZE, "{\"sessionid\":%d, \"userid\":%d}", _sessionid, _myUserId);
rq.setPostData((byte *)buf, strlen(buf));
rq.setContentType("application/json");
rq.start();
while(rq.state() == Networking::PROCESSING) {
g_system->delayMillis(5);
}
return _lastResult;
}
void Net::endSessionCallback(Common::JSONValue *response) {
_lastResult = 1;
}
void Net::endSessionErrorCallback(Networking::ErrorResponse error) {
warning("Error in endSession(): %ld %s", error.httpResponseCode, error.response.c_str());
_lastResult = 0;
}
void Net::disableSessionJoining() {
debug(1, "Net::disableSessionJoining()"); // PN_DisableSessionPlayerJoin
Networking::PostRequest *rq = new Networking::PostRequest(_serverprefix + "/disablesession",
nullptr,
new Common::Callback<Net, Networking::ErrorResponse>(this, &Net::disableSessionJoiningErrorCallback));
char *buf = (char *)malloc(MAX_PACKET_SIZE);
snprintf(buf, MAX_PACKET_SIZE, "{\"sessionid\":%d}", _sessionid);
rq->setPostData((byte *)buf, strlen(buf));
rq->setContentType("application/json");
rq->start();
ConnMan.addRequest(rq);
}
void Net::disableSessionJoiningErrorCallback(Networking::ErrorResponse error) {
warning("Error in disableSessionJoining(): %ld %s", error.httpResponseCode, error.response.c_str());
}
void Net::enableSessionJoining() {
warning("STUB: Net::enableSessionJoining()"); // PN_EnableSessionPlayerJoin
}
void Net::setBotsCount(int botsCount) {
warning("STUB: Net::setBotsCount(%d)", botsCount); // PN_SetAIPlayerCountKludge
}
int32 Net::setProviderByName(int32 parameter1, int32 parameter2) {
char name[MAX_PROVIDER_NAME];
char ipaddress[MAX_IP_SIZE];
ipaddress[0] = '\0';
_vm->getStringFromArray(parameter1, name, sizeof(name));
if (parameter2)
_vm->getStringFromArray(parameter2, ipaddress, sizeof(ipaddress));
debug(1, "Net::setProviderByName(\"%s\", \"%s\")", name, ipaddress); // PN_SetProviderByName
// Emulate that we found a TCP/IP provider
return 1;
}
void Net::setFakeLatency(int time) {
_latencyTime = time;
debug("NETWORK: Setting Fake Latency to %d ms", _latencyTime);
_fakeLatency = true;
}
bool Net::destroyPlayer(int32 playerDPID) {
// bool PNETWIN_destroyplayer(DPID idPlayer)
debug(1, "Net::destroyPlayer(%d)", playerDPID);
Networking::PostRequest *rq = new Networking::PostRequest(_serverprefix + "/removeuser",
nullptr,
new Common::Callback<Net, Networking::ErrorResponse>(this, &Net::destroyPlayerErrorCallback));
char *buf = (char *)malloc(MAX_PACKET_SIZE);
snprintf(buf, MAX_PACKET_SIZE, "{\"sessionid\":%d, \"userid\":%d}", _sessionid, playerDPID);
rq->setPostData((byte *)buf, strlen(buf));
rq->setContentType("application/json");
rq->start();
ConnMan.addRequest(rq);
return true;
}
void Net::destroyPlayerErrorCallback(Networking::ErrorResponse error) {
warning("Error in destroyPlayer(): %ld %s", error.httpResponseCode, error.response.c_str());
}
int32 Net::startQuerySessions() {
if (!_sessionsBeingQueried) { // Do not run parallel queries
debug(1, "Net::startQuerySessions()"); // StartQuerySessions
Networking::PostRequest *rq = new Networking::PostRequest(_serverprefix + "/lobbies",
new Common::Callback<Net, Common::JSONValue *>(this, &Net::startQuerySessionsCallback),
new Common::Callback<Net, Networking::ErrorResponse>(this, &Net::startQuerySessionsErrorCallback));
_sessionsBeingQueried = true;
rq->start();
ConnMan.addRequest(rq);
}
if (!_sessions)
return 0;
debug(1, "Net::startQuerySessions(): got %lu", _sessions->countChildren());
return _sessions->countChildren();
}
void Net::startQuerySessionsCallback(Common::JSONValue *response) {
debug(1, "startQuerySessions: Got: '%s' which is %lu", response->stringify().c_str(), response->countChildren());
_sessionsBeingQueried = false;
delete _sessions;
_sessions = new Common::JSONValue(*response);
}
void Net::startQuerySessionsErrorCallback(Networking::ErrorResponse error) {
warning("Error in startQuerySessions(): %ld %s", error.httpResponseCode, error.response.c_str());
_sessionsBeingQueried = false;
}
int32 Net::updateQuerySessions() {
debug(1, "Net::updateQuerySessions()"); // UpdateQuerySessions
return startQuerySessions();
}
void Net::stopQuerySessions() {
debug(1, "Net::stopQuerySessions()"); // StopQuerySessions
_sessionsBeingQueried = false;
// No op
}
int Net::querySessions() {
warning("STUB: Net::querySessions()"); // PN_QuerySessions
return 0;
}
int Net::queryProviders() {
debug(1, "Net::queryProviders()"); // PN_QueryProviders
// Emulate that we have 1 provider, TCP/IP
return 1;
}
int Net::setProvider(int providerIndex) {
warning("STUB: Net::setProvider(%d)", providerIndex); // PN_SetProvider
return 0;
}
int Net::closeProvider() {
debug(1, "Net::closeProvider()"); // PN_CloseProvider
return 1;
}
bool Net::initAll() {
warning("STUB: Net::initAll()"); // PN_DoInitAll
return false;
}
bool Net::initProvider() {
warning("STUB: Net::initProvider()"); // PN_DoInitProvider
return false;
}
bool Net::initSession() {
warning("STUB: Net::initSession()"); // PN_DoInitSession
return false;
}
bool Net::initUser() {
warning("STUB: Net::initUser()"); // PN_DoInitUser
return false;
}
void Net::remoteStartScript(int typeOfSend, int sendTypeParam, int priority, int argsCount, int32 *args) {
Common::String res = "\"params\": [";
if (argsCount > 2)
for (int i = 0; i < argsCount - 1; i++)
res += Common::String::format("%d, ", args[i]);
if (argsCount > 1)
res += Common::String::format("%d]", args[argsCount - 1]);
else
res += "]";
debug(1, "Net::remoteStartScript(%d, %d, %d, %d, ...)", typeOfSend, sendTypeParam, priority, argsCount); // PN_RemoteStartScriptCommand
remoteSendData(typeOfSend, sendTypeParam, PACKETTYPE_REMOTESTARTSCRIPT, res);
}
int Net::remoteSendData(int typeOfSend, int sendTypeParam, int type, Common::String data, int defaultRes, bool wait, int callid) {
// Since I am lazy, instead of constructing the JSON object manually
// I'd rather parse it
Common::String res = Common::String::format(
"{\"sessionid\":%d, \"from\":%d, \"to\":%d, \"toparam\": %d, "
"\"type\":%d, \"timestamp\": %d, \"size\": 1, \"data\": { %s } }", _sessionid, _myUserId,
typeOfSend, sendTypeParam, type, g_system->getMillis(), data.c_str());
byte *buf = (byte *)malloc(res.size() + 1);
strncpy((char *)buf, res.c_str(), res.size());
debug("Package to send: %s", res.c_str());
Networking::PostRequest *rq = new Networking::PostRequest(_serverprefix + "/packet",
nullptr,
new Common::Callback<Net, Networking::ErrorResponse>(this, &Net::remoteSendDataErrorCallback));
rq->setPostData(buf, res.size());
rq->setContentType("application/json");
rq->start();
ConnMan.addRequest(rq);
if (!wait)
return 0;
uint32 timeout = g_system->getMillis() + 1000;
while (g_system->getMillis() < timeout) {
if (remoteReceiveData()) {
if (_packetdata->child("data")->hasChild("callid")) {
if (_packetdata->child("data")->child("callid")->asIntegerNumber() == callid) {
return _packetdata->child("data")->child("result")->asIntegerNumber();
}
}
warning("Net::remoteSendData(): Received wrong package: %s", _packetdata->stringify().c_str());
}
_vm->parseEvents();
}
if (!_sessions)
return 0;
return defaultRes;
}
void Net::remoteSendDataErrorCallback(Networking::ErrorResponse error) {
warning("Error in remoteSendData(): %ld %s", error.httpResponseCode, error.response.c_str());
}
void Net::remoteSendArray(int typeOfSend, int sendTypeParam, int priority, int arrayIndex) {
debug(1, "Net::remoteSendArray(%d, %d, %d, %d)", typeOfSend, sendTypeParam, priority, arrayIndex & ~0x33539000); // PN_RemoteSendArrayCommand
ScummEngine_v100he::ArrayHeader *ah = (ScummEngine_v100he::ArrayHeader *)_vm->getResourceAddress(rtString, arrayIndex & ~0x33539000);
Common::String jsonData = Common::String::format(
"\"type\":%d, \"dim1start\":%d, \"dim1end\":%d, \"dim2start\":%d, \"dim2end\":%d, \"data\": [",
ah->type, ah->dim1start, ah->dim1end, ah->dim2start, ah->dim2end);
int32 size = (FROM_LE_32(ah->dim1end) - FROM_LE_32(ah->dim1start) + 1) *
(FROM_LE_32(ah->dim2end) - FROM_LE_32(ah->dim2start) + 1);
for (int i = 0; i < size; i++) {
int32 data;
switch (FROM_LE_32(ah->type)) {
case ScummEngine_v100he::kByteArray:
case ScummEngine_v100he::kStringArray:
data = ah->data[i];
break;
case ScummEngine_v100he::kIntArray:
data = (int16)READ_LE_UINT16(ah->data + i * 2);
break;
case ScummEngine_v100he::kDwordArray:
data = (int32)READ_LE_UINT32(ah->data + i * 4);
break;
default:
error("Net::remoteSendArray(): Unknown array type %d for array %d", FROM_LE_32(ah->type), arrayIndex);
}
jsonData += Common::String::format("%d", data);
if (i < size - 1)
jsonData += ", ";
else
jsonData += "]";
}
remoteSendData(typeOfSend, sendTypeParam, PACKETTYPE_REMOTESENDSCUMMARRAY, jsonData);
}
int Net::remoteStartScriptFunction(int typeOfSend, int sendTypeParam, int priority, int defaultReturnValue, int argsCount, int32 *args) {
int callid = _vm->_rnd.getRandomNumber(1000000);
Common::String res = Common::String::format("\"callid\":%d, \"params\": [", callid);
if (argsCount > 2)
for (int i = 0; i < argsCount - 1; i++)
res += Common::String::format("%d, ", args[i]);
if (argsCount > 1)
res += Common::String::format("%d]", args[argsCount - 1]);
else
res += "]";
debug(1, "Net::remoteStartScriptFunction(%d, %d, %d, %d, %d, ...)", typeOfSend, sendTypeParam, priority, defaultReturnValue, argsCount); // PN_RemoteStartScriptFunction
return remoteSendData(typeOfSend, sendTypeParam, PACKETTYPE_REMOTESTARTSCRIPTRETURN, res, defaultReturnValue, true, callid);
}
bool Net::getHostName(char *hostname, int length) {
warning("STUB: Net::getHostName(\"%s\", %d)", hostname, length); // PN_GetHostName
return false;
}
bool Net::getIPfromName(char *ip, int ipLength, char *nameBuffer) {
warning("STUB: Net::getIPfromName(\"%s\", %d, \"%s\")", ip, ipLength, nameBuffer); // PN_GetIPfromName
return false;
}
void Net::getSessionName(int sessionNumber, char *buffer, int length) {
debug(1, "Net::getSessionName(%d, ..., %d)", sessionNumber, length); // PN_GetSessionName
if (!_sessions) {
*buffer = '\0';
warning("Net::getSessionName(): no sessions");
return;
}
if (sessionNumber >= (int)_sessions->countChildren()) {
*buffer = '\0';
warning("Net::getSessionName(): session number too big: %d >= %lu", sessionNumber, _sessions->countChildren());
return;
}
Common::strlcpy(buffer, _sessions->child(sessionNumber)->child("name")->asString().c_str(), length);
}
int Net::getSessionPlayerCount(int sessionNumber) {
debug(1, "Net::getSessionPlayerCount(%d)", sessionNumber); // case GET_SESSION_PLAYER_COUNT_KLUDGE:
if (!_sessions) {
warning("Net::getSessionPlayerCount(): no sessions");
return 0;
}
if (sessionNumber >= (int)_sessions->countChildren()) {
warning("Net::getSessionPlayerCount(): session number too big: %d >= %lu", sessionNumber, _sessions->countChildren());
return 0;
}
if (!_sessions->child(sessionNumber)->hasChild("players")) {
warning("Net::getSessionPlayerCount(): no players in session");
return 0;
}
return _sessions->child(sessionNumber)->child("players")->countChildren();
}
void Net::getProviderName(int providerIndex, char *buffer, int length) {
warning("STUB: Net::getProviderName(%d, \"%s\", %d)", providerIndex, buffer, length); // PN_GetProviderName
}
bool Net::remoteReceiveData() {
Networking::PostRequest rq(_serverprefix + "/getpacket",
new Common::Callback<Net, Common::JSONValue *>(this, &Net::remoteReceiveDataCallback),
new Common::Callback<Net, Networking::ErrorResponse>(this, &Net::remoteReceiveDataErrorCallback));
char *buf = (char *)malloc(MAX_PACKET_SIZE);
snprintf(buf, MAX_PACKET_SIZE, "{\"sessionid\":%d, \"playerid\":%d}", _sessionid, _myUserId);
rq.setPostData((byte *)buf, strlen(buf));
rq.setContentType("application/json");
delete _packetdata;
_packetdata = nullptr;
rq.start();
while(rq.state() == Networking::PROCESSING) {
g_system->delayMillis(5);
}
if (!_packetdata || _packetdata->child("size")->asIntegerNumber() == 0)
return false;
uint from = _packetdata->child("from")->asIntegerNumber();
uint type = _packetdata->child("type")->asIntegerNumber();
uint32 *params;
switch (type) {
case PACKETTYPE_REMOTESTARTSCRIPT:
{
int datalen = _packetdata->child("data")->child("params")->asArray().size();
params = (uint32 *)_tmpbuffer;
for (int i = 0; i < datalen; i++) {
*params = _packetdata->child("data")->child("params")->asArray()[i]->asIntegerNumber();
params++;
}
_vm->runScript(_vm->VAR(_vm->VAR_REMOTE_START_SCRIPT), 1, 0, (int *)_tmpbuffer);
}
break;
case PACKETTYPE_REMOTESTARTSCRIPTRETURN:
{
int datalen = _packetdata->child("data")->child("params")->asArray().size();
params = (uint32 *)_tmpbuffer;
for (int i = 0; i < datalen; i++) {
*params = _packetdata->child("data")->child("params")->asArray()[i]->asIntegerNumber();
params++;
}
_vm->runScript(_vm->VAR(_vm->VAR_REMOTE_START_SCRIPT), 1, 0, (int *)_tmpbuffer);
int result = _vm->pop();
Common::String res = Common::String::format("\"result\": %d, \"callid\": %d", result,
(int)_packetdata->child("data")->child("callid")->asIntegerNumber());
remoteSendData(PN_SENDTYPE_INDIVIDUAL, from, PACKETTYPE_REMOTESTARTSCRIPTRESULT, res);
}
break;
case PACKETTYPE_REMOTESTARTSCRIPTRESULT:
//
// Ignore it.
//
break;
case PACKETTYPE_REMOTESENDSCUMMARRAY:
{
int newArray = 0;
// Assume that the packet data contains a "SCUMM PACKAGE"
// and unpack it into an scumm array :-)
int dim1start = _packetdata->child("data")->child("dim1start")->asIntegerNumber();
int dim1end = _packetdata->child("data")->child("dim1end")->asIntegerNumber();
int dim2start = _packetdata->child("data")->child("dim2start")->asIntegerNumber();
int dim2end = _packetdata->child("data")->child("dim2end")->asIntegerNumber();
int atype = _packetdata->child("data")->child("type")->asIntegerNumber();
byte *data = _vm->defineArray(0, atype, dim2start, dim2end, dim1start, dim1end, true, &newArray);
int32 size = (dim1end - dim1start + 1) * (dim2end - dim2start + 1);
int32 value;
for (int i = 0; i < size; i++) {
value = _packetdata->child("data")->child("data")->asArray()[i]->asIntegerNumber();
switch (atype) {
case ScummEngine_v100he::kByteArray:
case ScummEngine_v100he::kStringArray:
data[i] = value;
break;
case ScummEngine_v100he::kIntArray:
WRITE_LE_UINT16(data + i * 2, value);
break;
case ScummEngine_v100he::kDwordArray:
WRITE_LE_UINT32(data + i * 4, value);
break;
default:
error("Net::remoteReceiveData(): Unknown array type %d", atype);
}
}
memset(_tmpbuffer, 0, 25 * 4);
WRITE_UINT32(_tmpbuffer, newArray);
// Quick start the script (1st param is the new array)
_vm->runScript(_vm->VAR(_vm->VAR_NETWORK_RECEIVE_ARRAY_SCRIPT), 1, 0, (int *)_tmpbuffer);
}
break;
default:
warning("Moonbase: Received unknown network command %d", type);
}
return true;
}
void Net::remoteReceiveDataCallback(Common::JSONValue *response) {
_packetdata = new Common::JSONValue(*response);
if (_packetdata->child("size")->asIntegerNumber() != 0)
debug(1, "remoteReceiveData: Got: '%s'", response->stringify().c_str());
}
void Net::remoteReceiveDataErrorCallback(Networking::ErrorResponse error) {
warning("Error in remoteReceiveData(): %ld %s", error.httpResponseCode, error.response.c_str());
}
void Net::doNetworkOnceAFrame(int msecs) {
if (_sessionid == -1 || _myUserId == -1)
return;
uint32 tickCount = g_system->getMillis() + msecs;
while (remoteReceiveData()) {
if (tickCount >= g_system->getMillis()) {
break;
}
}
}
} // End of namespace Scumm
|