forked from bepo13/destiny-stl-generator-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataParse.py
61 lines (46 loc) · 1.9 KB
/
DataParse.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
import io
import struct
import numpy as np
def VertexParse(dataBytes, datatype, offset, stride):
dataOut = []
i = offset
while i < len(dataBytes):
if datatype == "_vertex_format_attribute_short4":
dataOut.append(np.array(struct.unpack('<hhhh', dataBytes[i:i+8])))
elif datatype == "_vertex_format_attribute_float4":
dataOut.append(np.array(struct.unpack('<ffff', dataBytes[i:i+16])))
else:
print("Unknown type, please file an issue for this item...")
return
i += stride
return dataOut
class DataParse:
def __init__(self, byteData):
self.data = io.BytesIO(byteData)
return
def seek(self, address):
self.data.seek(address, 0)
return
def read(self, length):
return self.data.read(length)
def readString(self, length):
return self.data.read(length).decode('utf-8').rstrip('\0')
def readUTF(self):
length = int.from_bytes(self.data.read(2), byteorder='little')
return self.data.read(length).decode('utf-8')
def readInt8(self):
return struct.unpack('<b', self.data.read(1))[0]
def readInt16(self):
return struct.unpack('<h', self.data.read(2))[0]
def readInt32(self):
return struct.unpack('<l', self.data.read(4))[0]
def readInt64(self):
return struct.unpack('<q', self.data.read(8))[0]
def readFloat(self):
return struct.unpack('f', self.data.read(4))[0]
def readVector2D(self):
return np.array([self.readFloat(), self.readFloat()], dtype='float')
def readVector3D(self):
return np.array([self.readFloat(), self.readFloat(), self.readFloat()], dtype='float')
def readVector4D(self):
return np.array([self.readFloat(), self.readFloat(), self.readFloat(), self.readFloat()], dtype='float')