#!/usr/bin/env python3

import os
import zlib

"""
mkgamezips - Creates the ucode/gamezips.bin from ucode/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:
    * 2 bytes of probable garbage data (set to 0x0000 by this script)
    * 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).
"""
def main():
    zips = get_zips()

    fd = open('build/ntsc-final/ucode/gamezips.bin', '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 data
    padding_index = 0
    for index, zip in enumerate(zips):
        if pos % 2 == 1:
            try:
                pad_value = padding[padding_index]
            except IndexError:
                pad_value = 0
            fd.write(pad_value.to_bytes(1, 'big'))

            padding_index += 1
            pos += 1

        try:
            checksum = checksumsmaybe[index]
        except IndexError:
            checksum = 0
        fd.write(checksum.to_bytes(2, 'big'))

        fd.write(zip)
        pos += len(zip)

    fd.close()

def get_filecontents(filename):
    fd = open(filename, 'rb')
    binary = fd.read()
    fd.close()
    return binary

def get_zips():
    binary = get_filecontents('build/ntsc-final/ucode/game.bin')
    parts = [binary[i:i+0x1000] for i in range(0, len(binary), 0x1000)]
    return [zip(part) for part in parts]

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 = [
    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,
]

padding = [
    0x00, 0xc2, 0xf9, 0x3a, 0x4d, 0x18, 0x8a, 0x07,
    0x4c, 0x68, 0x38, 0x17, 0x3c, 0x94, 0x98, 0x25,
    0x82, 0x6f, 0xf7, 0x2e, 0x41, 0xed, 0xe4, 0x88,
    0xf5, 0x48, 0x59, 0x5a, 0x7a, 0xb4, 0x3b, 0xf9,
    0xc7, 0xc7, 0xab, 0xf6, 0xef, 0x23, 0x8b, 0x6a,
    0x58, 0xb0, 0x27, 0x84, 0x77, 0x88, 0xe1, 0x34,
    0xf5, 0xf4, 0xa9, 0xb9, 0x30, 0x8a, 0x64, 0x23,
    0xb5, 0x6c, 0x87, 0xff, 0xd4, 0x84, 0xe4, 0x7c,
    0x93, 0xa0, 0x5b, 0x41, 0x28, 0xfa, 0x65, 0x3e,
    0xad, 0x51, 0x35, 0xf9, 0xec, 0x6a, 0xe9, 0xaf,
    0xe0, 0x7f, 0xe5, 0x8e, 0x0e, 0x6b, 0x42, 0x97,
    0xee, 0xad, 0x5d, 0xba, 0x91, 0x7c, 0xd6, 0x91,
    0xb3, 0xbd, 0x5f, 0x3d, 0x48, 0xd1, 0x37, 0xba,
    0xfc, 0x83, 0x51, 0x2b, 0xcb, 0x2f, 0x6b, 0xbf,
    0xb0, 0xe5, 0x9c, 0xac, 0x1d, 0x63, 0xcb, 0xa5,
    0x5e, 0x66, 0x24, 0x2d, 0xe3, 0x86, 0x3e, 0x0c,
    0xcf, 0x1a, 0x57, 0xc9, 0x4b, 0x29, 0x70, 0x31,
    0xbb, 0x55, 0xc4, 0x42, 0x62, 0x5e, 0x9a, 0xa0,
    0xff, 0x41, 0xf5, 0x62, 0x9f, 0xc9, 0x61, 0xee,
    0xbe, 0x7b, 0x0e, 0xf2, 0xd7, 0xf1, 0x90, 0x69,
    0xfa, 0xff, 0xf7, 0xb1, 0x3a, 0x27, 0xac, 0xc2,
    0x57, 0x7e, 0xcc, 0x92, 0xdd, 0x2d, 0x63, 0xa0,
    0x53, 0x74, 0x35, 0xbb, 0x24, 0xde, 0x6d, 0xbb,
    0x2c, 0xe5, 0xff, 0xeb, 0x37, 0xde, 0xd0, 0x6e,
    0x96, 0xfa, 0xbe, 0x79, 0xe3, 0x1e, 0x7f, 0xff,
    0x67, 0x86, 0x86, 0x86, 0x15, 0x6e,
]

main()
