Skip to content

Instantly share code, notes, and snippets.

@heisvoid
Last active July 20, 2019 10:28
Show Gist options
  • Save heisvoid/bf8ac365d5e474642a82 to your computer and use it in GitHub Desktop.
Save heisvoid/bf8ac365d5e474642a82 to your computer and use it in GitHub Desktop.
Verify type 0 sprite file in TWG
# -*- 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)
assert 320 >= bytes[0]
# height of a sprite
bytes = f.read(2)
bytes = struct.unpack('<H', bytes)
assert 200 >= bytes[0]
# unknown
f.seek(2, os.SEEK_CUR)
f.seek(2, os.SEEK_CUR)
buf = f.read(2)
buf = struct.unpack('<H', buf)
blocks = buf[0]
l = 0
for b in range(blocks):
buf = f.read(2)
buf = struct.unpack('<H', buf)
blanks = buf[0]
buf = f.read(1)
buf = struct.unpack('<B', buf)
i = buf[0]
buf = f.read(1)
buf = struct.unpack('<B', buf)
j = buf[0]
f.read(4 * i + j)
l = l + 2 + 1 + 1 + 4 * i + j
assert (l + 2) == len
assert f.tell() == file_len
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment