-
Notifications
You must be signed in to change notification settings - Fork 0
/
unserializer.go
330 lines (294 loc) · 7.45 KB
/
unserializer.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
package tfsreader
import (
"bytes"
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"time"
)
const (
ATTR_DESCRIPTION = 1
ATTR_EXT_FILE = 2
ATTR_TILE_FLAGS = 3
ATTR_ACTION_ID = 4
ATTR_UNIQUE_ID = 5
ATTR_TEXT = 6
ATTR_DESC = 7
ATTR_TELE_DEST = 8
ATTR_ITEM = 9
ATTR_DEPOT_ID = 10
ATTR_EXT_SPAWN_FILE = 11
ATTR_RUNE_CHARGES = 12
ATTR_EXT_HOUSE_FILE = 13
ATTR_HOUSEDOORID = 14
ATTR_COUNT = 15
ATTR_DURATION = 16
ATTR_DECAYING_STATE = 17
ATTR_WRITTENDATE = 18
ATTR_WRITTENBY = 19
ATTR_SLEEPERGUID = 20
ATTR_SLEEPSTART = 21
ATTR_CHARGES = 22
ATTR_CONTAINER_ITEMS = 23
ATTR_NAME = 24
ATTR_ARTICLE = 25
ATTR_PLURALNAME = 26
ATTR_WEIGHT = 27
ATTR_ATTACK = 28
ATTR_DEFENSE = 29
ATTR_EXTRADEFENSE = 30
ATTR_ARMOR = 31
ATTR_HITCHANCE = 32
ATTR_SHOOTRANGE = 33
ATTR_CUSTOM_ATTRIBUTES = 34
ATTR_DECAYTO = 35
ATTR_WRAPID = 36
ATTR_STOREITEM = 37
ATTR_SOULOWNER = 38
)
// Item defines an attribute item
type Item struct {
Name string `json:",omitempty"`
Count uint8 `json:",omitempty"`
Charges uint16 `json:",omitempty"`
WrapID uint16 `json:",omitempty"`
Text string `json:",omitempty"`
WrittenBy string `json:",omitempty"`
WrittenDate *time.Time `json:",omitempty"`
CustomAttributes []*CustomAttribute `json:",omitempty"`
Attack int32 `json:",omitempty"`
StoreItem uint8 `json:",omitempty"`
unserializedAttributes []uint8
SoulOwner string `json:",omitempty"`
}
// CustomAttribute defines an item attribute custom attribute
type CustomAttribute struct {
Key string
Value interface{}
}
// PrettyVisualize returns a JSON visualization
func (i *Item) PrettyVisualize() string {
v, err := json.MarshalIndent(i, "", "\t")
if err != nil {
return err.Error()
}
return string(v)
}
// Visualize returns a JSON visualization
func (i *Item) Visualize() string {
v, err := json.Marshal(i)
if err != nil {
return err.Error()
}
return string(v)
}
// UnserializeHexString unserializes the given hex string
func UnserializeHexString(h string) (*Item, error) {
data, err := hex.DecodeString(h)
if err != nil {
return nil, err
}
return Unserialize(data)
}
// Unserialize unserializes the given data
func Unserialize(data []byte) (*Item, error) {
var ret Item
buffer := bytes.NewBuffer(data)
for {
var attrType uint8
if err := binary.Read(buffer, binary.LittleEndian, &attrType); err != nil {
break
}
if attrType == 0 {
break
}
ret.unserializedAttributes = append(ret.unserializedAttributes, attrType)
switch attrType {
case ATTR_SOULOWNER:
txt, err := unserializeText(buffer)
if err != nil {
return nil, err
}
ret.SoulOwner = txt
case ATTR_STOREITEM:
st, err := unserializeStoreItem(buffer)
if err != nil {
return nil, err
}
ret.StoreItem = st
case ATTR_CHARGES:
charges, err := unserializeCharges(buffer)
if err != nil {
return nil, err
}
ret.Charges = charges
case ATTR_ATTACK:
attack, err := unserializeAttack(buffer)
if err != nil {
return nil, err
}
ret.Attack = attack
case ATTR_CUSTOM_ATTRIBUTES:
custom, err := unserializeCustomAttributes(buffer)
if err != nil {
return nil, err
}
ret.CustomAttributes = custom
case ATTR_NAME:
txt, err := unserializeText(buffer)
if err != nil {
return nil, err
}
ret.Name = txt
case ATTR_WRITTENBY:
txt, err := unserializeText(buffer)
if err != nil {
return nil, err
}
ret.WrittenBy = txt
case ATTR_WRITTENDATE:
d, err := unserializeDate(buffer)
if err != nil {
return nil, err
}
ret.WrittenDate = &d
case ATTR_TEXT:
txt, err := unserializeText(buffer)
if err != nil {
return nil, err
}
ret.Text = txt
case ATTR_WRAPID:
wrapId, err := unserializeWrap(buffer)
if err != nil {
return nil, err
}
ret.WrapID = wrapId
case ATTR_COUNT, ATTR_RUNE_CHARGES:
count, err := unserializeCount(buffer)
if err != nil {
return nil, err
}
ret.Count = count
case ATTR_DEPOT_ID:
io.CopyN(ioutil.Discard, buffer, 2)
default:
return nil, fmt.Errorf("Unkown attribute type: %d", attrType)
}
}
return &ret, nil
}
func unserializeCustomAttribute(k string, buffer *bytes.Buffer) (*CustomAttribute, error) {
var attrType uint8
if err := binary.Read(buffer, binary.LittleEndian, &attrType); err != nil {
return nil, err
}
var ret CustomAttribute
ret.Key = k
switch attrType {
case 1:
v, err := unserializeText(buffer)
if err != nil {
return nil, err
}
ret.Value = v
case 2:
var v int64
if err := binary.Read(buffer, binary.LittleEndian, &v); err != nil {
return nil, err
}
ret.Value = v
case 3:
var v float64
if err := binary.Read(buffer, binary.LittleEndian, &v); err != nil {
return nil, err
}
ret.Value = v
case 4:
var v bool
if err := binary.Read(buffer, binary.LittleEndian, &v); err != nil {
return nil, err
}
ret.Value = v
}
return &ret, nil
}
func unserializeCustomAttributes(buffer *bytes.Buffer) ([]*CustomAttribute, error) {
var s uint64
if err := binary.Read(buffer, binary.LittleEndian, &s); err != nil {
return nil, err
}
list := make([]*CustomAttribute, 0, s)
for x := uint64(0); x < s; x++ {
attrKey, err := readString(buffer)
if err != nil {
return nil, err
}
customAttr, err := unserializeCustomAttribute(attrKey, buffer)
if err != nil {
return nil, err
}
list = append(list, customAttr)
}
return list, nil
}
func unserializeStoreItem(buffer *bytes.Buffer) (uint8, error) {
var v uint8
if err := binary.Read(buffer, binary.LittleEndian, &v); err != nil {
return 0, err
}
return v, nil
}
func unserializeAttack(buffer *bytes.Buffer) (int32, error) {
var v int32
if err := binary.Read(buffer, binary.LittleEndian, &v); err != nil {
return 0, err
}
return v, nil
}
func unserializeDate(buffer *bytes.Buffer) (time.Time, error) {
var d uint32
if err := binary.Read(buffer, binary.LittleEndian, &d); err != nil {
return time.Time{}, err
}
t := time.Unix(int64(d), 0).UTC()
return t, nil
}
func readString(buffer *bytes.Buffer) (string, error) {
var strLen uint16
if err := binary.Read(buffer, binary.LittleEndian, &strLen); err != nil {
return "", err
}
data := make([]byte, strLen)
if err := binary.Read(buffer, binary.LittleEndian, &data); err != nil {
return "", err
}
return string(data), nil
}
func unserializeText(buffer *bytes.Buffer) (string, error) {
return readString(buffer)
}
func unserializeCharges(buffer *bytes.Buffer) (uint16, error) {
var count uint16
if err := binary.Read(buffer, binary.LittleEndian, &count); err != nil {
return 0, err
}
return count, nil
}
func unserializeCount(buffer *bytes.Buffer) (uint8, error) {
var count uint8
if err := binary.Read(buffer, binary.LittleEndian, &count); err != nil {
return 0, err
}
return count, nil
}
func unserializeWrap(buffer *bytes.Buffer) (uint16, error) {
var wrapId uint16
if err := binary.Read(buffer, binary.LittleEndian, &wrapId); err != nil {
return 0, err
}
return wrapId, nil
}