4cade/bin/buildslideshow.py

64 lines
2.3 KiB
Python
Raw Normal View History

2024-06-11 15:21:32 +00:00
#!/usr/bin/env python3
# parameters
# stdin - input containing slideshow (e.g. some file in res/SS/)
# stdout - binary OKVS data structure
2024-06-11 15:36:20 +00:00
# 1 - name of slideshow file (used to decide whether if this is an action slideshow)
# 2 - name of file containing list of games with metadata (e.g. build/GAMES.CONF)
2024-06-11 15:21:32 +00:00
import argparse
2024-06-11 15:36:20 +00:00
import os.path
2024-06-11 17:58:34 +00:00
from struct import pack
2024-06-11 15:21:32 +00:00
import sys
# indexes into |flags| as string
iNeedsJoystick = 0
iNeeds128K = 1
# maps of flag raw string value -> value in final flags byte
kNeedsJoystick = {'0': 0, '1': 128}
kNeeds128K = {'0': 0, '1': 64}
def build(records, args):
with open(args.games_file, 'r') as games_file_handle:
games_list = [x.strip() for x in games_file_handle.readlines()]
games_list = [x.replace('=',',').split(',')
for x in games_list
if x and x[0] not in ('#', '[')]
2024-06-11 15:36:20 +00:00
if os.path.basename(args.slideshow_file).startswith('ACT'):
2024-06-11 17:58:34 +00:00
games_cache = dict([(key, (flags, displayname)) for flags, key, displayname in games_list])
2024-06-11 15:36:20 +00:00
else:
2024-06-11 17:58:34 +00:00
games_cache = dict([(key, (flags, '')) for flags, key, displayname in games_list])
2024-06-11 15:21:32 +00:00
# yield OKVS header (2 x 2 bytes, unsigned int, little-endian)
2024-06-11 17:58:34 +00:00
yield pack('<2H', len(records), 0)
2024-06-11 15:21:32 +00:00
for key, dummy, value in records:
2024-06-11 17:58:34 +00:00
flags, displayname = games_cache[value or key]
2024-06-11 15:21:32 +00:00
# yield record length (1 byte)
2024-06-11 17:58:34 +00:00
yield pack('B', len(key) + len(value) + len(displayname) + 5)
2024-06-11 15:21:32 +00:00
# yield key (Pascal-style string)
2024-06-11 17:58:34 +00:00
yield pack(f'{len(key)+1}p', key.encode('ascii'))
2024-06-11 15:21:32 +00:00
# yield value (Pascal-style string)
2024-06-11 17:58:34 +00:00
yield pack(f'{len(value)+1}p', value.encode('ascii'))
2024-06-11 15:21:32 +00:00
# yield display name (Pascal-style string)
2024-06-11 17:58:34 +00:00
yield pack(f'{len(displayname)+1}p', displayname.encode('ascii'))
2024-06-11 15:21:32 +00:00
# yield flags
2024-06-11 17:58:34 +00:00
yield pack('B', kNeedsJoystick[flags[iNeedsJoystick]] + \
2024-06-11 15:21:32 +00:00
kNeeds128K[flags[iNeeds128K]])
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Build indexed OKVS structure from slideshow configuration file")
2024-06-11 15:36:20 +00:00
parser.add_argument("slideshow_file")
2024-06-11 15:21:32 +00:00
parser.add_argument("games_file")
args = parser.parse_args()
records = [x.strip() for x in sys.stdin.readlines()]
records = [x.partition('=') for x in records if x and x[0] not in ('#', '[')]
for b in build(records, args):
sys.stdout.buffer.write(b)