-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands.go
252 lines (232 loc) · 5.05 KB
/
commands.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
package liter
import (
"errors"
"io"
"sync"
)
var (
ErrParserIdNotExists = errors.New("liter.CommandNode: parser id is not exists")
ErrParserInstanceNotExists = errors.New("liter.CommandNode: command parser instance not exists")
)
type CmdPropEncoder interface {
Id() VarInt
String() String
Encode(b *PacketBuilder, value any) (err error)
Decode(r *PacketReader) (value any, err error)
}
var (
cmdMux sync.RWMutex
cmdIDMap = make(map[VarInt]CmdPropEncoder, 50)
cmdSIDMap = make(map[string]CmdPropEncoder, 50)
)
func RegisterCmdEncoder(p CmdPropEncoder) (ok bool) {
id := p.Id()
sid := p.String()
cmdMux.Lock()
defer cmdMux.Unlock()
if _, ok := cmdIDMap[id]; ok {
return false
}
if _, ok := cmdSIDMap[sid]; ok {
return false
}
cmdIDMap[id] = p
cmdSIDMap[sid] = p
return true
}
func UnregisterCmdEncoder(p CmdPropEncoder) (ok bool) {
id := p.Id()
sid := p.String()
cmdMux.Lock()
defer cmdMux.Unlock()
old1, ok1 := cmdIDMap[id]
if !ok1 {
return false
}
old2, ok2 := cmdSIDMap[sid]
if !ok2 || p != old1 || p != old2 { // return false if instances not match
return false
}
delete(cmdIDMap, id)
delete(cmdSIDMap, sid)
return true
}
type CmdFlag Byte
const (
// Values
CmdTypeRoot CmdFlag = 0x00
CmdTypeLiteral CmdFlag = 0x01
CmdTypeArg CmdFlag = 0x02
// Masks
CmdTypeMask CmdFlag = 0x03 // 0: root, 1: literal, 2: argument. 3 is not used.
CmdExec CmdFlag = 0x04 // Set if the node stack to this point constitutes a valid command.
CmdRedirect CmdFlag = 0x08 // Set if the node redirects to another node.
CmdSuggest CmdFlag = 0x10 // Only present for argument nodes.
)
type CommandNode struct {
Flags CmdFlag
Children []VarInt
RedirectNode Optional[VarInt]
Name Optional[String]
ParserId Optional[VarInt] // 1.19+
ParserStrId Optional[String] // 1.7 ~ 1.18
Properties any
SuggestionsType Optional[String]
}
var _ Packet = (*CommandNode)(nil)
func (c *CommandNode) Type() CmdFlag {
return c.Flags & CmdTypeMask
}
func (c *CommandNode) HasFlag(f CmdFlag) bool {
return c.Flags&f != 0
}
func (c *CommandNode) Vaild() bool {
return c.HasFlag(CmdExec)
}
func (c *CommandNode) GetParserId() (VarInt, error) {
if !c.ParserId.Ok {
if !c.ParserStrId.Ok {
return -1, ErrParserIdNotExists
}
cmdMux.RLock()
p, ok := cmdSIDMap[c.ParserStrId.V]
cmdMux.RUnlock()
if !ok {
return -1, ErrParserInstanceNotExists
}
c.ParserId.Set(p.Id())
}
return c.ParserId.V, nil
}
func (c *CommandNode) GetParserStrId() (String, error) {
if !c.ParserStrId.Ok {
if !c.ParserId.Ok {
return "", ErrParserIdNotExists
}
cmdMux.RLock()
p, ok := cmdIDMap[c.ParserId.V]
cmdMux.RUnlock()
if !ok {
return "", ErrParserInstanceNotExists
}
c.ParserStrId.Set(p.String())
}
return c.ParserStrId.V, nil
}
func (c *CommandNode) GetParser() (CmdPropEncoder, error) {
if c.ParserId.Ok {
cmdMux.RLock()
p, ok := cmdIDMap[c.ParserId.V]
cmdMux.RUnlock()
if !ok {
return nil, ErrParserInstanceNotExists
}
return p, nil
}
if c.ParserStrId.Ok {
cmdMux.RLock()
p, ok := cmdSIDMap[c.ParserStrId.V]
cmdMux.RUnlock()
if !ok {
return nil, ErrParserInstanceNotExists
}
return p, nil
}
return nil, ErrParserIdNotExists
}
func (c *CommandNode) Encode(b *PacketBuilder) {
b.VarInt((VarInt)(c.Flags))
b.VarInt((VarInt)(len(c.Children)))
for _, child := range c.Children {
b.VarInt(child)
}
if c.HasFlag(CmdRedirect) {
b.VarInt(c.RedirectNode.Assert())
}
t := c.Type()
if t == CmdTypeLiteral || t == CmdTypeArg {
b.String(c.Name.Assert())
}
if t == CmdTypeArg {
p, err := c.GetParser()
if err != nil {
panic(err)
}
b.VarInt(p.Id())
if err = p.Encode(b, c.Properties); err != nil {
panic(err)
}
}
if c.HasFlag(CmdSuggest) {
b.String(c.SuggestionsType.Assert())
}
}
func (c *CommandNode) DecodeFrom(r *PacketReader) (err error) {
protocol := r.Protocol()
var ok bool
var flags VarInt
if flags, ok = r.VarInt(); !ok {
return io.EOF
}
c.Flags = (CmdFlag)(flags)
var l VarInt
if l, ok = r.VarInt(); !ok {
return io.EOF
}
c.Children = make([]VarInt, l)
for i, _ := range c.Children {
if c.Children[i], ok = r.VarInt(); !ok {
return io.EOF
}
}
if c.HasFlag(CmdRedirect) {
var v VarInt
if v, ok = r.VarInt(); !ok {
return io.EOF
}
c.RedirectNode.Set(v)
}
t := c.Type()
if t == CmdTypeLiteral || t == CmdTypeArg {
var v String
if v, ok = r.String(); !ok {
return io.EOF
}
c.Name.Set(v)
}
if t == CmdTypeArg {
var p CmdPropEncoder
if protocol < V1_19 {
var v String
if v, ok = r.String(); !ok {
return io.EOF
}
c.ParserStrId.Set(v)
c.ParserId.Ok = false
if p, err = c.GetParser(); err != nil {
return
}
} else {
var v VarInt
if v, ok = r.VarInt(); !ok {
return io.EOF
}
c.ParserId.Set(v)
c.ParserStrId.Ok = false
if p, err = c.GetParser(); err != nil {
return
}
}
if c.Properties, err = p.Decode(r); err != nil {
return
}
}
if c.HasFlag(CmdSuggest) {
var v String
if v, ok = r.String(); !ok {
return io.EOF
}
c.SuggestionsType.Set(v)
}
return
}