diff --git a/tools/mkgamezips b/tools/mkgamezips index 516862a39..2e0885299 100755 --- a/tools/mkgamezips +++ b/tools/mkgamezips @@ -13,15 +13,18 @@ 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: - * 2 bytes of probable garbage data (set to 0x0000 by this script) - * Zip data (starting with 0x1173001000) + * 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 (set to 0x00 by this script). + byte is probable garbage data. """ -def main(): - zips = get_zips() - fd = open('build/%s/segments/gamezips.bin' % os.environ['ROMID'], 'wb') +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 @@ -34,7 +37,7 @@ def main(): # Last pointer points to end fd.write(pos.to_bytes(4, byteorder='big')) - # Write data + # Write checksums and compressed data padding_index = 0 for index, zip in enumerate(zips): if pos % 2 == 1: @@ -47,25 +50,15 @@ def main(): padding_index += 1 pos += 1 - try: - checksum = checksumsmaybe[os.environ['ROMID']][index] - except IndexError: - checksum = 0 - except KeyError: - checksum = 0 - fd.write(checksum.to_bytes(2, 'big')) - + fd.write(sums[index].to_bytes(2, 'big')) fd.write(zip) + pos += len(zip) - try: - checksum = checksumsmaybe[os.environ['ROMID']][index + 1] - except IndexError: - checksum = 0 - except KeyError: - checksum = 0 - fd.write(checksum.to_bytes(2, 'big')) - + # 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): @@ -74,135 +67,26 @@ def get_filecontents(filename): fd.close() return binary -def get_zips(): +def get_bins(): binary = get_filecontents('build/%s/segments/game.bin' % os.environ['ROMID']) - parts = [binary[i:i+0x1000] for i in range(0, len(binary), 0x1000)] - return [zip(part) for part in parts] + 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() -checksumsmaybe = { - 'ntsc-beta': [], - 'ntsc-1.0': [ - 0x8771, 0xa4fb, 0x5c84, 0xfd94, 0x1701, 0xe0aa, 0x6ada, 0xe275, - 0x6ef7, 0xefff, 0x66fc, 0xc431, 0x181b, 0x9b35, 0x0dd8, 0xc3db, - 0xeb0b, 0xd57c, 0xa805, 0x2283, 0x6b95, 0xeafc, 0x50b6, 0x3438, - 0xe492, 0x264b, 0x478c, 0x45ea, 0x45af, 0x9d13, 0x479a, 0x6008, - 0x218c, 0x542d, 0x006d, 0x6d36, 0x9c5d, 0xefbb, 0x7e4c, 0xc9f4, - 0xe689, 0x1864, 0xe1d4, 0x75f4, 0x89c5, 0x1c30, 0x87ff, 0x24d9, - 0xa2a3, 0x7404, 0x3b17, 0x74ea, 0x0ebd, 0x373d, 0x6d14, 0x01af, - 0x3733, 0xfa97, 0xcd7c, 0xc14f, 0xe42f, 0xec78, 0xa4d4, 0x0ffa, - 0x9398, 0x71af, 0xc36b, 0xcae3, 0xb734, 0xd2e3, 0x458c, 0x523a, - 0xf1a7, 0x5541, 0xdebe, 0xb010, 0xc9c6, 0xf407, 0x262e, 0x0c38, - 0x64fb, 0xe98f, 0x480f, 0xa9b1, 0x9bc9, 0xc90b, 0x992a, 0xe898, - 0xb739, 0x810f, 0xfc27, 0x78db, 0x1023, 0x6938, 0x9bb8, 0x194a, - 0x1150, 0x2f2a, 0x09ab, 0xbbeb, 0x2b18, 0x38ce, 0xf41a, 0xddca, - 0x2709, 0xc20d, 0xd7bd, 0xdfea, 0x0d85, 0x21d4, 0x9c22, 0x4dcf, - 0x10f6, 0xb1dd, 0x8013, 0xa472, 0x551a, 0x466b, 0x441a, 0xbea5, - 0x502d, 0x933e, 0xd2f9, 0x45e3, 0xbc8c, 0x9024, 0xb869, 0x5a52, - 0xac98, 0x2deb, 0x97e3, 0xa75e, 0x0560, 0x8a93, 0x5ce6, 0x0547, - 0x2148, 0x904d, 0x4c0a, 0x9c58, 0xf104, 0x3585, 0xbf08, 0x8c40, - 0xfe94, 0x4d27, 0x47f5, 0x81b7, 0x0b15, 0x6a26, 0x5bd7, 0x3312, - 0x6334, 0x6136, 0xbd49, 0x031b, 0x0b16, 0xf171, 0x2022, 0xe7be, - 0x4168, 0x7f12, 0x24d5, 0x1fe5, 0x9463, 0x6e5a, 0x6587, 0x688a, - 0xe09d, 0x9e4c, 0xdf7b, 0xf51a, 0xbe62, 0x764c, 0xf985, 0x0e51, - 0x03cc, 0xbe67, 0x830e, 0x6bc7, 0xb9fb, 0x2471, 0xd339, 0x2c75, - 0xc868, 0x71c1, 0xe298, 0x4348, 0x5a5f, 0xc986, 0x1fce, 0x5040, - 0x8494, 0x10ef, 0x5fa8, 0x8819, 0xb2b3, 0x5a5e, 0x3c47, 0x1052, - 0x55b5, 0x0c0d, 0xd2a6, 0x36ab, 0x986e, 0xa66e, 0x3af8, 0x42c5, - 0xeb9f, 0x5fb0, 0xc907, 0xc844, 0xc62f, 0x8f88, 0x2ac1, 0x73ee, - 0xa477, 0x9657, 0x2f77, 0x15f8, 0xc290, 0xe9b2, 0x4c13, 0x3f92, - 0x7a79, 0x4c26, 0x3a12, 0x7db1, 0x447c, 0xce14, 0xf3a1, 0xda71, - 0x42f0, 0x005d, 0x712f, 0x351e, 0xc7bc, 0x41cf, 0x8692, 0x58ae, - 0xbf1d, 0xe486, 0x8b94, 0x96e4, 0xdde7, 0xe5be, 0xd875, 0xa0d3, - 0x00f3, 0x8fa5, 0x9bac, 0x7c7b, 0xcc91, 0x2e98, 0x2c68, 0xd914, - 0x2fb7, 0xc053, 0x877b, 0x5013, 0xe89a, 0x93ce, 0xc46a, 0x816d, - 0xe53b, 0x75ce, 0x3912, 0x352c, 0x8317, 0xd656, 0x2d43, 0xdf03, - 0x4d09, 0xa527, 0xc32e, 0x6e02, 0x8d14, 0xcb69, 0x2706, 0xc34b, - 0xb84e, 0xc7ff, 0xc4bb, 0x2f93, 0x9abe, 0x3ccc, 0x0679, 0x5202, - 0xc143, 0x72f8, 0x670d, 0xf02d, 0x3538, 0xdec1, 0x9ca8, 0x8320, - 0x051f, 0x930f, 0xed41, 0x207c, 0x05c0, 0x88ef, 0xa2a6, 0x324c, - 0x94f9, 0xdfbf, 0x5a94, 0xd8eb, 0xb047, 0x509e, 0xc3bd, 0xfbb0, - 0x8801, 0xfa90, 0x2619, 0x094b, 0x9e32, 0xe3e5, 0x8c5c, 0x21d2, - 0x307b, 0x211e, 0xd978, 0x7f4d, 0x061b, 0xdf01, 0xba5b, 0x018e, - 0x3357, 0x6b35, 0xdce3, 0x7ff3, 0x87f5, 0x8d6f, 0x5c17, 0xb9ad, - 0xf322, 0x09bf, 0x1ad5, 0x1a34, 0xb50f, 0x3383, 0xe2ad, 0xc715, - 0xee1d, 0x79c6, 0xd9a9, 0x36c9, 0x085a, 0xbf78, 0x73d7, 0x22cb, - 0xa0d7, 0xeae3, 0xbee2, 0x2dba, 0xac57, 0x2e4d, 0x0746, 0xf958, - 0x2cc2, 0xb747, 0x8a7b, 0x6220, 0x120e, 0x9a16, 0x9209, 0x6862, - 0xb29b, 0xd9d9, 0x3d37, 0x5437, 0xc359, 0x22ed, 0x58bc, 0x326c, - 0x1b92, 0x24f0, 0x2f17, 0xc23f, 0xbed2, 0xa325, 0x188d, 0xb1a0, - 0xf866, 0xde92, 0x381e, 0x360a, 0x349f, 0x9862, 0xdb23, 0xf615, - 0xac3b, 0x67c6, 0x615a, 0x7f30, 0xe343, 0x7b70, 0xd4f4, 0xcc14, - 0x899d, 0x6db2, 0x981d, 0x69d2, 0x0b86, 0xaa2f, 0xeb5d, 0x327a, - 0x2442, 0x0563, 0x762d, 0x0302, 0x790e, 0xc6bd, 0xc22f, 0xc186, - 0x8f10, 0x52be, 0xb809, 0x7de5, 0xf3c4, 0x775b, 0xd18e, 0x380e, - 0xd5fb, 0x6b76, 0x0d68, 0x69ef, 0xdee9, 0xb1e3, 0x0000, 0x0000, - 0x0000, 0x0921, 0x287f, 0xc49f, 0x9767, 0xf7fe, 0x14eb, 0x5bbd, - 0x56a1, 0x8617, 0xbf01, - ], - 'ntsc-final': [ - 0x94f1, 0xb0c7, 0x6f10, 0x0d50, 0x2361, 0xe64a, 0x7c32, 0xf8f5, - 0x8d57, 0x08bf, 0x7ebe, 0xd019, 0x181b, 0xaf2b, 0x1ced, 0xd81c, - 0xf9cf, 0xdf5c, 0xbb7d, 0x28ab, 0x7107, 0xef7c, 0x671e, 0x6a34, - 0x01d2, 0x54e3, 0x69a8, 0x523e, 0x5547, 0xb295, 0x4dc9, 0x7e6c, - 0x3c24, 0x8881, 0x0ebe, 0x7632, 0xaa69, 0x009d, 0x8348, 0xcee0, - 0xedc5, 0x2554, 0xfa94, 0x75f4, 0x950d, 0xb140, 0x97df, 0x2b99, - 0xa2a3, 0x847c, 0x557f, 0x7e76, 0x2365, 0x546f, 0x76b8, 0x156f, - 0x41c3, 0x3903, 0xd96d, 0x8b7c, 0x01ff, 0xf71c, 0xdc78, 0x633a, - 0xa6b8, 0x7d63, 0xf57b, 0xd3e3, 0xed34, 0xf6e3, 0x4aac, 0x6372, - 0x12eb, 0x7561, 0xf7e8, 0xc75e, 0xd432, 0x0453, 0x2746, 0x0c38, - 0x6a9d, 0xf293, 0x53f7, 0xb231, 0x9c55, 0xd107, 0xa106, 0xf5ed, - 0xc5a9, 0x915f, 0x0673, 0x7f9b, 0x150f, 0x6af0, 0xb018, 0x20b2, - 0x2504, 0x3d10, 0x11a7, 0xc62e, 0x368a, 0x3b48, 0xf668, 0xddea, - 0x2ba9, 0xd265, 0xed1a, 0xed92, 0x1439, 0x33d4, 0xbe72, 0x547b, - 0x13e6, 0xdf3d, 0xc3b3, 0xd04e, 0x7b7a, 0x521f, 0x5f0a, 0xd1af, - 0xa7fb, 0xcc9a, 0xffe1, 0xa06f, 0xe354, 0xb57c, 0xbd77, 0x686e, - 0xb747, 0x3e2d, 0xf883, 0x135e, 0x161d, 0x92f7, 0x7422, 0x1e7f, - 0x3bef, 0x9f6c, 0x4eda, 0xacd8, 0x0574, 0x4277, 0xc814, 0xa2e0, - 0x155c, 0x5bf7, 0x545e, 0x8a4f, 0x2cd5, 0x7c52, 0x94a7, 0x423e, - 0x69f4, 0x6402, 0xd3cb, 0x142f, 0x1774, 0x22f9, 0x2bf2, 0x0bc8, - 0x57f0, 0x80c6, 0x3201, 0x578d, 0xd123, 0x7a2a, 0x7789, 0x6f56, - 0xef5d, 0xa920, 0xeb3f, 0x0aee, 0xe022, 0x92fc, 0x3b5d, 0x2cb1, - 0x18ba, 0xc647, 0x830e, 0x6d77, 0xc92b, 0x26a1, 0xe118, 0x3eb0, - 0xd9af, 0x85ba, 0x0b4f, 0x4af7, 0x5ed7, 0xe071, 0x2f50, 0x6a91, - 0x2311, 0x4f9d, 0x4455, 0xfb64, 0xbed3, 0x118d, 0xe117, 0x1cc4, - 0x3e27, 0xb43e, 0xec01, 0xf2b0, 0x37da, 0x2800, 0xc531, 0xd63f, - 0x7fcd, 0xb361, 0xd265, 0x7700, 0xa774, 0x1f29, 0x58a1, 0x7866, - 0x0a3c, 0x6ee2, 0x598d, 0x9386, 0xa123, 0x8b36, 0xb54b, 0xcf5e, - 0x865b, 0x306d, 0x84e4, 0x9a6e, 0x0cc4, 0x5d88, 0xf586, 0x2714, - 0x5fc0, 0xf0e5, 0x3265, 0x9908, 0x1b5e, 0xcd12, 0xc68c, 0x9850, - 0xb2dc, 0xcff0, 0x8e41, 0x8fb5, 0xdf13, 0xc46e, 0x08f5, 0x3c18, - 0x48d7, 0xd98b, 0x8790, 0xf416, 0x7c5c, 0x301f, 0xd40f, 0x2f1f, - 0xb385, 0x2f7d, 0x646b, 0x5433, 0x2b90, 0x74d4, 0x5ea9, 0xd811, - 0xc4c8, 0x03ce, 0x056f, 0x84fa, 0x3555, 0x34c8, 0x69e6, 0x8f18, - 0xa6df, 0xe03e, 0x22c9, 0x3fca, 0x8c43, 0x8fb1, 0x0c68, 0xf91c, - 0x2858, 0x1123, 0x061b, 0xfcea, 0xfcd5, 0x688e, 0xb033, 0xfe83, - 0x8d7b, 0xcb4e, 0xdd38, 0xa602, 0xb60d, 0xd447, 0x6f55, 0x40d5, - 0x83f7, 0x49bc, 0x8923, 0xec93, 0x3242, 0x3db1, 0x0462, 0x5f44, - 0xab72, 0xcd4c, 0x352b, 0xd602, 0x556a, 0xc7c7, 0x227e, 0x780f, - 0x46f1, 0xb296, 0xed78, 0x7949, 0x7617, 0x444f, 0x7f4a, 0x612b, - 0xe63d, 0xb286, 0xe1d2, 0x42e1, 0xd136, 0xddf0, 0x3a29, 0xa623, - 0x0107, 0xf839, 0x506a, 0xfc70, 0x0410, 0x24fb, 0x35e6, 0x08a1, - 0x668f, 0xffbe, 0xf4cf, 0xd093, 0xcf0a, 0x6bc4, 0xa173, 0xa12e, - 0xb552, 0x8da1, 0xa0f5, 0xa909, 0x40d5, 0xcbb3, 0x8e23, 0xe87e, - 0x3532, 0xcc6b, 0x00ca, 0x98d5, 0x9dbd, 0xa205, 0xb458, 0x2a0d, - 0xa580, 0xcf78, 0xb691, 0x22e2, 0x0455, 0xcfa7, 0x8505, 0x3387, - 0xf6e6, 0x98de, 0x736e, 0xa5a8, 0x3cf1, 0x4dd8, 0xf44c, 0xd7ba, - 0x1c78, 0xb226, 0x6bd6, 0xa3e3, 0xee95, 0x4993, 0x174f, 0x355f, - 0xb614, 0x6599, 0xb16a, 0x20d4, 0xc414, 0xc0d4, 0x5a48, 0x66b6, - 0xf118, 0x5ba9, 0x083b, 0x9c9b, 0xa53b, 0x257c, 0xdd0d, 0xbe59, - 0x8069, 0x06fc, 0xdd59, 0x38e2, 0x0abc, 0xf175, 0x3017, 0xed5d, - 0x0d10, 0xe322, 0x8a99, 0xe560, 0x943c, 0x4054, 0x28c3, 0xa9ac, - 0x80d9, 0x6fdb, 0xeb49, 0x3800, 0xe65d, 0xbb45, 0xca90, 0x4af8, - 0xe1a3, 0x9a50, 0xbc1c, 0xaf92, 0xa169, 0x465e, 0x0000, 0x0000, - 0x0000, 0x101c, 0x7ca7, 0x0831, 0x0a52, 0x4cb1, 0x7a86, 0xeff6, - 0xeecb, 0xbf74, 0xfd01, - ], -} - padding = { 'ntsc-beta': [], 'ntsc-1.0': [