/
bncs.go
100 lines (93 loc) · 3.64 KB
/
bncs.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
// Author: Niels A.D.
// Project: gowarcraft3 (https://github.com/nielsAD/gowarcraft3)
// License: Mozilla Public License, v2.0
// Package bncs implements the old Battle.net chat protocol for Warcraft III.
// The protocol is used for multiple classic games, but this package only
// implements the small set of packets required for Warcraft III to log
// on and enter chat.
//
// Based on protocol documentation by https://bnetdocs.org/
//
// Each packet type is mapped to a struct type that implements the Packet
// interface. To deserialize from a binary stream, use bncs.Read().
//
// This package tries to keep ammortized heap memory allocations to 0.
//
// General serialization format:
//
// (UINT8) Protocol signature (0xFF)
// (UINT8) Packet type ID
// (UINT16) Packet size
// [Packet Data]
//
package bncs
import (
"github.com/nielsAD/gowarcraft3/protocol"
"github.com/nielsAD/gowarcraft3/protocol/w3gs"
)
// Packet interface.
type Packet interface {
Serialize(buf *protocol.Buffer, enc *Encoding) error
Deserialize(buf *protocol.Buffer, enc *Encoding) error
}
// Encoding options for (de)serialization
type Encoding struct {
w3gs.Encoding
// Assume request when deserializing ambiguous packet IDs
Request bool
}
// DefaultFactory maps packet IDs to matching type
var DefaultFactory = MapFactory{
PidNull: func(_ *Encoding) Packet { return &KeepAlive{} },
PidStopAdv: func(_ *Encoding) Packet { return &StopAdv{} },
PidJoinChannel: func(_ *Encoding) Packet { return &JoinChannel{} },
PidChatCommand: func(_ *Encoding) Packet { return &ChatCommand{} },
PidChatEvent: func(_ *Encoding) Packet { return &ChatEvent{} },
PidFloodDetected: func(_ *Encoding) Packet { return &FloodDetected{} },
PidMessageBox: func(_ *Encoding) Packet { return &MessageBox{} },
PidNotifyJoin: func(_ *Encoding) Packet { return &NotifyJoin{} },
PidPing: func(_ *Encoding) Packet { return &Ping{} },
PidNetGamePort: func(_ *Encoding) Packet { return &NetGamePort{} },
PidSetEmail: func(_ *Encoding) Packet { return &SetEmail{} },
PidClanInfo: func(_ *Encoding) Packet { return &ClanInfo{} },
PidGetAdvListEx: ReqResp(
func(_ *Encoding) Packet { return &GetAdvListReq{} },
func(_ *Encoding) Packet { return &GetAdvListResp{} },
),
PidEnterChat: ReqResp(
func(_ *Encoding) Packet { return &EnterChatReq{} },
func(_ *Encoding) Packet { return &EnterChatResp{} },
),
PidStartAdvex3: ReqResp(
func(_ *Encoding) Packet { return &StartAdvex3Req{} },
func(_ *Encoding) Packet { return &StartAdvex3Resp{} },
),
PidAuthInfo: ReqResp(
func(_ *Encoding) Packet { return &AuthInfoReq{} },
func(_ *Encoding) Packet { return &AuthInfoResp{} },
),
PidAuthCheck: ReqResp(
func(_ *Encoding) Packet { return &AuthCheckReq{} },
func(_ *Encoding) Packet { return &AuthCheckResp{} },
),
PidAuthAccountCreate: ReqResp(
func(_ *Encoding) Packet { return &AuthAccountCreateReq{} },
func(_ *Encoding) Packet { return &AuthAccountCreateResp{} },
),
PidAuthAccountLogon: ReqResp(
func(_ *Encoding) Packet { return &AuthAccountLogonReq{} },
func(_ *Encoding) Packet { return &AuthAccountLogonResp{} },
),
PidAuthAccountLogonProof: ReqResp(
func(_ *Encoding) Packet { return &AuthAccountLogonProofReq{} },
func(_ *Encoding) Packet { return &AuthAccountLogonProofResp{} },
),
PidAuthAccountChange: ReqResp(
func(_ *Encoding) Packet { return &AuthAccountChangePassReq{} },
func(_ *Encoding) Packet { return &AuthAccountChangePassResp{} },
),
PidAuthAccountChangeProof: ReqResp(
func(_ *Encoding) Packet { return &AuthAccountChangePassProofReq{} },
func(_ *Encoding) Packet { return &AuthAccountChangePassProofResp{} },
),
}