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

File diff suppressed because it is too large. Load diff
+129 -8
View File
@@ -1,16 +1,32 @@
// SPDX-License-Identifier: GPL-3.0-only
// Implementation of diamond_image.hpp: the images built from the embedded data of the generated .inc files.
// Implementation of diamond_image.hpp: the images built from the embedded numbers of the generated .inc files plus the
// texts of openbmh.dat.
#include "formats/diamond_image.hpp"
#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <string>
#include <system_error>
#ifdef _WIN32
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#elif defined(__APPLE__)
#include <mach-o/dyld.h>
#else
#include <unistd.h>
#endif
namespace bmh {
namespace {
struct Chunk { uint32_t address; uint32_t size; const uint8_t* bytes; };
struct Range { uint32_t address; uint32_t length; };
namespace german {
#include "formats/diamond_constants.inc"
@@ -19,6 +35,19 @@ namespace english {
#include "formats/fltd_constants.inc"
} // namespace english
constexpr char kMagic[12] = "OPENBMH-DAT"; // 11 characters and the NUL
constexpr char kFileName[] = "openbmh.dat";
std::string lower(std::string s) {
for (char& c : s) c = char(std::tolower(static_cast<unsigned char>(c)));
return s;
}
uint32_t word(const Bytes& b, size_t at) {
if (at + 4 > b.size()) throw FormatError("openbmh.dat: truncated");
return uint32_t(b[at]) | uint32_t(b[at + 1]) << 8 | uint32_t(b[at + 2]) << 16 | uint32_t(b[at + 3]) << 24;
}
LeImage build(uint32_t base, uint32_t size, const Chunk* first, const Chunk* last) {
Bytes bytes(size, 0);
for (const Chunk* chunk = first; chunk != last; ++chunk)
@@ -26,19 +55,111 @@ LeImage build(uint32_t base, uint32_t size, const Chunk* first, const Chunk* las
return LeImage::from_bytes(base, std::move(bytes));
}
std::string lower(std::string s) {
for (char& c : s) c = char(std::tolower(static_cast<unsigned char>(c)));
return s;
std::filesystem::path executable_directory() {
std::error_code ec;
#ifdef _WIN32
wchar_t buffer[32768];
const DWORD n = GetModuleFileNameW(nullptr, buffer, DWORD(std::size(buffer)));
if (n == 0 || n >= std::size(buffer)) return {};
return std::filesystem::path(buffer).parent_path();
#elif defined(__APPLE__)
char buffer[4096];
uint32_t size = sizeof buffer;
if (_NSGetExecutablePath(buffer, &size) != 0) return {};
return std::filesystem::weakly_canonical(buffer, ec).parent_path();
#else
char buffer[4096];
const ssize_t n = readlink("/proc/self/exe", buffer, sizeof buffer - 1);
if (n <= 0) return {};
buffer[n] = 0;
return std::filesystem::path(buffer).parent_path();
#endif
}
std::filesystem::path file_in(const std::filesystem::path& directory) {
if (directory.empty()) return {};
std::error_code ec;
for (const auto& entry : std::filesystem::directory_iterator(directory, ec))
if (entry.is_regular_file(ec) && lower(entry.path().filename().string()) == kFileName) return entry.path();
return {};
}
void add_texts(LeImage& image, int edition, const std::filesystem::path& game_directory) {
const auto file = find_texts_file(game_directory);
if (file.empty()) return;
try {
apply_texts(image, edition, read_file(file));
} catch (const std::exception&) {
// a damaged texts file leaves the texts empty; the game itself still runs
}
}
} // namespace
LeImage diamond_constants_image() {
return build(german::kImageBase, german::kImageSize, std::begin(german::kChunks), std::end(german::kChunks));
std::filesystem::path find_texts_file(const std::filesystem::path& game_directory) {
if (const char* env = std::getenv("OPENBMH_DAT"); env && *env) {
std::error_code ec;
if (std::filesystem::is_regular_file(env, ec)) return env;
}
for (const auto& directory : {game_directory, executable_directory(), std::filesystem::path(".")})
if (auto file = file_in(directory); !file.empty()) return file;
return {};
}
LeImage english_constants_image() {
return build(english::kImageBase, english::kImageSize, std::begin(english::kChunks), std::end(english::kChunks));
std::string texts_signature(const std::filesystem::path& game_directory) {
const auto file = find_texts_file(game_directory);
if (file.empty()) return {};
try {
const Bytes data = read_file(file);
uint64_t hash = 1469598103934665603ull; // FNV-1a
for (uint8_t b : data) hash = (hash ^ b) * 1099511628211ull;
return std::to_string(data.size()) + "-" + std::to_string(hash);
} catch (const std::exception&) {
return {};
}
}
bool apply_texts(LeImage& image, int edition, const Bytes& file) {
if (file.size() < sizeof kMagic + 4 || !std::equal(kMagic, kMagic + sizeof kMagic, file.begin()))
throw FormatError("openbmh.dat: not an OpenBMH data file");
const Range* known = edition == 1 ? std::begin(german::kTextRanges) : std::begin(english::kTextRanges);
const Range* known_end = edition == 1 ? std::end(german::kTextRanges) : std::end(english::kTextRanges);
const uint32_t sets = word(file, sizeof kMagic);
if (sets > 16) throw FormatError("openbmh.dat: too many sets");
size_t at = sizeof kMagic + 4;
bool found = false;
for (uint32_t s = 0; s < sets; ++s) {
const uint32_t id = word(file, at), count = word(file, at + 4);
if (count > 65536) throw FormatError("openbmh.dat: too many ranges");
const size_t table = at + 8;
size_t data = table + size_t(count) * 8;
for (uint32_t n = 0; n < count; ++n) {
const uint32_t address = word(file, table + n * 8ull), length = word(file, table + n * 8ull + 4);
if (data + length > file.size()) throw FormatError("openbmh.dat: truncated");
if (int(id) == edition) {
const bool allowed = std::any_of(known, known_end,
[&](const Range& r) { return r.address == address && length <= r.length; });
if (!allowed) throw FormatError("openbmh.dat: a range is not a text of the game");
image.patch(address, file.data() + data, length);
found = true;
}
data += length;
}
at = data;
}
return found;
}
LeImage diamond_constants_image(const std::filesystem::path& game_directory) {
LeImage image = build(german::kImageBase, german::kImageSize, std::begin(german::kChunks), std::end(german::kChunks));
add_texts(image, 1, game_directory);
return image;
}
LeImage english_constants_image(const std::filesystem::path& game_directory) {
LeImage image = build(english::kImageBase, english::kImageSize, std::begin(english::kChunks), std::end(english::kChunks));
add_texts(image, 2, game_directory);
return image;
}
bool is_executable_name(const std::filesystem::path& file_name) { return lower(file_name.string()) == "bmh.exe"; }
+27 -8
View File
@@ -1,7 +1,9 @@
// SPDX-License-Identifier: GPL-3.0-only
// The game without BMH.exe and FLTD.EXE: OpenBMH carries the numbers and texts of the Diamond 3.7 executable (tables,
// rates, positions, prices, live commentary, messages; the generated formats/diamond_constants.inc) and of the English
// Football Limited (formats/fltd_constants.inc) and needs only the asset files of the game.
// The game without BMH.exe and FLTD.EXE: OpenBMH carries the numbers of the Diamond 3.7 executable (tables, rates,
// positions, prices; the generated formats/diamond_constants.inc) and of the English Football Limited
// (formats/fltd_constants.inc) and needs only the asset files of the game. The game texts that live in the executables
// (live commentary, messages) are not part of the source: they are in the data file openbmh.dat, which ships with the
// release next to the programs (tools/diamond_constants/generate.py makes it). Without that file those texts are empty.
//
// load_le() returns diamond_constants_image() for a BMH.exe that does not exist, so every reader of the executable image
// works unchanged. A BMH.exe that is there is used as before (it carries its own edition's numbers and texts). For an
@@ -10,18 +12,35 @@
#pragma once
#include <filesystem>
#include <string>
#include "formats/binary.hpp"
#include "formats/le.hpp"
namespace bmh {
// The German (Diamond 3.7) image built from the embedded data.
LeImage diamond_constants_image();
// openbmh.dat: "OPENBMH-DAT" NUL, u32 set count, then per set u32 edition (1 German, 2 English), u32 range count, count
// times (u32 address, u32 length), then the bytes of all ranges. A range must be one of the edition's text ranges, so
// the file can never change a number of the image.
//
// The file is looked for in: the environment variable OPENBMH_DAT, the folder of the game data, the folder of the
// running program, the current folder. Returns an empty path when there is none.
std::filesystem::path find_texts_file(const std::filesystem::path& game_directory);
// The English Football Limited image in the German address space, built from the embedded data (what
// launch::translate_fltd() makes from FLTD.EXE).
LeImage english_constants_image();
// A short text that changes when the texts file changes (empty without a file): used to rebuild cached English games.
std::string texts_signature(const std::filesystem::path& game_directory);
// The German (Diamond 3.7) image built from the embedded numbers plus the texts of openbmh.dat (found for
// `game_directory`).
LeImage diamond_constants_image(const std::filesystem::path& game_directory = {});
// The English Football Limited image in the German address space (what launch::translate_fltd() makes from
// FLTD.EXE), built from the embedded numbers plus the texts of openbmh.dat.
LeImage english_constants_image(const std::filesystem::path& game_directory = {});
// Puts the texts of an openbmh.dat file (edition 1 or 2) into `image`. Throws FormatError for malformed data or for a
// range that is not a text range of that edition. Returns false when the file has no set of that edition.
bool apply_texts(LeImage& image, int edition, const Bytes& file);
// Whether `file_name` is the name of the executable (BMH.exe in any case).
bool is_executable_name(const std::filesystem::path& file_name);
File diff suppressed because it is too large. Load diff
+3 -3
View File
@@ -99,12 +99,12 @@ void save_image(const LeImage& image, const std::filesystem::path& path) {
LeImage load_le(const std::filesystem::path& path) {
le_trace::enable_from_environment();
// The game runs from its asset files alone: for a BMH.exe that is not there the embedded numbers and texts of the
// Diamond 3.7 executable stand in for it.
// The game runs from its asset files alone: for a BMH.exe that is not there the embedded numbers of the Diamond 3.7 executable
// and the texts of openbmh.dat stand in for it.
{
std::error_code ec;
if (is_executable_name(path.filename()) && !std::filesystem::exists(path, ec))
return diamond_constants_image();
return diamond_constants_image(path.has_parent_path() ? path.parent_path() : std::filesystem::path("."));
}
const Bytes raw = read_file(path);
if (raw.size() >= sizeof kImageMagic + 8 && std::equal(kImageMagic, kImageMagic + sizeof kImageMagic, raw.begin())) {