-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
registry.go
223 lines (198 loc) · 6.29 KB
/
registry.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
package state
import (
"fmt"
"reflect"
"go.minekube.com/gate/pkg/edition/java/proto/version"
"go.minekube.com/gate/pkg/gate/proto"
)
// Registry stores server/client bound packets for a specific State.
type Registry struct {
State
ServerBound *PacketRegistry
ClientBound *PacketRegistry
}
// NewRegistry returns a new state registry.
func NewRegistry(state State) *Registry {
return &Registry{
State: state,
ServerBound: NewPacketRegistry(proto.ServerBound),
ClientBound: NewPacketRegistry(proto.ClientBound),
}
}
// PacketRegistry stores packets protocol versions sent to server or client.
type PacketRegistry struct {
Direction proto.Direction // The direction the registered packets are send to.
Protocols map[proto.Protocol]*ProtocolRegistry // The protocol versions.
// Whether to fallback to the minimum protocol version
// in case a protocol could not be found.
Fallback bool
}
func NewPacketRegistry(direction proto.Direction) *PacketRegistry {
r := &PacketRegistry{
Direction: direction,
Protocols: map[proto.Protocol]*ProtocolRegistry{},
Fallback: true, // fallback by default
}
for _, ver := range version.Versions {
if !version.Protocol(ver.Protocol).Legacy() && !version.Protocol(ver.Protocol).Unknown() {
r.Protocols[ver.Protocol] = &ProtocolRegistry{
Protocol: ver.Protocol,
PacketIDs: map[proto.PacketID]proto.PacketType{},
PacketTypes: map[proto.PacketType]proto.PacketID{},
}
}
}
return r
}
// ProtocolRegistry gets the ProtocolRegistry for a protocol.
func (p *PacketRegistry) ProtocolRegistry(protocol proto.Protocol) *ProtocolRegistry {
r := p.Protocols[protocol]
if r == nil && p.Fallback {
return p.ProtocolRegistry(version.MinimumVersion.Protocol)
}
return r // nil if not found
}
// ProtocolRegistry stores packets of a protocol version.
type ProtocolRegistry struct {
Protocol proto.Protocol // The protocol version of the registered packets.
PacketIDs map[proto.PacketID]proto.PacketType // Gets packet type by packet id.
PacketTypes map[proto.PacketType]proto.PacketID // Gets packet id by packet type.
}
// PacketID gets the packet id by the registered packet type.
func (r *ProtocolRegistry) PacketID(of proto.Packet) (id proto.PacketID, found bool) {
id, found = r.PacketTypes[proto.TypeOf(of)]
return
}
// CreatePacket returns a new zero valued instance of the type
// of the mapped packet id or nil if not found.
func (r *ProtocolRegistry) CreatePacket(id proto.PacketID) proto.Packet {
packetType, ok := r.PacketIDs[id]
if !ok {
return nil
}
p, ok := reflect.New(packetType).Interface().(proto.Packet)
if !ok {
// Shall never happen...
// Tried to create packet that does not implement Packet interface
return nil
}
return p
}
// Register registers a packet type with the corresponding protocol versions for each mapping.
func (p *PacketRegistry) Register(packetOf proto.Packet, mappings ...*PacketMapping) {
packetType := proto.TypeOf(packetOf)
var (
next *PacketMapping
from proto.Protocol
to proto.Protocol
)
for i, current := range mappings {
next = current
if i+1 < len(mappings) {
next = mappings[i+1]
}
from = current.Protocol
lastValid := current.LastValidProtocol
if lastValid != 0 {
if next != current {
panic("Cannot add a mapping after last valid mapping")
}
if from-lastValid > 0 {
panic("Last mapping version cannot be higher than highest mapping version")
}
}
if current == next {
if lastValid != 0 {
to = lastValid
} else {
to = version.MaximumVersion.Protocol
}
} else {
to = next.Protocol
}
lastInList := lastValid
if lastValid == 0 {
lastInList = version.MaximumVersion.Protocol
}
if from-to >= 0 && from != lastInList {
panic(fmt.Sprintf("Next mapping version (%s) should be lower then current (%s)", to, from))
}
versionRange(version.Versions, from, to, func(protocol proto.Protocol) bool {
if protocol == to && next != current {
return false
}
registry, ok := p.Protocols[protocol]
if !ok {
panic(fmt.Sprintf("Unknown protocol version %s", current.Protocol))
}
if typ, ok := registry.PacketIDs[current.ID]; ok {
panic(fmt.Sprintf("Can not register packet %s with id %#x for "+
"protocol %s because %s already registered with same id is conflicting. Check that all packet ids are updated.",
packetType, current.ID, registry.Protocol, typ.String()))
}
if _, ok = registry.PacketTypes[packetType]; ok {
panic(fmt.Sprintf("%T is already registered for protocol %s", packetOf, registry.Protocol))
}
registry.PacketIDs[current.ID] = packetType
registry.PacketTypes[packetType] = current.ID
return true
})
}
}
func FromDirection(direction proto.Direction, state *Registry, protocol proto.Protocol) *ProtocolRegistry {
if direction == proto.ServerBound {
return state.ServerBound.ProtocolRegistry(protocol)
}
return state.ClientBound.ProtocolRegistry(protocol)
}
type PacketMapping struct {
ID proto.PacketID
Protocol proto.Protocol
LastValidProtocol proto.Protocol // defaults to version.MaximumVersion.Protocol
}
// m returns new PacketMapping
func m(id proto.PacketID, version *proto.Version) *PacketMapping {
return ml(id, version, nil)
}
// ml returns new PacketMapping with optional lastValidProtocolVersion
//
// id packet Id
// version protocol version
// encodeOnly when true packet decoding will be disabled // *removed
// lastValidProtocolVersion last version this mapping is valid at
func ml(id proto.PacketID, version, lastValidProtocol *proto.Version) *PacketMapping {
var last proto.Protocol
if lastValidProtocol != nil {
last = lastValidProtocol.Protocol
}
return &PacketMapping{
ID: id,
Protocol: version.Protocol,
LastValidProtocol: last,
}
}
func versionRange(versions []*proto.Version, from, to proto.Protocol, fn func(proto.Protocol) bool) {
for _, ver := range versions {
if ver.Protocol >= from && ver.Protocol <= to {
if !fn(ver.Protocol) {
return
}
}
}
}
// String implements fmt.Stringer.
func (s State) String() string {
switch s {
case HandshakeState:
return "Handshake"
case StatusState:
return "Status"
case ConfigState:
return "Config"
case LoginState:
return "Login"
case PlayState:
return "Play"
}
return "UnknownState"
}