-
Notifications
You must be signed in to change notification settings - Fork 2
/
struct.go
368 lines (325 loc) · 8.35 KB
/
struct.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
package eqStruct
import (
"bytes"
"cmp"
"encoding/binary"
"fmt"
"reflect"
"strings"
"golang.org/x/exp/constraints"
"google.golang.org/protobuf/proto"
)
//go:generate enumer -type=EQType
type EQType uint32
const (
EQT_Unknown EQType = iota
EQT_PlayerProfile
EQT_PlayEverquestResponse
EQT_ZoneServerInfo
EQT_PlayRequest
EQT_ZoneEntryServer
EQT_LogServer
EQT_EnterWorld
EQT_LoginInfo
EQT_LoginAccepted
EQT_SpawnPositionUpdate
EQT_SpawnPositionUpdates
EQT_ZoneSpawns
EQT_ZoneSpawn
EQT_ClientUpdate
EQT_ManaUpdate
EQT_NewZone
EQT_StaminaUpdate
EQT_MoveDoor
EQT_SpawnAppearance
EQT_Action
EQT_BeginCast
EQT_Damage
EQT_ExpUpdate
EQT_Consider
EQT_Target
EQT_HPUpdate
EQT_Object
EQT_DeleteSpawn
EQT_ZoneEntryClient
EQT_ServerMOTD
EQT_WearChange
EQT_TintStruct
EQT_Death
EQT_RaidGeneral
EQT_RaidAddMember
EQT_RaidCreate
EQT_WorldApprove
EQT_GuildsList
EQT_GuildEntry
EQT_GuildUpdate
EQT_ZonePoints
EQT_ZonePoint
EQT_LFG
EQT_Weather
EQT_Time
EQT_SpawnDoor
EQT_SpawnDoors
EQT_LFGAppearance
EQT_ChannelMessage
EQT_ZoneChange
EQT_ZoneChangeReq
EQT_ManaChange
EQT_CorpsePosition
EQT_DoorClick
EQT_MessageFormatted
EQT_BuffFade
EQT_Animation
EQT_Charm
EQT_CastSpell
EQT_MemorizeSpell
EQT_RandomReq
EQT_RandomReply
EQT_EntityID
EQT_PetCommand
EQT_ItemMove
EQT_MessageSpecial
)
var typeRegistry = map[EQType]any{
EQT_PlayerProfile: (*PlayerProfile)(nil),
EQT_PlayEverquestResponse: (*PlayRequest)(nil), // TODO: verify
EQT_ZoneServerInfo: (*ZoneServerInfo)(nil),
EQT_PlayRequest: (*PlayRequest)(nil),
EQT_ZoneEntryServer: (*ZoneEntryServer)(nil),
EQT_LogServer: (*LogServer)(nil),
EQT_EnterWorld: (*EnterWorld)(nil),
EQT_LoginInfo: (*LoginInfo)(nil),
EQT_LoginAccepted: (*LoginAccepted)(nil),
EQT_SpawnPositionUpdate: (*SpawnPositionUpdate)(nil),
EQT_SpawnPositionUpdates: (*SpawnPositionUpdates)(nil),
EQT_ZoneSpawns: (*ZoneSpawns)(nil),
EQT_ZoneSpawn: (*ZoneSpawn)(nil),
EQT_ClientUpdate: (*SpawnPositionUpdate)(nil), // TODO: verify
EQT_ManaUpdate: (*ManaUpdate)(nil),
EQT_NewZone: (*NewZone)(nil),
EQT_StaminaUpdate: (*StaminaUpdate)(nil),
EQT_MoveDoor: (*MoveDoor)(nil),
EQT_SpawnAppearance: (*SpawnAppearance)(nil),
EQT_Action: (*Action)(nil),
EQT_BeginCast: (*BeginCast)(nil),
EQT_Damage: (*Damage)(nil),
EQT_ExpUpdate: (*ExpUpdate)(nil),
EQT_Consider: (*Consider)(nil),
EQT_Target: (*Target)(nil),
EQT_HPUpdate: (*HPUpdate)(nil),
EQT_Object: (*Object)(nil),
EQT_DeleteSpawn: (*DeleteSpawn)(nil),
EQT_ZoneEntryClient: (*ZoneEntryClient)(nil),
EQT_ServerMOTD: (*ServerMOTD)(nil),
EQT_WearChange: (*WearChange)(nil),
EQT_TintStruct: (*TintStruct)(nil),
EQT_Death: (*Death)(nil),
EQT_RaidGeneral: (*RaidGeneral)(nil),
EQT_RaidAddMember: (*RaidAddMember)(nil),
EQT_RaidCreate: (*RaidCreate)(nil),
EQT_WorldApprove: (*WorldApprove)(nil),
EQT_GuildsList: (*GuildsList)(nil),
EQT_GuildEntry: (*GuildEntry)(nil),
EQT_GuildUpdate: (*GuildUpdate)(nil),
EQT_ZonePoints: (*GuildUpdate)(nil),
EQT_ZonePoint: (*ZonePoint)(nil),
EQT_LFG: (*LFG)(nil),
EQT_Weather: (*Weather)(nil),
EQT_Time: (*Time)(nil),
EQT_SpawnDoor: (*SpawnDoor)(nil),
EQT_SpawnDoors: (*SpawnDoors)(nil),
EQT_LFGAppearance: (*LFGAppearance)(nil),
EQT_ChannelMessage: (*ChannelMessage)(nil),
EQT_ZoneChange: (*ZoneChange)(nil),
EQT_ZoneChangeReq: (*ZoneChangeReq)(nil),
EQT_ManaChange: (*ManaChange)(nil),
EQT_CorpsePosition: (*CorpsePosition)(nil),
EQT_DoorClick: (*DoorClick)(nil),
EQT_MessageFormatted: (*MessageFormatted)(nil),
EQT_BuffFade: (*BuffFade)(nil),
EQT_Animation: (*Animation)(nil),
EQT_Charm: (*Charm)(nil),
EQT_CastSpell: (*CastSpell)(nil),
EQT_MemorizeSpell: (*MemorizeSpell)(nil),
EQT_RandomReq: (*RandomReq)(nil),
EQT_RandomReply: (*RandomReply)(nil),
EQT_EntityID: (*EntityID)(nil),
EQT_PetCommand: (*PetCommand)(nil),
EQT_ItemMove: (*ItemMove)(nil),
EQT_MessageSpecial: (*MessageSpecial)(nil),
}
func (t *EQType) TypeOf() reflect.Type {
typedNil, ok := typeRegistry[*t]
if !ok {
panic("missing type definition")
}
return reflect.TypeOf(typedNil).Elem()
}
type EQStruct interface {
EQType() EQType
bp() *int
Unmarshal(b []byte) (int, error) // This is only temporary until the VM is implemented.
ProtoMess() proto.Message
}
type EQTypes interface {
uint8 | uint16 | uint32 | int8 | int16 | int32 | float32 | string | []byte
}
func EQReadBigEndian[T EQTypes](b []byte, s EQStruct, field *T, size int) error {
switch t := any(field).(type) {
case *uint32:
return EQReadUint32BigEndian(b, s, t)
case *uint16:
return EQReadUint16BigEndian(b, s, t)
case *int32:
return EQReadInt32BigEndian(b, s, t)
case *int16:
return EQReadInt16BigEndian(b, s, t)
default:
return EQRead(b, s, field, size)
}
}
func EQRead[T EQTypes](b []byte, s EQStruct, field *T, size int) error {
switch t := any(field).(type) {
case *uint32:
return EQReadUint32(b, s, t)
case *uint16:
return EQReadUint16(b, s, t)
case *int16:
return EQReadInt16(b, s, t)
case *int8:
return EQReadInt8(b, s, t)
case *int32:
return EQReadInt32(b, s, t)
case *uint8:
return EQReadUint8(b, s, t)
case *float32:
return EQReadFloat32(b, s, t)
case *[]byte:
return EQReadBytes(b, s, t, size)
case *string:
if size == 0 {
return EQReadStringNullTerm(b, s, t)
}
return EQReadString(b, s, t, size)
default:
return fmt.Errorf("unsupported type %t", t)
}
}
func EQReadUint32(b []byte, s EQStruct, field *uint32) error {
p := s.bp()
c := b[*p:]
*field = binary.LittleEndian.Uint32(c)
*p += 4
return nil
}
func EQReadUint16(b []byte, s EQStruct, field *uint16) error {
p := s.bp()
c := b[*p:]
*field = binary.LittleEndian.Uint16(c)
*p += 2
return nil
}
func EQReadUint32BigEndian(b []byte, s EQStruct, field *uint32) error {
p := s.bp()
c := b[*p:]
*field = binary.BigEndian.Uint32(c)
*p += 4
return nil
}
func EQReadUint16BigEndian(b []byte, s EQStruct, field *uint16) error {
p := s.bp()
c := b[*p:]
*field = binary.BigEndian.Uint16(c)
*p += 2
return nil
}
func EQReadInt16(b []byte, s EQStruct, field *int16) error {
p := s.bp()
c := b[*p:]
*field = int16(binary.LittleEndian.Uint16(c))
*p += 2
return nil
}
func EQReadInt32(b []byte, s EQStruct, field *int32) error {
p := s.bp()
c := b[*p:]
*field = int32(binary.LittleEndian.Uint32(c))
*p += 4
return nil
}
func EQReadInt16BigEndian(b []byte, s EQStruct, field *int16) error {
p := s.bp()
c := b[*p:]
*field = int16(binary.BigEndian.Uint16(c))
*p += 2
return nil
}
func EQReadInt32BigEndian(b []byte, s EQStruct, field *int32) error {
p := s.bp()
c := b[*p:]
*field = int32(binary.BigEndian.Uint32(c))
*p += 4
return nil
}
func EQReadUint8(b []byte, s EQStruct, field *uint8) error {
p := s.bp()
c := b[*p]
*field = uint8(c)
*p++
return nil
}
func EQReadInt8(b []byte, s EQStruct, field *int8) error {
p := s.bp()
c := b[*p]
*field = int8(c)
*p++
return nil
}
func EQReadFloat32(b []byte, s EQStruct, field *float32) error {
p := s.bp()
c := b[*p]
*field = float32(c)
*p += 4
return nil
}
func EQReadBytes(b []byte, s EQStruct, field *[]byte, maxLength int) error {
p := s.bp()
stop := min(len(b), *p+maxLength)
lg := stop - *p
bh := make([]byte, lg)
copy(bh, b[*p:stop])
*field = bh
*p += lg
return nil
}
func EQReadString(b []byte, s EQStruct, field *string, maxLength int) error {
p := s.bp()
stop := min(len(b), *p+maxLength)
lg := stop - *p
bh := make([]byte, lg)
copy(bh, b[*p:stop])
bh = bytes.Trim(bh, "\x00")
*field = string(bh)
*p += lg
return nil
}
func EQReadStringNullTerm(b []byte, s EQStruct, field *string) error {
p := s.bp()
var str strings.Builder
for {
if b[*p] == 0 {
break
}
str.WriteByte(b[*p])
*p++
}
*field = str.String()
*p++
return nil
}
func min[T constraints.Ordered](x, y T) T {
if cmp.Less[T](x, y) {
return x
}
return y
}