-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblack-readers.js
303 lines (235 loc) · 6.69 KB
/
black-readers.js
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
import classes from './black-classes.js'
export const idSymbol = Symbol('id')
function addBinaryReaderDebugInformation(object, reader) {
object.offset = reader.offset
object.remainingLength = reader.length
object.fileOffsetBuffer = reader.view.byteOffset - reader.fileStart
object.fileOffsetCurrent = object.fileOffsetBuffer + object.offset
if (reader.length >= 1) {
object.u8 = reader.copy().readU8()
}
if (reader.length >= 2) {
object.u16 = reader.copy().readU16()
try {
object.stringU16 = reader.copy().readStringU16()
} catch {}
}
if (reader.length >= 4) {
object.u32 = reader.copy().readU32()
object.f32 = reader.copy().readF32()
}
object.data = reader.copy().readU8Array(Math.min(reader.length, 64))
object.view = reader.view
}
class PropertyDataError extends Error {
constructor(type, propertyName, objectReader, error) {
super(error.message)
if (Error.captureStackTrace) {
Error.captureStackTrace(this, PropertyDataError);
}
this.name = "PropertyDataError"
this.type = type
this.propertyName = propertyName
this.error = error
addBinaryReaderDebugInformation(this, objectReader)
}
}
class UnknownClassError extends Error {
constructor(type) {
super(type)
if (Error.captureStackTrace) {
Error.captureStackTrace(this, UnknownClassError);
}
this.name = "UnknownClassError"
this.type = type
}
}
class UnknownPropertyError extends Error {
constructor(type, propertyName, objectReader) {
super(`"${propertyName}" for ${type}`)
if (Error.captureStackTrace) {
Error.captureStackTrace(this, UnknownPropertyError);
}
this.name = "UnknownPropertyError"
this.type = type
this.propertyName = propertyName
addBinaryReaderDebugInformation(this, objectReader)
}
}
export function array(reader) {
let count = reader.readU32()
let result = []
let debugContext = reader.debugContext
debugContext.group(`Array(count: ${count})`)
for (let i = 0; i < count; i++) {
result[i] = object(reader)
}
debugContext.groupEnd()
return result
}
array.complexReader = true
export function boolean(reader) {
return reader.readU8() != 0
}
export function byte(reader) {
return reader.readU8()
}
export function float(reader) {
return reader.readF32()
}
export function matrix4(reader) {
var buffer = new ArrayBuffer(64)
return [
vector4(reader, new Float32Array(buffer, 0, 4)),
vector4(reader, new Float32Array(buffer, 16, 4)),
vector4(reader, new Float32Array(buffer, 32, 4)),
vector4(reader, new Float32Array(buffer, 48, 4))
]
}
export function object(reader, id = null) {
let context = reader.context
let debugContext = reader.debugContext
if (arguments.length == 1) {
id = reader.readU32()
if (id == 0) {
return null
} else if (reader.references.has(id)) {
return reader.references.get(id)
}
}
let length = reader.readU32()
let objectReader = reader.readBinaryReader(length)
let type = objectReader.readStringU16()
debugContext.group(`object ${type}(id: ${id}, length: ${length})`)
let result = context.constructType(type)
result[idSymbol] = id
if (arguments.length == 1) {
reader.references.set(id, result)
}
if (!classes.has(type)) {
throw new UnknownClassError(type)
}
let properties = classes.get(type)
while (!objectReader.atEnd()) {
let propertyName = objectReader.readStringU16()
if (properties.has(propertyName)) {
try {
let propertyReader = properties.get(propertyName)
if (propertyReader.complexReader) {
debugContext.group(propertyName)
}
let value = propertyReader(objectReader)
if (result instanceof Map) {
result.set(propertyName, value)
} else {
result[propertyName] = value
}
if (propertyReader.complexReader) {
debugContext.groupEnd()
} else {
debugContext.property(propertyName, value)
}
} catch (e) {
//throw e;
if (e instanceof UnknownPropertyError) {
throw e
} else {
throw new PropertyDataError(type, propertyName, objectReader, e)
}
}
} else {
throw new UnknownPropertyError(type, propertyName, objectReader)
}
}
objectReader.expectEnd("object did not read to end?")
debugContext.groupEnd()
return result
}
object.complexReader = true
export function path(reader) {
let value = reader.readStringU16()
if (typeof reader.context.pathHandler == "function")
{
value = reader.context.pathHandler(value)
}
return value
}
export function rawObject(reader) {
return object(reader, null)
}
rawObject.complexReader = true
export function string(reader) {
return reader.readStringU16()
}
export function ushort(reader) {
return reader.readU16()
}
export function uint(reader) {
return reader.readU32()
}
export function vector2(reader, result = new Float32Array(2)) {
result[0] = reader.readF32()
result[1] = reader.readF32()
return result
}
export function vector3(reader, result = new Float32Array(3)) {
result[0] = reader.readF32()
result[1] = reader.readF32()
result[2] = reader.readF32()
return result
}
export function vector4(reader, result = new Float32Array(4)) {
result[0] = reader.readF32()
result[1] = reader.readF32()
result[2] = reader.readF32()
result[3] = reader.readF32()
return result
}
export function color(reader, result = new Float32Array(4)) {
result[0] = reader.readF32()
result[1] = reader.readF32()
result[2] = reader.readF32()
result[3] = reader.readF32()
if (typeof reader.context.color == "function") {
return reader.context.color(result)
}
return result;
}
export function quaternion(reader, result = new Float32Array(4)) {
result[0] = reader.readF32()
result[1] = reader.readF32()
result[2] = reader.readF32()
result[3] = reader.readF32()
if (typeof reader.context.quaternion == "function") {
return reader.context.quaternion(result);
}
return result;
}
// Complicated
export function indexBuffer(reader) {
let count = reader.readU32()
let byteSize = reader.readU16()
if (byteSize == 4) {
return reader.readU32Array(count)
} else {
throw "unsupported for now"
}
}
export function struct(struct) {
return function(reader) {
return struct.readStruct(reader)
}
}
export function structList(struct) {
return function(reader) {
let count = reader.readU32()
let byteSize = reader.readU16()
let result = []
for (let i = 0; i < count; i++) {
let structReader = reader.readBinaryReader(byteSize)
result[i] = struct.readStruct(structReader)
structReader.expectEnd("struct read to end")
}
return result
}
}