diff --git a/Makefile b/Makefile index 8ab731ebd..5ebb9dfcf 100644 --- a/Makefile +++ b/Makefile @@ -144,7 +144,7 @@ $(X): $(GAMES.CONF) # precompute binary data structure for mega-attract mode configuration file $(ATTRACT.IDX): $(MD) - bin/buildokvs.sh < res/ATTRACT.CONF > "$@" + bin/buildokvs.py < res/ATTRACT.CONF > "$@" # precompute binary data structure and substitute special characters in global help $(HELPTEXT): $(MD) @@ -170,7 +170,7 @@ $(SS): $(SS.SOURCES) | $(MD) # precompute binary data structures for each game's mini-attract configuration file $(ATTRACT): $(ATTRACT.SOURCES) | $(MD) mkdir -p "$@" - $(PARALLEL) 'bin/buildokvs.sh < "{}" > "$@/{/}"' ::: res/ATTRACT/* + $(PARALLEL) 'bin/buildokvs.py < "{}" > "$@/{/}"' ::: res/ATTRACT/* (cd "$(ATTRACT)"/ && for f in [ABCDEFGHIJKLMNOP]*; do echo "$$f"; done) > "$(MINI.ATTRACT0.LIST)" (cd "$(ATTRACT)"/ && for f in [QRSTUVWXYZ]*; do echo "$$f"; done) > "$(MINI.ATTRACT1.LIST)" @touch "$@" diff --git a/bin/buildokvs.py b/bin/buildokvs.py new file mode 100755 index 000000000..395724971 --- /dev/null +++ b/bin/buildokvs.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 + +# parameters +# stdin - input containing key=value pairs (e.g. res/ATTRACT.CONF or some file in res/ATTRACT/) +# stdout - binary OKVS data structure + +import struct +import sys + +def build(records): + # yield OKVS header (2 x 2 bytes, unsigned int, little-endian) + yield struct.pack('<2H', len(records), 0) + + for key, dummy, value in records: + # yield record length (1 byte) + yield struct.pack('B', len(key) + len(value) + 3) + + # yield key (Pascal-style string) + yield struct.pack(f'{len(key)+1}p', key.encode('ascii')) + + # yield value (Pascal-style string) + yield struct.pack(f'{len(value)+1}p', value.encode('ascii')) + +if __name__ == "__main__": + 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): + sys.stdout.buffer.write(b) diff --git a/bin/buildokvs.sh b/bin/buildokvs.sh deleted file mode 100755 index 03dd0c756..000000000 --- a/bin/buildokvs.sh +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/bash - -# make temp file with just the key/value pairs (strip blank lines, comments, eof marker) -records=$(mktemp) -tr -d "\r" | awk '!/^$|^#/' > "$records" - -# make temp assembly source file that represents the binary OKVS data structure -source=$(mktemp) -(echo "*=0" # dummy program counter for assembler - echo "!le16 $(wc -l <"$records"), 0" # OKVS header - while IFS="=" read -r key value; do - echo "!byte ${#key}+${#value}+3" # OKVS record length - echo "!byte ${#key}" # OKVS key length - echo "!text \"$key\"" # OKVS key - echo "!byte ${#value}" # OKVS value length - echo "!text \"$value\"" # OKVS value - done < "$records") > "$source" - -# assemble temp source file into binary OKVS data structure, then output that -out=$(mktemp) -acme -o "$out" "$source" -cat "$out" - -# clean up -rm "$out" -rm "$source" -rm "$records" diff --git a/bin/buildsearch.py b/bin/buildsearch.py index 7beaf67cf..48d12c611 100755 --- a/bin/buildsearch.py +++ b/bin/buildsearch.py @@ -49,15 +49,13 @@ def build(records, args): !word {record_count:>8} """) - # yield OKVS record count (2 bytes, unsigned int, little-endian) - yield struct.pack('