-
Notifications
You must be signed in to change notification settings - Fork 2
/
io_binary.py
244 lines (190 loc) · 6.81 KB
/
io_binary.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
import struct
import binascii
class BinaryReader:
def __init__(self, stream):
self.file = stream
def close(self):
self.file.close()
def seek(self, position):
self.file.seek(position)
def seekBy(self, value):
self.file.seek(self.file.tell() + value)
def tell(self):
return self.file.tell()
# reading
def readHex(self, length):
hexdata = self.file.read(length)
hexstr = ""
for h in hexdata:
hexstr = hex(h)[2:].zfill(2) + hexstr
return hexstr
def readInt64(self):
return struct.unpack("q", self.file.read(8))[0]
def readUInt64(self):
return struct.unpack("Q", self.file.read(8))[0]
def readInt(self):
return struct.unpack("i", self.file.read(4))[0]
def readUInt(self):
return struct.unpack("I", self.file.read(4))[0]
def readUShort(self):
return struct.unpack("H", self.file.read(2))[0]
def readShort(self):
return struct.unpack("h", self.file.read(2))[0]
def readByte(self):
return struct.unpack("b", self.file.read(1))[0]
def readUByte(self):
return struct.unpack("B", self.file.read(1))[0]
def readFloat(self):
return struct.unpack("!f", bytes.fromhex(self.readHex(4)))[0]
def readShortVec(self, size):
vec = [0] * size
for i in range(size):
vec[i] = self.readShort()
return vec
def readShortQuantizedVec(self, size):
vec = [0] * size
for i in range(size):
vec[i] = self.readShort() / 0x7FFF
return vec
def readShortQuantizedVecScaledBiased(self, size, scale, bias):
vec = [0] * size
for i in range(size):
vec[i] = ((self.readShort() * scale[i]) / 0x7FFF) + bias[i]
return vec
def readUByteQuantizedVec(self, size):
vec = [0] * size
for i in range(size):
vec[i] = ((self.readUByte() * 2) / 255) - 1
return vec
def readUByteVec(self, size):
vec = [0] * size
for i in range(size):
vec[i] = self.readUByte()
return vec
def peekHex(self, length):
hexdata = self.file.read(length)
hexstr = ""
for h in hexdata:
hexstr = hex(h)[2:].zfill(2) + hexstr
file.seek(file.tell() - length)
return hexstr
def readUShortToFloatVec(self, size):
vec = [0] * size
for i in range(size):
vec[i] = ((self.readUShort() * float(2.0)) / float(65535.0)) - float(1.0)
return vec
def readFloatVec(self, size):
vec = [0] * size
for i in range(size):
vec[i] = self.readFloat()
return vec
def readUBytesToBitBoolArray(self, size):
bit_array = ""
bool_array = []
bytes = self.readUByteVec(size)
for i in range(size):
bit_array = bin(bytes[i])[2:].zfill(8) + bit_array
for i in bit_array[::-1]:
bool_array.append(True if i == "1" else False)
return {"count": sum(bool_array), "bones": bool_array}
def readUIntVec(self, size):
vec = [0] * size
for i in range(size):
vec[i] = self.readUInt()
return vec
def readIntVec(self, size):
vec = [0] * size
for i in range(size):
vec[i] = self.readInt()
return vec
def readString(self, length):
string = []
for char in range(length):
c = self.file.read(1)
if c != b"\x00":
string.append(c)
return b"".join(string)
def readCString(self):
string = []
while True:
c = self.file.read(1)
if c == b"\x00":
return b"".join(string)
string.append(c)
# writing
def align(self, num, bit=0x0):
padding = (num - (self.tell() % num)) % num
self.writeHex(bytes([bit] * padding))
def writeHex(self, bytes):
self.file.write(bytes)
def writeInt64(self, val):
self.file.write(val.to_bytes(8, byteorder="little", signed=True))
def writeUInt64(self, val):
self.file.write(val.to_bytes(8, byteorder="little", signed=False))
def writeInt(self, val):
self.file.write(val.to_bytes(4, byteorder="little", signed=True))
def writeUInt(self, val):
self.file.write(val.to_bytes(4, byteorder="little", signed=False))
def writeShort(self, val):
self.file.write(val.to_bytes(2, byteorder="little", signed=True))
def writeUShort(self, val):
self.file.write(val.to_bytes(2, byteorder="little", signed=False))
def writeByte(self, val):
self.file.write(val.to_bytes(1, byteorder="little", signed=True))
def writeUByte(self, val):
self.file.write(val.to_bytes(1, byteorder="little", signed=False))
def writeFloat(self, val):
self.file.write(struct.pack("f", val))
def writeShortVec(self, vec):
for val in vec:
self.writeShort(val)
def writeShortQuantizedVec(self, vec):
for i in range(len(vec)):
self.writeShort(int(round(vec[i] * 0x7FFF)))
def writeShortQuantizedVecScaledBiased(self, vec, scale, bias):
for i in range(len(vec)):
value = int(round(((vec[i] - bias[i]) * 0x7FFF) / scale[i]))
if value > 0x7FFF:
value = 0x7FFF
elif value < -0x7FFF:
value = -0x7FFF
self.writeShort(value)
def writeUByteQuantizedVec(self, vec):
for val in vec:
self.writeUByte(int(round(((val + 1) * 255) / 2)))
def writeUByteVec(self, vec):
for val in vec:
self.writeUByte(val)
def writeUShortFromFloatVec(self, vec):
for val in vec:
self.writeUShort(int(round(((val + 1) * 0xFFFF) / 2)))
def writeFloatVec(self, vec):
for val in vec:
self.writeFloat(val)
def writeUBytesFromBitBoolArray(self, vec):
print(
"Attempted to use writeUBytesFromBitBoolArray, but this function is not implemented yet!"
)
def writeUIntVec(self, vec):
for val in vec:
self.writeUInt(val)
def writeIntVec(self, vec):
for val in vec:
self.writeInt(val)
def writeCString(self, string):
if len(string) > 0:
self.writeHex(string)
self.writeUByte(0)
def writeString(self, string, length):
self.writeHex(string)
self.writeUByteVec([0] * (length - len(string)))
def IOI_round(self, float):
# please upgrade to a modulo solution
byte_const = 1 / 255
rounded_byte = 0
byte = int(float / byte_const)
if abs(float - (byte * byte_const)) > abs(float - ((byte + 1) * byte_const)):
rounded_byte = byte
else:
rounded_byte = byte + 1
return rounded_byte - 1