Run from the asset files alone: built-in numbers and texts for BMH and FLTD
The game no longer needs BMH.EXE or FLTD.EXE. The numbers and the game texts of the Diamond 3.7 executable and of the English Football Limited are generated into formats/diamond_constants.inc and formats/fltd_constants.inc (tools/diamond_constants). A missing BMH.exe loads the embedded German image, an English installation without FLTD.EXE gets the embedded English image; an executable that is present is used as before. The default manager name is OpenBMH. The launcher accepts directories and archives that hold only the asset files. Replays of a recorded session give identical output with and without BMH.EXE, and the English image equals the translated one. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6aa7c4f4a2
commit
524b9e4158
15 files changed
+4262
-23
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,46 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// Implementation of diamond_image.hpp: the images built from the embedded data of the generated .inc files.
|
||||
#include "formats/diamond_image.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <string>
|
||||
|
||||
namespace bmh {
|
||||
|
||||
namespace {
|
||||
|
||||
struct Chunk { uint32_t address; uint32_t size; const uint8_t* bytes; };
|
||||
|
||||
namespace german {
|
||||
#include "formats/diamond_constants.inc"
|
||||
} // namespace german
|
||||
namespace english {
|
||||
#include "formats/fltd_constants.inc"
|
||||
} // namespace english
|
||||
|
||||
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)
|
||||
std::copy(chunk->bytes, chunk->bytes + chunk->size, bytes.begin() + (chunk->address - base));
|
||||
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;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
LeImage diamond_constants_image() {
|
||||
return build(german::kImageBase, german::kImageSize, std::begin(german::kChunks), std::end(german::kChunks));
|
||||
}
|
||||
|
||||
LeImage english_constants_image() {
|
||||
return build(english::kImageBase, english::kImageSize, std::begin(english::kChunks), std::end(english::kChunks));
|
||||
}
|
||||
|
||||
bool is_executable_name(const std::filesystem::path& file_name) { return lower(file_name.string()) == "bmh.exe"; }
|
||||
|
||||
} // namespace bmh
|
||||
@@ -0,0 +1,29 @@
|
||||
// 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.
|
||||
//
|
||||
// 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
|
||||
// English installation the launcher (launch/fltd.cpp) writes english_constants_image() as the image of the game
|
||||
// directory when FLTD.EXE is missing.
|
||||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
#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();
|
||||
|
||||
// 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();
|
||||
|
||||
// Whether `file_name` is the name of the executable (BMH.exe in any case).
|
||||
bool is_executable_name(const std::filesystem::path& file_name);
|
||||
|
||||
} // namespace bmh
|
||||
File diff suppressed because it is too large.
Load diff
+66
-1
@@ -6,7 +6,11 @@
|
||||
// LeImage::cstring() reads NUL-terminated strings from the image. No global state.
|
||||
#include "formats/le.hpp"
|
||||
|
||||
#include "formats/diamond_image.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
|
||||
@@ -22,10 +26,63 @@ struct Object {
|
||||
|
||||
std::string LeImage::cstring(uint32_t address) const {
|
||||
std::string s;
|
||||
while (contains(address) && u8(address) != 0) s.push_back(char(u8(address++)));
|
||||
const uint32_t first = address;
|
||||
while (contains(address) && bytes_[address - base_] != 0) s.push_back(char(bytes_[address++ - base_]));
|
||||
if (le_trace::hook) le_trace::hook(first, uint32_t(s.size()) + 1, 't');
|
||||
return s;
|
||||
}
|
||||
|
||||
namespace le_trace {
|
||||
void (*hook)(uint32_t, uint32_t, char) = nullptr;
|
||||
|
||||
namespace {
|
||||
// One flag byte per image address: 1 number read, 2 string read, 4 start of a bulk read.
|
||||
std::vector<uint8_t>& marks() {
|
||||
static std::vector<uint8_t> value(0x100000, 0);
|
||||
return value;
|
||||
}
|
||||
std::string& trace_path() {
|
||||
static std::string value;
|
||||
return value;
|
||||
}
|
||||
void record(uint32_t address, uint32_t length, char kind) {
|
||||
auto& m = marks();
|
||||
const uint8_t flag = kind == 'n' ? 1 : kind == 't' ? 2 : 4;
|
||||
if (address < 0x10000 || address - 0x10000 >= m.size()) return;
|
||||
if (length == 0) length = 1;
|
||||
for (uint32_t i = 0; i < length && address - 0x10000 + i < m.size(); ++i) m[address - 0x10000 + i] |= flag;
|
||||
}
|
||||
// Appends the marked ranges ("<flag> <start> <length>" in hex, one per line) to the trace file.
|
||||
void flush() {
|
||||
if (trace_path().empty()) return;
|
||||
std::ofstream out(trace_path(), std::ios::app);
|
||||
const auto& m = marks();
|
||||
for (uint8_t flag : {uint8_t(1), uint8_t(2), uint8_t(4)}) {
|
||||
size_t i = 0;
|
||||
while (i < m.size()) {
|
||||
if (!(m[i] & flag)) { ++i; continue; }
|
||||
size_t j = i;
|
||||
while (j < m.size() && (m[j] & flag)) ++j;
|
||||
out << std::hex << unsigned(flag) << ' ' << (i + 0x10000) << ' ' << (j - i) << '\n';
|
||||
i = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void enable_from_environment() {
|
||||
static bool done = false;
|
||||
if (done) return;
|
||||
done = true;
|
||||
if (const char* path = std::getenv("OPENBMH_LE_TRACE"); path && *path) {
|
||||
trace_path() = path;
|
||||
(void)marks(); // constructed before atexit(flush) is registered, so it is destroyed after flush has run
|
||||
hook = record;
|
||||
std::atexit(flush);
|
||||
}
|
||||
}
|
||||
} // namespace le_trace
|
||||
|
||||
namespace {
|
||||
constexpr char kImageMagic[14] = "OPENBMH-IMAGE"; // 13 characters and the NUL
|
||||
}
|
||||
@@ -41,6 +98,14 @@ 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.
|
||||
{
|
||||
std::error_code ec;
|
||||
if (is_executable_name(path.filename()) && !std::filesystem::exists(path, ec))
|
||||
return diamond_constants_image();
|
||||
}
|
||||
const Bytes raw = read_file(path);
|
||||
if (raw.size() >= sizeof kImageMagic + 8 && std::equal(kImageMagic, kImageMagic + sizeof kImageMagic, raw.begin())) {
|
||||
const auto word = [&](size_t at) { return uint32_t(raw[at] | raw[at + 1] << 8 | raw[at + 2] << 16) | uint32_t(raw[at + 3]) << 24; };
|
||||
|
||||
+16
-2
@@ -16,19 +16,33 @@
|
||||
|
||||
namespace bmh {
|
||||
|
||||
// Read recording for building the embedded constants (formats/diamond_constants.inc): when the environment variable
|
||||
// OPENBMH_LE_TRACE names a file, every image read is noted (numbers, strings, bulk pointers) and appended to that
|
||||
// file when the program ends. Without the variable the hook is a null pointer and costs one branch per read.
|
||||
namespace le_trace {
|
||||
extern void (*hook)(uint32_t address, uint32_t length, char kind); // kind: 'n' number, 't' string, 'b' bulk pointer
|
||||
void enable_from_environment();
|
||||
} // namespace le_trace
|
||||
|
||||
class LeImage {
|
||||
public:
|
||||
uint32_t base() const { return base_; }
|
||||
uint32_t size() const { return uint32_t(bytes_.size()); }
|
||||
bool contains(uint32_t address) const { return address >= base_ && address - base_ < bytes_.size(); }
|
||||
uint8_t u8(uint32_t address) const { return bytes_.at(address - base_); }
|
||||
uint8_t u8(uint32_t address) const {
|
||||
if (le_trace::hook) le_trace::hook(address, 1, 'n');
|
||||
return bytes_.at(address - base_);
|
||||
}
|
||||
uint16_t u16(uint32_t address) const { return uint16_t(u8(address) | u8(address + 1) << 8); }
|
||||
uint32_t u32(uint32_t address) const { return uint32_t(u16(address)) | uint32_t(u16(address + 2)) << 16; }
|
||||
// A NUL terminated string at `address` (ISO 646-DE bytes, see textd.hpp).
|
||||
std::string cstring(uint32_t address) const;
|
||||
// The string a pointer variable at `address` points to (e.g. Weekly_AdvertisingOffers_TextPointer).
|
||||
std::string string_at_pointer(uint32_t address) const { return cstring(u32(address)); }
|
||||
const uint8_t* data(uint32_t address) const { return bytes_.data() + (address - base_); }
|
||||
const uint8_t* data(uint32_t address) const {
|
||||
if (le_trace::hook) le_trace::hook(address, 0, 'b');
|
||||
return bytes_.data() + (address - base_);
|
||||
}
|
||||
// Overwrites bytes inside a prepared image for verified address translation.
|
||||
void patch(uint32_t address, const uint8_t* bytes, size_t count) {
|
||||
for (size_t i = 0; i < count; ++i) bytes_.at(address - base_ + i) = bytes[i];
|
||||
|
||||
Reference in new issue
Block a user