/
packets.go
2643 lines (2294 loc) · 63.8 KB
/
packets.go
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
// Author: Niels A.D.
// Project: gowarcraft3 (https://github.com/nielsAD/gowarcraft3)
// License: Mozilla Public License, v2.0
package w3gs
import (
"hash/crc32"
"io"
"math"
"go.dedis.ch/protobuf"
"github.com/nielsAD/gowarcraft3/protocol"
)
func readPacketHeader(buf *protocol.Buffer) (byte, int) {
var bsize = buf.Size()
if bsize < 4 {
return 0, -1
}
buf.Skip(1)
var pid = buf.ReadUInt8()
var psize = int(buf.ReadUInt16())
if psize > bsize {
return pid, -1
}
return pid, psize
}
func readPacketSize(buf *protocol.Buffer) int {
var _, psize = readPacketHeader(buf)
return psize
}
// UnknownPacket is used to store unknown packets.
type UnknownPacket struct {
ID byte
Blob []byte
}
// Serialize encodes the struct into its binary form.
func (pkt *UnknownPacket) Serialize(buf *protocol.Buffer, enc *Encoding) error {
buf.WriteUInt8(ProtocolSig)
buf.WriteUInt8(pkt.ID)
buf.WriteUInt16(uint16(4 + len(pkt.Blob)))
buf.WriteBlob(pkt.Blob)
return nil
}
// Deserialize decodes the binary data generated by Serialize.
func (pkt *UnknownPacket) Deserialize(buf *protocol.Buffer, enc *Encoding) error {
var pid, size = readPacketHeader(buf)
if size < 4 {
return ErrInvalidPacketSize
}
pkt.ID = pid
pkt.Blob = append(pkt.Blob[:0], buf.ReadBlob(size-4)...)
return nil
}
// Ping implements the [0x01] W3GS_PING_FROM_HOST packet (S -> C).
//
// This is sent every 30 seconds to make sure that the client is still responsive.
//
// Format:
//
// (UINT32) Ping (GetTickCount)
//
type Ping struct {
Payload uint32
}
// Serialize encodes the struct into its binary form.
func (pkt *Ping) Serialize(buf *protocol.Buffer, enc *Encoding) error {
buf.WriteUInt8(ProtocolSig)
buf.WriteUInt8(PidPingFromHost)
buf.WriteUInt16(8)
buf.WriteUInt32(pkt.Payload)
return nil
}
// Deserialize decodes the binary data generated by Serialize.
func (pkt *Ping) Deserialize(buf *protocol.Buffer, enc *Encoding) error {
if readPacketSize(buf) != 8 {
return ErrInvalidPacketSize
}
pkt.Payload = buf.ReadUInt32()
return nil
}
// Pong implements the [0x46] W3GS_PONG_TO_HOST packet (C -> S).
//
// This is sent in response to 0x01 W3GS_PING_FROM_HOST.
//
// 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)
//
// Format:
//
// (UINT32) Pong (copy of Ping payload)
//
type Pong struct {
Ping
}
// Serialize encodes the struct into its binary form.
func (pkt *Pong) Serialize(buf *protocol.Buffer, enc *Encoding) error {
var start = buf.Size()
if err := pkt.Ping.Serialize(buf, enc); err != nil {
return err
}
buf.WriteUInt8At(start+1, PidPongToHost)
return nil
}
// PeerPing implements the [0x35] W3GS_PING_FROM_OTHERS packet (C -> C).
//
// Client requests an echo from another client (occurs every 10 seconds).
//
// Format:
//
// (UINT32) Ping
// (UINT32) Peer set (see [0x37] W3GS_ClientInfo)
// (UINT32) Game ticks
//
type PeerPing struct {
Payload uint32
PeerSet protocol.BitSet32
GameTicks uint32
}
// Serialize encodes the struct into its binary form.
func (pkt *PeerPing) Serialize(buf *protocol.Buffer, enc *Encoding) error {
buf.WriteUInt8(ProtocolSig)
buf.WriteUInt8(PidPingFromOthers)
buf.WriteUInt16(16)
buf.WriteUInt32(pkt.Payload)
buf.WriteUInt32(uint32(pkt.PeerSet))
buf.WriteUInt32(pkt.GameTicks)
return nil
}
// Deserialize decodes the binary data generated by Serialize.
func (pkt *PeerPing) Deserialize(buf *protocol.Buffer, enc *Encoding) error {
if readPacketSize(buf) != 16 {
return ErrInvalidPacketSize
}
pkt.Payload = buf.ReadUInt32()
pkt.PeerSet = protocol.BitSet32(buf.ReadUInt32())
pkt.GameTicks = buf.ReadUInt32()
return nil
}
// PeerPong implements the [0x36] W3GS_PONG_TO_OTHERS packet (C -> C).
//
// This is sent in response to an echo from another client.
//
// Format:
//
// (UINT32) Pong (copy of Ping payload)
//
type PeerPong struct {
Ping
}
// Serialize encodes the struct into its binary form.
func (pkt *PeerPong) Serialize(buf *protocol.Buffer, enc *Encoding) error {
var start = buf.Size()
if err := pkt.Ping.Serialize(buf, enc); err != nil {
return err
}
buf.WriteUInt8At(start+1, PidPongToOthers)
return nil
}
// Join implements the [0x1E] W3GS_ReqJoin packet (C -> S).
//
// A client sends this to the host to enter the game lobby.
//
// The internal IP uses the Windows in_addr structure which is thus used by the sockaddr_in structure.
//
// Format:
//
// (UINT32) Host Counter (Game ID)
// (UINT32) Entry Key (used in LAN)
// (UINT8) Unknown (0x00)
// (UINT16) Listen Port
// (UINT32) Join counter
// (STRING) Player name
// (UINT8) Number of bytes that follow (0x01 < 1.29; 0x02 >= 1.29)
// (UINT8)[] Unknown (0x00)
// (UINT16) AF_INET (0x02)
// (UINT16) Internal Port
// (UINT32) Internal IP
// (UINT32) Unknown (0x00)
// (UINT32) Unknown (0x00)
//
type Join struct {
HostCounter uint32
EntryKey uint32
ListenPort uint16
JoinCounter uint32
PlayerName string
InternalAddr protocol.SockAddr
}
// Serialize encodes the struct into its binary form.
func (pkt *Join) Serialize(buf *protocol.Buffer, enc *Encoding) error {
buf.WriteUInt8(ProtocolSig)
buf.WriteUInt8(PidReqJoin)
buf.WriteUInt16(uint16(39 + len(pkt.PlayerName)))
buf.WriteUInt32(pkt.HostCounter)
buf.WriteUInt32(pkt.EntryKey)
buf.WriteUInt8(0)
buf.WriteUInt16(pkt.ListenPort)
buf.WriteUInt32(pkt.JoinCounter)
buf.WriteCString(pkt.PlayerName)
buf.WriteUInt8(2)
buf.WriteUInt8(0)
buf.WriteUInt8(0)
if err := buf.WriteSockAddr(&pkt.InternalAddr); err != nil {
return err
}
return nil
}
// Deserialize decodes the binary data generated by Serialize.
func (pkt *Join) Deserialize(buf *protocol.Buffer, enc *Encoding) error {
var size = readPacketSize(buf)
if size < 37 {
return ErrInvalidPacketSize
}
pkt.HostCounter = buf.ReadUInt32()
pkt.EntryKey = buf.ReadUInt32()
if buf.ReadUInt8() != 0 {
return ErrUnexpectedConst
}
pkt.ListenPort = buf.ReadUInt16()
pkt.JoinCounter = buf.ReadUInt32()
var err error
if pkt.PlayerName, err = buf.ReadCString(); err != nil {
return err
}
if size < 37+len(pkt.PlayerName) {
return ErrInvalidPacketSize
}
var skip = int(buf.ReadUInt8())
if size != 37+len(pkt.PlayerName)+skip {
return ErrInvalidPacketSize
}
buf.Skip(skip)
if pkt.InternalAddr, err = buf.ReadSockAddr(); err != nil {
return err
}
return nil
}
// RejectJoin implements the [0x05] W3GS_RejectJoin packet (S -> C).
//
// This is sent in a response to a request to join the game lobby and indicates that the request was denied.
//
// Reason:
//
// 0x09 RejectJoin_FULL The game lobby is full
// 0x07 RejectJoin_INVALID There was an error in your request
// 0x10 RejectJoin_STARTED The game has already been started
// 0x27 RejectJoin_WRONGPASS The password you sent was incorrect
//
// Format:
//
// (UINT32) Reason
//
type RejectJoin struct {
Reason RejectReason
}
// Serialize encodes the struct into its binary form.
func (pkt *RejectJoin) Serialize(buf *protocol.Buffer, enc *Encoding) error {
buf.WriteUInt8(ProtocolSig)
buf.WriteUInt8(PidRejectJoin)
buf.WriteUInt16(8)
buf.WriteUInt32(uint32(pkt.Reason))
return nil
}
// Deserialize decodes the binary data generated by Serialize.
func (pkt *RejectJoin) Deserialize(buf *protocol.Buffer, enc *Encoding) error {
if readPacketSize(buf) != 8 {
return ErrInvalidPacketSize
}
pkt.Reason = RejectReason(buf.ReadUInt32())
return nil
}
// SlotInfoJoin implements the [0x04] W3GS_SlotInfoJoin packet (S -> C).
//
// This is sent to tell the client about the game slots, upon entry of the lobby.
//
// Format:
//
// Embedded [0x09] W3GS_SlotInfo:
// (UINT16) Length of Slot data
// (UINT8) Number of slots
// (UINT8)[] Slot data
// (UINT32) Random seed
// (UINT8) Slots layout
// (UINT8) Number of player slots without observers
//
// (UINT8) Player number
// (UINT16) AF_INET (0x02)
// (UINT16) Port
// (UINT32) External IP
// (UINT32) Unknown (0x00)
// (UINT32) Unknown (0x00)
//
type SlotInfoJoin struct {
SlotInfo
PlayerID uint8
ExternalAddr protocol.SockAddr
}
// Serialize encodes the struct into its binary form.
func (pkt *SlotInfoJoin) Serialize(buf *protocol.Buffer, enc *Encoding) error {
buf.WriteUInt8(ProtocolSig)
buf.WriteUInt8(PidSlotInfoJoin)
buf.WriteUInt16(uint16(30 + len(pkt.Slots)*9))
pkt.SlotInfo.SerializeContent(buf, enc)
buf.WriteUInt8(pkt.PlayerID)
if err := buf.WriteSockAddr(&pkt.ExternalAddr); err != nil {
return err
}
return nil
}
// Deserialize decodes the binary data generated by Serialize.
func (pkt *SlotInfoJoin) Deserialize(buf *protocol.Buffer, enc *Encoding) error {
var size = readPacketSize(buf)
if size < 23 {
return ErrInvalidPacketSize
}
if err := pkt.SlotInfo.DeserializeContent(buf, enc); err != nil {
return err
}
if size != 30+len(pkt.Slots)*9 && !(size == 23 && len(pkt.Slots) == 0) {
return ErrInvalidPacketSize
}
pkt.PlayerID = buf.ReadUInt8()
var err error
if pkt.ExternalAddr, err = buf.ReadSockAddr(); err != nil {
return err
}
return nil
}
// SlotInfo implements the [0x09] W3GS_SlotInfo packet (S -> C).
//
// This is sent for slot updates.
//
// The length of slot info should always be `0x0B`.
//
// Format:
//
// (UINT16) Length of Slot data
// (UINT8) Number of slots
// (UINT8)[] Slot data
// (UINT32) Random seed (GetTickCount)
// (UINT8) Slots layout
// (UINT8) Number of player slots without observers
//
// For each slot:
// (UINT8) Player number
// (UINT8) Download status
// (UINT8) Slot status
// (UINT8) Computer status
// (UINT8) Team
// (UINT8) Color
// (UINT8) Race
// (UINT8) Computer type
// (UINT8) Handicap
//
type SlotInfo struct {
Slots []SlotData
RandomSeed uint32
SlotLayout SlotLayout
NumPlayers uint8
}
// SlotData stores the info for a single game slot.
//
// Download status:
//
// - This is a percentage of their download progress. As such, this value can only be between 0 and 100.
//
// Slot status:
//
// 0x00 Open
// 0x01 Closed
// 0x02 Occupied
//
// Computer status:
//
// 0x00 Human
// 0x01 Computer
//
// Race:
//
// 0x01 Human
// 0x02 Orc
// 0x04 Night Elf
// 0x08 Undead
// 0x20 Random
// 0x40 Selectable
//
// Computer type:
//
// 0x00 Easy
// 0x01 Normal / Human
// 0x02 Hard
//
// Format:
//
// (UINT8) Player number
// (UINT8) Download status
// (UINT8) Slot status
// (UINT8) Computer status
// (UINT8) Team
// (UINT8) Color
// (UINT8) Race
// (UINT8) Computer type
// (UINT8) Handicap
//
type SlotData struct {
PlayerID uint8
DownloadStatus uint8
SlotStatus SlotStatus
Computer bool
Team uint8
Color uint8
Race RacePref
ComputerType AI
Handicap uint8
}
// Serialize encodes the struct into its binary form.
func (pkt *SlotInfo) Serialize(buf *protocol.Buffer, enc *Encoding) error {
buf.WriteUInt8(ProtocolSig)
buf.WriteUInt8(PidSlotInfo)
buf.WriteUInt16(uint16(13 + len(pkt.Slots)*9))
pkt.SerializeContent(buf, enc)
return nil
}
// Deserialize decodes the binary data generated by Serialize.
func (pkt *SlotInfo) Deserialize(buf *protocol.Buffer, enc *Encoding) error {
var size = readPacketSize(buf)
if size < 13 {
return ErrInvalidPacketSize
}
if err := pkt.DeserializeContent(buf, enc); err != nil {
return err
}
if size != 13+len(pkt.Slots)*9 {
return ErrInvalidPacketSize
}
return nil
}
// SerializeContent encodes the struct into its binary form without packet ID.
func (pkt *SlotInfo) SerializeContent(buf *protocol.Buffer, enc *Encoding) {
buf.WriteUInt16(uint16(7 + len(pkt.Slots)*9))
buf.WriteUInt8(uint8(len(pkt.Slots)))
for i := 0; i < len(pkt.Slots); i++ {
buf.WriteUInt8(pkt.Slots[i].PlayerID)
buf.WriteUInt8(pkt.Slots[i].DownloadStatus)
buf.WriteUInt8(uint8(pkt.Slots[i].SlotStatus))
buf.WriteBool8(pkt.Slots[i].Computer)
buf.WriteUInt8(pkt.Slots[i].Team)
buf.WriteUInt8(pkt.Slots[i].Color)
buf.WriteUInt8(uint8(pkt.Slots[i].Race))
buf.WriteUInt8(uint8(pkt.Slots[i].ComputerType))
buf.WriteUInt8(uint8(pkt.Slots[i].Handicap))
}
buf.WriteUInt32(pkt.RandomSeed)
buf.WriteUInt8(uint8(pkt.SlotLayout))
buf.WriteUInt8(pkt.NumPlayers)
}
// DeserializeContent decodes the binary data generated by SerializeContent.
func (pkt *SlotInfo) DeserializeContent(buf *protocol.Buffer, enc *Encoding) error {
var dataSize = int(buf.ReadUInt16())
if dataSize == 0 {
pkt.Slots = pkt.Slots[:0]
pkt.RandomSeed = 0
pkt.SlotLayout = 0
pkt.NumPlayers = 0
return nil
}
if buf.Size() < dataSize {
return ErrInvalidPacketSize
}
var numSlots = int(buf.ReadUInt8())
var slotSize = 9
if numSlots > 0 {
slotSize = (dataSize - 7) / numSlots
}
if dataSize != 7+numSlots*slotSize {
return ErrInvalidPacketSize
}
if cap(pkt.Slots) < numSlots {
pkt.Slots = make([]SlotData, 0, numSlots)
}
pkt.Slots = pkt.Slots[:numSlots]
for i := 0; i < len(pkt.Slots); i++ {
pkt.Slots[i].PlayerID = buf.ReadUInt8()
pkt.Slots[i].DownloadStatus = buf.ReadUInt8()
pkt.Slots[i].SlotStatus = SlotStatus(buf.ReadUInt8())
pkt.Slots[i].Computer = buf.ReadBool8()
pkt.Slots[i].Team = buf.ReadUInt8()
pkt.Slots[i].Color = buf.ReadUInt8()
pkt.Slots[i].Race = RacePref(buf.ReadUInt8())
if slotSize > 7 {
pkt.Slots[i].ComputerType = AI(buf.ReadUInt8())
} else {
pkt.Slots[i].ComputerType = ComputerNormal
}
if slotSize > 8 {
pkt.Slots[i].Handicap = buf.ReadUInt8()
} else {
pkt.Slots[i].Handicap = 100
}
}
pkt.RandomSeed = buf.ReadUInt32()
pkt.SlotLayout = SlotLayout(buf.ReadUInt8())
pkt.NumPlayers = buf.ReadUInt8()
return nil
}
// PlayerInfo implements the [0x06] W3GS_PlayerInfo packet (S -> C).
//
// Tells a client about a player'buf information.
//
// The external and internal IP are always zero for the host.
//
// Format:
//
// (UINT32) Join Counter
// (UINT8) Player number
// (STRING) Player name
// (UINT8) Number of bytes that follow (0x01)
// (UINT8)[] Unknown (0x00)
// (UINT16) AF_INET (0x02)
// (UINT16) Port
// (UINT32) External IP
// (UINT32) Unknown (0x00)
// (UINT32) Unknown (0x00)
// (UINT16) AF_INET (0x02)
// (UINT16) Port
// (UINT32) Internal IP
// (UINT32) Unknown (0x00)
// (UINT32) Unknown (0x00)
//
type PlayerInfo struct {
JoinCounter uint32
PlayerID uint8
PlayerName string
ExternalAddr protocol.SockAddr
InternalAddr protocol.SockAddr
}
// Serialize encodes the struct into its binary form.
func (pkt *PlayerInfo) Serialize(buf *protocol.Buffer, enc *Encoding) error {
buf.WriteUInt8(ProtocolSig)
buf.WriteUInt8(PidPlayerInfo)
buf.WriteUInt16(uint16(44 + len(pkt.PlayerName)))
buf.WriteUInt32(pkt.JoinCounter)
buf.WriteUInt8(pkt.PlayerID)
buf.WriteCString(pkt.PlayerName)
buf.WriteUInt8(1)
buf.WriteUInt8(0)
if err := buf.WriteSockAddr(&pkt.ExternalAddr); err != nil {
return err
}
if err := buf.WriteSockAddr(&pkt.InternalAddr); err != nil {
return err
}
return nil
}
// Deserialize decodes the binary data generated by Serialize.
func (pkt *PlayerInfo) Deserialize(buf *protocol.Buffer, enc *Encoding) error {
var size = readPacketSize(buf)
if size < 43 {
return ErrInvalidPacketSize
}
pkt.JoinCounter = buf.ReadUInt32()
pkt.PlayerID = buf.ReadUInt8()
var err error
if pkt.PlayerName, err = buf.ReadCString(); err != nil {
return err
}
if size < 43+len(pkt.PlayerName) {
return ErrInvalidPacketSize
}
var skip = int(buf.ReadUInt8())
if size != 43+len(pkt.PlayerName)+skip {
return ErrInvalidPacketSize
}
buf.Skip(skip)
if pkt.ExternalAddr, err = buf.ReadSockAddr(); err != nil {
return err
}
if pkt.InternalAddr, err = buf.ReadSockAddr(); err != nil {
return err
}
return nil
}
// Leave implements the [0x21] W3GS_LeaveReq packet (C -> S).
//
// A client requests to leave.
//
// Reason:
//
// 0x01 PLAYERLEAVE_DISCONNECT
// 0x07 PLAYERLEAVE_LOST
// 0x08 PLAYERLEAVE_LOSTBUILDINGS
// 0x09 PLAYERLEAVE_WON
// 0x0A PLAYERLEAVE_DRAW
// 0x0B PLAYERLEAVE_OBSERVER
// 0x0D PLAYERLEAVE_LOBBY
//
// Format:
//
// (UINT32) Reason
//
type Leave struct {
Reason LeaveReason
}
// Serialize encodes the struct into its binary form.
func (pkt *Leave) Serialize(buf *protocol.Buffer, enc *Encoding) error {
buf.WriteUInt8(ProtocolSig)
buf.WriteUInt8(PidLeaveReq)
buf.WriteUInt16(8)
buf.WriteUInt32(uint32(pkt.Reason))
return nil
}
// Deserialize decodes the binary data generated by Serialize.
func (pkt *Leave) Deserialize(buf *protocol.Buffer, enc *Encoding) error {
if readPacketSize(buf) != 8 {
return ErrInvalidPacketSize
}
pkt.Reason = LeaveReason(buf.ReadUInt32())
return nil
}
// PlayerKicked implements the [0x1C] W3GS_PlayerKicked packet (S -> C).
//
// The host has kicked the client from the lobby.
//
// Reason:
//
// 0x01 PLAYERLEAVE_DISCONNECT
// 0x07 PLAYERLEAVE_LOST
// 0x08 PLAYERLEAVE_LOSTBUILDINGS
// 0x09 PLAYERLEAVE_WON
// 0x0A PLAYERLEAVE_DRAW
// 0x0B PLAYERLEAVE_OBSERVER
// 0x0D PLAYERLEAVE_LOBBY
//
// Format:
//
// (UINT32) Reason
//
type PlayerKicked struct {
Leave
}
// Serialize encodes the struct into its binary form.
func (pkt *PlayerKicked) Serialize(buf *protocol.Buffer, enc *Encoding) error {
var start = buf.Size()
if err := pkt.Leave.Serialize(buf, enc); err != nil {
return err
}
buf.WriteUInt8At(start+1, PidPlayerKicked)
return nil
}
// LeaveAck implements the [0x1B] W3GS_LeaveAck packet (S -> C).
//
// This is the response to [0x21] W3GS_LeaveReq.
//
// You will leave the game once the connection is terminated.
//
// Format:
//
// [blank]
//
type LeaveAck struct {
// Empty
}
// Serialize encodes the struct into its binary form.
func (pkt *LeaveAck) Serialize(buf *protocol.Buffer, enc *Encoding) error {
buf.WriteUInt8(ProtocolSig)
buf.WriteUInt8(PidLeaveAck)
buf.WriteUInt16(4)
return nil
}
// Deserialize decodes the binary data generated by Serialize.
func (pkt *LeaveAck) Deserialize(buf *protocol.Buffer, enc *Encoding) error {
if readPacketSize(buf) != 4 {
return ErrInvalidPacketSize
}
return nil
}
// PlayerLeft implements the [0x07] W3GS_PlayerLeft packet (S -> C).
//
// This is received from the game host when a player leaves.
//
// Reason:
//
// 0x01 PLAYERLEAVE_DISCONNECT
// 0x07 PLAYERLEAVE_LOST
// 0x08 PLAYERLEAVE_LOSTBUILDINGS
// 0x09 PLAYERLEAVE_WON
// 0x0A PLAYERLEAVE_DRAW
// 0x0B PLAYERLEAVE_OBSERVER
// 0x0D PLAYERLEAVE_LOBBY
//
// Format:
//
// (UINT8) Player number
// (UINT32) Reason
//
type PlayerLeft struct {
PlayerID uint8
Reason LeaveReason
}
// Serialize encodes the struct into its binary form.
func (pkt *PlayerLeft) Serialize(buf *protocol.Buffer, enc *Encoding) error {
buf.WriteUInt8(ProtocolSig)
buf.WriteUInt8(PidPlayerLeft)
buf.WriteUInt16(9)
buf.WriteUInt8(pkt.PlayerID)
buf.WriteUInt32(uint32(pkt.Reason))
return nil
}
// Deserialize decodes the binary data generated by Serialize.
func (pkt *PlayerLeft) Deserialize(buf *protocol.Buffer, enc *Encoding) error {
if readPacketSize(buf) != 9 {
return ErrInvalidPacketSize
}
pkt.PlayerID = buf.ReadUInt8()
pkt.Reason = LeaveReason(buf.ReadUInt32())
return nil
}
// CountDownStart implements the [0x0A] W3GS_COUNTDOWN_START packet (S -> C).
//
// The game has begun the countdown to start.
//
// The official clients countdown from 5 seconds, however it is possible to use any time you wish. For example, the GHost++ bot uses 10 seconds when auto-hosted, but 5 seconds when started using an administrative command.
//
// Format:
//
// [blank]
//
type CountDownStart struct {
// Empty
}
// Serialize encodes the struct into its binary form.
func (pkt *CountDownStart) Serialize(buf *protocol.Buffer, enc *Encoding) error {
buf.WriteUInt8(ProtocolSig)
buf.WriteUInt8(PidCountDownStart)
buf.WriteUInt16(4)
return nil
}
// Deserialize decodes the binary data generated by Serialize.
func (pkt *CountDownStart) Deserialize(buf *protocol.Buffer, enc *Encoding) error {
if readPacketSize(buf) != 4 {
return ErrInvalidPacketSize
}
return nil
}
// CountDownEnd implements the [0x0B] W3GS_COUNTDOWN_END packet (S -> C).
//
// The game has finished the countdown and has now started. Players should see a loading screen for the map once this is received.
//
// [0x0A] W3GS_COUNTDOWN_START should be received before this packet, even if there is no countdown or if the countdown was 0 seconds.
//
// Format:
//
// [blank]
//
type CountDownEnd struct {
// Empty
}
// Serialize encodes the struct into its binary form.
func (pkt *CountDownEnd) Serialize(buf *protocol.Buffer, enc *Encoding) error {
buf.WriteUInt8(ProtocolSig)
buf.WriteUInt8(PidCountDownEnd)
buf.WriteUInt16(4)
return nil
}
// Deserialize decodes the binary data generated by Serialize.
func (pkt *CountDownEnd) Deserialize(buf *protocol.Buffer, enc *Encoding) error {
if readPacketSize(buf) != 4 {
return ErrInvalidPacketSize
}
return nil
}
// GameLoaded implements the [0x23] W3GS_GAMELOADED_SELF packet (C -> S).
//
// The client sends this to the host when they have finished loading the map.
//
// Format:
//
// [blank]
//
type GameLoaded struct {
// Empty
}
// Serialize encodes the struct into its binary form.
func (pkt *GameLoaded) Serialize(buf *protocol.Buffer, enc *Encoding) error {
buf.WriteUInt8(ProtocolSig)
buf.WriteUInt8(PidGameLoadedSelf)
buf.WriteUInt16(4)
return nil
}
// Deserialize decodes the binary data generated by Serialize.
func (pkt *GameLoaded) Deserialize(buf *protocol.Buffer, enc *Encoding) error {
if readPacketSize(buf) != 4 {
return ErrInvalidPacketSize
}
return nil
}
// PlayerLoaded implements the [0x08] W3GS_PlayerLoaded packet (S -> C).
//
// Sent to all other clients in-game to notify that a player has finished loading.
//
// Format:
//
// (UINT8) Player number
//
type PlayerLoaded struct {
PlayerID uint8
}
// Serialize encodes the struct into its binary form.
func (pkt *PlayerLoaded) Serialize(buf *protocol.Buffer, enc *Encoding) error {
buf.WriteUInt8(ProtocolSig)
buf.WriteUInt8(PidPlayerLoaded)
buf.WriteUInt16(5)
buf.WriteUInt8(pkt.PlayerID)
return nil
}
// Deserialize decodes the binary data generated by Serialize.
func (pkt *PlayerLoaded) Deserialize(buf *protocol.Buffer, enc *Encoding) error {
if readPacketSize(buf) != 5 {
return ErrInvalidPacketSize
}
pkt.PlayerID = buf.ReadUInt8()
return nil
}
// GameOver implements the [0x14] W3GS_GameOver packet (S -> C).
//
// Notify clients that the game has ended and the connection will be closed.
//
// Format:
//
// (UINT8) Player number
//
type GameOver struct {
PlayerID uint8
}
// Serialize encodes the struct into its binary form.
func (pkt *GameOver) Serialize(buf *protocol.Buffer, enc *Encoding) error {
buf.WriteUInt8(ProtocolSig)
buf.WriteUInt8(PidGameOver)
buf.WriteUInt16(5)
buf.WriteUInt8(pkt.PlayerID)
return nil
}
// Deserialize decodes the binary data generated by Serialize.
func (pkt *GameOver) Deserialize(buf *protocol.Buffer, enc *Encoding) error {
if readPacketSize(buf) != 5 {
return ErrInvalidPacketSize
}
pkt.PlayerID = buf.ReadUInt8()
return nil
}
// StartLag implements the [0x10] W3GS_START_LAG packet (S -> C).
//
// This is sent to the clients to initiate the lag screen.
//
// Format:
//
// (UINT8) Number of lagging players
//
// For every lagging player:
// (UINT8) Player number
// (UINT32) Lag duration in milliseconds
//
type StartLag struct {
Players []LagPlayer
}
// Serialize encodes the struct into its binary form.
func (pkt *StartLag) Serialize(buf *protocol.Buffer, enc *Encoding) error {
buf.WriteUInt8(ProtocolSig)
buf.WriteUInt8(PidStartLag)
buf.WriteUInt16(uint16(5 + len(pkt.Players)*5))
buf.WriteUInt8(uint8(len(pkt.Players)))
for i := 0; i < len(pkt.Players); i++ {
pkt.Players[i].SerializeContent(buf, enc)
}
return nil