Skip to content

Instantly share code, notes, and snippets.

@heisvoid
Created August 7, 2024 13:22
Show Gist options
  • Save heisvoid/fa7b8a4306efcb0617bf2d802b933e33 to your computer and use it in GitHub Desktop.
Save heisvoid/fa7b8a4306efcb0617bf2d802b933e33 to your computer and use it in GitHub Desktop.
Verify sprite type 6 in TWG(The War of Genesis 2)
# -*- coding: utf-8 -*-
import argparse
import struct
import os
parser = argparse.ArgumentParser(description='Check sprite file.')
parser.add_argument('-f', required=True, help='sprite file', dest='spr')
args = parser.parse_args()
with open(args.spr, 'rb') as f:
f.seek(0, os.SEEK_END)
file_len = f.tell()
f.seek(0, os.SEEK_SET)
bytes = f.read(0x40)
assert b'New Fast&Compress Sprite Ver3.0 for 13h mode!!!. SOFTMAX(C)1994.' == bytes
f.seek(2, os.SEEK_CUR)
# total number of sprites
bytes = f.read(2)
bytes = struct.unpack('<H', bytes) # little-endian, unsigned short
num = bytes[0]
# zero padding
f.seek(60, os.SEEK_CUR)
for n in range(num):
# total length of compressed pixel data
bytes = f.read(4)
bytes = struct.unpack('<I', bytes) # little-endian, unsigned int
len = bytes[0]
# width of a sprite
bytes = f.read(2)
bytes = struct.unpack('<H', bytes)
width = bytes[0]
assert 320 >= width
# height of a sprite
bytes = f.read(2)
bytes = struct.unpack('<H', bytes)
height = bytes[0]
assert 200 >= height
assert (4 + width * height) == len
# unknown
f.seek(2, os.SEEK_CUR)
f.seek(2, os.SEEK_CUR)
bytes = f.read(2)
bytes = struct.unpack('<H', bytes)
assert width == bytes[0]
bytes = f.read(2)
bytes = struct.unpack('<H', bytes)
assert height == bytes[0]
# (width * height) pixels
f.seek(len - 4, os.SEEK_CUR)
assert f.tell() == file_len
while f.tell() < file_len:
buf = f.read(1)
buf = struct.unpack('<B', buf)
assert 0 == buf[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment