Complete Classic BMH and FLTD installation imports (by Codex)

This commit is contained in:
Codex committed 2026-10-18 10:07:38 +02:00
1 parent 65b087c1d0
commit 3a021fc160
32 files changed
+2297 -203

No files matched your search

+124
View File
@@ -0,0 +1,124 @@
// SPDX-License-Identifier: GPL-3.0-only
#include "import/classic.hpp"
#include "formats/executable_layout.hpp"
#include "launch/game_data.hpp"
#include "launch/fltd.hpp"
#include "game/career.hpp"
#include "game/season.hpp"
#include "game/save.hpp"
#include "game/schedule.hpp"
#include "game/desk_actions.hpp"
#include <bit>
#include <chrono>
#include <iostream>
using namespace bmh;
namespace fs = std::filesystem;
namespace {
void check(bool ok, const std::string& why) { if (!ok) throw std::runtime_error(why); }
fs::path file(const fs::path& root, const std::string& name) {
const auto value = launch::find_file(root, name);
if (!value) throw std::runtime_error("missing " + name);
return *value;
}
void install(const LeImage& exe) {
install_fixture_tables(exe);
uint8_t sizes[9], first[8], counts[8];
for (uint32_t i = 0; i < 9; ++i) sizes[i] = exe.u8(layout::LeagueSizes + i);
set_league_sizes(sizes); kEnglishCompetitions = sizes[0] == 20;
for (uint32_t i = 0; i < 8; ++i) {
first[i] = exe.u8(layout::CalendarFirstLeague + i); counts[i] = exe.u8(layout::CalendarLeagueCounts + i);
kGroupBelow[i] = exe.u8(layout::CareerSession_InstallLeagueSizes_RuntimeField5 + i);
kGroupBelowCount[i] = exe.u8(layout::CareerSession_InstallLeagueSizes_RuntimeField4 + i);
}
set_league_groups(first, counts);
for (uint32_t i = 0; i < 9; ++i) {
kLeagueRelegation[i] = exe.u8(layout::CareerSession_InstallLeagueSizes_RuntimeField2 + i);
kLeagueEurope[i] = exe.u8(layout::CareerSession_InstallLeagueSizes_RuntimeField + i);
kLeagueSuffix[i] = i < 5 ? char(exe.u8(layout::CareerSession_InstallLeagueSizes_RuntimeField3 + i * 2)) : 0;
}
kNewcomerFirst = exe.u16(layout::CareerSession_DispatchLineup_RuntimeField);
kNewcomerCount = exe.u16(layout::CareerSession_InstallLeagueSizes_RuntimeField6);
}
struct Run { Bytes state; int32_t game; uint32_t watcom; uint64_t game_calls, watcom_calls; };
Run start(const LeImage& exe, const Database& db, const TextDb& texts, const Bytes& men, const Bytes& dat) {
install(exe);
auto state = startup_state(exe, db, men, dat);
state.texts = &texts;
state.currency_suffix = exe.cstring(layout::CurrencySuffixText);
configure_training_camp_data(state, exe);
for (size_t i = 0; i < state.credit_rate_table.size(); ++i) state.credit_rate_table[i] = exe.u8(layout::BankCredit_BankCreditContext_RuntimeField3 + uint32_t(i));
state.credit_divisor = std::bit_cast<double>(uint64_t(exe.u32(layout::BankCredit_BankCreditContext_RuntimeField)) |
uint64_t(exe.u32(layout::BankCredit_BankCreditContext_RuntimeField2)) << 32);
state.credit_rate_table_available = true;
GameRandom game; WatcomRandom watcom(8128); MinuteReport report;
startup_players(state, game, watcom);
check(new_career_before_dialog(state, game, watcom, report), "pre-dialog initialization: " + report.reason);
state.memory.set8(layout::CareerDuration, 20);
state.memory.set8(layout::Difficulty, 1); state.level = 1;
state.memory.set8(layout::ManagerCount, 1);
state.memory.set8(layout::GameMode, 3);
state.memory.set32(layout::CompetitionSelection, 3);
state.manager_records.at(0x21c) = 1;
career_managers_begin(state);
check(career_add_manager(state, exe, 0, {"IMPORT QA", 0, 0, 1, 0}, false, game, watcom, report), "manager initialization: " + report.reason);
career_managers_end(state, false);
check(career_season_setup(state, game, watcom, report), "season setup: " + report.reason);
check(career_first_steps(state, game, watcom, report), "first steps: " + report.reason);
// Compare in RAM only: this is an installation test, never an FBM converter.
return {save_stream(state), game.seed(), watcom.seed(), game.calls(), watcom.calls()};
}
} // namespace
int main(int argc, char** argv) {
fs::path temp;
try {
if (argc < 2 || std::string(argv[1]).empty()) return 77;
const fs::path root = argv[1];
const bool english = bool(launch::find_file(root, "FLTD.EXE")) && !launch::find_file(root, "BMH.EXE");
const auto exe = english ? launch::translate_fltd(file(root, "FLTD.EXE")) : load_le(file(root, "BMH.EXE"));
const auto source = english ? launch::translate_fltd(file(root, "FLTD.EXE"), false) : load_le(file(root, "BMH.EXE"), false);
const auto db = load_database(file(root, english ? "M3E.MAN" : "M3D.MAN"));
const auto texts = load_textd(file(root, english ? "TEXTE.BLK" : "TEXTD.BLK"));
const auto men = read_file(file(root, "M3.MEN")), dat = read_file(file(root, "M3.DAT"));
const auto original_run = start(exe, db, texts, men, dat);
const auto converted = obw::import_installation(root, {"2026-07-01", "Private installation test"});
temp = fs::temp_directory_path() / ("openbmh-classic-qa-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));
fs::create_directory(temp);
obw::write(converted, temp / "world.obw", false);
auto imported = obw::load_classic(obw::read(temp / "world.obw"));
const auto imported_run = start(imported.executable, imported.database, imported.texts, imported.desktops, imported.formations);
check(original_run.state == imported_run.state, "new-career first-season state differs after OBW round trip");
check(original_run.game == imported_run.game && original_run.watcom == imported_run.watcom &&
original_run.game_calls == imported_run.game_calls && original_run.watcom_calls == imported_run.watcom_calls,
"initialization random sequence differs");
for (uint32_t at = layout::GermanDataSegmentBegin; at < layout::GermanImageEnd; ++at) {
check(exe.u8(at) == imported.executable.u8(at), "effective data-table byte changed");
check(source.u8(at) == imported.source_tables.u8(at), "source data-table byte changed");
}
if (english) for (uint32_t at = layout::EnDataStart; at < layout::Career_CompetitionPointersEnglish_RuntimeField; ++at)
check(exe.u8(at) == imported.executable.u8(at) && source.u8(at) == imported.source_tables.u8(at), "English data-table byte changed");
for (const auto address : {layout::FreeKickTakerLabel, layout::PenaltyTakerLabel, layout::CornerTakerLabel, layout::AutomaticTakersLabel})
check(exe.cstring(address) == imported.executable.cstring(address) && source.cstring(address) == imported.source_tables.cstring(address), "embedded literal changed");
for (const auto& [name, image] : imported.images) {
if (!name.starts_with("kb/") && !name.starts_with("w/")) continue;
const auto folder = file(root, name.starts_with("kb/") ? "KB" : "W");
const auto original = load_vga(file(folder, name.substr(name.find('/') + 1) + ".VGA"));
check(original.width == image.width && original.height == image.height && original.pixels == image.pixels, "original atlas pixels changed");
}
const auto pal = load_palette(file(root, "M3PC.COL"));
check(imported.palette.argb == pal.argb, "palette changed");
for (const auto& [name, font] : imported.fonts) {
const auto original = load_font(file(file(root, "F"), name));
check(original.bitmap == font.bitmap && original.height == font.height && original.glyphs.size() == font.glyphs.size(), "font changed");
}
for (const auto& [name, bytes] : imported.audio) check(bytes == read_file(file(file(root, "S"), name)), "audio bytes changed");
std::cout << (english ? "FLTD" : "BMH") << " Classic: " << converted.size() << " entries; " << original_run.state.size()
<< " first-season bytes identical; RNG draws G=" << original_run.game_calls << " W=" << original_run.watcom_calls
<< "; source/effective data regions, pixels, palette, fonts and audio retained\n";
fs::remove_all(temp);
return 0;
} catch (const std::exception& error) {
if (!temp.empty()) { std::error_code ec; fs::remove_all(temp, ec); }
std::cerr << error.what() << '\n'; return 1;
}
}
+77 -3
View File
@@ -1,5 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-only
#include "import/obw.hpp"
#include "import/classic.hpp"
#include "import/assets.hpp"
#include "formats/executable_layout.hpp"
#include "launch/archive.hpp"
@@ -102,7 +104,7 @@ LeImage executable(bool english) {
std::vector<std::pair<int, uint32_t>>{{4, layout::CareerSession_RuntimeError_RuntimeField}, {6, layout::CareerSession_RuntimeError_RuntimeField2},
{12, layout::CareerSession_RuntimeError_RuntimeField3}, {16, layout::CareerSession_RuntimeError_ExecutableField3},
{18, layout::CareerSession_RuntimeError_ExecutableField}, {20, layout::CareerSession_RuntimeError_ExecutableField2}};
uint32_t cursor = base + 400000; // synthetic string storage, outside pointer tables
uint32_t cursor = layout::GermanImageEnd - 4096; // synthetic data storage, outside pointer tables
for (const auto& [teams, address] : bindings) {
put(address, cursor, 4); ++cursor;
std::vector<int> order;
@@ -118,6 +120,18 @@ LeImage executable(bool english) {
}
}
put(layout::Career_BuildStadium_RuntimeField4 - 1, 2);
put(base, 0xdeadbeef, 4); // an executable instruction region must never be exported
for (uint32_t i = 0; i < 5; ++i) {
put(layout::ClubPlayerDetail_ClubPlayerDetailRedraw_RuntimeField2 + i, 4);
put(layout::ClubPlayerDetail_DrawPortrait_RuntimeField + i, 2 + i);
put(layout::ClubPlayerDetail_DrawPortrait_RuntimeField2 + i, 3 + i);
put(layout::ClubPlayerDetail_DrawPortrait_RuntimeField3 + i, 3);
put(layout::ClubPlayerDetail_DrawPortrait_RuntimeField4 + i, 2);
put(layout::ClubPlayerDetail_DrawPortrait_RuntimeField7 + i * 2, 20, 2);
}
put(layout::ClubPlayerDetail_DrawPortrait_RuntimeField, 253);
put(layout::ClubPlayerDetail_DrawPortrait_RuntimeField3, 70);
put(layout::ClubPlayerDetail_DrawPortrait_RuntimeField7, 319, 2);
const std::string suffix = english ? " &" : " DM";
for (size_t i = 0; i < suffix.size(); ++i) put(layout::CurrencySuffixText + uint32_t(i), uint8_t(suffix[i]));
return LeImage::from_bytes(base, std::move(bytes));
@@ -225,10 +239,70 @@ int main() {
Bytes raw_texts(7);
for (const auto& value : texts.strings) { raw_texts.insert(raw_texts.end(), value.begin(), value.end()); raw_texts.push_back(0); }
save(input / "textd.blk", raw_texts);
const auto converted = obw::import_installation(input, options);
auto partial_options = options;
partial_options.database_only = true;
const auto converted = obw::import_installation(input, partial_options);
check(converted.contains("x-legacy/source-database.json"), "retain exact database for future mappings");
check(read_file(input / "m3d.man") == raw_database, "source database unchanged");
check(obw::import_installation(root, options) == converted, "follow one nested installation");
check(obw::import_installation(root, partial_options) == converted, "follow one nested installation");
rejects([&] { obw::import_installation(input, options); }, "full mod requires its media and startup data");
// Complete synthetic content profile, including embedded NULs and media.
for (const auto* dir : {"KB", "W", "F", "S"}) fs::create_directory(input / dir);
save(input / "M3.MEN", Bytes(864, 7)); save(input / "M3.DAT", Bytes(1408, 9));
save(input / "M3PC.COL", Bytes(768, 31));
Bytes font(37, 0);
word(font, 0, 1); font[3] = 2; word(font, 4, 1); font[8] = font[9] = 'A'; word(font, 16, 1);
font[28] = 0x80; word(font, 31, 1); word(font, 35, 1);
save(input / "F/BMH2.FNT", font); save(input / "F/BMH3.FNT", font);
Bytes bank{'F','O','R','M',8,0,0,0,'A','L','P','T','R','A','U','M'};
for (const char* name : {"2000", "2001", "2002", "2003"}) save(input / "S" / name, bank);
const auto image = [&](const fs::path& path) {
Bytes vga(8, 0), rle;
constexpr int width = 320, height = 240;
for (int i = 0; i < width * height; i += 128) {
rle.push_back(127);
for (int k = 0; k < 128; ++k) rle.push_back(uint8_t((i + k) % 251));
}
rle.push_back(0); word(vga, 0, width); word(vga, 2, height); word(vga, 4, uint32_t(rle.size()), 4);
vga.insert(vga.end(), rle.begin(), rle.end()); save(path, vga);
};
image(input / "KB/0.VGA"); image(input / "KB/29.VGA"); image(input / "W/0.VGA");
auto classic = converted;
obw::complete_classic(classic, input, db, exe, exe, false, options);
obw::validate(classic);
const auto loaded = obw::load_classic(classic);
check(loaded.desktops == Bytes(864, 7) && loaded.formations == Bytes(1408, 9), "all startup seed bytes survive");
check(loaded.database.players[0].name == db.players[0].name && loaded.texts.blob == Bytes(raw_texts.begin() + 7, raw_texts.end()), "source records and text blob survive");
check(loaded.images.at("kb/29").pixels.at(12 * 320 + 17) == uint8_t((12 * 320 + 17) % 251), "indexed media round trip");
check(loaded.images.at("portraits/layer-0-0").pixels.at(67) == uint8_t((3 * 320 + 320) % 251), "portrait feature preserves packed row wrap");
check(loaded.fonts.at("bmh2.fnt").glyph('A').bit_width == 1 && loaded.audio.at("2000") == bank, "font and audio round trip");
check(loaded.executable.u32(0x10000) == 0, "no executable code in compatibility image");
const auto portrait = obw::portrait_recipe("ABC", exe);
check(*f(portrait, "background").integer() >= 0 && f(portrait, "layers").array()->size() == 5, "frozen part recipe");
const auto classic_zip = root / "classic.obw";
obw::write(classic, classic_zip, false);
check(obw::read(classic_zip) == classic, "binary Classic ZIP round trip");
auto damaged = classic;
damaged.at("x-classic/desktop-seed.bin")[0] ^= 1;
rejects([&] { obw::validate(damaged); }, "inventory catches binary corruption");
damaged = classic; damaged["state/rng.json"] = "{}";
rejects([&] { obw::validate(damaged); }, "reject injected save state");
damaged = classic;
auto renamed = document(damaged, "world/players.json");
auto& renamed_rows = std::get<J::Array>(renamed.object()->at("items").value);
const auto appearance = f(renamed_rows[0], "appearance");
renamed_rows[0].object()->at("name").object()->at("last") = "Renamed";
damaged["world/players.json"] = cli::dump_json(renamed);
obw::refresh_classic_inventory(damaged);
obw::validate(damaged);
check(cli::dump_json(f(renamed_rows[0], "appearance")) == cli::dump_json(appearance), "rename keeps frozen face");
renamed_rows[0].object()->at("appearance").object()->at("background") = 4;
damaged["world/players.json"] = cli::dump_json(renamed);
obw::refresh_classic_inventory(damaged);
rejects([&] { obw::validate(damaged); }, "reject invalid appearance even with updated inventory");
auto wrong_options = options; wrong_options.mod = "FLTD-classic";
rejects([&] { obw::complete_classic(damaged, input, db, exe, exe, false, wrong_options); }, "mod/edition mismatch rejected");
save(input / "m3d.man", Bytes{0, 1});
rejects([&] { obw::import_installation(input, options); }, "reject truncated legacy database");
fs::remove_all(root);
+1 -1
View File
@@ -22,7 +22,7 @@ class PackageAllowlistTests(unittest.TestCase):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
# Empty authored placeholders test names/structure, not executable behavior.
for name in ['openbmh_play', 'openbmh_cli', 'README.md', 'LICENSE',
for name in ['openbmh_play', 'openbmh_cli', 'openbmh_import', 'README.md', 'LICENSE',
'THIRD_PARTY_NOTICES.md', 'ACKNOWLEDGEMENTS.md', 'CONTRIBUTING.md']:
(root / name).write_text('synthetic')
notes = root / 'docs/release/0.1/RELEASE_NOTES.md'