-
Notifications
You must be signed in to change notification settings - Fork 15
/
gameprotocol.cpp
1049 lines (861 loc) · 35.4 KB
/
gameprotocol.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
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
/*
ent-ghost
Copyright [2011-2013] [Jack Lu]
This file is part of the ent-ghost source code.
ent-ghost 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 3 of the License, or
(at your option) any later version.
ent-ghost source code 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 ent-ghost source code. If not, see <http://www.gnu.org/licenses/>.
ent-ghost is modified from GHost++ (http://ghostplusplus.googlecode.com/)
GHost++ is Copyright [2008] [Trevor Hogan]
*/
#include "ghost.h"
#include "util.h"
#include "crc32.h"
#include "gameplayer.h"
#include "gameprotocol.h"
#include "game_base.h"
//
// CGameProtocol
//
CGameProtocol :: CGameProtocol( CGHost *nGHost ) : m_GHost( nGHost )
{
}
CGameProtocol :: ~CGameProtocol( )
{
}
///////////////////////
// RECEIVE FUNCTIONS //
///////////////////////
CIncomingJoinPlayer *CGameProtocol :: RECEIVE_W3GS_REQJOIN( BYTEARRAY data )
{
// DEBUG_Print( "RECEIVED W3GS_REQJOIN" );
// DEBUG_Print( data );
// 2 bytes -> Header
// 2 bytes -> Length
// 4 bytes -> Host Counter (Game ID)
// 4 bytes -> Entry Key (used in LAN)
// 1 byte -> ???
// 2 bytes -> Listen Port
// 4 bytes -> Peer Key
// null terminated string -> Name
// 4 bytes -> ???
// 2 bytes -> InternalPort (???)
// 4 bytes -> InternalIP
if( ValidateLength( data ) && data.size( ) >= 20 )
{
uint32_t HostCounter = UTIL_ByteArrayToUInt32( data, false, 4 );
uint32_t EntryKey = UTIL_ByteArrayToUInt32( data, false, 8 );
BYTEARRAY Name = UTIL_ExtractCString( data, 19 );
if( !Name.empty( ) && data.size( ) >= Name.size( ) + 30 )
{
BYTEARRAY InternalIP = BYTEARRAY( data.begin( ) + Name.size( ) + 26, data.begin( ) + Name.size( ) + 30 );
return new CIncomingJoinPlayer( HostCounter, EntryKey, string( Name.begin( ), Name.end( ) ), InternalIP );
}
}
return NULL;
}
uint32_t CGameProtocol :: RECEIVE_W3GS_LEAVEGAME( BYTEARRAY data )
{
// DEBUG_Print( "RECEIVED W3GS_LEAVEGAME" );
// DEBUG_Print( data );
// 2 bytes -> Header
// 2 bytes -> Length
// 4 bytes -> Reason
if( ValidateLength( data ) && data.size( ) >= 8 )
return UTIL_ByteArrayToUInt32( data, false, 4 );
return 0;
}
bool CGameProtocol :: RECEIVE_W3GS_GAMELOADED_SELF( BYTEARRAY data )
{
// DEBUG_Print( "RECEIVED W3GS_GAMELOADED_SELF" );
// DEBUG_Print( data );
// 2 bytes -> Header
// 2 bytes -> Length
if( ValidateLength( data ) )
return true;
return false;
}
CIncomingAction *CGameProtocol :: RECEIVE_W3GS_OUTGOING_ACTION( BYTEARRAY data, unsigned char PID )
{
// DEBUG_Print( "RECEIVED W3GS_OUTGOING_ACTION" );
// DEBUG_Print( data );
// 2 bytes -> Header
// 2 bytes -> Length
// 4 bytes -> CRC
// remainder of packet -> Action
if( PID != 255 && ValidateLength( data ) && data.size( ) >= 8 )
{
BYTEARRAY CRC = BYTEARRAY( data.begin( ) + 4, data.begin( ) + 8 );
BYTEARRAY Action = BYTEARRAY( data.begin( ) + 8, data.end( ) );
return new CIncomingAction( PID, CRC, Action );
}
return NULL;
}
uint32_t CGameProtocol :: RECEIVE_W3GS_OUTGOING_KEEPALIVE( BYTEARRAY data )
{
// DEBUG_Print( "RECEIVED W3GS_OUTGOING_KEEPALIVE" );
// DEBUG_Print( data );
// 2 bytes -> Header
// 2 bytes -> Length
// 1 byte -> ???
// 4 bytes -> CheckSum??? (used in replays)
if( ValidateLength( data ) && data.size( ) == 9 )
return UTIL_ByteArrayToUInt32( data, false, 5 );
return 0;
}
CIncomingChatPlayer *CGameProtocol :: RECEIVE_W3GS_CHAT_TO_HOST( BYTEARRAY data )
{
// DEBUG_Print( "RECEIVED W3GS_CHAT_TO_HOST" );
// DEBUG_Print( data );
// 2 bytes -> Header
// 2 bytes -> Length
// 1 byte -> Total
// for( 1 .. Total )
// 1 byte -> ToPID
// 1 byte -> FromPID
// 1 byte -> Flag
// if( Flag == 16 )
// null term string -> Message
// elseif( Flag == 17 )
// 1 byte -> Team
// elseif( Flag == 18 )
// 1 byte -> Colour
// elseif( Flag == 19 )
// 1 byte -> Race
// elseif( Flag == 20 )
// 1 byte -> Handicap
// elseif( Flag == 32 )
// 4 bytes -> ExtraFlags
// null term string -> Message
if( ValidateLength( data ) )
{
unsigned int i = 5;
unsigned char Total = data[4];
if( Total > 0 && Total <= 16 && data.size( ) >= i + Total )
{
BYTEARRAY ToPIDs = BYTEARRAY( data.begin( ) + i, data.begin( ) + i + Total );
i += Total;
unsigned char FromPID = data[i];
unsigned char Flag = data[i + 1];
i += 2;
if( Flag == 16 && data.size( ) >= i + 1 )
{
// chat message
BYTEARRAY Message = UTIL_ExtractCString( data, i );
return new CIncomingChatPlayer( FromPID, ToPIDs, Flag, string( Message.begin( ), Message.end( ) ) );
}
else if( ( Flag >= 17 && Flag <= 20 ) && data.size( ) >= i + 1 )
{
// team/colour/race/handicap change request
unsigned char Byte = data[i];
return new CIncomingChatPlayer( FromPID, ToPIDs, Flag, Byte );
}
else if( Flag == 32 && data.size( ) >= i + 5 )
{
// chat message with extra flags
BYTEARRAY ExtraFlags = BYTEARRAY( data.begin( ) + i, data.begin( ) + i + 4 );
BYTEARRAY Message = UTIL_ExtractCString( data, i + 4 );
return new CIncomingChatPlayer( FromPID, ToPIDs, Flag, string( Message.begin( ), Message.end( ) ), ExtraFlags );
}
}
}
return NULL;
}
bool CGameProtocol :: RECEIVE_W3GS_SEARCHGAME( BYTEARRAY data, unsigned char war3Version )
{
uint32_t ProductID = 1462982736; // "W3XP"
uint32_t Version = war3Version;
// DEBUG_Print( "RECEIVED W3GS_SEARCHGAME" );
// DEBUG_Print( data );
// 2 bytes -> Header
// 2 bytes -> Length
// 4 bytes -> ProductID
// 4 bytes -> Version
// 4 bytes -> ??? (Zero)
if( ValidateLength( data ) && data.size( ) >= 16 )
{
if( UTIL_ByteArrayToUInt32( data, false, 4 ) == ProductID )
{
if( UTIL_ByteArrayToUInt32( data, false, 8 ) == Version )
{
if( UTIL_ByteArrayToUInt32( data, false, 12 ) == 0 )
return true;
}
}
}
return false;
}
CIncomingMapSize *CGameProtocol :: RECEIVE_W3GS_MAPSIZE( BYTEARRAY data, BYTEARRAY mapSize )
{
// DEBUG_Print( "RECEIVED W3GS_MAPSIZE" );
// DEBUG_Print( data );
// 2 bytes -> Header
// 2 bytes -> Length
// 4 bytes -> ???
// 1 byte -> SizeFlag (1 = have map, 3 = continue download)
// 4 bytes -> MapSize
if( ValidateLength( data ) && data.size( ) >= 13 )
return new CIncomingMapSize( data[8], UTIL_ByteArrayToUInt32( data, false, 9 ) );
return NULL;
}
uint32_t CGameProtocol :: RECEIVE_W3GS_MAPPARTOK( BYTEARRAY data )
{
// DEBUG_Print( "RECEIVED W3GS_MAPPARTOK" );
// DEBUG_Print( data );
// 2 bytes -> Header
// 2 bytes -> Length
// 1 byte -> SenderPID
// 1 byte -> ReceiverPID
// 4 bytes -> ???
// 4 bytes -> MapSize
if( ValidateLength( data ) && data.size( ) >= 14 )
return UTIL_ByteArrayToUInt32( data, false, 10 );
return 0;
}
uint32_t CGameProtocol :: RECEIVE_W3GS_PONG_TO_HOST( BYTEARRAY data )
{
// DEBUG_Print( "RECEIVED W3GS_PONG_TO_HOST" );
// DEBUG_Print( data );
// 2 bytes -> Header
// 2 bytes -> Length
// 4 bytes -> Pong
// the pong value is just a copy of whatever was sent in SEND_W3GS_PING_FROM_HOST which was GetTicks( ) at the time of sending
// so as long as we trust that the client isn't trying to fake us out and mess with the pong value we can find the round trip time by simple subtraction
// (the subtraction is done elsewhere because the very first pong value seems to be 1 and we want to discard that one)
if( ValidateLength( data ) && data.size( ) >= 8 )
return UTIL_ByteArrayToUInt32( data, false, 4 );
return 1;
}
////////////////////
// SEND FUNCTIONS //
////////////////////
BYTEARRAY CGameProtocol :: SEND_W3GS_PING_FROM_HOST( )
{
BYTEARRAY packet;
packet.push_back( W3GS_HEADER_CONSTANT ); // W3GS header constant
packet.push_back( W3GS_PING_FROM_HOST ); // W3GS_PING_FROM_HOST
packet.push_back( 0 ); // packet length will be assigned later
packet.push_back( 0 ); // packet length will be assigned later
UTIL_AppendByteArray( packet, GetTicks( ), false ); // ping value
AssignLength( packet );
// DEBUG_Print( "SENT W3GS_PING_FROM_HOST" );
// DEBUG_Print( packet );
return packet;
}
BYTEARRAY CGameProtocol :: SEND_W3GS_SLOTINFOJOIN( unsigned char PID, BYTEARRAY port, BYTEARRAY externalIP, vector<CGameSlot> &slots, uint32_t randomSeed, unsigned char layoutStyle, unsigned char playerSlots )
{
unsigned char Zeros[] = { 0, 0, 0, 0 };
BYTEARRAY SlotInfo = EncodeSlotInfo( slots, randomSeed, layoutStyle, playerSlots );
BYTEARRAY packet;
if( port.size( ) == 2 && externalIP.size( ) == 4 )
{
packet.push_back( W3GS_HEADER_CONSTANT ); // W3GS header constant
packet.push_back( W3GS_SLOTINFOJOIN ); // W3GS_SLOTINFOJOIN
packet.push_back( 0 ); // packet length will be assigned later
packet.push_back( 0 ); // packet length will be assigned later
UTIL_AppendByteArray( packet, (uint16_t)SlotInfo.size( ), false ); // SlotInfo length
UTIL_AppendByteArrayFast( packet, SlotInfo ); // SlotInfo
packet.push_back( PID ); // PID
packet.push_back( 2 ); // AF_INET
packet.push_back( 0 ); // AF_INET continued...
UTIL_AppendByteArray( packet, port ); // port
UTIL_AppendByteArrayFast( packet, externalIP ); // external IP
UTIL_AppendByteArray( packet, Zeros, 4 ); // ???
UTIL_AppendByteArray( packet, Zeros, 4 ); // ???
AssignLength( packet );
}
else
CONSOLE_Print( "[GAMEPROTO] invalid parameters passed to SEND_W3GS_SLOTINFOJOIN" );
// DEBUG_Print( "SENT W3GS_SLOTINFOJOIN" );
// DEBUG_Print( packet );
return packet;
}
BYTEARRAY CGameProtocol :: SEND_W3GS_REJECTJOIN( uint32_t reason )
{
BYTEARRAY packet;
packet.push_back( W3GS_HEADER_CONSTANT ); // W3GS header constant
packet.push_back( W3GS_REJECTJOIN ); // W3GS_REJECTJOIN
packet.push_back( 0 ); // packet length will be assigned later
packet.push_back( 0 ); // packet length will be assigned later
UTIL_AppendByteArray( packet, reason, false ); // reason
AssignLength( packet );
// DEBUG_Print( "SENT W3GS_REJECTJOIN" );
// DEBUG_Print( packet );
return packet;
}
BYTEARRAY CGameProtocol :: SEND_W3GS_PLAYERINFO( unsigned char PID, string name, BYTEARRAY externalIP, BYTEARRAY internalIP )
{
unsigned char PlayerJoinCounter[] = { 2, 0, 0, 0 };
unsigned char Zeros[] = { 0, 0, 0, 0 };
BYTEARRAY packet;
if( !name.empty( ) && name.size( ) <= 15 && externalIP.size( ) == 4 && internalIP.size( ) == 4 )
{
packet.push_back( W3GS_HEADER_CONSTANT ); // W3GS header constant
packet.push_back( W3GS_PLAYERINFO ); // W3GS_PLAYERINFO
packet.push_back( 0 ); // packet length will be assigned later
packet.push_back( 0 ); // packet length will be assigned later
UTIL_AppendByteArray( packet, PlayerJoinCounter, 4 ); // player join counter
packet.push_back( PID ); // PID
UTIL_AppendByteArrayFast( packet, name ); // player name
packet.push_back( 1 ); // ???
packet.push_back( 0 ); // ???
packet.push_back( 2 ); // AF_INET
packet.push_back( 0 ); // AF_INET continued...
packet.push_back( 0 ); // port
packet.push_back( 0 ); // port continued...
UTIL_AppendByteArrayFast( packet, externalIP ); // external IP
UTIL_AppendByteArray( packet, Zeros, 4 ); // ???
UTIL_AppendByteArray( packet, Zeros, 4 ); // ???
packet.push_back( 2 ); // AF_INET
packet.push_back( 0 ); // AF_INET continued...
packet.push_back( 0 ); // port
packet.push_back( 0 ); // port continued...
UTIL_AppendByteArrayFast( packet, internalIP ); // internal IP
UTIL_AppendByteArray( packet, Zeros, 4 ); // ???
UTIL_AppendByteArray( packet, Zeros, 4 ); // ???
AssignLength( packet );
}
else
CONSOLE_Print( "[GAMEPROTO] invalid parameters passed to SEND_W3GS_PLAYERINFO" );
// DEBUG_Print( "SENT W3GS_PLAYERINFO" );
// DEBUG_Print( packet );
return packet;
}
BYTEARRAY CGameProtocol :: SEND_W3GS_PLAYERLEAVE_OTHERS( unsigned char PID, uint32_t leftCode )
{
BYTEARRAY packet;
if( PID != 255 )
{
packet.push_back( W3GS_HEADER_CONSTANT ); // W3GS header constant
packet.push_back( W3GS_PLAYERLEAVE_OTHERS ); // W3GS_PLAYERLEAVE_OTHERS
packet.push_back( 0 ); // packet length will be assigned later
packet.push_back( 0 ); // packet length will be assigned later
packet.push_back( PID ); // PID
UTIL_AppendByteArray( packet, leftCode, false ); // left code (see PLAYERLEAVE_ constants in gameprotocol.h)
AssignLength( packet );
}
else
CONSOLE_Print( "[GAMEPROTO] invalid parameters passed to SEND_W3GS_PLAYERLEAVE_OTHERS" );
// DEBUG_Print( "SENT W3GS_PLAYERLEAVE_OTHERS" );
// DEBUG_Print( packet );
return packet;
}
BYTEARRAY CGameProtocol :: SEND_W3GS_GAMELOADED_OTHERS( unsigned char PID )
{
BYTEARRAY packet;
if( PID != 255 )
{
packet.push_back( W3GS_HEADER_CONSTANT ); // W3GS header constant
packet.push_back( W3GS_GAMELOADED_OTHERS ); // W3GS_GAMELOADED_OTHERS
packet.push_back( 0 ); // packet length will be assigned later
packet.push_back( 0 ); // packet length will be assigned later
packet.push_back( PID ); // PID
AssignLength( packet );
}
else
CONSOLE_Print( "[GAMEPROTO] invalid parameters passed to SEND_W3GS_GAMELOADED_OTHERS" );
// DEBUG_Print( "SENT W3GS_GAMELOADED_OTHERS" );
// DEBUG_Print( packet );
return packet;
}
BYTEARRAY CGameProtocol :: SEND_W3GS_SLOTINFO( vector<CGameSlot> &slots, uint32_t randomSeed, unsigned char layoutStyle, unsigned char playerSlots )
{
BYTEARRAY SlotInfo = EncodeSlotInfo( slots, randomSeed, layoutStyle, playerSlots );
BYTEARRAY packet;
packet.push_back( W3GS_HEADER_CONSTANT ); // W3GS header constant
packet.push_back( W3GS_SLOTINFO ); // W3GS_SLOTINFO
packet.push_back( 0 ); // packet length will be assigned later
packet.push_back( 0 ); // packet length will be assigned later
UTIL_AppendByteArray( packet, (uint16_t)SlotInfo.size( ), false ); // SlotInfo length
UTIL_AppendByteArrayFast( packet, SlotInfo ); // SlotInfo
AssignLength( packet );
// DEBUG_Print( "SENT W3GS_SLOTINFO" );
// DEBUG_Print( packet );
return packet;
}
BYTEARRAY CGameProtocol :: SEND_W3GS_COUNTDOWN_START( )
{
BYTEARRAY packet;
packet.push_back( W3GS_HEADER_CONSTANT ); // W3GS header constant
packet.push_back( W3GS_COUNTDOWN_START ); // W3GS_COUNTDOWN_START
packet.push_back( 0 ); // packet length will be assigned later
packet.push_back( 0 ); // packet length will be assigned later
AssignLength( packet );
// DEBUG_Print( "SENT W3GS_COUNTDOWN_START" );
// DEBUG_Print( packet );
return packet;
}
BYTEARRAY CGameProtocol :: SEND_W3GS_COUNTDOWN_END( )
{
BYTEARRAY packet;
packet.push_back( W3GS_HEADER_CONSTANT ); // W3GS header constant
packet.push_back( W3GS_COUNTDOWN_END ); // W3GS_COUNTDOWN_END
packet.push_back( 0 ); // packet length will be assigned later
packet.push_back( 0 ); // packet length will be assigned later
AssignLength( packet );
// DEBUG_Print( "SENT W3GS_COUNTDOWN_END" );
// DEBUG_Print( packet );
return packet;
}
BYTEARRAY CGameProtocol :: SEND_W3GS_INCOMING_ACTION( queue<CIncomingAction *> actions, uint16_t sendInterval )
{
BYTEARRAY packet;
packet.push_back( W3GS_HEADER_CONSTANT ); // W3GS header constant
packet.push_back( W3GS_INCOMING_ACTION ); // W3GS_INCOMING_ACTION
packet.push_back( 0 ); // packet length will be assigned later
packet.push_back( 0 ); // packet length will be assigned later
UTIL_AppendByteArray( packet, sendInterval, false ); // send interval
// create subpacket
if( !actions.empty( ) )
{
BYTEARRAY subpacket;
while( !actions.empty( ) )
{
CIncomingAction *Action = actions.front( );
actions.pop( );
subpacket.push_back( Action->GetPID( ) );
UTIL_AppendByteArray( subpacket, (uint16_t)Action->GetAction( )->size( ), false );
UTIL_AppendByteArrayFast( subpacket, *Action->GetAction( ) );
}
// calculate crc (we only care about the first 2 bytes though)
BYTEARRAY crc32 = UTIL_CreateByteArray( m_GHost->m_CRC->FullCRC( (unsigned char *)string( subpacket.begin( ), subpacket.end( ) ).c_str( ), subpacket.size( ) ), false );
crc32.resize( 2 );
// finish subpacket
UTIL_AppendByteArrayFast( packet, crc32 ); // crc
UTIL_AppendByteArrayFast( packet, subpacket ); // subpacket
}
AssignLength( packet );
// DEBUG_Print( "SENT W3GS_INCOMING_ACTION" );
// DEBUG_Print( packet );
return packet;
}
BYTEARRAY CGameProtocol :: SEND_W3GS_CHAT_FROM_HOST( unsigned char fromPID, BYTEARRAY toPIDs, unsigned char flag, BYTEARRAY flagExtra, string message )
{
BYTEARRAY packet;
if( !toPIDs.empty( ) && !message.empty( ) && message.size( ) < 255 )
{
packet.push_back( W3GS_HEADER_CONSTANT ); // W3GS header constant
packet.push_back( W3GS_CHAT_FROM_HOST ); // W3GS_CHAT_FROM_HOST
packet.push_back( 0 ); // packet length will be assigned later
packet.push_back( 0 ); // packet length will be assigned later
packet.push_back( toPIDs.size( ) ); // number of receivers
UTIL_AppendByteArrayFast( packet, toPIDs ); // receivers
packet.push_back( fromPID ); // sender
packet.push_back( flag ); // flag
UTIL_AppendByteArrayFast( packet, flagExtra ); // extra flag
UTIL_AppendByteArrayFast( packet, message ); // message
AssignLength( packet );
}
else
CONSOLE_Print( "[GAMEPROTO] invalid parameters passed to SEND_W3GS_CHAT_FROM_HOST" );
// DEBUG_Print( "SENT W3GS_CHAT_FROM_HOST" );
// DEBUG_Print( packet );
return packet;
}
BYTEARRAY CGameProtocol :: SEND_W3GS_START_LAG( vector<CGamePlayer *> players, bool loadInGame )
{
BYTEARRAY packet;
unsigned char NumLaggers = 0;
for( vector<CGamePlayer *> :: iterator i = players.begin( ); i != players.end( ); i++ )
{
if( loadInGame )
{
if( !(*i)->GetFinishedLoading( ) )
++NumLaggers;
}
else
{
if( (*i)->GetLagging( ) )
++NumLaggers;
}
}
if( NumLaggers > 0 )
{
packet.push_back( W3GS_HEADER_CONSTANT ); // W3GS header constant
packet.push_back( W3GS_START_LAG ); // W3GS_START_LAG
packet.push_back( 0 ); // packet length will be assigned later
packet.push_back( 0 ); // packet length will be assigned later
packet.push_back( NumLaggers );
for( vector<CGamePlayer *> :: iterator i = players.begin( ); i != players.end( ); i++ )
{
if( loadInGame )
{
if( !(*i)->GetFinishedLoading( ) )
{
packet.push_back( (*i)->GetPID( ) );
UTIL_AppendByteArray( packet, (uint32_t)0, false );
}
}
else
{
if( (*i)->GetLagging( ) )
{
packet.push_back( (*i)->GetPID( ) );
UTIL_AppendByteArray( packet, GetTicks( ) - (*i)->GetStartedLaggingTicks( ), false );
}
}
}
AssignLength( packet );
}
else
CONSOLE_Print( "[GAMEPROTO] no laggers passed to SEND_W3GS_START_LAG" );
// DEBUG_Print( "SENT W3GS_START_LAG" );
// DEBUG_Print( packet );
return packet;
}
BYTEARRAY CGameProtocol :: SEND_W3GS_STOP_LAG( CGamePlayer *player, bool loadInGame )
{
BYTEARRAY packet;
packet.push_back( W3GS_HEADER_CONSTANT ); // W3GS header constant
packet.push_back( W3GS_STOP_LAG ); // W3GS_STOP_LAG
packet.push_back( 0 ); // packet length will be assigned later
packet.push_back( 0 ); // packet length will be assigned later
packet.push_back( player->GetPID( ) );
if( loadInGame )
UTIL_AppendByteArray( packet, (uint32_t)0, false );
else
UTIL_AppendByteArray( packet, GetTicks( ) - player->GetStartedLaggingTicks( ), false );
AssignLength( packet );
// DEBUG_Print( "SENT W3GS_STOP_LAG" );
// DEBUG_Print( packet );
return packet;
}
BYTEARRAY CGameProtocol :: SEND_W3GS_SEARCHGAME( bool TFT, unsigned char war3Version )
{
unsigned char ProductID_ROC[] = { 51, 82, 65, 87 }; // "WAR3"
unsigned char ProductID_TFT[] = { 80, 88, 51, 87 }; // "W3XP"
unsigned char Version[] = { war3Version, 0, 0, 0 };
unsigned char Unknown[] = { 0, 0, 0, 0 };
BYTEARRAY packet;
packet.push_back( W3GS_HEADER_CONSTANT ); // W3GS header constant
packet.push_back( W3GS_SEARCHGAME ); // W3GS_SEARCHGAME
packet.push_back( 0 ); // packet length will be assigned later
packet.push_back( 0 ); // packet length will be assigned later
if( TFT )
UTIL_AppendByteArray( packet, ProductID_TFT, 4 ); // Product ID (TFT)
else
UTIL_AppendByteArray( packet, ProductID_ROC, 4 ); // Product ID (ROC)
UTIL_AppendByteArray( packet, Version, 4 ); // Version
UTIL_AppendByteArray( packet, Unknown, 4 ); // ???
AssignLength( packet );
// DEBUG_Print( "SENT W3GS_SEARCHGAME" );
// DEBUG_Print( packet );
return packet;
}
BYTEARRAY CGameProtocol :: SEND_W3GS_GAMEINFO( bool TFT, unsigned char war3Version, BYTEARRAY mapGameType, BYTEARRAY mapFlags, BYTEARRAY mapWidth, BYTEARRAY mapHeight, string gameName, string hostName, uint32_t upTime, string mapPath, BYTEARRAY mapCRC, uint32_t slotsTotal, uint32_t slotsOpen, uint16_t port, uint32_t hostCounter, uint32_t entryKey )
{
unsigned char ProductID_ROC[] = { 51, 82, 65, 87 }; // "WAR3"
unsigned char ProductID_TFT[] = { 80, 88, 51, 87 }; // "W3XP"
unsigned char Version[] = { war3Version, 0, 0, 0 };
unsigned char Unknown2[] = { 1, 0, 0, 0 };
BYTEARRAY packet;
if( mapGameType.size( ) == 4 && mapFlags.size( ) == 4 && mapWidth.size( ) == 2 && mapHeight.size( ) == 2 && !gameName.empty( ) && !hostName.empty( ) && !mapPath.empty( ) && mapCRC.size( ) == 4 )
{
// make the stat string
BYTEARRAY StatString;
UTIL_AppendByteArrayFast( StatString, mapFlags );
StatString.push_back( 0 );
UTIL_AppendByteArrayFast( StatString, mapWidth );
UTIL_AppendByteArrayFast( StatString, mapHeight );
UTIL_AppendByteArrayFast( StatString, mapCRC );
UTIL_AppendByteArrayFast( StatString, mapPath );
UTIL_AppendByteArrayFast( StatString, hostName );
StatString.push_back( 0 );
StatString = UTIL_EncodeStatString( StatString );
// make the rest of the packet
packet.push_back( W3GS_HEADER_CONSTANT ); // W3GS header constant
packet.push_back( W3GS_GAMEINFO ); // W3GS_GAMEINFO
packet.push_back( 0 ); // packet length will be assigned later
packet.push_back( 0 ); // packet length will be assigned later
if( TFT )
UTIL_AppendByteArray( packet, ProductID_TFT, 4 ); // Product ID (TFT)
else
UTIL_AppendByteArray( packet, ProductID_ROC, 4 ); // Product ID (ROC)
UTIL_AppendByteArray( packet, Version, 4 ); // Version
UTIL_AppendByteArray( packet, hostCounter, false ); // Host Counter
UTIL_AppendByteArray( packet, entryKey, false ); // Entry Key
UTIL_AppendByteArrayFast( packet, gameName ); // Game Name
packet.push_back( 0 ); // ??? (maybe game password)
UTIL_AppendByteArrayFast( packet, StatString ); // Stat String
packet.push_back( 0 ); // Stat String null terminator (the stat string is encoded to remove all even numbers i.e. zeros)
UTIL_AppendByteArray( packet, slotsTotal, false ); // Slots Total
UTIL_AppendByteArrayFast( packet, mapGameType ); // Game Type
UTIL_AppendByteArray( packet, Unknown2, 4 ); // ???
UTIL_AppendByteArray( packet, slotsOpen, false ); // Slots Open
UTIL_AppendByteArray( packet, upTime, false ); // time since creation
UTIL_AppendByteArray( packet, port, false ); // port
AssignLength( packet );
}
else
CONSOLE_Print( "[GAMEPROTO] invalid parameters passed to SEND_W3GS_GAMEINFO" );
// DEBUG_Print( "SENT W3GS_GAMEINFO" );
// DEBUG_Print( packet );
return packet;
}
BYTEARRAY CGameProtocol :: SEND_CUSTOM_GAMELIST( string username, string gamename, string owner, uint32_t slotsTaken, uint32_t slotsTotal )
{
BYTEARRAY packet;
packet.push_back( 3 );
packet.push_back( 4 );
UTIL_AppendByteArrayFast( packet, username );
UTIL_AppendByteArrayFast( packet, gamename );
UTIL_AppendByteArrayFast( packet, owner );
UTIL_AppendByteArray( packet, slotsTaken, false );
UTIL_AppendByteArray( packet, slotsTotal, false );
return packet;
}
BYTEARRAY CGameProtocol :: SEND_W3GS_CREATEGAME( bool TFT, unsigned char war3Version )
{
unsigned char ProductID_ROC[] = { 51, 82, 65, 87 }; // "WAR3"
unsigned char ProductID_TFT[] = { 80, 88, 51, 87 }; // "W3XP"
unsigned char Version[] = { war3Version, 0, 0, 0 };
unsigned char HostCounter[] = { 1, 0, 0, 0 };
BYTEARRAY packet;
packet.push_back( W3GS_HEADER_CONSTANT ); // W3GS header constant
packet.push_back( W3GS_CREATEGAME ); // W3GS_CREATEGAME
packet.push_back( 0 ); // packet length will be assigned later
packet.push_back( 0 ); // packet length will be assigned later
if( TFT )
UTIL_AppendByteArray( packet, ProductID_TFT, 4 ); // Product ID (TFT)
else
UTIL_AppendByteArray( packet, ProductID_ROC, 4 ); // Product ID (ROC)
UTIL_AppendByteArray( packet, Version, 4 ); // Version
UTIL_AppendByteArray( packet, HostCounter, 4 ); // Host Counter
AssignLength( packet );
// DEBUG_Print( "SENT W3GS_CREATEGAME" );
// DEBUG_Print( packet );
return packet;
}
BYTEARRAY CGameProtocol :: SEND_W3GS_REFRESHGAME( uint32_t players, uint32_t playerSlots )
{
unsigned char HostCounter[] = { 1, 0, 0, 0 };
BYTEARRAY packet;
packet.push_back( W3GS_HEADER_CONSTANT ); // W3GS header constant
packet.push_back( W3GS_REFRESHGAME ); // W3GS_REFRESHGAME
packet.push_back( 0 ); // packet length will be assigned later
packet.push_back( 0 ); // packet length will be assigned later
UTIL_AppendByteArray( packet, HostCounter, 4 ); // Host Counter
UTIL_AppendByteArray( packet, players, false ); // Players
UTIL_AppendByteArray( packet, playerSlots, false ); // Player Slots
AssignLength( packet );
// DEBUG_Print( "SENT W3GS_REFRESHGAME" );
// DEBUG_Print( packet );
return packet;
}
BYTEARRAY CGameProtocol :: SEND_W3GS_DECREATEGAME( )
{
unsigned char HostCounter[] = { 1, 0, 0, 0 };
BYTEARRAY packet;
packet.push_back( W3GS_HEADER_CONSTANT ); // W3GS header constant
packet.push_back( W3GS_DECREATEGAME ); // W3GS_DECREATEGAME
packet.push_back( 0 ); // packet length will be assigned later
packet.push_back( 0 ); // packet length will be assigned later
UTIL_AppendByteArray( packet, HostCounter, 4 ); // Host Counter
AssignLength( packet );
// DEBUG_Print( "SENT W3GS_DECREATEGAME" );
// DEBUG_Print( packet );
return packet;
}
BYTEARRAY CGameProtocol :: SEND_W3GS_MAPCHECK( string mapPath, BYTEARRAY mapSize, BYTEARRAY mapInfo, BYTEARRAY mapCRC, BYTEARRAY mapSHA1 )
{
unsigned char Unknown[] = { 1, 0, 0, 0 };
BYTEARRAY packet;
if( !mapPath.empty( ) && mapSize.size( ) == 4 && mapInfo.size( ) == 4 && mapCRC.size( ) == 4 && mapSHA1.size( ) == 20 )
{
packet.push_back( W3GS_HEADER_CONSTANT ); // W3GS header constant
packet.push_back( W3GS_MAPCHECK ); // W3GS_MAPCHECK
packet.push_back( 0 ); // packet length will be assigned later
packet.push_back( 0 ); // packet length will be assigned later
UTIL_AppendByteArray( packet, Unknown, 4 ); // ???
UTIL_AppendByteArrayFast( packet, mapPath ); // map path
UTIL_AppendByteArrayFast( packet, mapSize ); // map size
UTIL_AppendByteArrayFast( packet, mapInfo ); // map info
UTIL_AppendByteArrayFast( packet, mapCRC ); // map crc
UTIL_AppendByteArrayFast( packet, mapSHA1 ); // map sha1
AssignLength( packet );
}
else
CONSOLE_Print( "[GAMEPROTO] invalid parameters passed to SEND_W3GS_MAPCHECK" );
// DEBUG_Print( "SENT W3GS_MAPCHECK" );
// DEBUG_Print( packet );
return packet;
}
BYTEARRAY CGameProtocol :: SEND_W3GS_STARTDOWNLOAD( unsigned char fromPID )
{
unsigned char Unknown[] = { 1, 0, 0, 0 };
BYTEARRAY packet;
packet.push_back( W3GS_HEADER_CONSTANT ); // W3GS header constant
packet.push_back( W3GS_STARTDOWNLOAD ); // W3GS_STARTDOWNLOAD
packet.push_back( 0 ); // packet length will be assigned later
packet.push_back( 0 ); // packet length will be assigned later
UTIL_AppendByteArray( packet, Unknown, 4 ); // ???
packet.push_back( fromPID ); // from PID
AssignLength( packet );
// DEBUG_Print( "SENT W3GS_STARTDOWNLOAD" );
// DEBUG_Print( packet );
return packet;
}
BYTEARRAY CGameProtocol :: SEND_W3GS_MAPPART( unsigned char fromPID, unsigned char toPID, uint32_t start, string *mapData )
{
unsigned char Unknown[] = { 1, 0, 0, 0 };
BYTEARRAY packet;
if( start < mapData->size( ) )
{
packet.push_back( W3GS_HEADER_CONSTANT ); // W3GS header constant
packet.push_back( W3GS_MAPPART ); // W3GS_MAPPART
packet.push_back( 0 ); // packet length will be assigned later
packet.push_back( 0 ); // packet length will be assigned later
packet.push_back( toPID ); // to PID
packet.push_back( fromPID ); // from PID
UTIL_AppendByteArray( packet, Unknown, 4 ); // ???
UTIL_AppendByteArray( packet, start, false ); // start position
// calculate end position (don't send more than 1442 map bytes in one packet)
uint32_t End = start + 1442;
if( End > mapData->size( ) )
End = mapData->size( );
// calculate crc
BYTEARRAY crc32 = UTIL_CreateByteArray( m_GHost->m_CRC->FullCRC( (unsigned char *)mapData->c_str( ) + start, End - start ), false );
UTIL_AppendByteArrayFast( packet, crc32 );
// map data
BYTEARRAY Data = UTIL_CreateByteArray( (unsigned char *)mapData->c_str( ) + start, End - start );
UTIL_AppendByteArrayFast( packet, Data );
AssignLength( packet );
}
else
CONSOLE_Print( "[GAMEPROTO] invalid parameters passed to SEND_W3GS_MAPPART" );
// DEBUG_Print( "SENT W3GS_MAPPART" );
// DEBUG_Print( packet );
return packet;
}
BYTEARRAY CGameProtocol :: SEND_W3GS_INCOMING_ACTION2( queue<CIncomingAction *> actions )
{
BYTEARRAY packet;
packet.push_back( W3GS_HEADER_CONSTANT ); // W3GS header constant
packet.push_back( W3GS_INCOMING_ACTION2 ); // W3GS_INCOMING_ACTION2
packet.push_back( 0 ); // packet length will be assigned later
packet.push_back( 0 ); // packet length will be assigned later
packet.push_back( 0 ); // ??? (send interval?)
packet.push_back( 0 ); // ??? (send interval?)
// create subpacket
if( !actions.empty( ) )
{
BYTEARRAY subpacket;
while( !actions.empty( ) )
{
CIncomingAction *Action = actions.front( );
actions.pop( );
subpacket.push_back( Action->GetPID( ) );
UTIL_AppendByteArray( subpacket, (uint16_t)Action->GetAction( )->size( ), false );
UTIL_AppendByteArrayFast( subpacket, *Action->GetAction( ) );
}
// calculate crc (we only care about the first 2 bytes though)
BYTEARRAY crc32 = UTIL_CreateByteArray( m_GHost->m_CRC->FullCRC( (unsigned char *)string( subpacket.begin( ), subpacket.end( ) ).c_str( ), subpacket.size( ) ), false );
crc32.resize( 2 );
// finish subpacket
UTIL_AppendByteArrayFast( packet, crc32 ); // crc
UTIL_AppendByteArrayFast( packet, subpacket ); // subpacket
}
AssignLength( packet );
// DEBUG_Print( "SENT W3GS_INCOMING_ACTION2" );
// DEBUG_Print( packet );
return packet;
}
/////////////////////
// OTHER FUNCTIONS //
/////////////////////
bool CGameProtocol :: AssignLength( BYTEARRAY &content )
{
// insert the actual length of the content array into bytes 3 and 4 (indices 2 and 3)
BYTEARRAY LengthBytes;
if( content.size( ) >= 4 && content.size( ) <= 65535 )
{
LengthBytes = UTIL_CreateByteArray( (uint16_t)content.size( ), false );
content[2] = LengthBytes[0];
content[3] = LengthBytes[1];
return true;
}
return false;
}
bool CGameProtocol :: ValidateLength( BYTEARRAY &content )
{
// verify that bytes 3 and 4 (indices 2 and 3) of the content array describe the length
uint16_t Length;
BYTEARRAY LengthBytes;
if( content.size( ) >= 4 && content.size( ) <= 65535 )
{
LengthBytes.push_back( content[2] );
LengthBytes.push_back( content[3] );
Length = UTIL_ByteArrayToUInt16( LengthBytes, false );
if( Length == content.size( ) )
return true;
}
return false;
}
BYTEARRAY CGameProtocol :: EncodeSlotInfo( vector<CGameSlot> &slots, uint32_t randomSeed, unsigned char layoutStyle, unsigned char playerSlots )
{
BYTEARRAY SlotInfo;
SlotInfo.push_back( (unsigned char)slots.size( ) ); // number of slots
for( unsigned int i = 0; i < slots.size( ); ++i )
UTIL_AppendByteArray( SlotInfo, slots[i].GetByteArray( ) );
UTIL_AppendByteArray( SlotInfo, randomSeed, false ); // random seed
SlotInfo.push_back( layoutStyle ); // LayoutStyle (0 = melee, 1 = custom forces, 3 = custom forces + fixed player settings)
SlotInfo.push_back( playerSlots ); // number of player slots (non observer)
return SlotInfo;
}
//
// CIncomingJoinPlayer
//
CIncomingJoinPlayer :: CIncomingJoinPlayer( uint32_t nHostCounter, uint32_t nEntryKey, string nName, BYTEARRAY &nInternalIP ) : m_HostCounter( nHostCounter ), m_EntryKey( nEntryKey ), m_Name( nName ), m_InternalIP( nInternalIP )
{
}
CIncomingJoinPlayer :: ~CIncomingJoinPlayer( )
{
}
//
// CIncomingAction
//
CIncomingAction :: CIncomingAction( unsigned char nPID, BYTEARRAY &nCRC, BYTEARRAY &nAction ) : m_PID( nPID ), m_CRC( nCRC ), m_Action( nAction )
{
}