154 lines
5.5 KiB
Python
Executable File
154 lines
5.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import os
|
|
import zlib
|
|
|
|
"""
|
|
mkgamezips - Creates the segments/gamezips.bin from segments/game.bin
|
|
|
|
game.bin is the compiled game code from ld. This game code is split into
|
|
4KB chunks. Each chunk is individually zipped.
|
|
|
|
The format of the gamezips binary is:
|
|
* Array of offsets to each zip, where each offset is 4 bytes and relative to the
|
|
start of the gamezips segment.
|
|
* After the array of offsets comes the data it points to. Each data consists of:
|
|
* A 2 byte checksum of the uncompressed chunk.
|
|
* Zip data (starting with 0x1173001000).
|
|
* Optional single byte to align it to the next 2 byte boundary. The added
|
|
byte is probable garbage data.
|
|
"""
|
|
|
|
def main():
|
|
bins = get_bins()
|
|
sums = get_sums(bins)
|
|
zips = get_zips(bins)
|
|
|
|
fd = open('build/%s/segments/gamezips.bin' % os.environ['ROMID'], 'wb+')
|
|
pos = len(zips) * 4 + 4
|
|
|
|
# Write pointer array
|
|
for zip in zips:
|
|
fd.write(pos.to_bytes(4, byteorder='big'))
|
|
pos += 2 + len(zip)
|
|
if pos % 2 == 1:
|
|
pos += 1
|
|
|
|
# Last pointer points to end
|
|
fd.write(pos.to_bytes(4, byteorder='big'))
|
|
|
|
# Write checksums and compressed data
|
|
padding_index = 0
|
|
for index, zip in enumerate(zips):
|
|
if pos % 2 == 1:
|
|
try:
|
|
pad_value = padding[os.environ['ROMID']][padding_index]
|
|
except IndexError:
|
|
pad_value = 0
|
|
fd.write(pad_value.to_bytes(1, 'big'))
|
|
|
|
padding_index += 1
|
|
pos += 1
|
|
|
|
fd.write(sums[index].to_bytes(2, 'big'))
|
|
fd.write(zip)
|
|
|
|
pos += len(zip)
|
|
|
|
# The previous two bytes are repeated at the end
|
|
fd.seek(-2, os.SEEK_CUR)
|
|
value = fd.read(2)
|
|
fd.write(value)
|
|
fd.close()
|
|
|
|
def get_filecontents(filename):
|
|
fd = open(filename, 'rb')
|
|
binary = fd.read()
|
|
fd.close()
|
|
return binary
|
|
|
|
def get_bins():
|
|
binary = get_filecontents('build/%s/segments/game.bin' % os.environ['ROMID'])
|
|
return [binary[i:i+0x1000] for i in range(0, len(binary), 0x1000)]
|
|
|
|
def get_sums(bins):
|
|
return [sum(part) for part in bins]
|
|
|
|
def get_zips(bins):
|
|
return [zip(part) for part in bins]
|
|
|
|
def sum(binary):
|
|
sum = 0
|
|
for i in range(0, len(binary), 4):
|
|
sum += int.from_bytes(binary[i:i+4], 'big')
|
|
return sum & 0xffff
|
|
|
|
def zip(binary):
|
|
obj = zlib.compressobj(level=9, wbits=-15)
|
|
return b'\x11\x73' + len(binary).to_bytes(3, 'big') + obj.compress(binary) + obj.flush()
|
|
|
|
padding = {
|
|
'ntsc-beta': [],
|
|
'ntsc-1.0': [
|
|
0x00, 0x00, 0x00, 0x73, 0x75, 0x47, 0xa4, 0x79,
|
|
0x91, 0x1e, 0xd8, 0xd8, 0x7b, 0xe2, 0x13, 0x51,
|
|
0xaa, 0x2a, 0xa1, 0x5a, 0xbd, 0x24, 0x87, 0x0d,
|
|
0xdc, 0x50, 0xd3, 0xe2, 0x39, 0x9f, 0x1a, 0x86,
|
|
0x73, 0xc5, 0x72, 0xdd, 0xc8, 0xa9, 0xd9, 0x21,
|
|
0xf2, 0xd4, 0x66, 0xa5, 0xbb, 0xfa, 0xbe, 0x6d,
|
|
0xc4, 0xce, 0x63, 0x6f, 0xba, 0xf7, 0xe3, 0xc9,
|
|
0xd4, 0xd3, 0xfb, 0x9c, 0xff, 0x76, 0xcf, 0x20,
|
|
0x6e, 0xbb, 0x2c, 0x90, 0xd3, 0xc1, 0xc4, 0x9f,
|
|
0xb4, 0x70, 0x38, 0xe3, 0x22, 0x3e, 0xec, 0x5c,
|
|
0xa4, 0xdc, 0x39, 0xe8, 0x8b, 0x5e, 0xcc, 0x3e,
|
|
0xa2, 0x62, 0x87, 0x20, 0xd3, 0x05, 0x1c, 0xa5,
|
|
0xd4, 0xca, 0x0e, 0xef, 0x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