-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommand-output.cpp
More file actions
2868 lines (2638 loc) · 103 KB
/
command-output.cpp
File metadata and controls
2868 lines (2638 loc) · 103 KB
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
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <windows.h>
#include <stdio.h>
#include "text.h"
#include "operation.h"
#include "console.h"
#include "netconfig.h"
#include "updates.h"
#include "quest.h"
#include "encryption.h"
#include "license.h"
#include "version.h"
#include "battleparamentry.h"
#include "itemraretable.h"
#include "player.h"
#include "map.h"
#include "client.h"
#include "listenthread.h"
#include "lobby.h"
#include "server.h"
#include "command-functions.h"
#include "command-output-structures.h"
extern CFGFile* config;
extern UPDATE_LIST* updatelist;
////////////////////////////////////////////////////////////////////////////////
// Sending routines
// a lock for the console, so only one client can print data blocks to it at a time
OPERATION_LOCK sendCommandConsoleLock = {0,0};
// encrypts and sends a command to a client
int SendCommandToClient(CLIENT* c,void* data)
{
if (!c || !data) return 5;
COMMAND_HEADER_PC* pc = (COMMAND_HEADER_PC*)data;
COMMAND_HEADER_DCGC* gc = (COMMAND_HEADER_DCGC*)data;
COMMAND_HEADER_BB* bb = (COMMAND_HEADER_BB*)data;
DWORD datasize,sendsize;
operation_lock(c);
switch (c->version)
{
case VERSION_GAMECUBE:
case VERSION_DC:
case VERSION_FUZZIQER:
datasize = gc->size;
if (c->encrypt) sendsize = ((datasize + 3) & ~3);
else sendsize = datasize;
break;
case VERSION_PC:
case VERSION_PATCH:
datasize = pc->size;
if (c->encrypt) sendsize = ((datasize + 3) & ~3);
else sendsize = datasize;
break;
case VERSION_BLUEBURST:
datasize = bb->size;
if (c->encrypt) sendsize = ((datasize + 7) & ~7);
else sendsize = datasize;
break;
default:
return 20;
}
// print the data if it's small enough and data display is enabled
if (CFGIsValuePresent(config,"Show_Client_Data"))
{
operation_lock(&sendCommandConsoleLock);
ConsolePrintColor("$0A> Sending to %S [version %d] [%04X bytes]:\n",c->name,c->version,datasize);
if (datasize < (unsigned)CFGGetNumber(config,"Maximum_Display_Command_Size_Send")) CRYPT_PrintData(data,datasize);
operation_unlock(&sendCommandConsoleLock);
}
// copy the command into a separate buffer
void* encbuffer = malloc(sendsize);
if (!encbuffer)
{
operation_unlock(c);
return 1;
}
memcpy(encbuffer,data,datasize);
memset((void*)((DWORD)encbuffer + datasize),0,sendsize - datasize);
// encrypt, send, and clean up
if (c->encrypt) CRYPT_CryptData(&c->cryptout,encbuffer,sendsize,true);
int rv = SendClient(c,encbuffer,sendsize);
if (!rv) c->lastsend = GetTickCount();
free(encbuffer);
operation_unlock(c);
return rv;
}
// appends a command header to a data block and sends to a client
int SendCommandToClient(CLIENT* c,int command,int flag,void* data,int size)
{
COMMAND_DCPCGC_SUBCOMMAND* dcpcgc;
COMMAND_BB_SUBCOMMAND* bb;
int rv;
switch (c->version)
{
case VERSION_GAMECUBE:
case VERSION_DC:
case VERSION_FUZZIQER:
dcpcgc = (COMMAND_DCPCGC_SUBCOMMAND*)malloc(4 + size);
if (!dcpcgc) return 5;
dcpcgc->headerdcgc.command = command;
dcpcgc->headerdcgc.flag = flag;
dcpcgc->headerdcgc.size = 4 + size;
memcpy(dcpcgc->entry,data,size);
rv = SendCommandToClient(c,dcpcgc);
free(dcpcgc);
break;
case VERSION_PC:
case VERSION_PATCH:
dcpcgc = (COMMAND_DCPCGC_SUBCOMMAND*)malloc(4 + size);
if (!dcpcgc) return 5;
dcpcgc->headerpc.command = command;
dcpcgc->headerpc.flag = flag;
dcpcgc->headerpc.size = 4 + size;
memcpy(dcpcgc->entry,data,size);
rv = SendCommandToClient(c,dcpcgc);
free(dcpcgc);
break;
case VERSION_BLUEBURST:
bb = (COMMAND_BB_SUBCOMMAND*)malloc(8 + size);
if (!bb) return 5;
bb->header.command = command;
bb->header.flag = flag;
bb->header.size = 8 + size;
memcpy(bb->entry,data,size);
rv = SendCommandToClient(c,bb);
free(bb);
break;
default:
return 20;
}
return rv;
}
// sends a command to everyone in a lobby/game, except the given client.
// if c is NULL, then everyone in the lobby/game receives the command.
int SendCommandToLobby(LOBBY* l,CLIENT* c,void** data)
{
int x,rv = 0,rv2;
if (!l || !data) return 5;
operation_lock(l);
for (x = 0; x < 12; x++)
{
if (!l->clients[x]) continue;
if (c && (l->clients[x] == c)) continue;
if (!data[l->clients[x]->version]) rv2 = 7853325;
else rv2 = SendCommandToClient(l->clients[x],data[l->clients[x]->version]);
if (rv2) rv = rv2;
}
operation_unlock(l);
return rv;
}
// sends a command to everyone on a server, except the given client.
// again, c can be NULL.
int SendCommandToServer(SERVER* s,CLIENT* c,void** data)
{
unsigned int x,rv = 0,rv2;
if (!s || !data) return 5;
operation_lock(s);
for (x = 0; x < s->numClients; x++)
{
if (c && (s->clients[x] == c)) continue;
if (!data[s->clients[x]->version]) rv2 = 7853325;
else rv2 = SendCommandToClient(s->clients[x],data[s->clients[x]->version]);
if (rv2 != 0) rv = rv2;
}
operation_unlock(s);
return rv;
}
// Similar to SendCommandToClient, it appends headers to a data block and sends the command to a lobby
// except the given client.
int SendSubcommandToLobby(LOBBY* l,CLIENT* c,int command,int flag,void* sub,int size)
{
COMMAND_DCPCGC_SUBCOMMAND* pc = (COMMAND_DCPCGC_SUBCOMMAND*)malloc(4 + size);
if (!pc) return 1;
COMMAND_DCPCGC_SUBCOMMAND* dcgc = (COMMAND_DCPCGC_SUBCOMMAND*)malloc(4 + size);
if (!dcgc)
{
free(pc);
return 1;
}
COMMAND_BB_SUBCOMMAND* bb = (COMMAND_BB_SUBCOMMAND*)malloc(8 + size);
if (!bb)
{
free(dcgc);
free(pc);
return 1;
}
pc->headerpc.size = 4 + size;
pc->headerpc.command = command;
pc->headerpc.flag = flag;
memcpy(pc->entry,sub,size);
if (!TranslateCommandPCDCGC(&pc->headerpc,&dcgc->headerdcgc) || !TranslateCommandPCBB(&pc->headerpc,&bb->header))
{
free(bb);
free(dcgc);
free(pc);
return 486786415;
}
void* data[VERSION_MAX] = {dcgc,bb,pc,dcgc,NULL,NULL};
int errors = SendCommandToLobby(l,c,data);
free(bb);
free(dcgc);
free(pc);
return errors;
}
////////////////////////////////////////////////////////////////////////////////
// command sending functions follow. in general, they're written in such a way that
// you don't need to think about anything, even the client's version, before
// calling them. for this reason, some of them are quite complex. many are split
// into several functions, one for each version of PSO, named with suffixes like
// _GC, _BB, and the like. in these cases, the function without the suffix simply
// calls the appropriate function for the client's version. thus, if you change
// something in one of the version-specific functions, you may have to change it
// in all of them.
////////////////////////////////////////////////////////////////////////////////
// CommandServerInit: this function sends the command that initializes encryption
// strings needed for various functions
char* CommandServerInit_anticopyright = "This server is in no way affiliated, sponsored, or supported by SEGA Enterprises or SONICTEAM. The preceding message exists only in order to remain compatible with programs that expect it.";
char* CommandServerInit_dcPortMap = "DreamCast Port Map. Copyright SEGA Enterprises. 1999";
char* CommandServerInit_dcLobbyServer = "DreamCast Lobby Server. Copyright SEGA Enterprises. 1999";
char* CommandServerInit_bb = "Phantasy Star Online Blue Burst Game Server. Copyright 1999-2004 SONICTEAM.";
char* CommandServerInit_patch = "Patch Server. Copyright SonicTeam, LTD. 2001";
//int CommandServerInit_DC(CLIENT* c,bool startServer)
int CommandServerInit_PC(CLIENT* c,bool startserver)
{
COMMAND_PCPATCH_ENCRYPTIONINIT* p = (COMMAND_PCPATCH_ENCRYPTIONINIT*)malloc(0x108);
if (!p) return 1;
p->header.command = 0x17;
p->header.flag = 0;
p->header.size = 0x0108;
// it's possible that PC doesn't use the DC lobby server message; we'll have to test this to find out
strcpy(p->copyright,startserver ? CommandServerInit_dcPortMap : CommandServerInit_dcLobbyServer);
p->serverkey = (rand() << 16) | rand();
p->clientkey = (rand() << 16) | rand();
strcpy(p->aftermessage,CommandServerInit_anticopyright);
CRYPT_CreateKeys(&c->cryptout,&p->serverkey,CRYPT_PC);
CRYPT_CreateKeys(&c->cryptin,&p->clientkey,CRYPT_PC);
int rv = SendCommandToClient(c,p);
free(p);
c->encrypt = true;
return rv;
}
int CommandServerInit_GC(CLIENT* c,bool startserver)
{
COMMAND_GC_ENCRYPTIONINIT* p = (COMMAND_GC_ENCRYPTIONINIT*)malloc(0x110);
p->header.command = (startserver ? 0x17 : 0x02);
p->header.flag = 0;
p->header.size = 0x0108;
strcpy(p->copyright,startserver ? CommandServerInit_dcPortMap : CommandServerInit_dcLobbyServer);
p->serverkey = (rand() << 16) | rand();
p->clientkey = (rand() << 16) | rand();
strcpy(p->aftermessage,CommandServerInit_anticopyright);
CRYPT_CreateKeys(&c->cryptout,&p->serverkey,CRYPT_GAMECUBE);
CRYPT_CreateKeys(&c->cryptin,&p->clientkey,CRYPT_GAMECUBE);
int rv = SendCommandToClient(c,p);
free(p);
c->encrypt = true;
return rv;
}
int CommandServerInit_BB(CLIENT* c,bool)
{
int x;
COMMAND_BB_ENCRYPTIONINIT* p = (COMMAND_BB_ENCRYPTIONINIT*)malloc(0x184);
if (!p) return 1;
memset(p,0,0x0184);
p->header.command = 0x03;
p->header.flag = 0;
p->header.size = 0x0184;
strcpy(p->copyright,CommandServerInit_bb);
for (x = 0; x < 0x30; x++)
{
p->serverkey[x] = rand();
p->clientkey[x] = rand();
}
strcpy(p->aftermessage,CommandServerInit_anticopyright);
CRYPT_CreateKeys(&c->cryptout,p->serverkey,CRYPT_BLUEBURST);
CRYPT_CreateKeys(&c->cryptin,p->clientkey,CRYPT_BLUEBURST);
int rv = SendCommandToClient(c,p);
free(p);
c->encrypt = true;
return rv;
}
int CommandServerInit_Patch(CLIENT* c,bool startServer)
{
COMMAND_PCPATCH_ENCRYPTIONINIT p;
p.header.command = 0x02;
p.header.flag = 0;
p.header.size = 0x4C;
strcpy(p.copyright,CommandServerInit_patch);
p.serverkey = (rand() << 16) | rand();
p.clientkey = (rand() << 16) | rand();
CRYPT_CreateKeys(&c->cryptout,&p.serverkey,CRYPT_PC);
CRYPT_CreateKeys(&c->cryptin,&p.clientkey,CRYPT_PC);
int rv = SendCommandToClient(c,&p);
c->encrypt = true;
return rv;
}
int (*CommandServerInit_Functions[])(CLIENT* c,bool startServer) = {CommandServerInit_GC,CommandServerInit_BB,CommandServerInit_PC,/*CommandServerInit_DC*/NULL,CommandServerInit_Patch,/*CommandServerInit_Fuzziqer*/NULL};
int CommandServerInit(CLIENT* c,bool startserver)
{
if (!c) return 5;
if (!CommandServerInit_Functions[c->version]) return 20;
return CommandServerInit_Functions[c->version](c,startserver);
}
// For non-BB clients, updates the client's guild card and security data
// (BB clients use CommandBBClientInit)
int CommandUpdateClientConfig(CLIENT* c)
{
if (c->version == VERSION_BLUEBURST) return 541973568;
COMMAND_DCPCGC_SETSECURITY p;
if (c->version == VERSION_PC)
{
p.headerpc.size = 0x002C;
p.headerpc.command = 0x04;
p.headerpc.flag = 0x00;
} else {
p.headerdcgc.command = 0x04;
p.headerdcgc.flag = 0x00;
p.headerdcgc.size = 0x002C;
}
p.playerTag = 0x00000100;
p.serialNumber = c->license.serialNumber;
memcpy(&p.cfg,&c->cfg,sizeof(CLIENTCONFIG));
return SendCommandToClient(c,&p);
}
//int CommandApproveLicense(CLIENT* c)
//int CommandApproveRegisteredLicense(CLIENT* c)
//int CommandGetCharacterInfo(SERVER* s,CLIENT* c)
////////////////////////////////////////////////////////////////////////////////
// CommandReconnect: sends the command that tells PSO to reconnect to a different address and port.
int CommandReconnect_DCPCGC(CLIENT* c,DWORD address,int port)
{
COMMAND_DCPCGC_RECONNECT p;
if (c->version == VERSION_PC)
{
p.headerpc.size = 0x000C;
p.headerpc.command = 0x19;
p.headerpc.flag = 0x00;
} else {
p.headerdcgc.command = 0x19;
p.headerdcgc.flag = 0x00;
p.headerdcgc.size = 0x000C;
}
p.address = address;
p.port = port;
return SendCommandToClient(c,&p);
}
int CommandReconnect_BB(CLIENT* c,DWORD address,int port)
{
COMMAND_BB_RECONNECT p;
p.header.size = 0x0010;
p.header.command = 0x0019;
p.header.flag = 0x00000000;
p.address = address;
p.port = port;
return SendCommandToClient(c,&p);
}
int (*CommandReconnect_Functions[])(CLIENT* c,DWORD address,int port) = {CommandReconnect_DCPCGC,CommandReconnect_BB,CommandReconnect_DCPCGC,CommandReconnect_DCPCGC,NULL,NULL};
int CommandReconnect(CLIENT* c,DWORD address,int port)
{
if (!c) return 5;
if (!CommandReconnect_Functions[c->version]) return 20;
return CommandReconnect_Functions[c->version](c,address,port);
}
// sends the command (first used by Schthack) that separates PC and GC users that connect on the same port
int CommandSelectiveReconnectPC(CLIENT* c,DWORD address,int port)
{
if (!c) return 5;
if (c->version == VERSION_BLUEBURST) return 89356594;
COMMAND_DCPCGC_RECONNECT* p = (COMMAND_DCPCGC_RECONNECT*)malloc(0xB0);
if (!p) return 1;
p->headerdcgc.command = 0xB0;
p->headerdcgc.flag = 0x00;
p->headerdcgc.size = 0x0019;
p->address = address;
p->port = port;
COMMAND_HEADER_DCGC* p2 = (COMMAND_HEADER_DCGC*)((DWORD)p + 0x19);
p2->command = 0xB0;
p2->flag = 0x00;
p2->size = 0x0097;
int rv = SendCommandToClient(c,p);
free(p);
return rv;
}
////////////////////////////////////////////////////////////////////////////////
// BB only commands
// sends the command that signals an error or updates the client's guild card number and security data
int CommandBBClientInit(CLIENT* c,DWORD error)
{
if (!c) return 5;
COMMAND_BB_SETSECURITY p;
p.header.size = sizeof(COMMAND_BB_SETSECURITY);
p.header.command = 0x00E6;
p.header.flag = 0x00000000;
p.error = error;
p.playerTag = 0x00010000;
p.serialNumber = c->license.serialNumber;
p.teamID = (rand() << 16) | rand();//0xFFFFFFFF;
memcpy(&p.cfg,&c->cfg,sizeof(CLIENTCONFIG_BB));
p.caps = 0x00000102;
return SendCommandToClient(c,&p);
}
// sends the client's key config
int CommandBBSendKeyConfig(CLIENT* c)
{
if (!c) return 5;
if (c->version != VERSION_BLUEBURST) return 8;
COMMAND_BB_KEYCONFIG p;
p.header.size = sizeof(COMMAND_BB_KEYCONFIG);
p.header.command = 0x00E2;
p.header.flag = 0x00000000;
memcpy(&p.keyconfig,&c->playerInfo.keyConfig,sizeof(BB_KEY_TEAM_CONFIG));
return SendCommandToClient(c,&p);
}
// sends a player preview. these are used by the caracter select and character creation mechanism.
// see ApplyPlayerPreview for more information
int CommandBBSendPlayerPreview(CLIENT* c,DWORD playernum)
{
if (!c) return 5;
COMMAND_BB_PLAYERPREVIEW p;
COMMAND_BB_NOPLAYERPREVIEW p2;
char signature[0x40];
bool oldformat = false;
DWORD bytesread;
char filename[MAX_PATH];
sprintf(filename,"system\\players\\player_%s_%ld.nsc",c->license.username,playernum + 1);
HANDLE file = CreateFile(filename,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
if (file == INVALID_HANDLE_VALUE)
{
oldformat = true;
sprintf(filename,"system\\players\\%s-player-%ld.pbb",c->license.username,playernum);
file = CreateFile(filename,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
}
if (file == INVALID_HANDLE_VALUE)
{
p2.header.size = 0x0010;
p2.header.command = 0x00E4;
p2.header.flag = 0x00000000;
p2.playernum = playernum;
p2.error = 2;
return SendCommandToClient(c,&p2);
} else {
if (oldformat)
{
p.header.size = 0x0088;
p.header.command = 0x00E5;
p.header.flag = 0x00000000;
p.playernum = playernum;
SetFilePointer(file,0x0364,NULL,FILE_BEGIN);
ReadFile(file,&p.preview.level,0x0004,&bytesread,NULL);
ReadFile(file,&p.preview.exp,0x0004,&bytesread,NULL);
SetFilePointer(file,0x0370,NULL,FILE_BEGIN);
ReadFile(file,p.preview.guildcard,0x74,&bytesread,NULL);
} else {
ReadFile(file,signature,0x40,&bytesread,NULL);
if (!strcmp(signature,PLAYER_FILE_SIGNATURE))
{
p.header.size = 0x0088;
p.header.command = 0x00E5;
p.header.flag = 0x00000000;
p.playernum = playernum;
ReadFile(file,&p.preview,sizeof(BB_PLAYER_DISPDATA_PREVIEW),&bytesread,NULL);
if (bytesread != sizeof(BB_PLAYER_DISPDATA_PREVIEW))
{
CloseHandle(file);
return 8905;
}
} else {
p2.header.size = 0x0010;
p2.header.command = 0x00E4;
p2.header.flag = 0x00000000;
p2.playernum = playernum;
p2.error = 1;
CloseHandle(file);
return SendCommandToClient(c,&p2);
}
}
CloseHandle(file);
return SendCommandToClient(c,&p);
}
return 5438493;
}
// sent in response to the client's 01E8 command
int CommandBBAcceptClientChecksum(CLIENT* c)
{
if (!c) return 5;
COMMAND_BB_ACCEPTCLIENTCHECKSUM p;
p.header.size = 0x000C;
p.header.command = 0x02E8;
p.header.flag = 0x00000000;
p.verify = 2;
return SendCommandToClient(c,&p);
}
// sends the "I'm about to send your guild card file" command
int CommandBBSendGuildCardHeader(CLIENT* c)
{
if (!c) return 5;
if (c->version != VERSION_BLUEBURST) return 5;
COMMAND_BB_GUILDCARDHEADER h;
h.header.size = 0x0014;
h.header.command = 0x01DC;
h.header.flag = 0x00000000;
h.unknown = 1;
h.filesize = sizeof(BB_GUILDCARD_FILE);
h.checksum = CalculateGuildCardChecksum(&c->playerInfo.guildcards,sizeof(BB_GUILDCARD_FILE));//0x0EA14A8B;
return SendCommandToClient(c,&h);
}
// sends a chunk of guild card data
int CommandBBSendGuildCardData(CLIENT* c,DWORD chunk)
{
if (!c) return 5;
unsigned int beginOffset = (chunk * 0x6800);
if (beginOffset >= sizeof(BB_GUILDCARD_FILE)) return 849365;
unsigned int dataSize = sizeof(BB_GUILDCARD_FILE) - beginOffset;
if (dataSize > 0x6800) dataSize = 0x6800;
COMMAND_BB_GUILDCARDCHUNK* p = (COMMAND_BB_GUILDCARDCHUNK*)malloc(0x6810);
if (!p) return 1;
p->header.size = dataSize + 0x0010;
p->header.command = 0x02DC;
p->header.flag = 0x00000000;
p->unknown = 0;
p->chunkID = chunk;
memcpy(p->data,(void*)((DWORD)(&c->playerInfo.guildcards) + beginOffset),dataSize);
int rv = SendCommandToClient(c,p);
free(p);
return rv;
}
// sends the game data (battleparamentry files, etc.)
int CommandBBSendStreamFile(SERVER* s,CLIENT* c)
{
if (!s || !c) return 5;
int errors;
DWORD x,size,bytesread;
HANDLE file;
file = CreateFile("system\\blueburst\\streamfile.ind",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
if (file == INVALID_HANDLE_VALUE) return 537;
size = GetFileSize(file,NULL);
COMMAND_BB_STREAMFILEINDEX* pi = (COMMAND_BB_STREAMFILEINDEX*)malloc(size + 4);
if (!pi)
{
CloseHandle(file);
return 1;
}
ReadFile(file,(void*)((DWORD)pi + 4),size,&bytesread,NULL);
CloseHandle(file);
if (bytesread != size)
{
free(pi);
return 539;
}
pi->size = size + 4;
pi->command = 0x01EB;
errors = SendCommandToClient(c,pi);
if (errors)
{
free(pi);
return errors;
}
COMMAND_BB_STREAMFILECHUNK* pc = (COMMAND_BB_STREAMFILECHUNK*)malloc(0x680C);
DWORD dataInBuffer = 0,fileDataRemaining,thisReadSize,chunknum = 0;
char filename[MAX_PATH];
if (!pc)
{
free(pi);
return 1;
}
for (x = 0; (x < pi->num) && !errors; x++)
{
strcpy(filename,"system\\blueburst\\");
strcat(filename,pi->entry[x].filename);
file = CreateFile(filename,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
if (file == INVALID_HANDLE_VALUE)
{
errors = 5387441;
break;
}
fileDataRemaining = pi->entry[x].size;
while (fileDataRemaining && !errors)
{
thisReadSize = 0x6800 - dataInBuffer;
if (thisReadSize > fileDataRemaining) thisReadSize = fileDataRemaining;
ReadFile(file,&pc->data[dataInBuffer],thisReadSize,&bytesread,NULL);
if (bytesread != thisReadSize)
{
errors = 5387442;
break;
}
dataInBuffer += thisReadSize;
fileDataRemaining -= thisReadSize;
if (dataInBuffer == 0x6800)
{
errors = ProcessCommands(s,c,0x0005,0x03EB,0);
pc->header.size = (dataInBuffer + 15) & ~3;
pc->header.command = 0x02EB;
pc->header.flag = 0x00000000;
pc->chunknum = chunknum;
if (!errors) errors = SendCommandToClient(c,pc);
dataInBuffer = 0;
chunknum++;
}
}
CloseHandle(file);
}
if ((dataInBuffer > 0) && !errors)
{
errors = ProcessCommands(s,c,0x0005,0x03EB,0);
pc->header.size = (dataInBuffer + 15) & ~3;
pc->header.command = 0x02EB;
pc->header.flag = 0x00000000;
pc->chunknum = chunknum;
if (!errors) SendCommandToClient(c,pc);
}
free(pc);
free(pi);
return errors;
}
// accepts the player's choice at char select
int CommandBBApprovePlayerChoice(CLIENT* c)
{
if (!c) return 5;
COMMAND_BB_ACCEPTCLIENTCHECKSUM p;
p.header.size = 0x0010;
p.header.command = 0x00E4;
p.header.flag = 0x00000000;
p.verify = c->cfg.bbplayernum;
p.unused = 1;
return SendCommandToClient(c,&p);
}
// sends player data to the client (usually sent right before entering lobby)
int CommandBBSendPlayerInfo(CLIENT* c)
{
if (!c) return 5;
COMMAND_BB_PLAYERSAVE p;
p.header.size = 0x399C;
p.header.command = 0x00E7;
p.header.flag = 0x00000000;
ExportCompletePlayerData(&p.data,&c->playerInfo);
return SendCommandToClient(c,&p);
}
////////////////////////////////////////////////////////////////////////////////
// message functions
//int CommandScrollingMessage(CLIENT* c,wchar_t* text) // for BB, this is command EE with the same format as 1A/D5
int CommandPatchCheckDirectory(CLIENT* c,char* dir)
{
if (!c) return 5;
COMMAND_PATCH_CHECKDIR p;
memset(&p,0,0x44);
p.header.size = 0x0044;
p.header.command = 0x09;
p.header.flag = 0;
strcpy(p.dir,dir);
return SendCommandToClient(c,&p);
}
////////////////////////////////////////////////////////////////////////////////
// Message functions (CommandMessageBox, CommandLobbyName, CommandQuestInfo)
// CommandMessageBox: this function displays a large message box (1A/D5) on the client's screen.
// CommandLobbyName: this function tells the client the current lobby's/game's name.
// CommandQuestInfo: displays the quest info box when a client is selecting a quest.
int CommandLargeBasicMessage_PCPatch(CLIENT* c,int command,wchar_t* text)
{
COMMAND_PC_LARGEMESSAGE* p = (COMMAND_PC_LARGEMESSAGE*)malloc(0x808);
if (!p) return 1;
p->header.command = command;
p->header.flag = 0;
p->header.size = 0x0008 + (((2 * wcslen(text)) + 5) & ~3);
wcscpy(p->text,text);
tx_add_color(p->text);
int rv = SendCommandToClient(c,p);
free(p);
return rv;
}
int CommandLargeBasicMessage_DCGC(CLIENT* c,int command,wchar_t* text)
{
COMMAND_GC_LARGEMESSAGE* p = (COMMAND_GC_LARGEMESSAGE*)malloc(0x408);
if (!p) return 1;
p->header.command = command;
p->header.flag = 0;
p->header.size = 0x0004 + ((tx_convert_to_sjis(p->text,text) + 5) & ~3);
tx_add_color(p->text);
int rv = SendCommandToClient(c,p);
free(p);
return rv;
}
int CommandLargeBasicMessage_BB(CLIENT* c,int command,wchar_t* text)
{
COMMAND_BB_LARGEMESSAGE* p = (COMMAND_BB_LARGEMESSAGE*)malloc(0x80C);
if (!p) return 1;
p->header.command = command;
p->header.flag = 0;
p->header.size = 0x0008 + (((2 * wcslen(text)) + 5) & ~3);
wcscpy(p->text,text);
tx_add_color(p->text);
int rv = SendCommandToClient(c,p);
free(p);
return rv;
}
int (*CommandLargeBasicMessage_Functions[])(CLIENT* c,int command,wchar_t* text) = {CommandLargeBasicMessage_DCGC,CommandLargeBasicMessage_BB,CommandLargeBasicMessage_PCPatch,CommandLargeBasicMessage_DCGC,CommandLargeBasicMessage_PCPatch,NULL};
int CommandLargeBasicMessage(CLIENT* c,int command,wchar_t* text)
{
if (!c || !text) return 5;
if (!CommandLargeBasicMessage_Functions[c->version]) return 20;
return CommandLargeBasicMessage_Functions[c->version](c,command,text);
}
int CommandMessageBox(CLIENT* c,wchar_t* text) { return CommandLargeBasicMessage(c,((c->version == VERSION_PATCH) ? 0x13 : 0x1A),text); }
int CommandLobbyName(CLIENT* c,wchar_t* text) { return CommandLargeBasicMessage(c,0x8A,text); }
int CommandQuestInfo(CLIENT* c,wchar_t* text) { return CommandLargeBasicMessage(c,0xA3,text); }
////////////////////////////////////////////////////////////////////////////////
// message commands (CommandLobbyMessageBox CommandShipInfo CommandTextMessage )
// these use a slightly different format than the above.
// these functions are designed to broadcast messages, so the version-specific functions
// simply generate data for specific versions, and do not send anything.
// CommandLobbyMessageBox: shows a small message box in the lower-left corner of the screen. used when errors occur joining a game, for example.
// CommandShipInfo: shows a small message box in the lower-right corner of the screen. on GC (and maybe other versions) the client cannot make this disappear if used in the lobby.
// CommandTextMessage: sends a text message that appears near the middle of the screen. using colors may help it stand out (i.e. $C6 for yellow)
//void* DataCommandBasicMessage_DC(CLIENT* c,wchar_t* text)
void* DataCommandBasicMessage_PC(int command,wchar_t* text)
{
COMMAND_PC_MESSAGE* p = (COMMAND_PC_MESSAGE*)malloc(0x810);
if (!p) return NULL;
p->header.command = command;
p->header.flag = 0;
p->header.size = 0x000C + (((2 * wcslen(text)) + 5) & ~3);
p->unused = 0;
p->serialNumber = 0;
wcscpy(p->text,text);
tx_add_color(p->text);
return p;
}
void* DataCommandBasicMessage_GC(int command,wchar_t* text)
{
COMMAND_GC_MESSAGE* p = (COMMAND_GC_MESSAGE*)malloc(0x408);
if (!p) return NULL;
p->header.command = command;
p->header.flag = 0;
p->header.size = 0x000C + ((tx_convert_to_sjis(p->text,text) + 5) & ~3);
p->unused = 0;
p->serialNumber = 0;
tx_add_color(p->text);
return p;
}
void* DataCommandBasicMessage_BB(int command,wchar_t* text)
{
COMMAND_BB_MESSAGE* p = (COMMAND_BB_MESSAGE*)malloc(0x80C);
if (!p) return NULL;
p->header.command = command;
p->header.flag = 0;
p->header.size = 0x0010 + (((2 * wcslen(text)) + 5) & ~3);
p->unused = 0;
p->serialNumber = 0;
wcscpy(p->text,text);
tx_add_color(p->text);
return p;
}
void* (*DataCommandBasicMessage_Functions[])(int command,wchar_t* text) = {DataCommandBasicMessage_GC,DataCommandBasicMessage_BB,DataCommandBasicMessage_PC,/*DataCommandBasicMessage_DC*/NULL,NULL,NULL};
int CommandBasicMessage(SERVER* s,LOBBY* li,CLIENT* c,int command,wchar_t* text)
{
if ((!s && !li && !c) || !text) return 5;
void* data[VERSION_MAX];
int x;
for (x = 0; x < VERSION_MAX; x++)
{
if (!DataCommandBasicMessage_Functions[x]) data[x] = NULL;
else data[x] = DataCommandBasicMessage_Functions[x](command,text);
}
int errors;
if (s) errors = SendCommandToServer(s,NULL,data);
else if (li) errors = SendCommandToLobby(li,NULL,data);
else errors = SendCommandToClient(c,data[c->version]);
for (x = 0; x < VERSION_MAX; x++) if (data[x]) free(data[x]);
return errors;
}
int CommandLobbyMessageBox(SERVER* s,LOBBY* li,CLIENT* c,wchar_t* text) { return CommandBasicMessage(s,li,c,0x0001,text); }
int CommandShipInfo(SERVER* s,LOBBY* li,CLIENT* c,wchar_t* text) { return CommandBasicMessage(s,li,c,0x0011,text); }
int CommandTextMessage(SERVER* s,LOBBY* li,CLIENT* c,wchar_t* text) { return CommandBasicMessage(s,li,c,0x00B0,text); }
////////////////////////////////////////////////////////////////////////////////
// CommandChat: sends a message from a specific player to the lobby
void* DataCommandChat_PC(DWORD serialNumber,wchar_t* name,wchar_t* text)
{
wchar_t textbuffer[0x200];
wcscpy(textbuffer,text);
tx_add_language_marker(textbuffer,L'J');
COMMAND_PC_MESSAGE* p = (COMMAND_PC_MESSAGE*)malloc(0x810);
if (!p) return NULL;
p->header.command = 0x06;
p->header.flag = 0;
p->unused = 0;
p->serialNumber = serialNumber;
wcscpy(p->text,name);
tx_remove_language_marker(p->text);
wcscat(p->text,L"\t");
wcscat(p->text,textbuffer);
tx_add_color(p->text);
p->header.size = 0x000C + (((2 * wcslen(p->text)) + 5) & ~3);
return p;
}
void* DataCommandChat_DCGC(DWORD serialNumber,wchar_t* name,wchar_t* text)
{
wchar_t textbuffer[0x200];
wcscpy(textbuffer,text);
tx_add_language_marker(textbuffer,L'J');
COMMAND_GC_MESSAGE* p = (COMMAND_GC_MESSAGE*)malloc(0x410);
if (!p) return NULL;
p->header.command = 0x06;
p->header.flag = 0;
p->unused = 0;
p->serialNumber = serialNumber;
tx_convert_to_sjis(p->text,name);
tx_remove_language_marker(p->text);
tx_convert_to_sjis(&p->text[strlen(p->text)],"\t");
tx_convert_to_sjis(&p->text[strlen(p->text)],textbuffer);
tx_add_color(p->text);
p->header.size = 0x000C + ((strlen(p->text) + 5) & ~3);
return p;
}
void* DataCommandChat_BB(DWORD serialNumber,wchar_t* name,wchar_t* text)
{
wchar_t textbuffer[0x200];
wcscpy(textbuffer,text);
tx_add_language_marker(textbuffer,L'J');
COMMAND_BB_MESSAGE* p = (COMMAND_BB_MESSAGE*)malloc(0x814);
if (!p) return NULL;
p->header.command = 0x0006;
p->header.flag = 0;
p->unused = 0;
p->serialNumber = serialNumber;
wcscpy(p->text,name);
tx_add_language_marker(p->text,L'J');
wcscat(p->text,L"\t");
wcscat(p->text,textbuffer);
tx_add_color(p->text);
p->header.size = 0x0010 + (((2 * wcslen(p->text)) + 5) & ~3);
return p;
}
void* (*DataCommandChat_Functions[])(DWORD serialNumber,wchar_t* name,wchar_t* text) = {DataCommandChat_DCGC,DataCommandChat_BB,DataCommandChat_PC,DataCommandChat_DCGC,NULL,NULL};
int CommandChat(LOBBY* li,CLIENT* c,wchar_t* text)
{
if ((!li && !c) || !text) return 5;
void* data[VERSION_MAX];
int x;
for (x = 0; x < VERSION_MAX; x++)
{
if (!DataCommandChat_Functions[x]) data[x] = NULL;
else data[x] = DataCommandChat_Functions[x](c->license.serialNumber,c->playerInfo.disp.playername,text);
}
int errors;
errors = SendCommandToLobby(li,NULL,data);
for (x = 0; x < VERSION_MAX; x++) if (data[x]) free(data[x]);
return errors;
}
////////////////////////////////////////////////////////////////////////////////
// CommandSendInfoBoard: sends the info board to a player.
//int CommandSendInfoBoard_DC(LOBBY* l,CLIENT* c)
int CommandSendInfoBoard_PC(LOBBY* l,CLIENT* c)
{
COMMAND_PC_INFOBOARD* p = (COMMAND_PC_INFOBOARD*)malloc(4);
if (!p) return 1;
p->header.size = 0x0004;
p->header.command = 0xD8;
p->header.flag = 0x00;
unsigned int x;
operation_lock(l);
for (x = 0; x < l->maxClients; x++)
{
if (!l->clients[x]) continue; // there's no client here
p->header.flag++;
p->header.size += 0x0178;
p = (COMMAND_PC_INFOBOARD*)realloc(p,p->header.size);
if (!p)
{
operation_unlock(l);
return 1;
}
memset(p->entry[p->header.flag - 1].name,0,0x20);
memset(p->entry[p->header.flag - 1].message,0,0x0158);
wcscpy(p->entry[p->header.flag - 1].name,l->clients[x]->playerInfo.disp.playername);
wcscpy(p->entry[p->header.flag - 1].message,l->clients[x]->playerInfo.infoboard);
tx_add_color(p->entry[p->header.flag - 1].message);
}
operation_unlock(l);
int errors = SendCommandToClient(c,p);
free(p);
return errors;
}
int CommandSendInfoBoard_GC(LOBBY* l,CLIENT* c)
{
COMMAND_GC_INFOBOARD* p = (COMMAND_GC_INFOBOARD*)malloc(4);
if (!p) return 1;
p->header.size = 0x0004;
p->header.command = 0xD8;
p->header.flag = 0x00;
unsigned int x;
operation_lock(l);
for (x = 0; x < l->maxClients; x++)
{
if (!l->clients[x]) continue; // there's no client here
p->header.flag++;
p->header.size += 0xBC;
p = (COMMAND_GC_INFOBOARD*)realloc(p,p->header.size);
if (!p)
{
operation_unlock(l);
return 1;
}
memset(p->entry[p->header.flag - 1].name,0,0x10);
memset(p->entry[p->header.flag - 1].message,0,0xAC);
tx_convert_to_sjis(p->entry[p->header.flag - 1].name,l->clients[x]->playerInfo.disp.playername);
tx_convert_to_sjis(p->entry[p->header.flag - 1].message,l->clients[x]->playerInfo.infoboard);
tx_add_color(p->entry[p->header.flag - 1].message);
}
operation_unlock(l);
int errors = SendCommandToClient(c,p);
free(p);
return errors;
}
int CommandSendInfoBoard_BB(LOBBY* l,CLIENT* c)
{
COMMAND_BB_INFOBOARD* p = (COMMAND_BB_INFOBOARD*)malloc(8);
if (!p) return 1;