forked from OoTRandomizer/OoT-Randomizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ntype.py
230 lines (152 loc) · 6.1 KB
/
ntype.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
# Written by mzxrules
import struct
class uint16:
_struct = struct.Struct('>H')
def write(buffer, address, value):
struct.pack_into('>H', buffer, address, value)
def read(buffer, address=0):
return uint16._struct.unpack_from(buffer, address)[0]
def bytes(value):
value = value & 0xFFFF
return [(value >> 8) & 0xFF, value & 0xFF]
def value(values):
return (values[0] << 8) | values[1]
class uint32:
_struct = struct.Struct('>I')
def write(buffer, address, value):
struct.pack_into('>I', buffer, address, value)
def read(buffer, address=0):
return uint32._struct.unpack_from(buffer, address)[0]
def bytes(value):
value = value & 0xFFFFFFFF
return [(value >> 24) & 0xFF, (value >> 16) & 0xFF, (value >> 8) & 0xFF, value & 0xFF]
def value(values):
return (values[0] << 24) | (values[1] << 16) | (values[2] << 8) | values[3]
class int32:
_struct = struct.Struct('>i')
def write(buffer, address, value):
struct.pack_into('>i', buffer, address, value)
def read(buffer, address=0):
return int32._struct.unpack_from(buffer, address)[0]
def bytes(value):
value = value & 0xFFFFFFFF
return [(value >> 24) & 0xFF, (value >> 16) & 0xFF, (value >> 8) & 0xFF, value & 0xFF]
def value(values):
value = (values[0] << 24) | (values[1] << 16) | (values[2] << 8) | values[3]
if value >= 0x80000000:
value ^= 0xFFFFFFFF
value += 1
return value
class uint24:
def write(buffer, address, value):
byte_arr = bytes(value)
buffer[address:address + 3] = byte_arr[0:3]
def read(buffer, address=0):
return (buffer[address+0] << 16) | (buffer[address+1] << 8) | buffer[address+2]
def bytes(value):
value = value & 0xFFFFFF
return [(value >> 16) & 0xFF, (value >> 8) & 0xFF, value & 0xFF]
def value(values):
return (values[0] << 16) | (values[1] << 8) | values[2]
class BigStream(object):
def __init__(self, buffer:bytearray):
self.last_address = 0
self.buffer = buffer
def seek_address(self, address=None, delta=None):
if delta is None:
self.last_address = address
else:
self.last_address += delta
def eof(self):
return self.last_address >= len(self.buffer)
def read_byte(self, address=None):
if address == None:
address = self.last_address
self.last_address = address + 1
return self.buffer[address]
def read_bytes(self, address=None, length=1):
if address == None:
address = self.last_address
self.last_address = address + length
return self.buffer[address : address + length]
def read_int16(self, address=None):
if address == None:
address = self.last_address
return uint16.value(self.read_bytes(address, 2))
def read_int24(self, address=None):
if address == None:
address = self.last_address
return uint24.value(self.read_bytes(address, 3))
def read_int32(self, address=None):
if address == None:
address = self.last_address
return uint32.value(self.read_bytes(address, 4))
def write_byte(self, address, value):
if address == None:
address = self.last_address
self.buffer[address] = value
self.last_address = address + 1
def write_sbyte(self, address, value):
if address == None:
address = self.last_address
self.write_bytes(address, struct.pack('b', value))
def write_int16(self, address, value):
if address == None:
address = self.last_address
self.write_bytes(address, uint16.bytes(value))
def write_int24(self, address, value):
if address == None:
address = self.last_address
self.write_bytes(address, uint24.bytes(value))
def write_int32(self, address, value):
if address == None:
address = self.last_address
self.write_bytes(address, uint32.bytes(value))
def write_f32(self, address, value:float):
if address == None:
address = self.last_address
self.write_bytes(address, struct.pack('>f', value))
def write_bytes(self, startaddress, values):
if startaddress == None:
startaddress = self.last_address
self.last_address = startaddress + len(values)
self.buffer[startaddress:startaddress + len(values)] = values
def write_int16s(self, startaddress, values):
if startaddress == None:
startaddress = self.last_address
for i, value in enumerate(values):
self.write_int16(startaddress + (i * 2), value)
def write_int24s(self, startaddress, values):
if startaddress == None:
startaddress = self.last_address
for i, value in enumerate(values):
self.write_int24(startaddress + (i * 3), value)
def write_int32s(self, startaddress, values):
if startaddress == None:
startaddress = self.last_address
for i, value in enumerate(values):
self.write_int32(startaddress + (i * 4), value)
def append_byte(self, value):
self.buffer.append(value)
def append_sbyte(self, value):
self.append_bytes(struct.pack('b', value))
def append_int16(self, value):
self.append_bytes(uint16.bytes(value))
def append_int24(self, value):
self.append_bytes(uint24.bytes(value))
def append_int32(self, value):
self.append_bytes(uint32.bytes(value))
def append_f32(self, value:float):
self.append_bytes(struct.pack('>f', value))
def append_bytes(self, values):
for value in values:
self.append_byte(value)
def append_int16s(self, values):
for value in values:
self.append_int16(value)
def append_int24s(self, values):
for value in values:
self.append_int24(value)
def append_int32s(self, values):
for value in values:
self.append_int32(value)