4cade/bin/buildsearch.py

106 lines
3.6 KiB
Python
Raw Normal View History

2024-06-10 23:45:17 +00:00
#!/usr/bin/env python3
# parameters
# stdin - input containing list of game metadata, filename, display name (e.g. GAMES.CONF or some subset of it)
# stdout - binary OKVS data structure
# 1 - output filename for game count in assembler code format
# 2 - input filename of HGR titles, offsets, and sizes
# 3 - input filename of DHGR titles, offsets, and sizes
import argparse
import pprint
2024-06-11 17:58:34 +00:00
from struct import pack
2024-06-10 23:45:17 +00:00
import sys
gSearchIndex = 0x6000 # must match gSearchIndex in src/constants.a
# indexes into |flags| as string
iDHGRTitle = 2
iCheatCategory = 3
iSingleLoad = 4
# maps of flag raw string value -> value in final flags byte
kHasDHGRTitle = {'0': 0, '1': 128}
kSingleLoad = {'0': 0, '1': 64}
def parse_log_file(filename):
2024-06-11 17:58:34 +00:00
if not filename:
return {}
with open(filename, 'r') as f:
lines = [x.strip().split(',') for x in f.readlines()]
lines = [(title, (int(offset), int(size))) for title, offset, size in lines]
return dict(lines)
2024-06-10 23:45:17 +00:00
def build(records, args):
# records is [(flags, key, value), (flags, key, value) ...]
hgr_cache = parse_log_file(args.input_hgr_log_file)
dhgr_cache = parse_log_file(args.input_dhgr_log_file)
cache_ptr = {'0': hgr_cache, '1': dhgr_cache}
record_count = len(records)
# generate source file with game count
with open(args.output_game_count_file, 'w') as file_handle:
file_handle.write(f""";
; Game count
;
; This file is automatically generated
;
!word {record_count:>8}
""")
# lookup table is stored after all record data, so first calculate total record size
# record_count * (length-prefixed key + length-prefixed value + 8 other bytes)
# then lookup table address is that + gSearchIndex + 4 bytes for the OKVS header
total_record_size = len("".join([x for xs in records for x in xs[1:]])) + 10*record_count
2024-06-11 15:52:36 +00:00
# yield OKVS header (2 x 2 bytes, unsigned int, little-endian)
2024-06-11 17:58:34 +00:00
yield pack('<2H', record_count, total_record_size + gSearchIndex + 4)
2024-06-10 23:45:17 +00:00
rec_key_address = gSearchIndex + 5
key_addresses = []
for flags, key, value in records:
key_addresses.append(rec_key_address)
rec_length = len(key) + len(value) + 10
rec_key_address += rec_length
# yield record length (1 byte)
2024-06-11 17:58:34 +00:00
yield pack('B', rec_length)
2024-06-10 23:45:17 +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-10 23:45:17 +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-10 23:45:17 +00:00
2024-06-11 17:58:34 +00:00
yield pack('B', 1)
2024-06-10 23:45:17 +00:00
# yield flags
has_dhgr_title = dhgr_cache and flags[iDHGRTitle] or '0'
2024-06-11 17:58:34 +00:00
yield pack('B', kHasDHGRTitle[has_dhgr_title] + \
2024-06-10 23:45:17 +00:00
kSingleLoad[flags[iSingleLoad]] + \
int(flags[iCheatCategory]))
rec_offset, rec_size = cache_ptr[has_dhgr_title][key]
# yield record offset (3 bytes, big-endian, unsigned long)
2024-06-11 17:58:34 +00:00
yield pack('>L', rec_offset)[1:]
2024-06-10 23:45:17 +00:00
# yield record size (2 bytes, little-endian, unsigned short)
2024-06-11 17:58:34 +00:00
yield pack('<H', rec_size)
2024-06-10 23:45:17 +00:00
# yield lookup table
2024-06-11 17:58:34 +00:00
yield pack(f'<{record_count}H', *key_addresses)
2024-06-10 23:45:17 +00:00
if __name__ == "__main__":
2024-06-11 17:58:34 +00:00
parser = argparse.ArgumentParser(description="Build indexed OKVS structure for search index")
2024-06-10 23:45:17 +00:00
parser.add_argument("output_game_count_file")
parser.add_argument("input_hgr_log_file")
parser.add_argument("input_dhgr_log_file")
args = parser.parse_args()
records = [x.strip() for x in sys.stdin.readlines()]
records = [x.replace('=',',').split(',')
for x in records
if x and x[0] not in ('#', '[')]
for b in build(records, args):
sys.stdout.buffer.write(b)