forked from OoTRandomizer/OoT-Randomizer
-
Notifications
You must be signed in to change notification settings - Fork 21
/
ntype.py
236 lines (181 loc) · 7.77 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
231
232
233
234
235
236
# Originally written by mzxrules
from __future__ import annotations
from collections.abc import Sequence
from typing import Optional
import struct
class uint16:
_struct = struct.Struct('>H')
@staticmethod
def write(buffer: bytearray, address: int, value: int) -> None:
struct.pack_into('>H', buffer, address, value)
@classmethod
def read(cls, buffer: bytearray, address: int = 0) -> int:
return cls._struct.unpack_from(buffer, address)[0]
@staticmethod
def bytes(value: int) -> bytearray:
value = value & 0xFFFF
return bytearray([(value >> 8) & 0xFF, value & 0xFF])
@staticmethod
def value(values: Sequence[int]) -> int:
return ((values[0] & 0xFF) << 8) | (values[1] & 0xFF)
class uint32:
_struct = struct.Struct('>I')
@staticmethod
def write(buffer: bytearray, address: int, value: int) -> None:
struct.pack_into('>I', buffer, address, value)
@classmethod
def read(cls, buffer: bytearray, address: int = 0) -> int:
return cls._struct.unpack_from(buffer, address)[0]
@staticmethod
def bytes(value: int) -> bytearray:
value = value & 0xFFFFFFFF
return bytearray([(value >> 24) & 0xFF, (value >> 16) & 0xFF, (value >> 8) & 0xFF, value & 0xFF])
@staticmethod
def value(values: Sequence[int]) -> int:
return ((values[0] & 0xFF) << 24) | ((values[1] & 0xFF) << 16) | ((values[2] & 0xFF) << 8) | (values[3] & 0xFF)
class int32:
_struct = struct.Struct('>i')
@staticmethod
def write(buffer: bytearray, address: int, value: int) -> None:
struct.pack_into('>i', buffer, address, value)
@classmethod
def read(cls, buffer: bytearray, address: int = 0) -> int:
return cls._struct.unpack_from(buffer, address)[0]
@staticmethod
def bytes(value: int) -> bytearray:
value = value & 0xFFFFFFFF
return bytearray([(value >> 24) & 0xFF, (value >> 16) & 0xFF, (value >> 8) & 0xFF, value & 0xFF])
@staticmethod
def value(values: Sequence[int]) -> int:
value: int = ((values[0] & 0xFF) << 24) | ((values[1] & 0xFF) << 16) | ((values[2] & 0xFF) << 8) | (values[3] & 0xFF)
if value >= 0x80000000:
value ^= 0xFFFFFFFF
value += 1
return value
class uint24:
@staticmethod
def write(buffer: bytearray, address: int, value: int) -> None:
byte_arr: bytes = bytes(value)
buffer[address:address + 3] = byte_arr[0:3]
@staticmethod
def read(buffer: bytearray, address: int = 0) -> int:
return (buffer[address+0] << 16) | (buffer[address+1] << 8) | buffer[address+2]
@staticmethod
def bytes(value: int) -> bytearray:
value = value & 0xFFFFFF
return bytearray([(value >> 16) & 0xFF, (value >> 8) & 0xFF, value & 0xFF])
@staticmethod
def value(values: Sequence[int]) -> int:
return ((values[0] & 0xFF) << 16) | ((values[1] & 0xFF) << 8) | (values[2] & 0xFF)
class BigStream:
def __init__(self, buffer: bytearray) -> None:
self.last_address: int = 0
self.buffer: bytearray = buffer
def seek_address(self, address: Optional[int] = None, delta: Optional[int] = None) -> None:
if address is not None:
self.last_address = address
if delta is not None:
self.last_address += delta
def eof(self) -> bool:
return self.last_address >= len(self.buffer)
def read_byte(self, address: Optional[int] = None) -> int:
if address is None:
address = self.last_address
self.last_address = address + 1
return self.buffer[address]
def read_bytes(self, address: Optional[int] = None, length: int = 1) -> bytearray:
if address is None:
address = self.last_address
self.last_address = address + length
return self.buffer[address: address + length]
def read_int16(self, address: Optional[int] = None) -> int:
if address is None:
address = self.last_address
return uint16.value(self.read_bytes(address, 2))
def read_int24(self, address: Optional[int] = None) -> int:
if address is None:
address = self.last_address
return uint24.value(self.read_bytes(address, 3))
def read_int32(self, address: Optional[int] = None) -> int:
if address is None:
address = self.last_address
return uint32.value(self.read_bytes(address, 4))
def write_byte(self, address: Optional[int], value: int) -> None:
if address is None:
address = self.last_address
self.buffer[address] = value
self.last_address = address + 1
def write_sbyte(self, address: Optional[int], value: int) -> None:
if address is None:
address = self.last_address
self.write_bytes(address, struct.pack('b', value))
def write_int16(self, address: Optional[int], value: int) -> None:
if address is None:
address = self.last_address
self.write_bytes(address, uint16.bytes(value))
def write_int24(self, address: Optional[int], value: int) -> None:
if address is None:
address = self.last_address
self.write_bytes(address, uint24.bytes(value))
def write_int32(self, address: Optional[int], value: int) -> None:
if address is None:
address = self.last_address
self.write_bytes(address, uint32.bytes(value))
def write_f32(self, address: Optional[int], value: float) -> None:
if address is None:
address = self.last_address
self.write_bytes(address, struct.pack('>f', value))
def write_bytes(self, address: Optional[int], values: Sequence[int]) -> None:
if address is None:
address = self.last_address
self.last_address = address + len(values)
self.buffer[address:address + len(values)] = values
def write_int16s(self, address: Optional[int], values: Sequence[int]) -> None:
if address is None:
address = self.last_address
i: int
value: int
for i, value in enumerate(values):
self.write_int16(address + (i * 2), value)
def write_int24s(self, address: Optional[int], values: Sequence[int]) -> None:
if address is None:
address = self.last_address
i: int
value: int
for i, value in enumerate(values):
self.write_int24(address + (i * 3), value)
def write_int32s(self, address: Optional[int], values: Sequence[int]) -> None:
if address is None:
address = self.last_address
i: int
value: int
for i, value in enumerate(values):
self.write_int32(address + (i * 4), value)
def append_byte(self, value: int) -> None:
self.buffer.append(value)
def append_sbyte(self, value: int) -> None:
self.append_bytes(struct.pack('b', value))
def append_int16(self, value: int) -> None:
self.append_bytes(uint16.bytes(value))
def append_int24(self, value: int) -> None:
self.append_bytes(uint24.bytes(value))
def append_int32(self, value: int) -> None:
self.append_bytes(uint32.bytes(value))
def append_f32(self, value: float) -> None:
self.append_bytes(struct.pack('>f', value))
def append_bytes(self, values: Sequence[int]) -> None:
value: int
for value in values:
self.append_byte(value)
def append_int16s(self, values: Sequence[int]) -> None:
value: int
for value in values:
self.append_int16(value)
def append_int24s(self, values: Sequence[int]) -> None:
value: int
for value in values:
self.append_int24(value)
def append_int32s(self, values: Sequence[int]) -> None:
value: int
for value in values:
self.append_int32(value)