Move the game texts out of the source into openbmh.dat

The numbers of the Diamond 3.7 executable and of Football Limited stay built in,
the game texts go into data/openbmh.dat, which the release ships next to the
programs (found in OPENBMH_DAT, the game folder, the program folder, the current
folder). Without the file those texts are empty. A replay of a recorded session
gives the same output as with the real BMH.exe when the file is there, and differs
only in the seven text lines when it is missing. week_check no longer counts its
probe re-rating of the team as draws of the port (the apparent deviation from
minute 10 of the DOSBox recording was this measuring error).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
ClaudeandClaude Sonnet 5.5 committed 2026-11-08 14:32:27 +01:00
1 parent 524b9e4158
commit 9e593f921f
16 files changed
+2015 -1753

No files matched your search

+75 -30
View File
@@ -1,8 +1,10 @@
#!/usr/bin/env python3
"""Generates the embedded game images of OpenBMH, so that the game runs from the asset files alone (no BMH.exe, no
"""Generates the embedded game data of OpenBMH, so that the game runs from the asset files alone (no BMH.exe, no
FLTD.EXE): src/formats/diamond_constants.inc (German, Diamond 3.7) and src/formats/fltd_constants.inc (English
Football Limited). Both hold every non-zero byte of the data the engine reads - tables, rates, positions, prices and the
game texts - as runs of bytes; zero runs are not stored.
Football Limited) hold every non-zero byte of the numbers the engine reads - tables, rates, positions, prices - as runs of
bytes; zero runs are not stored. The game texts (live commentary, messages) are NOT in the source: they go into the data
file data/openbmh.dat that ships with the release (formats/diamond_image.hpp); the .inc files only list where they are.
After regenerating, put the new SHA-256 of data/openbmh.dat into REVIEWED_BINARY_FILES of tools/public_release/check_public_tree.py.
German (a Diamond 3.7 BMH.exe, read here and never copied into the repository):
python generate.py german --exe BMH.exe --trace trace1.txt [--trace trace2.txt ...] --out ../../src/formats/diamond_constants.inc
@@ -19,12 +21,17 @@ English (the German layout image of FLTD.EXE, the cached "BMH.exe" of an English
import argparse
import re
import struct
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent))
import classify # noqa: E402
DATA_LO, DATA_PAGES = 0xA0000, 12 # the initialised pages of the data object (file backed)
DATA_HI = DATA_LO + DATA_PAGES * 4096
CODE = (0x10000, 0x10000 + 438272)
PATCH = (0x80000, 0x81000)
EN_LO, EN_HI = 0x6B000, 0x776A8 # the English data segment, also placed at its own addresses
def load_le(path):
@@ -134,6 +141,16 @@ def read_traces(paths):
return numbers, bulk
def read_string_traces(paths):
out = []
for path in paths:
for line in Path(path).read_text().splitlines():
flag, start, length = (int(x, 16) for x in line.split())
if flag == 2:
out.append((start, length))
return out
def merge(ranges, gap=0):
out = []
for start, end in sorted((s, s + l) for s, l in ranges):
@@ -157,10 +174,11 @@ def nonzero_runs(data, base, lo, hi, gap=8):
return runs
def write_inc(path, title, base, data, chunks):
def write_inc(path, title, base, data, chunks, texts):
out = ["// SPDX-License-Identifier: GPL-3.0-only",
f"// Generated by tools/diamond_constants/generate.py from {title} - do not edit.",
"// Every non-zero byte of the data the engine reads (tables, rates, positions, prices, game texts), as runs.",
"// Every non-zero byte of the numbers the engine reads (tables, rates, positions, prices) as runs; the game texts",
"// are not part of it: kTextRanges lists where they lie, their bytes come from openbmh.dat.",
"// Included inside a namespace by formats/diamond_image.cpp.",
f"inline constexpr uint32_t kImageBase = {base}, kImageSize = {len(data)};"]
total = 0
@@ -173,48 +191,75 @@ def write_inc(path, title, base, data, chunks):
for i, (start, end) in enumerate(chunks):
out.append(f" {{{start}, {end - start}, kChunk{i}}},")
out.append("};")
out.append("inline constexpr Range kTextRanges[] = {")
for start, length in texts:
out.append(f" {{{start}, {length}}},")
out.append("};")
Path(path).write_text("\n".join(out) + "\n")
print(f"{len(chunks)} chunks, {total} bytes")
print(f"{path}: {len(chunks)} chunks, {total} bytes of numbers; {len(texts)} text ranges, {sum(l for _, l in texts)} bytes of text")
def zeroed(data, base, texts):
out = bytearray(data)
for start, length in texts:
out[start - base:start - base + length] = bytes(length)
return out
def dat_set(edition, data, base, texts):
out = struct.pack("<II", edition, len(texts))
for start, length in texts:
out += struct.pack("<II", start, length)
for start, length in texts:
out += bytes(data[start - base:start - base + length])
return out
def main():
ap = argparse.ArgumentParser()
sub = ap.add_subparsers(dest="mode", required=True)
de = sub.add_parser("german")
de.add_argument("--exe", required=True)
de.add_argument("--trace", action="append", default=[])
de.add_argument("--layout", default=str(Path(__file__).parents[2] / "src/formats/executable_layout.hpp"))
de.add_argument("--out", required=True)
en = sub.add_parser("english")
en.add_argument("--image", required=True)
en.add_argument("--out", required=True)
ap.add_argument("--exe", required=True, help="a Diamond 3.7 BMH.exe")
ap.add_argument("--english-image", required=True, help="the cached German layout image of FLTD.EXE (OPENBMH-IMAGE)")
ap.add_argument("--trace", action="append", default=[])
ap.add_argument("--layout", default=str(Path(__file__).parents[2] / "src/formats/executable_layout.hpp"))
ap.add_argument("--out-dir", required=True, help="src/formats")
ap.add_argument("--dat", required=True, help="data/openbmh.dat")
args = ap.parse_args()
if args.mode == "english":
base, data = load_openbmh_image(args.image)
write_inc(args.out, "the German layout image of FLTD.EXE (Football Limited)", base, data,
nonzero_runs(data, base, base, base + len(data)))
return
base, data = load_le(args.exe)
numbers, bulk = read_traces(args.trace)
layout = Path(args.layout).read_text()
# numbers read from the code or patch object: the bytes read, a margin, and a window after bulk pointers
out_dir = Path(args.out_dir)
# German (Diamond 3.7)
base, data = load_le(args.exe)
numbers_read, bulk = read_traces(args.trace)
strings_read = [(s, l) for s, l in read_string_traces(args.trace) if DATA_LO <= s < DATA_HI]
pointers = [int(a, 16) for n, a in re.findall(r"inline constexpr auto (\w*TextPointer\w*) = 0x([0-9a-fA-F]+);", layout)]
texts_de = classify.text_ranges(data, base, [(DATA_LO, DATA_HI)], [(DATA_LO, DATA_HI)], [CODE, PATCH], pointers, strings_read)
wanted = []
for s, l in numbers:
for s, l in numbers_read:
if CODE[0] <= s < CODE[1] or PATCH[0] <= s < PATCH[1]:
wanted.append((max(s - 4, 0), l + 8))
for s, l in bulk:
if CODE[0] <= s < CODE[1] or PATCH[0] <= s < PATCH[1]:
wanted.append((s, 256))
# and, whatever the traces saw, a window at every layout constant that names a place in the code objects
for name, address in re.findall(r"inline constexpr auto (\w+) = 0x([0-9a-fA-F]+);", layout):
a = int(address, 16)
if CODE[0] <= a < CODE[1] or PATCH[0] <= a < PATCH[1]:
wanted.append((a, 16))
chunks = sorted(nonzero_runs(data, base, DATA_LO, DATA_HI) + merge(wanted, 16))
write_inc(args.out, "BMH 2026-27 Diamond edition 3.7", base, data, chunks)
numbers_de = zeroed(data, base, texts_de)
chunks_de = sorted(nonzero_runs(numbers_de, base, DATA_LO, DATA_HI) + merge(wanted, 16))
write_inc(out_dir / "diamond_constants.inc", "BMH 2026-27 Diamond edition 3.7", base, numbers_de, chunks_de, texts_de)
# English (Football Limited, in the German address space)
ebase, edata = load_openbmh_image(args.english_image)
en_segment = (EN_LO, EN_HI)
texts_en = classify.text_ranges(edata, ebase, [(DATA_LO, DATA_HI), en_segment], [(DATA_LO, DATA_HI), en_segment], [], [], [])
numbers_en = zeroed(edata, ebase, texts_en)
write_inc(out_dir / "fltd_constants.inc", "the German layout image of FLTD.EXE (Football Limited)", ebase, numbers_en,
nonzero_runs(numbers_en, ebase, ebase, ebase + len(edata)), texts_en)
blob = b"OPENBMH-DAT\0" + struct.pack("<I", 2) + dat_set(1, data, base, texts_de) + dat_set(2, edata, ebase, texts_en)
Path(args.dat).write_bytes(blob)
print(f"{args.dat}: {len(blob)} bytes")
if __name__ == "__main__":
main()
main()