forked from niacdoial/blemd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryReader.py
407 lines (322 loc) · 12 KB
/
BinaryReader.py
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#! /usr/bin/python3
import os
from .common import MessageBox, newfile
from array import array
import logging
log = logging.getLogger('bpy.ops.import_mesh.bmd.fileIn')
def bitshiftu8(val, shifter):
if val // 256:
raise ValueError("not a U8")
a8 = val & 0b00000001
a7 = val & 0b00000010
a6 = val & 0b00000100
a5 = val & 0b00001000
a4 = val & 0b00010000
a3 = val & 0b00100000
a2 = val & 0b01000000
a1 = val & 0b10000000
class ReturnValue:
# <variable srcPos>
# <variable dstPos>
# -- int
def __init__(self): # GENERATED!
self.srcPos = 0
self.dstPos = 0
def __iadd__(self, other):
self.srcPos += other.srcPos
self.dstPos += other.dstPos
def decodeBlock(src, dst):
currCodeByte = None # u8
r = ReturnValue()
pos = dst.tell()
dst.seek(0, 2) # end
currByteCode = src.read(1)
if currByteCode == b'':
# EOF
raise EOFError()
r.srcPos += 1
for _ in range(8): # bytecode is for 8 "instructions"
if (currCodeByte & 0x80) != 0:
# straight copy
temp = src.read(1)
if temp == b'':
break # EOF
dst.write(temp)
r.dstPos += 1
r.srcPos += 1
else:
# RLE part
byte1 = src.read(1) # u4 + u12, little-end
byte2 = src.read(1) # (second part)
r.srcPos += 2
dist = ((ord(byte1) & 0xF) << 8) | ord(byte2) # -- u32 ((byte1 & 0xF) << 8) | byte2;
copySource = dst.tell() - (dist + 1) # -- u32 copySource = r.dstPos - (dist + 1);
if copySource < 0:
# MessageBox ("copySource < 0 ??? " + str(r.dstPos) +":"+ str(dist) +":"+ str(copySource))
raise ValueError("ERROR")
numBytes = ord(byte1) >> 4 # u32 byte1 >> 4
if numBytes == 0:
#if r.srcPos >= srcSize:
# return r
numBytes = ord(src.read(1)) + 0x12
r.srcPos += 1
else:
numBytes += 2
# copy run
dst.seek(copySource)
temp = b''
#if r.dstPos >= uncompressedSize:
# return r
temp += dst.read(numBytes)
r.dstPos += len(temp)
dst.seek(0, 2) # end
dst.write(temp)
# use next bit from "code" byte
currCodeByte <<= 1 # currCodeByte <<= 1
dst.seek(pos)
return r
class CompressedStream:
def __init__(self, fileobj):
self.cf = fileobj
self.cursor = ReturnValue
self.tempFileName = os.path.splitext(self.cf.name)[0] + ".tempYaz0"
newfile(self.tempFileName)
self.cursor.dstPos = os.stat(self.tempFileName).st_size
self.uf = open(self.tempFileName, 'rb+')
def read(self, length):
while self.cursor.dstPos - self.uf.tell() < length:
if self.cursor.srcPos == 0:
# in case of existing cached data not long enough, recreate it
self.cursor.dstPos = 0
self.uf.close()
self.uf = open(self.tempFileName, 'wb+')
try:
self.cursor += decodeBlock(self.cf, self.uf)
except Exception as err:
log.warning("reached compressed stream EOF: %s", err)
return self.uf.read(length)
def close(self):
self.cf.close()
self.uf.close()
def tell(self):
return self.uf.tell()
def seek(self, where, mode=0):
if mode == 1: # cursor
while where > self.cursor.dstPos - self.uf.tell():
self.cursor += decodeBlock(self.cf, self.uf)
self.uf.seek(where, 1)
elif mode == 0: # from beginning
while where > self.cursor.dstPos:
self.cursor += decodeBlock(self.cf, self.uf)
self.uf.seek(where)
else:
raise ValueError('plz impelment seek_2 4 copmresed strim')
class BinaryReader:
"""# <variable _f>
# -- binary file stream
# <variable _size>
# <variable _tempFileName>
# <function>
# <function>
# -- get a string with a fixed size
# <function>
# -- 4 bytes, unsigned int
# <function>
# <function>
# -- TODO: Dosn't work
# <function>
# -- seek with an offset to the current position
# <function>
# -- seek to an absolute position
# <function>
# <function>
# <function>
# -- read a null terminated string from an absolute offset
# <function>
# -- 2 bytes, unsigned short
# <function>
# -- returns string[]
# <function>
# -- 2 bytes, signed short. Using two's complement
# <function>
# -- must reverse value for read. big endian [could also write / read from another file]
# -- http://java.sun.com/j2se/1.3/docs/api/java/lang/Float.html#intBitsToFloat(int)
# <function>"""
def __init__(self): # GENERATED!
pass
def Close(self):
self._f.close()
if hasattr(self, "_tempFileName"):
pass # os.remove(self._tempFileName) # DEBUG: plz uncomment this when tests finished
def DecodeYaz0(self, src, srcSize, dst, uncompressedSize):
r = ReturnValue() # current read/write positions
validBitCount = 0 # u32 number of valid bits left in "code" byte
currCodeByte = None # u8
while r.dstPos < uncompressedSize:
# read new "code" byte if the current one is used up
if validBitCount == 0:
if r.srcPos >= srcSize:
return r
currCodeByte = src[r.srcPos]
r.srcPos += 1
validBitCount = 8
if (currCodeByte & 0x80) != 0:
# straight copy
if r.srcPos >= srcSize:
return r
dst[r.dstPos] = src[r.srcPos]
r.dstPos += 1
r.srcPos += 1
else:
# RLE part
if r.srcPos >= srcSize - 1:
return r
byte1 = src[r.srcPos] # u8
byte2 = src[r.srcPos + 1] # u8
r.srcPos += 2
dist = ((byte1 & 0xF)<<8) | byte2 # -- u32 ((byte1 & 0xF) << 8) | byte2;
copySource = r.dstPos - (dist + 1) # -- u32 copySource = r.dstPos - (dist + 1);
if copySource < 0:
# MessageBox ("copySource < 0 ??? " + str(r.dstPos) +":"+ str(dist) +":"+ str(copySource))
raise ValueError("ERROR")
numBytes = byte1 >> 4 # u32 byte1 >> 4
if numBytes == 0:
if r.srcPos >= srcSize:
return r
numBytes = src[r.srcPos] + 0x12
r.srcPos += 1
else:
numBytes += 2
# -- copy run
for i in range(numBytes):
if r.dstPos >= uncompressedSize:
return r
dst[r.dstPos] = dst[copySource]
copySource += 1
r.dstPos += 1
# use next bit from "code" byte
currCodeByte <<= 1 # -- currCodeByte <<= 1
validBitCount -= 1
return r
def ReadFixedLengthString(self, len):
strRead = b""
for _ in range(len):
strRead += (self._f.read(1))
return strRead.decode('cp1252')
def ReadDWORD(self):
w1 = ord(self._f.read(1))
w2 = ord(self._f.read(1))
w3 = ord(self._f.read(1))
w4 = ord(self._f.read(1))
d = (w1 << 24) | (w2 << 16)
d |= w3 << 8
d |= w4
return d
def Open(self, srcPath, compressed_stream=False):
self._f = open(srcPath, "rb+")
self.read = self._f.read
# --fseek self._f 0 seek_end
# --self._size = ftell self._f
self._f.seek(0)
self.filesz = os.stat(self._f.fileno()).st_size
if self._f is None:
log.fatal("Unable to open file " + srcPath)
raise ValueError("Unable to open file " + srcPath)
tag = self.ReadFixedLengthString(4)
# self._f.seek(0)
if tag != "Yaz0":
return # not compressed, return file directly
# yaz0-compressed file - uncompress as read, for optimisation, or decompress first
elif compressed_stream == False:
uncompressedSize = self.ReadDWORD()
compressedSize = len(self._f.read()) - 8 # -- 16 byte header (including "Yaz0" and size)
self._f.seek(16) # -- seek to start of data
srcData = array('B') # [0]*compressedSize # two arrays of u8s
# --srcData[compressedSize] = 0 -- Pre-initialize array size
dstData = array('B') # [0]*uncompressedSize
# --dstData[uncompressedSize] = 0 -- Pre-initialize array size
for _ in range(uncompressedSize):
dstData.append(0)
for _ in range(compressedSize):
srcData.append(ord(self._f.read(1)))
self._f.close()
r = self.DecodeYaz0(srcData, compressedSize, dstData, uncompressedSize)
# --write decompressed data to a temporary file and
# --return handle to this file
self._tempFileName = os.path.splitext(srcPath)[0] + ".tempYaz0"
self._f = open(self._tempFileName, "wb") # -- creates file if not found
for i in range(r.dstPos):
self._f.write(dstData[i].to_bytes(1, 'big'))
self._f.close()
# -- open temp file for reading
self._f = open(self._tempFileName, "rb+")
self._f.seek(0)
elif compressed_stream:
self._f = CompressedStream(self._f)
def is_eof(self):
return self._f.tell() >= self.filesz
# return self._f.tell() >= self._size
def SeekCur(self, offset):
self._f.seek(self._f.tell() + offset)
def SeekSet(self, position):
self._f.seek(position)
def Position(self):
return self._f.tell()
def GetByte(self):
return ord(self._f.read(1))
def ReadString(self, offset):
t = self._f.tell()
self._f.seek(offset)
strRead = b""
c = self._f.read(1)
strRead += c
while c != b'\x00':
c = self._f.read(1)
strRead += c
strRead = strRead[:-1] # strip final '\x00'
self._f.seek(t)
return strRead.decode('cp1252')
def ReadWORD(self):
w1 = ord(self._f.read(1))
w2 = ord(self._f.read(1))
w = (w1 << 8) | w2 # -- w = (w1 << 8) | w2;
if w < 0:
log.fatal("ReadWORD should be unsigned")
raise ValueError("ReadWORD should be unsigned")
return w
def ReadStringTable(self, pos):
oldPos = self.Position()
self.SeekSet(pos)
count = self.ReadWORD() # -- orig = 35 read = 35. current pos = ok?
unknown1 = self.ReadWORD() # -- skip pad bytes
result = []
for _ in range(count):
unknown = self.ReadWORD()
stringOffset = self.ReadWORD()
s = self.ReadString(pos + stringOffset)
result.append(s)
self.SeekSet(oldPos)
return result
def GetSHORT(self):
signedValue = self.ReadWORD() # -- unsigned
negativeSign = signedValue // (2**15)
if negativeSign :
# -- flip bits and add 1
# -- signedValue = bit.not signedValue // Dosn't work?
signedValue = signedValue ^ 65535 # -- 65535 == all 1's
signedValue += 1
signedValue = signedValue * -1
return signedValue
def GetFloat(self):
bits = self.ReadDWORD()
s =-1
if (bits >> 31) == 0 :
s = 1
e = (bits >> 23) & 0xff
m = 0
if e == 0 :
m =(bits & 0x7fffff) << 1
else:
m = (bits & 0x7fffff) | 0x800000
fx = s * m * (2**(e-150))
return fx