-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathconfiguration.go
361 lines (319 loc) · 8.74 KB
/
configuration.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
package bot
import (
"bytes"
"errors"
"io"
"github.com/Tnze/go-mc/chat"
"github.com/Tnze/go-mc/data/packetid"
"github.com/Tnze/go-mc/nbt"
"github.com/Tnze/go-mc/net"
pk "github.com/Tnze/go-mc/net/packet"
)
type ConfigHandler interface {
EnableFeature(features []pk.Identifier)
PushResourcePack(res ResourcePack)
PopResourcePack(id pk.UUID)
PopAllResourcePack()
SelectDataPacks(packs []DataPack) []DataPack
}
type ResourcePack struct {
ID pk.UUID
URL string
Hash string
Forced bool
PromptMessage *chat.Message // Optional
}
type ConfigErr struct {
Stage string
Err error
}
func (l ConfigErr) Error() string {
return "bot: configuration error: [" + l.Stage + "] " + l.Err.Error()
}
func (l ConfigErr) Unwrap() error {
return l.Err
}
func (c *Client) joinConfiguration(conn *net.Conn) error {
for {
var p pk.Packet
if err := conn.ReadPacket(&p); err != nil {
return ConfigErr{"config custom payload", err}
}
switch packetid.ClientboundPacketID(p.ID) {
case packetid.ClientboundConfigCookieRequest:
var key pk.Identifier
err := p.Scan(&key)
if err != nil {
return ConfigErr{"cookie request", err}
}
cookieContent := c.Cookies[string(key)]
err = conn.WritePacket(pk.Marshal(
packetid.ServerboundConfigCookieResponse,
key, pk.OptionEncoder[pk.ByteArray]{
Has: cookieContent != nil,
Val: pk.ByteArray(cookieContent),
},
))
if err != nil {
return ConfigErr{"cookie response", err}
}
case packetid.ClientboundConfigCustomPayload:
var channel pk.Identifier
var data pk.PluginMessageData
err := p.Scan(&channel, &data)
if err != nil {
return ConfigErr{"custom payload", err}
}
// TODO: Provide configuration custom data handling interface
//
// There are two types of Custom packet.
// One for Login stage, the other for config and play stage.
// The first one called "Custom Query", and the second one called "Custom Payload".
// We can know the different by their name, the "query" is one request to one response, paired.
// But the second one can be sent in any order.
//
// And the custome payload packet seems to be same in config stage and play stage.
// How do we provide API for that?
case packetid.ClientboundConfigDisconnect:
const ErrStage = "disconnect"
var reason chat.Message
err := p.Scan(&reason)
if err != nil {
return ConfigErr{ErrStage, err}
}
return ConfigErr{ErrStage, DisconnectErr(reason)}
case packetid.ClientboundConfigFinishConfiguration:
err := conn.WritePacket(pk.Marshal(
packetid.ServerboundConfigFinishConfiguration,
))
if err != nil {
return ConfigErr{"finish config", err}
}
return nil
case packetid.ClientboundConfigKeepAlive:
const ErrStage = "keep alive"
var keepAliveID pk.Long
err := p.Scan(&keepAliveID)
if err != nil {
return ConfigErr{ErrStage, err}
}
// send it back
err = conn.WritePacket(pk.Marshal(
packetid.ServerboundConfigKeepAlive,
keepAliveID,
))
if err != nil {
return ConfigErr{ErrStage, err}
}
case packetid.ClientboundConfigPing:
var pingID pk.Int
err := p.Scan(&pingID)
if err != nil {
return ConfigErr{"ping", err}
}
// send it back
err = conn.WritePacket(pk.Marshal(
packetid.ServerboundConfigPong,
pingID,
))
if err != nil {
return ConfigErr{"pong", err}
}
case packetid.ClientboundConfigResetChat:
// TODO
case packetid.ClientboundConfigRegistryData:
const ErrStage = "registry"
// err := p.Scan(pk.NBT(&c.ConfigData.Registries))
// if err != nil {
// return ConfigErr{"registry data", err}
// }
var registryID pk.Identifier
var length pk.VarInt
r := bytes.NewReader(p.Data)
_, err := registryID.ReadFrom(r)
if err != nil {
return ConfigErr{ErrStage, err}
}
_, err = length.ReadFrom(r)
if err != nil {
return ConfigErr{ErrStage, err}
}
registry := c.Registries.Registry(string(registryID))
if registry == nil {
return ConfigErr{ErrStage, errors.New("unknown registry: " + string(registryID))}
}
for i := 0; i < int(length); i++ {
var entryId pk.Identifier
var hasData pk.Boolean
var data nbt.RawMessage
_, err = entryId.ReadFrom(r)
if err != nil {
return ConfigErr{ErrStage, err}
}
_, err = hasData.ReadFrom(r)
if err != nil {
return ConfigErr{ErrStage, err}
}
if hasData {
_, err = pk.NBT(&data).ReadFrom(r)
if err != nil {
return ConfigErr{ErrStage, err}
}
err = registry.InsertWithNBT(string(entryId), data)
if err != nil {
return ConfigErr{ErrStage, err}
}
}
}
case packetid.ClientboundConfigResourcePackPop:
var id pk.Option[pk.UUID, *pk.UUID]
err := p.Scan(&id)
if err != nil {
return ConfigErr{"resource pack pop", err}
}
case packetid.ClientboundConfigResourcePackPush:
var id pk.UUID
var Url, Hash pk.String
var Forced pk.Boolean
var PromptMessage pk.Option[chat.Message, *chat.Message]
err := p.Scan(
&id,
&Url,
&Hash,
&Forced,
&PromptMessage,
)
if err != nil {
return ConfigErr{"resource pack", err}
}
res := ResourcePack{
ID: id,
URL: string(Url),
Hash: string(Hash),
Forced: bool(Forced),
}
if PromptMessage.Has {
res.PromptMessage = &PromptMessage.Val
}
c.ConfigHandler.PushResourcePack(res)
case packetid.ClientboundConfigStoreCookie:
var key pk.Identifier
var payload pk.ByteArray
err := p.Scan(&key, &payload)
if err != nil {
return ConfigErr{"store cookie", err}
}
c.Cookies[string(key)] = []byte(payload)
case packetid.ClientboundConfigTransfer:
var host pk.String
var port pk.VarInt
err := p.Scan(&host, &port)
if err != nil {
return ConfigErr{"store cookie", err}
}
// TODO: trnasfer to the specific server
// How does it work? Just connect the new server, and re-start at handshake?
case packetid.ClientboundConfigUpdateEnabledFeatures:
features := []pk.Identifier{}
err := p.Scan(pk.Array(&features))
if err != nil {
return ConfigErr{"update enabled features", err}
}
c.ConfigHandler.EnableFeature(features)
case packetid.ClientboundConfigUpdateTags:
// TODO: Handle Tags
case packetid.ClientboundConfigSelectKnownPacks:
const ErrStage = "select known packs"
packs := []DataPack{}
err := p.Scan(pk.Array(&packs))
if err != nil {
return ConfigErr{ErrStage, err}
}
knwonPacks := c.ConfigHandler.SelectDataPacks(packs)
err = conn.WritePacket(pk.Marshal(
packetid.ServerboundConfigSelectKnownPacks,
pk.Array(knwonPacks),
))
if err != nil {
return ConfigErr{ErrStage, err}
}
case packetid.ClientboundConfigCustomReportDetails:
const ErrStage = "custom report details"
var length pk.VarInt
var title, description pk.String
r := bytes.NewReader(p.Data)
_, err := length.ReadFrom(r)
if err != nil {
return ConfigErr{ErrStage, err}
}
for i := 0; i < int(length); i++ {
_, err = title.ReadFrom(r)
if err != nil {
return ConfigErr{ErrStage, err}
}
_, err = description.ReadFrom(r)
if err != nil {
return ConfigErr{ErrStage, err}
}
c.CustomReportDetails[string(title)] = string(description)
}
case packetid.ClientboundConfigServerLinks:
// TODO
}
}
}
type DataPack struct {
Namespace string
ID string
Version string
}
func (d DataPack) WriteTo(w io.Writer) (n int64, err error) {
n, err = pk.String(d.Namespace).WriteTo(w)
if err != nil {
return n, err
}
n1, err := pk.String(d.ID).WriteTo(w)
if err != nil {
return n + n1, err
}
n2, err := pk.String(d.Version).WriteTo(w)
return n + n1 + n2, err
}
func (d *DataPack) ReadFrom(r io.Reader) (n int64, err error) {
n, err = (*pk.String)(&d.Namespace).ReadFrom(r)
if err != nil {
return n, err
}
n1, err := (*pk.String)(&d.ID).ReadFrom(r)
if err != nil {
return n + n1, err
}
n2, err := (*pk.String)(&d.Version).ReadFrom(r)
return n + n1 + n2, err
}
type DefaultConfigHandler struct {
resourcesPack []ResourcePack
}
func NewDefaultConfigHandler() *DefaultConfigHandler {
return &DefaultConfigHandler{
resourcesPack: make([]ResourcePack, 0),
}
}
func (d *DefaultConfigHandler) EnableFeature(features []pk.Identifier) {}
func (d *DefaultConfigHandler) PushResourcePack(res ResourcePack) {
d.resourcesPack = append(d.resourcesPack, res)
}
func (d *DefaultConfigHandler) PopResourcePack(id pk.UUID) {
for i, v := range d.resourcesPack {
if id == v.ID {
d.resourcesPack = append(d.resourcesPack[:i], d.resourcesPack[i+1:]...)
break
}
}
}
func (d *DefaultConfigHandler) PopAllResourcePack() {
d.resourcesPack = d.resourcesPack[:0]
}
func (d *DefaultConfigHandler) SelectDataPacks(packs []DataPack) []DataPack {
return []DataPack{}
}