Skip to content

Instantly share code, notes, and snippets.

@heisvoid
Last active July 20, 2019 10:45
Show Gist options
  • Save heisvoid/85daba372473a5e364e3 to your computer and use it in GitHub Desktop.
Save heisvoid/85daba372473a5e364e3 to your computer and use it in GitHub Desktop.
Verify sprite type 2 in TWG
# -*- coding: utf-8 -*-
import argparse
import struct
import os
parser = argparse.ArgumentParser(description='Verify 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)
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)
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
# unknown
bytes = f.read(2)
bytes = struct.unpack('<H', bytes)
assert width == bytes[0] # maybe
# unknown
bytes = f.read(2)
bytes = struct.unpack('<H', bytes)
assert height == bytes[0] # maybe
f.seek(len - 4, os.SEEK_CUR)
assert (length - 4) == (width * height)
assert f.tell() == file_len
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment