diff --git a/tools/converters/pal16dis.py b/tools/converters/pal16dis.py index d62cc048..4297ce20 100644 --- a/tools/converters/pal16dis.py +++ b/tools/converters/pal16dis.py @@ -5,15 +5,11 @@ import struct def convert_binary_to_vtx_c_source(src_path, dest_path): # Load data with open(src_path, "rb") as binary_file, open(dest_path, "w") as c_file: - while True: - chunk = binary_file.read(2) # 2 bytes for each 16-bit palette entry - - if len(chunk) < 2: - break - - pal = struct.unpack(">H", chunk[0:2]) # 2 bytes - - c_file.write(f"{hex(pal[0])}, ") + all_bin = binary_file.read() + # parse all data as an array of u16, + # use tuple's str method to create "(1, 2, 3)", and then sub-string to remove the parentheses + c_file.write( + str(struct.unpack(">" + "H" * (len(all_bin) // 2), all_bin))[1:-1]) def main(): @@ -21,7 +17,8 @@ def main(): description="Converts a binary file to an array of u16 type" ) parser.add_argument("src_path", type=str, help="Binary source file path") - parser.add_argument("dest_path", type=str, help="Destination C include file path") + parser.add_argument("dest_path", type=str, + help="Destination C include file path") args = parser.parse_args() convert_binary_to_vtx_c_source(args.src_path, args.dest_path) diff --git a/tools/converters/vtxdis.py b/tools/converters/vtxdis.py index 31177462..88185332 100644 --- a/tools/converters/vtxdis.py +++ b/tools/converters/vtxdis.py @@ -5,20 +5,10 @@ import struct def convert_binary_to_vtx_c_source(src_path, dest_path): # Load data with open(src_path, "rb") as binary_file, open(dest_path, "w") as c_file: - while True: - chunk = binary_file.read(16) # 16 bytes for each Vtx struct - - if len(chunk) < 16: - break - - ob = struct.unpack(">hhh", chunk[0:6]) # 3 * 2 bytes - flag = struct.unpack(">H", chunk[6:8]) # 2 bytes - tc = struct.unpack(">hh", chunk[8:12]) # 2 * 2 bytes - cn = struct.unpack(">BBBB", chunk[12:16]) # 4 * 1 byte - - c_file.write( - f" {{{ob[0]}, {ob[1]}, {ob[2]}, {flag[0]}, {tc[0]}, {tc[1]}, {cn[0]}, {cn[1]}, {cn[2]}, {cn[3]}}},\n" - ) + # iter through all data, consuming one vtx at a time + for vtx_tuple in struct.iter_unpack(">hhhHhhBBBB", binary_file.read()): + # use tuple's str method to create "(1, 2, 3)", and then sub-string to remove the parentheses + c_file.write(f" {{{str(vtx_tuple)[1:-1]}}},\n") def main(): @@ -26,7 +16,8 @@ def main(): description="Converts a binary file to an array of N64 Vtx type" ) parser.add_argument("src_path", type=str, help="Binary source file path") - parser.add_argument("dest_path", type=str, help="Destination C include file path") + parser.add_argument("dest_path", type=str, + help="Destination C include file path") args = parser.parse_args() convert_binary_to_vtx_c_source(args.src_path, args.dest_path)