Skip to content

Instantly share code, notes, and snippets.

@heisvoid
Created July 20, 2019 10:48
Show Gist options
  • Save heisvoid/83503ef92d90d6df5eb921b1cabbf3d0 to your computer and use it in GitHub Desktop.
Save heisvoid/83503ef92d90d6df5eb921b1cabbf3d0 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import argparse
import struct
import os
parser = argparse.ArgumentParser(description='Check integrity of a 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)
buf = f.read(0x40)
assert b'New Fast&Compress Sprite Ver3.0 for 13h mode!!!. SOFTMAX(C)1994.' == buf
f.seek(2, os.SEEK_CUR)
# total number of sprites
buf = f.read(2)
buf = struct.unpack('<H', buf) # little-endian, unsigned short
num = buf[0]
# zero padding
f.seek(60, os.SEEK_CUR)
# unknown
buf = f.read(1)
buf = struct.unpack('<B', buf)
if 0 != buf[0]:
for i in range(4):
for j in range(2):
f.read(1)
f.read(1)
f.read(1)
f.read(1)
f.read(1)
for n in range(num):
# total length of compressed pixel data
buf = f.read(4)
buf = struct.unpack('<I', buf) # little-endian, unsigned int
length = buf[0]
# width of a sprite
buf = f.read(2)
buf = struct.unpack('<H', buf)
width = buf[0]
assert 320 >= width
# height of a sprite
buf = f.read(2)
buf = struct.unpack('<H', buf)
height = buf[0]
assert 200 >= height
# unknown
f.seek(2, os.SEEK_CUR)
f.seek(2, os.SEEK_CUR)
f.read(1)
f.read(1)
buf = f.read(2)
buf = struct.unpack('<H', buf)
blocks = buf[0]
l = 0
for b in range(blocks):
# number of blank pixels
buf = f.read(2)
buf = struct.unpack('<H', buf)
blanks = buf[0]
buf = f.read(1)
buf = struct.unpack('<B', buf) # little-endian, unsigned char
i = buf[0]
buf = f.read(1)
buf = struct.unpack('<B', buf)
j = buf[0]
# number of painted pixels
f.seek(4 * i + j, os.SEEK_CUR)
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