-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathips.py
executable file
·287 lines (231 loc) · 11.2 KB
/
ips.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# This code is largely copied from https://github.com/nleseul/ips_util
import itertools
import sys
class Patch:
@staticmethod
def load(filename):
loaded_patch = Patch()
with open(filename, 'rb') as file:
header = file.read(5)
if header != 'PATCH'.encode('ascii'):
raise Exception('Not a valid IPS patch file!')
while True:
address_bytes = file.read(3)
if address_bytes == 'EOF'.encode('ascii'):
break
address = int.from_bytes(address_bytes, byteorder='big')
length = int.from_bytes(file.read(2), byteorder='big')
rle_count = 0
if length == 0:
rle_count = int.from_bytes(file.read(2), byteorder='big')
length = 1
data = file.read(length)
if rle_count > 0:
loaded_patch.add_rle_record(address, data, rle_count)
else:
loaded_patch.add_record(address, data)
truncate_bytes = file.read(3)
if len(truncate_bytes) == 3:
loaded_patch.set_truncate_length(int.from_bytes(truncate_bytes, byteorder='big'))
return loaded_patch
@staticmethod
def create(original_data, patched_data):
# The heuristics for optimizing a patch were chosen with reference to
# the source code of Flips: https://github.com/Alcaro/Flips
patch = Patch()
run_in_progress = False
current_run_start = 0
current_run_data = bytearray()
runs = []
if len(original_data) > len(patched_data):
patch.set_truncate_length(len(patched_data))
original_data = original_data[:len(patched_data)]
elif len(original_data) < len(patched_data):
original_data += bytes([0] * (len(patched_data) - len(original_data)))
if original_data[-1] == 0 and patched_data[-1] == 0:
patch.add_record(len(patched_data) - 1, bytes([0]))
for index, (original, patched) in enumerate(zip(original_data, patched_data)):
if not run_in_progress:
if original != patched:
run_in_progress = True
current_run_start = index
current_run_data = bytearray([patched])
else:
if original == patched:
runs.append((current_run_start, current_run_data))
run_in_progress = False
else:
current_run_data.append(patched)
if run_in_progress:
runs.append((current_run_start, current_run_data))
for start, data in runs:
if start == int.from_bytes(b'EOF', byteorder='big'):
start -= 1
data = bytes([patched_data[start - 1]]) + data
grouped_byte_data = list([
{'val': key, 'count': sum(1 for _ in group), 'is_last': False}
for key,group in itertools.groupby(data)
])
grouped_byte_data[-1]['is_last'] = True
record_in_progress = bytearray()
pos = start
for group in grouped_byte_data:
if len(record_in_progress) > 0:
# We don't want to interrupt a record in progress with a new header unless
# this group is longer than two complete headers.
if group['count'] > 13:
patch.add_record(pos, record_in_progress)
pos += len(record_in_progress)
record_in_progress = bytearray()
patch.add_rle_record(pos, bytes([group['val']]), group['count'])
pos += group['count']
else:
record_in_progress += bytes([group['val']] * group['count'])
elif (group['count'] > 3 and group['is_last']) or group['count'] > 8:
# We benefit from making this an RLE record if the length is at least 8,
# or the length is at least 3 and we know it to be the last part of this diff.
# Make sure not to overflow the maximum length. Split it up if necessary.
remaining_length = group['count']
while remaining_length > 0xffff:
patch.add_rle_record(pos, bytes([group['val']]), 0xffff)
remaining_length -= 0xffff
pos += 0xffff
patch.add_rle_record(pos, bytes([group['val']]), remaining_length)
pos += remaining_length
else:
# Just begin a new standard record.
record_in_progress += bytes([group['val']] * group['count'])
if len(record_in_progress) > 0xffff:
patch.add_record(pos, record_in_progress[:0xffff])
record_in_progress = record_in_progress[0xffff:]
pos += 0xffff
# Finalize any record still in progress.
if len(record_in_progress) > 0:
patch.add_record(pos, record_in_progress)
return patch
def __init__(self):
self.records = []
self.truncate_length = None
def add_record(self, address, data):
if address == int.from_bytes(b'EOF', byteorder='big'):
raise RuntimeError('Start address {0:x} is invalid in the IPS format. Please shift your starting address back by one byte to avoid it.'.format(address))
if address > 0xffffff:
raise RuntimeError('Start address {0:x} is too large for the IPS format. Addresses must fit into 3 bytes.'.format(address))
if len(data) > 0xffff:
raise RuntimeError('Record with length {0} is too large for the IPS format. Records must be less than 65536 bytes.'.format(len(data)))
record = {'address': address, 'data': data}
self.records.append(record)
def add_rle_record(self, address, data, count):
if address == int.from_bytes(b'EOF', byteorder='big'):
raise RuntimeError('Start address {0:x} is invalid in the IPS format. Please shift your starting address back by one byte to avoid it.'.format(address))
if address > 0xffffff:
raise RuntimeError('Start address {0:x} is too large for the IPS format. Addresses must fit into 3 bytes.'.format(address))
if count > 0xffff:
raise RuntimeError('RLE record with length {0} is too large for the IPS format. RLE records must be less than 65536 bytes.'.format(count))
if len(data) != 1:
raise RuntimeError('Data for RLE record must be exactly one byte! Received {0}.'.format(data))
record = {'address': address, 'data': data, 'rle_count': count}
self.records.append(record)
def set_truncate_length(self, truncate_length):
self.truncate_length = truncate_length
def trace(self):
print('''Start End Size Data
------ ------ ----- ----''')
for record in self.records:
length = (record['rle_count'] if 'rle_count' in record else len(record['data']))
data = ''
if 'rle_count' in record:
data = '{0} x{1}'.format(record['data'].hex(), record['rle_count'])
elif len(record['data']) < 20:
data = record['data'].hex()
else:
data = record['data'][0:24].hex() + '...'
print('{0:06x} {1:06x} {2:>5} {3}'.format(record['address'], record['address'] + length - 1, length, data))
if self.truncate_length is not None:
print()
print('Truncate to {0} bytes'.format(self.truncate_length))
def encode(self):
encoded_bytes = bytearray()
encoded_bytes += 'PATCH'.encode('ascii')
for record in self.records:
encoded_bytes += record['address'].to_bytes(3, byteorder='big')
if 'rle_count' in record:
encoded_bytes += (0).to_bytes(2, byteorder='big')
encoded_bytes += record['rle_count'].to_bytes(2, byteorder='big')
else:
encoded_bytes += len(record['data']).to_bytes(2, byteorder='big')
encoded_bytes += record['data']
encoded_bytes += 'EOF'.encode('ascii')
if self.truncate_length is not None:
encoded_bytes += self.truncate_length.to_bytes(3, byteorder='big')
return encoded_bytes
def apply(self, in_data):
out_data = bytearray(in_data)
for record in self.records:
if record['address'] >= len(out_data):
out_data += bytes([0] * (record['address'] - len(out_data) + 1))
if 'rle_count' in record:
out_data[record['address'] : record['address'] + record['rle_count']] = b''.join([record['data']] * record['rle_count'])
else:
out_data[record['address'] : record['address'] + len(record['data'])] = record['data']
if self.truncate_length is not None:
out_data = out_data[:self.truncate_length]
return out_data
baseroms = ['pokecrystal', 'pokecrystal11', 'pokecrystal_au']
patches = ['practice', 'practice_igt']
valid_cmds = ['create', 'apply']
src = 'pokecrystal_au' # use AU ROM as source for creating IPS patch
dest = 'pokecrystal' # create pokecrystal.ips (and pokecrystal_igt.ips) that work on all baseroms
def create(baserom_file, patchedrom_file, ips_file):
source_file = None
target_file = None
header_data = None
with open(baserom_file, 'rb') as f:
source_file = f.read()
with open(patchedrom_file, 'rb') as f:
target_file = f.read()
# get header data
f.seek(0x134)
header_data = f.read(0x14e - 0x134)
patch = Patch.create(source_file, target_file)
# force adding a record with header data
patch.add_record(0x134, header_data)
with open(ips_file, 'w+b') as f:
f.write(patch.encode())
def apply(baserom_file, patchedrom_file, ips_file):
patch = Patch.load(ips_file)
in_file = None
with open(baserom_file, 'rb') as f:
in_file = f.read()
out_file = patch.apply(in_file)
with open(patchedrom_file, 'w+b') as f:
f.write(out_file)
def print_error():
print('Usage: ./ips.py [create|apply] (default: create)')
sys.exit()
if __name__ == '__main__':
cmd = 'create'
if len(sys.argv) > 2:
print_error()
elif len(sys.argv) == 2:
cmd = sys.argv[1]
if cmd not in valid_cmds:
print_error()
elif cmd == 'create':
baserom_file = f'{src}.gbc'
for patch in patches:
patch_name = f'{dest}_{patch}'
ips_file = f'{patch_name}.ips'
patchedrom_file = f'{patch_name}.gbc'
create(baserom_file, patchedrom_file, ips_file)
elif cmd == 'apply':
for patch in patches:
ips_file = f'{dest}_{patch}.ips'
for baserom in baseroms:
baserom_file = f'{baserom}.gbc'
# test to ensure patch files produce identical ROMs for each baserom
patchedrom_file = f'{baserom}_{patch}_test.gbc'
if cmd == 'apply':
apply(baserom_file, patchedrom_file, ips_file)