Skip to content

Instantly share code, notes, and snippets.

@heisvoid
Last active July 20, 2019 10:36
Show Gist options
  • Save heisvoid/e2f0684193907401fddf to your computer and use it in GitHub Desktop.
Save heisvoid/e2f0684193907401fddf to your computer and use it in GitHub Desktop.
Verify sprite type 1 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)
# each sprite data offset list
offsets = []
for n in range(num):
bytes = f.read(4)
bytes = struct.unpack('<I', bytes)
offsets.append(bytes[0])
for ofs in offsets:
f.seek(ofs, os.SEEK_SET)
# 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)
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)
chunks = buf[0]
l = 0
for c in range(chunks):
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) == length
assert f.tell() == file_len
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment