-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct.go
70 lines (64 loc) · 1.38 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
package osrscache
import (
"errors"
"fmt"
"io"
)
type Struct struct {
ID uint16
Params map[uint32]any
}
func NewStruct(id uint16) *Struct {
return &Struct{ID: id, Params: make(map[uint32]any)}
}
func (s *Struct) Read(data []byte) error {
reader := NewReader(data)
for {
opcode, err := reader.ReadUint8()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return fmt.Errorf("reading opcode: %w", err)
}
if opcode == 0 {
break
}
switch opcode {
case 249:
length, err := reader.ReadUint8()
if err != nil {
return fmt.Errorf("reading param length: %w", err)
}
s.Params = make(map[uint32]any, length)
for i := 0; i < int(length); i++ {
isString, err := reader.ReadUint8()
if err != nil {
return fmt.Errorf("reading is string: %w", err)
}
key, err := reader.ReadUint24()
if err != nil {
return fmt.Errorf("reading key: %w", err)
}
var value interface{}
if isString == 1 {
strValue, err := reader.ReadString()
if err != nil {
return fmt.Errorf("reading string value: %w", err)
}
value = strValue
} else {
intValue, err := reader.ReadUint32()
if err != nil {
return fmt.Errorf("reading uint32 value: %w", err)
}
value = intValue
}
s.Params[key] = value
}
default:
return fmt.Errorf("unsupported opcode: %d", opcode)
}
}
return nil
}