-
Notifications
You must be signed in to change notification settings - Fork 1
/
d2o.go
287 lines (247 loc) · 8.18 KB
/
d2o.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
package unpack
import (
"fmt"
"io"
"log"
)
type GameDataProcess struct {
stream *BinaryStream
sortIndex map[string]int64
queryableField []string
searchFieldIndex map[string]int64
searchFieldType map[string]int32
searchFieldCount map[string]int32
}
func (process *GameDataProcess) parseStream(D2OFileBinary *BinaryStream) {
length := int64(D2OFileBinary.ReadInt32())
off := D2OFileBinary.Position() + length + 4
for length > 0 {
available := D2OFileBinary.BytesAvailable()
string := D2OFileBinary.ReadString()
process.queryableField = append(process.queryableField, string)
process.searchFieldIndex[string] = int64(D2OFileBinary.ReadInt32()) + off
process.searchFieldType[string] = D2OFileBinary.ReadInt32()
process.searchFieldCount[string] = D2OFileBinary.ReadInt32()
length = length - (available - D2OFileBinary.BytesAvailable())
}
}
func NewGameDataProcess(D2OFileBinary *BinaryStream) *GameDataProcess {
process := new(GameDataProcess)
process.stream = D2OFileBinary
process.sortIndex = make(map[string]int64)
process.searchFieldIndex = make(map[string]int64)
process.searchFieldType = make(map[string]int32)
process.searchFieldCount = make(map[string]int32)
process.parseStream(D2OFileBinary)
return process
}
type GameDataField struct {
name string
innerReadMethods []func(*BinaryStream, int) interface{}
innerTypeNames []string
d2oReader *D2OReader
readData func(*BinaryStream, int) interface{}
}
func NewGameDataField(name string, d2oReader *D2OReader) *GameDataField {
field := new(GameDataField)
field.name = name
field.d2oReader = d2oReader
field.innerReadMethods = make([]func(*BinaryStream, int) interface{}, 0)
field.innerTypeNames = make([]string, 0)
return field
}
func (field *GameDataField) readType(D2OFileBinary *BinaryStream) {
readID := D2OFileBinary.ReadInt32()
field.readData = field.getReadMethod(readID, D2OFileBinary)
}
// readInteger
func (field *GameDataField) readInteger(D2OFileBinary *BinaryStream, vecIndex int) interface{} {
return D2OFileBinary.ReadInt32()
}
// readBoolean
func (field *GameDataField) readBoolean(D2OFileBinary *BinaryStream, vecIndex int) interface{} {
return D2OFileBinary.ReadBool()
}
// readString
func (field *GameDataField) readString(D2OFileBinary *BinaryStream, vecIndex int) interface{} {
str := D2OFileBinary.ReadString()
if str == "null" {
str = ""
}
return str
}
// readNumber
func (field *GameDataField) readNumber(D2OFileBinary *BinaryStream, vecIndex int) interface{} {
return D2OFileBinary.ReadFloat64()
}
// readI18n
func (field *GameDataField) readI18n(D2OFileBinary *BinaryStream, vecIndex int) interface{} {
return D2OFileBinary.ReadInt32()
}
// readUnsignedInteger
func (field *GameDataField) readUnsignedInteger(D2OFileBinary *BinaryStream, vecIndex int) interface{} {
return D2OFileBinary.ReadUint32()
}
// readVector
func (field *GameDataField) readVector(D2OFileBinary *BinaryStream, vecIndex int) interface{} {
vectorSize := int(D2OFileBinary.ReadInt32())
vector := make([]interface{}, vectorSize)
for i := 0; i < vectorSize; i++ {
vector[i] = field.innerReadMethods[vecIndex](D2OFileBinary, vecIndex+1)
}
return vector
}
// readObject
func (field *GameDataField) readObject(D2OFileBinary *BinaryStream, vecIndex int) interface{} {
objectID := D2OFileBinary.ReadInt32()
if objectID == -1431655766 {
return nil
}
obj := field.d2oReader.GetClassDefinition(objectID)
return obj.read(D2OFileBinary)
}
func (field *GameDataField) getReadMethod(readID int32, D2OFileBinary *BinaryStream) func(*BinaryStream, int) interface{} {
if readID == -1 {
return field.readInteger
} else if readID == -2 {
return field.readBoolean
} else if readID == -3 {
return field.readString
} else if readID == -4 {
return field.readNumber
} else if readID == -5 {
return field.readI18n
} else if readID == -6 {
return field.readUnsignedInteger
} else if readID == -99 {
field.innerTypeNames = append(field.innerTypeNames, D2OFileBinary.ReadString())
field.innerReadMethods = append([]func(*BinaryStream, int) interface{}{field.getReadMethod(D2OFileBinary.ReadInt32(), D2OFileBinary)}, field.innerReadMethods...)
return field.readVector
} else {
if readID > 0 {
return field.readObject
}
panic("Unknown type " + string(readID) + ".")
}
}
type GameDataClassDefinition struct {
class string
fields []*GameDataField
d2oReader *D2OReader
}
func NewGameDataClassDefinition(class_pkg string, class_name string, d2oReader *D2OReader) *GameDataClassDefinition {
return &GameDataClassDefinition{
class: class_pkg + "." + class_name,
fields: make([]*GameDataField, 0),
d2oReader: d2oReader,
}
}
func (classDef *GameDataClassDefinition) read(D2OFileBinary *BinaryStream) interface{} {
obj := make(map[string]interface{})
for _, field := range classDef.fields {
obj[field.name] = field.readData(D2OFileBinary, 0)
}
return obj
}
func (classDef *GameDataClassDefinition) addField(name string, D2OFileBinary *BinaryStream) {
field := NewGameDataField(name, classDef.d2oReader)
field.readType(D2OFileBinary)
classDef.fields = append(classDef.fields, field)
}
// D2OReader represents a reader for D2O files.
type D2OReader struct {
stream io.Reader
streamStartIndex int64
classes map[int32]*GameDataClassDefinition
counter int
D2OFileBinary *BinaryStream
gameDataProcessor *GameDataProcess
}
// NewD2OReader creates a new D2OReader instance with the given stream.
func NewD2OReader(stream io.ReadWriteSeeker) (*D2OReader, error) {
reader := &D2OReader{
stream: stream,
classes: make(map[int32]*GameDataClassDefinition),
streamStartIndex: 7,
counter: 0,
}
D2OFileBinary := BinaryStream{baseStream: stream, bigEndian: true}
reader.D2OFileBinary = &D2OFileBinary
stringHeader := string(D2OFileBinary.ReadBytes(3))
baseOffset := int32(0)
if stringHeader != "D2O" {
D2OFileBinary.Seek(0, io.SeekStart)
stringHeader := D2OFileBinary.ReadString()
if stringHeader != "AKSF" {
return nil, fmt.Errorf("malformed game data file")
}
D2OFileBinary.ReadUint16()
baseOffset = D2OFileBinary.ReadInt32()
D2OFileBinary.Seek(int64(baseOffset), io.SeekCurrent)
reader.streamStartIndex = D2OFileBinary.Position() + 7
stringHeaderBytes := string(D2OFileBinary.ReadBytes(3))
if stringHeaderBytes != "D2O" {
return nil, fmt.Errorf("malformed game data file")
}
}
offset := D2OFileBinary.ReadInt32()
D2OFileBinary.Seek(int64(baseOffset+offset), io.SeekStart)
indexNumber := D2OFileBinary.ReadInt32()
index := 0
indexOffsets := make(map[int32]int32)
for int32(index) < indexNumber {
indexID := D2OFileBinary.ReadInt32()
offset := D2OFileBinary.ReadInt32()
indexOffsets[indexID] = baseOffset + offset
reader.counter += 1
index = index + 8
}
classNumber := D2OFileBinary.ReadInt32()
classIndex := 0
for int32(classIndex) < classNumber {
classID := D2OFileBinary.ReadInt32()
reader.readClassDefinition(classID, &D2OFileBinary)
classIndex += 1
}
if D2OFileBinary.BytesAvailable() > 0 {
reader.gameDataProcessor = NewGameDataProcess(&D2OFileBinary)
}
return reader, nil
}
func (dr *D2OReader) GetObjects() []interface{} {
if dr.counter == 0 {
return nil
}
D2OFileBinary := dr.D2OFileBinary
D2OFileBinary.SetPosition(dr.streamStartIndex)
objects := make([]interface{}, 0)
i := 0
for i < dr.counter {
classId := D2OFileBinary.ReadInt32()
class := dr.classes[classId]
if class == nil {
log.Fatal("class is nil")
}
object := class.read(D2OFileBinary)
objects = append(objects, object)
i += 1
}
return objects
}
// GetClassDefinition returns the class definition for a given object_id.
func (dr *D2OReader) GetClassDefinition(objectID int32) *GameDataClassDefinition {
return dr.classes[objectID]
}
func (dr *D2OReader) readClassDefinition(classID int32, D2OFileBinary *BinaryStream) {
className := D2OFileBinary.ReadString()
classPackage := D2OFileBinary.ReadString()
classDef := NewGameDataClassDefinition(classPackage, className, dr)
fieldNumber := D2OFileBinary.ReadInt32()
fieldIndex := 0
for int32(fieldIndex) < fieldNumber {
field := D2OFileBinary.ReadString()
classDef.addField(field, D2OFileBinary)
fieldIndex += 1
}
dr.classes[classID] = classDef
}