From 1771cb5599013e8a344f30b0b172a5500471bcfb Mon Sep 17 00:00:00 2001 From: Codex Date: Sat, 31 Oct 2026 11:43:25 +0100 Subject: [PATCH] Load standalone community data without DOS programs and match Original crest backgrounds (by Codex) --- CMakeLists.txt | 11 ++- README.md | 2 + docs/poc-1.1/STANDALONE.md | 21 ++++++ docs/poc-1.1/STATUS.md | 4 +- src/formats/badge_policy.hpp | 1 + src/formats/binary.cpp | 3 + src/formats/data_profile.cpp | 6 +- src/formats/le.cpp | 43 ++++++++++++ src/formats/le.hpp | 11 +++ src/formats/resource_bundle.cpp | 92 +++++++++++++++++++++++++ src/formats/resource_bundle.hpp | 10 +++ src/game/calendar.cpp | 1 + src/game/environment.cpp | 1 + src/launch/file_dialog.cpp | 2 +- src/launch/game_data.cpp | 18 +++-- src/ui/setup.cpp | 2 + tests/resource_bundle_tests.cpp | 62 +++++++++++++++++ tools/package/make_community_package.py | 64 +++++++++++++++++ tools/poc/bundle_community.py | 48 +++++++++++++ tools/poc/export_runtime.cpp | 18 +++++ tools/poc/prepare_international.py | 43 +++++++++--- tools/poc/test_prepare_international.py | 10 ++- 22 files changed, 454 insertions(+), 19 deletions(-) create mode 100644 docs/poc-1.1/STANDALONE.md create mode 100644 src/formats/resource_bundle.cpp create mode 100644 src/formats/resource_bundle.hpp create mode 100644 tests/resource_bundle_tests.cpp create mode 100644 tools/package/make_community_package.py create mode 100644 tools/poc/bundle_community.py create mode 100644 tools/poc/export_runtime.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a0a4f1d..51b8027 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ if(APPLE) # Enable native languages before dependencies/subdirectories inherit rules. enable_language(OBJC OBJCXX) endif() -set(OPENBMH_PRERELEASE "poc.1" CACHE STRING "Prerelease identifier; empty for final release") +set(OPENBMH_PRERELEASE "poc.2" CACHE STRING "Prerelease identifier; empty for final release") set(OPENBMH_PRODUCT_VERSION "${PROJECT_VERSION}") if(OPENBMH_PRERELEASE) string(APPEND OPENBMH_PRODUCT_VERSION "-${OPENBMH_PRERELEASE}") @@ -207,6 +207,7 @@ bmh_add_data_test(NAME highscore_tests REQUIRED_OPTIONS BMH_TEST_HIGHSCORE_TESTS # Engine core: file formats and game logic, no platform dependencies add_library(bmhcore STATIC src/formats/binary.cpp + src/formats/resource_bundle.cpp src/formats/palette.cpp src/formats/vga.cpp src/formats/font.cpp @@ -305,6 +306,7 @@ FetchContent_MakeAvailable(miniz lzma_sdk nfd) add_library(bmh_miniz STATIC ${miniz_SOURCE_DIR}/miniz.c) target_include_directories(bmh_miniz PUBLIC ${miniz_SOURCE_DIR}) +target_link_libraries(bmhcore PRIVATE bmh_miniz) set(BMH_LZMA_C ${lzma_sdk_SOURCE_DIR}/C) add_library(bmh_7z STATIC ${BMH_LZMA_C}/7zAlloc.c ${BMH_LZMA_C}/7zArcIn.c ${BMH_LZMA_C}/7zBuf.c ${BMH_LZMA_C}/7zCrc.c @@ -1158,3 +1160,10 @@ bmh_add_data_test(NAME desktop_feedback_tests COMMAND desktop_feedback_tests "${ add_executable(data_profile_tests tests/data_profile_tests.cpp) target_link_libraries(data_profile_tests PRIVATE bmhcore) add_test(NAME data_profile_tests COMMAND data_profile_tests) + +# Offline private package preparation; never installs Original data. +add_executable(export_community_runtime tools/poc/export_runtime.cpp) +target_link_libraries(export_community_runtime PRIVATE bmhcore) +add_executable(resource_bundle_tests tests/resource_bundle_tests.cpp) +target_link_libraries(resource_bundle_tests PRIVATE bmhcore bmh_miniz) +add_test(NAME resource_bundle_tests COMMAND resource_bundle_tests) diff --git a/README.md b/README.md index ca78804..3e47428 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # OpenBMH +This is the **1.1 community development branch**. A prepared community package starts directly from `COMMUNITY.OBD`, without BMH.exe or a DOS installation. Original loose Classic installations remain supported. The stable 1.0 line stays on main. See [standalone community data](docs/poc-1.1/STANDALONE.md). + A faithful, standalone community reconstruction of *Bundesliga Manager Hattrick*, using the player's original installation and SDL2 interface. OpenBMH 1.0 builds on v0.1.2 with reviewed bug fixes and starts directly with the original game data. Original game files, graphics, audio and manuals are not distributed. German CD-compatible BMH and verified Diamond editions are the primary release targets. Football Limited remains experimental; the older floppy executable is not supported. diff --git a/docs/poc-1.1/STANDALONE.md b/docs/poc-1.1/STANDALONE.md new file mode 100644 index 0000000..9ab56d9 --- /dev/null +++ b/docs/poc-1.1/STANDALONE.md @@ -0,0 +1,21 @@ +# Standalone community data + +The 1.1 community edition can start from `COMMUNITY.OBD` without an installed DOS game. The stable main line and loose Classic installation support remain separate. + +`COMMUNITY.OBD` is a compressed read-only resource bundle. It contains the master database, text, palette, formations, desktop layout, fonts, indexed pictures/crests and audio containers required by OpenBMH. These are game content, not application code. Saves, highscores and user layouts remain normal writable files outside the bundle. + +The bundle's `RUNTIME.DAT` uses the `OBMHDAT1` schema: data-segment base/length, calendar epoch, resolved badge/environment policies, data-segment bytes and explicitly named auxiliary labels. The exporter discards the DOS header, DOS/4GW loader and machine-code objects. Calendar and verified edition policies are values, rather than retained code fingerprints. This is not a renamed or embedded BMH executable. + +The Classic address-view adapter still supplies the existing engine/UI table readers. It reads resource entries directly from the compressed bundle, without unpacking original executables or a cached DOS installation. Bundle paths are case-insensitive and reject duplicate/path-traversal entries; compressed/uncompressed limits bound allocations. Original loose installations retain their loading path. + +Offline preparation from privately supplied resources: + +```sh +cmake --build build --target export_community_runtime +python3 tools/poc/bundle_community.py --source /path/to/prepared-data \ + --exporter /path/to/build/export_community_runtime --out /path/to/new/community-data +``` + +Only required resource types are included. DOS programs, installers, editor executables, original saves, backup directories, manuals and unused picture formats are excluded. Import provenance stays in `IMPORT-REPORT.json`; `PACKAGE.json` records the hashes of the bundled resources. A bundled `OBMHPROF.INI` selects the expanded league profile, so no additional loose Original files are needed. + +Generated foreign crests now use the supplied Original domestic crest background texture. Transparent edges are composited onto that bright texture before palette reduction; actual black logo details remain black. The previous black rectangles and the light-on-dark monochrome workaround are removed. diff --git a/docs/poc-1.1/STATUS.md b/docs/poc-1.1/STATUS.md index a630037..f64144e 100644 --- a/docs/poc-1.1/STATUS.md +++ b/docs/poc-1.1/STATUS.md @@ -15,6 +15,8 @@ Implemented: Local integration evidence so far: the expanded career completed 180 turns with seed 1 (three season ends), and another 180 with seed 11 (two season ends). No simulator invariant violations or contract-policy errors were reported. These are automated manager-policy runs, not claims of complete human gameplay QA or validation of all editions/platforms. -The prepared private dataset has 445 club/country entries, 3,334 players and 263 individual foreign club crests. Original German rosters and all 2,574 Original player word records are preserved by assertions during preparation. The visual review identified 175 usable supplied foreign crests and 88 missing/shared Original placeholders. Those 88 now have explicit fictional club-specific monograms instead of the computer symbol; very dark monochrome crests receive a documented contrast variant. Donor fields retain their real/fictional provenance. No original game content is committed here. +The prepared private dataset has 445 club/country entries, 3,334 players and 263 individual foreign club crests. Original German rosters and all 2,574 Original player word records are preserved by assertions during preparation. The visual review identified 175 usable supplied foreign crests and 88 missing/shared Original placeholders. Those 88 now have explicit fictional club-specific monograms instead of the computer symbol; the updated crest preparation uses the bright Original background texture and preserves black logo details. Donor fields retain their real/fictional provenance. No original game content is committed here. Validation: 37 configured synthetic tests executed successfully, with 158 external-input tests explicitly skipped; four offline preparation tests passed. Extended save/reload and both cross-profile rejection paths passed. Two separate season-end snapshots each contain every fixture in all nine leagues. A Classic 80-turn run produces a byte-identical save to stable 1.0.2. Screenshots and saves are archived privately. Native platform CI also builds and tests Windows x64, macOS ARM64 and Linux x64; release QA receipts identify the exact successful revision. See [FORMAT.md](FORMAT.md) for dimensions, preparation and distribution boundaries. A development commit is not a completed release claim. + +The next community package uses a data-only `COMMUNITY.OBD` bundle and no DOS executables. See [STANDALONE.md](STANDALONE.md). The Windows community ZIP omits source archives and loose PNG/JPEG studies; corresponding sources remain available separately. diff --git a/src/formats/badge_policy.hpp b/src/formats/badge_policy.hpp index be29e04..95105b8 100644 --- a/src/formats/badge_policy.hpp +++ b/src/formats/badge_policy.hpp @@ -12,6 +12,7 @@ inline int original_foreign_badge_end = 138; inline bool expanded_badges = false; inline int original_badge_gate(const LeImage& exe) { + if(const auto* policy=exe.runtime_policy())return int(policy->badge_end); constexpr auto instruction = layout::ForeignBadgeGateInstruction; if (exe.contains(instruction) && exe.contains(instruction + 6) && exe.u8(instruction) == 0x66 && exe.u8(instruction + 1) == 0x81 && diff --git a/src/formats/binary.cpp b/src/formats/binary.cpp index 81f5054..38c7993 100644 --- a/src/formats/binary.cpp +++ b/src/formats/binary.cpp @@ -1,5 +1,6 @@ // SPDX-License-Identifier: GPL-3.0-only #include "formats/binary.hpp" +#include "formats/resource_bundle.hpp" #include #include @@ -7,6 +8,8 @@ namespace bmh { Bytes read_file(const std::filesystem::path& path) { + if (!std::filesystem::exists(path)) + if (auto bytes=bundled_resource(path)) return std::move(*bytes); std::ifstream in(path, std::ios::binary); if (!in) throw FormatError("cannot open " + path.string()); return Bytes(std::istreambuf_iterator(in), {}); diff --git a/src/formats/data_profile.cpp b/src/formats/data_profile.cpp index 6779e2a..e39f8bc 100644 --- a/src/formats/data_profile.cpp +++ b/src/formats/data_profile.cpp @@ -1,5 +1,6 @@ // SPDX-License-Identifier: GPL-3.0-only #include "formats/data_profile.hpp" +#include "formats/resource_bundle.hpp" #include #include #include @@ -24,8 +25,9 @@ void DataProfile::validate() const { } DataProfile load_data_profile(const std::filesystem::path& directory) { const auto file=directory/"OBMHPROF.INI"; - if(!std::filesystem::exists(file))return {}; - std::ifstream input(file);if(!input)throw FormatError("profile: cannot read metadata"); + if(!resource_exists(file))return {}; + const auto raw=read_file(file); + std::istringstream input(std::string(raw.begin(),raw.end())); std::string line,id;bool section=false,seen=false; while(std::getline(input,line)) { if(!line.empty()&&line.back()=='\r')line.pop_back(); diff --git a/src/formats/le.cpp b/src/formats/le.cpp index 5d883ec..27813ef 100644 --- a/src/formats/le.cpp +++ b/src/formats/le.cpp @@ -18,6 +18,10 @@ struct Object { } // namespace std::string LeImage::cstring(uint32_t address) const { + if (runtime_policy_) { + const auto label=runtime_policy_->labels.find(address); + if(label!=runtime_policy_->labels.end())return label->second; + } std::string s; while (contains(address) && u8(address) != 0) s.push_back(char(u8(address++))); return s; @@ -37,6 +41,25 @@ void save_image(const LeImage& image, const std::filesystem::path& path) { if (!file) throw FormatError("cannot write the OpenBMH image"); } +void save_runtime_image(const LeImage& image, const RuntimePolicy& policy, const std::filesystem::path& path) { + constexpr uint32_t start=layout::GermanDataSegmentBegin; + if(!image.contains(start) || policy.year<1600 || policy.year>9999 || policy.weekday>6 || + (policy.badge_end!=90 && policy.badge_end!=138) || policy.environment_verified>1 || policy.labels.size()>16) + throw FormatError("runtime data: unsupported source policies"); + const uint32_t size=image.base()+image.size()-start; + if(size>1024*1024)throw FormatError("runtime data: data segment too large"); + Bytes out{'O','B','M','H','D','A','T','1'}; + const auto put=[&](uint32_t value){for(int b=0;b<4;++b)out.push_back(uint8_t(value>>(8*b)));}; + for(uint32_t value:{start,size,policy.year,policy.weekday,policy.badge_end,policy.environment_verified,uint32_t(policy.labels.size())})put(value); + out.insert(out.end(),image.data(start),image.data(start)+size); + for(const auto& [address,text]:policy.labels) { + if(text.size()>1024)throw FormatError("runtime data: label too long"); + put(address);put(uint32_t(text.size()));out.insert(out.end(),text.begin(),text.end()); + } + std::ofstream file(path,std::ios::binary|std::ios::trunc);file.write(reinterpret_cast(out.data()),std::streamsize(out.size())); + if(!file)throw FormatError("cannot write runtime data"); +} + LeImage load_le(const std::filesystem::path& path) { const auto profile = load_data_profile(path.parent_path()); const auto apply_profile = [&](LeImage img) { @@ -89,6 +112,26 @@ LeImage load_le(const std::filesystem::path& path) { return img; }; const Bytes raw = read_file(path); + if(raw.size()>=8 && std::string(raw.begin(),raw.begin()+8)=="OBMHDAT1") { + if(raw.size()<36)throw FormatError("runtime data: truncated header"); + const uint32_t base=le32(raw,8),size=le32(raw,12),labels=le32(raw,32); + RuntimePolicy policy{le32(raw,16),le32(raw,20),le32(raw,24),le32(raw,28),{}}; + if(base!=layout::GermanDataSegmentBegin || size>1024*1024 || size_t(size)+36>raw.size() || labels>16 || + policy.year<1600 || policy.year>9999 || policy.weekday>6 || (policy.badge_end!=90 && policy.badge_end!=138) || policy.environment_verified>1) + throw FormatError("runtime data: invalid dimensions or policies"); + auto image=LeImage::from_bytes(base,Bytes(raw.begin()+36,raw.begin()+36+size)); + size_t at=36+size; + for(uint32_t i=0;iraw.size())throw FormatError("runtime data: truncated label"); + const auto address=le32(raw,at),length=le32(raw,at+4);at+=8; + if(length>1024 || at+length>raw.size() || !policy.labels.emplace(address,std::string(raw.begin()+at,raw.begin()+at+length)).second) + throw FormatError("runtime data: invalid label"); + at+=length; + } + if(at!=raw.size())throw FormatError("runtime data: trailing bytes"); + image.runtime_policy_=std::move(policy); + return apply_profile(std::move(image)); + } 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; }; const uint32_t base = word(sizeof kImageMagic), size = word(sizeof kImageMagic + 4); diff --git a/src/formats/le.hpp b/src/formats/le.hpp index b606c02..6e14cd7 100644 --- a/src/formats/le.hpp +++ b/src/formats/le.hpp @@ -11,11 +11,18 @@ #include #include #include +#include +#include #include "formats/binary.hpp" namespace bmh { +// Resolved edition policies, not copied machine instructions. +struct RuntimePolicy { + uint32_t year=0, weekday=0, badge_end=0, environment_verified=0; + std::map labels; +}; class LeImage { public: uint32_t base() const { return base_; } @@ -44,10 +51,12 @@ public: } const Bytes& bytes() const { return bytes_; } bool expanded_profile() const { return expanded_profile_; } + const RuntimePolicy* runtime_policy() const { return runtime_policy_ ? &*runtime_policy_ : nullptr; } friend LeImage load_le(const std::filesystem::path& path); private: + std::optional runtime_policy_; bool expanded_profile_ = false; uint32_t base_ = 0; Bytes bytes_; @@ -60,5 +69,7 @@ LeImage load_le(const std::filesystem::path& path); // The OpenBMH image format: "OPENBMH-IMAGE" NUL, u32 base, u32 size, the bytes. load_le() // reads it as well, so a translated image can stand in for BMH.exe in a data directory. void save_image(const LeImage& image, const std::filesystem::path& path); +// Exports only the data segment and resolved policy values, never DOS/LE/code objects. +void save_runtime_image(const LeImage&, const RuntimePolicy&, const std::filesystem::path&); } // namespace bmh diff --git a/src/formats/resource_bundle.cpp b/src/formats/resource_bundle.cpp new file mode 100644 index 0000000..d1512fb --- /dev/null +++ b/src/formats/resource_bundle.cpp @@ -0,0 +1,92 @@ +// SPDX-License-Identifier: GPL-3.0-only +#include "formats/resource_bundle.hpp" +#include "miniz.h" +#include +#include +#include +#include +#include +#include +#include +namespace bmh { +namespace { +std::string folded(std::string name) { + std::transform(name.begin(),name.end(),name.begin(),[](unsigned char ch){return char(std::toupper(ch));}); + return name; +} +struct Bundle { + Bytes file; + mz_zip_archive zip{}; + std::map entries; + explicit Bundle(const std::filesystem::path& path) { + const auto size=std::filesystem::file_size(path); + if(size>64*1024*1024)throw FormatError("community bundle exceeds size limit"); + std::ifstream input(path,std::ios::binary); + file.assign(std::istreambuf_iterator(input),{}); + if(!input.is_open() || file.size()!=size || !mz_zip_reader_init_mem(&zip,file.data(),file.size(),0)) + throw FormatError("cannot read community bundle"); + try { + uint64_t total=0; + const auto count=mz_zip_reader_get_num_files(&zip); + if(count>4096)throw FormatError("community bundle has too many entries"); + for(mz_uint i=0;i32*1024*1024 || total>128*1024*1024 || + !entries.emplace(folded(name),i).second)throw FormatError("ambiguous or oversized community resources"); + } + }catch(...){mz_zip_reader_end(&zip);throw;} + } + ~Bundle(){mz_zip_reader_end(&zip);} +}; +std::mutex bundle_mutex; +struct Cached { std::filesystem::file_time_type modified; uintmax_t size; std::shared_ptr bundle; }; +std::map cache; +std::pair,std::string> locate(const std::filesystem::path& requested) { + auto path=std::filesystem::absolute(requested).lexically_normal(); + auto root=path.parent_path(); + for(int depth=0;depth<4 && !root.empty();++depth) { + const auto archive=root/kCommunityBundle; + std::error_code ec; + if(std::filesystem::is_regular_file(archive,ec)) { + const auto modified=std::filesystem::last_write_time(archive); + const auto size=std::filesystem::file_size(archive); + auto found=cache.find(archive); + if(found==cache.end()||found->second.modified!=modified||found->second.size!=size) { + auto bundle=std::make_shared(archive); + if(cache.size()>=4)cache.clear(); + found=cache.insert_or_assign(archive,Cached{modified,size,std::move(bundle)}).first; + } + auto name=folded(path.lexically_relative(root).generic_string()); + // Compatibility address-view API; the archive contains no EXE image. + if(name=="BMH.EXE")name="RUNTIME.DAT"; + return {found->second.bundle,std::move(name)}; + } + const auto parent=root.parent_path();if(parent==root)break;root=parent; + } + return {}; +} +} +std::optional bundled_resource(const std::filesystem::path& path) { + std::lock_guard lock(bundle_mutex); + auto [bundle,name]=locate(path);if(!bundle)return std::nullopt; + const auto found=bundle->entries.find(name);if(found==bundle->entries.end())return std::nullopt; + mz_zip_archive_file_stat st{};mz_zip_reader_file_stat(&bundle->zip,found->second,&st); + Bytes result(size_t(st.m_uncomp_size)); + if(!mz_zip_reader_extract_to_mem(&bundle->zip,found->second,result.data(),result.size(),0)) + throw FormatError("damaged community resource: "+name); + return result; +} +bool resource_exists(const std::filesystem::path& path) { + std::error_code ec;if(std::filesystem::is_regular_file(path,ec))return true; + std::lock_guard lock(bundle_mutex);auto [bundle,name]=locate(path); + return bundle && bundle->entries.count(name)!=0; +} +} diff --git a/src/formats/resource_bundle.hpp b/src/formats/resource_bundle.hpp new file mode 100644 index 0000000..9cef532 --- /dev/null +++ b/src/formats/resource_bundle.hpp @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: GPL-3.0-only +#pragma once +#include "formats/binary.hpp" +#include +namespace bmh { +inline constexpr const char* kCommunityBundle = "COMMUNITY.OBD"; +// Read-only package lookup. Loose user saves always remain outside the bundle. +std::optional bundled_resource(const std::filesystem::path& path); +bool resource_exists(const std::filesystem::path& path); +} diff --git a/src/game/calendar.cpp b/src/game/calendar.cpp index 56f3f70..31f0d34 100644 --- a/src/game/calendar.cpp +++ b/src/game/calendar.cpp @@ -5,6 +5,7 @@ namespace bmh { namespace { CalendarEpoch installed_epoch{2026, 2}; } CalendarEpoch calendar_epoch_from_executable(const LeImage& exe) { + if(const auto* policy=exe.runtime_policy())return {int(policy->year),int(policy->weekday)}; constexpr auto setup = layout::CalendarDateSetup; constexpr uint8_t weekday_prefix[] = {0xc6,0x44,0x24,0x04}; constexpr uint8_t month_and_year_prefix[] = {0xc6,0x44,0x24,0x0c,0x07,0xc7,0x04,0x24}; diff --git a/src/game/environment.cpp b/src/game/environment.cpp index ecd537d..ec8e2c7 100644 --- a/src/game/environment.cpp +++ b/src/game/environment.cpp @@ -38,6 +38,7 @@ void install_environment_rules(const LeImage& exe) { if (!exe.contains(at) || exe.u8(at)==0) { supported_environment=false; return; } kEnvironmentStep[i]=int32_t(exe.u8(at))*1000; } + if(const auto* policy=exe.runtime_policy()) { supported_environment=policy->environment_verified!=0;return; } uint64_t fingerprint = 14695981039346656037ull; for (const auto [first,last] : {std::pair{layout::EnvironmentVerifiedCodeBegin,layout::EnvironmentVerifiedCodeEnd},std::pair{layout::EnvironmentVerifiedHookBegin,layout::EnvironmentVerifiedHookEnd}}) { if (!exe.contains(first) || !exe.contains(last-1)) { supported_environment=false; return; } diff --git a/src/launch/file_dialog.cpp b/src/launch/file_dialog.cpp index 88dac73..ca1f214 100644 --- a/src/launch/file_dialog.cpp +++ b/src/launch/file_dialog.cpp @@ -14,7 +14,7 @@ std::optional choose_game_file(const std::string& title) NFD::Guard guard; NFD::UniquePathU8 path; // one filter with all accepted kinds; the check after the choice decides - const nfdu8filteritem_t filters[] = {{"BMH game (ZIP, 7z, BMH.EXE, FLTD.EXE)", "zip,7z,exe"}}; + const nfdu8filteritem_t filters[] = {{"BMH game (ZIP, 7z, BMH.EXE, FLTD.EXE)", "zip,7z,exe,obd"}}; const nfdresult_t result = NFD::OpenDialog(path, filters, 1); if (result == NFD_CANCEL) return std::nullopt; if (result != NFD_OKAY) throw std::runtime_error(std::string("no file dialog: ") + NFD::GetError()); diff --git a/src/launch/game_data.cpp b/src/launch/game_data.cpp index df203b7..8f7709c 100644 --- a/src/launch/game_data.cpp +++ b/src/launch/game_data.cpp @@ -11,6 +11,8 @@ #include #include "formats/binary.hpp" +#include "formats/resource_bundle.hpp" +#include "formats/data_profile.hpp" #include "formats/le.hpp" #include "formats/m3d.hpp" #include "formats/textd.hpp" @@ -52,6 +54,7 @@ struct Family { const char* database; }; constexpr Family kFamilies[] = { + {GameFamily::german, "COMMUNITY.OBD", 0, "TEXTD.BLK", "M3D.MAN"}, {GameFamily::german, "BMH.EXE", 602527, "TEXTD.BLK", "M3D.MAN"}, {GameFamily::english, "FLTD.EXE", 507925, "TEXTE.BLK", "M3E.MAN"}, {GameFamily::disk1994, "M3.EXE", 0, "", "M3PC.MAN"}, @@ -82,7 +85,7 @@ void require(GameCheck& c, const fs::path& dir, const std::string& name, uint64_ return; } std::error_code ec; - const uint64_t actual = fs::file_size(*p, ec); + const uint64_t actual = fs::exists(*p,ec) ? fs::file_size(*p, ec) : read_file(*p).size(); if (ec || actual == 0) c.problems.push_back(shown + " is empty or unreadable"); else if (size != 0 && actual != size) c.problems.push_back(shown + " has " + std::to_string(actual) + " bytes instead of " + std::to_string(size)); @@ -90,7 +93,9 @@ void require(GameCheck& c, const fs::path& dir, const std::string& name, uint64_ // The shared picture/font/sound files of the sub directories KB, W, F and S. void require_media(GameCheck& c) { + const bool bundled=fs::is_regular_file(c.directory/kCommunityBundle); const auto sub = [&](const char* name) -> std::optional { + if(bundled)return c.directory/name; auto p = find_file(c.directory, name); if (!p || !fs::is_directory(*p)) { c.problems.push_back(std::string("the directory ") + name + " is missing"); @@ -142,14 +147,15 @@ std::string text_title(const std::string& raw) { void check_german(GameCheck& c) { // (a translated English game has BMH.exe in the OpenBMH image format, launch/fltd.hpp) + const bool bundled=fs::is_regular_file(c.directory/kCommunityBundle); bool image = false; - if (const auto exe = find_file(c.directory, "BMH.EXE")) { + if (const auto exe = find_file(c.directory, "BMH.EXE"); exe && !bundled) { std::ifstream in(*exe, std::ios::binary); char head[13] = {}; in.read(head, sizeof head); image = std::string(head, sizeof head) == "OPENBMH-IMAGE"; } - require(c, c.directory, "BMH.EXE", image ? 0 : 602527, "BMH.EXE"); + require(c, c.directory, "BMH.EXE", (image || bundled) ? 0 : 602527, bundled ? "RUNTIME.DAT" : "BMH.EXE"); require(c, c.directory, "TEXTD.BLK", 0, "TEXTD.BLK"); require(c, c.directory, "M3D.MAN", 0, "M3D.MAN"); for (const Required& r : kCommon) require(c, c.directory, r.name, r.size, r.name); @@ -158,7 +164,8 @@ void check_german(GameCheck& c) { // the files must also load: the engine reads tables from the EXE, the texts and the // database of clubs and players try { - load_le(*find_file(c.directory, "BMH.EXE")); + const auto runtime=load_le(*find_file(c.directory, "BMH.EXE")); + if(bundled && (!runtime.runtime_policy() || !runtime.expanded_profile()))throw FormatError("unsupported standalone runtime/profile"); } catch (const std::exception& e) { c.problems.push_back(std::string("BMH.EXE cannot be read: ") + e.what()); } @@ -212,7 +219,7 @@ bool is_game_file(const std::string& relative) { const size_t dot = base.find_last_of('.'); if (dot == std::string::npos) return false; const std::string ext = base.substr(dot + 1); - for (const char* e : {"EXE", "BLK", "MAN", "DAT", "MEN", "COL", "VGA", "FNT", "LBM", "EIN", "CFG", "FBM", "WAP"}) + for (const char* e : {"EXE", "BLK", "MAN", "DAT", "MEN", "COL", "VGA", "FNT", "LBM", "EIN", "CFG", "FBM", "WAP", "OBD"}) if (ext == e) return true; return false; } @@ -315,6 +322,7 @@ ResolvedGame resolve_game_unchecked(const fs::path& input, const fs::path& cache std::optional find_file(const fs::path& directory, const std::string& name) { std::error_code ec; const fs::path exact = directory / path_from_utf8(name); + if(resource_exists(exact))return exact; if (fs::exists(exact, ec)) return exact; const std::string wanted = upper(name); for (const auto& e : fs::directory_iterator(directory, ec)) diff --git a/src/ui/setup.cpp b/src/ui/setup.cpp index 2fb490b..3b36a07 100644 --- a/src/ui/setup.cpp +++ b/src/ui/setup.cpp @@ -1,5 +1,6 @@ // SPDX-License-Identifier: GPL-3.0-only #include "formats/executable_layout.hpp" +#include "formats/resource_bundle.hpp" #include "ui/setup.hpp" #include @@ -30,6 +31,7 @@ void copy_rect(Image& dst, const Image& src, int sx, int sy, int dx, int dy, } // namespace std::filesystem::path asset_path(const std::filesystem::path& directory, const std::string& name) { + if (!std::filesystem::exists(directory / name) && resource_exists(directory / name)) return directory / name; std::filesystem::path found; for (const auto& e : std::filesystem::directory_iterator(directory)) { if (e.is_regular_file() && folded(e.path().filename().string()) == folded(name)) { diff --git a/tests/resource_bundle_tests.cpp b/tests/resource_bundle_tests.cpp new file mode 100644 index 0000000..af4852b --- /dev/null +++ b/tests/resource_bundle_tests.cpp @@ -0,0 +1,62 @@ +// SPDX-License-Identifier: GPL-3.0-only +#include "formats/resource_bundle.hpp" +#include "formats/le.hpp" +#include "formats/data_profile.hpp" +#include "formats/executable_layout.hpp" +#include "game/calendar.hpp" +#include "miniz.h" +#include +#include +#include +#include +#include +using namespace bmh; +void check(bool ok,const char* what){if(!ok)throw std::runtime_error(what);} +void bundle(const std::filesystem::path& path,const std::vector>& files) { + mz_zip_archive zip{}; + check(mz_zip_writer_init_heap(&zip,0,0)!=0,"synthetic ZIP initialization"); + for(const auto& [name,bytes]:files)check(mz_zip_writer_add_mem(&zip,name.c_str(),bytes.data(),bytes.size(),MZ_BEST_COMPRESSION)!=0,"synthetic ZIP entry"); + void* data=nullptr;size_t size=0; + check(mz_zip_writer_finalize_heap_archive(&zip,&data,&size)!=0,"synthetic ZIP finalization"); + std::ofstream out(path,std::ios::binary);out.write(static_cast(data),std::streamsize(size)); + mz_free(data);mz_zip_writer_end(&zip);check(bool(out),"synthetic ZIP write"); +} +Bytes bytes(std::string text){return Bytes(text.begin(),text.end());} +int main()try { + const auto root=std::filesystem::temp_directory_path()/("openbmh-bundle-"+std::to_string(std::chrono::steady_clock::now().time_since_epoch().count())); + std::filesystem::create_directories(root); + Bytes data(0x20000,0); + const auto put=[&](uint32_t address,uint8_t value){data.at(address-layout::GermanDataSegmentBegin)=value;}; + for(int league=0;league<9;++league)put(layout::LeagueSizes+uint32_t(league),league<5?18:12); + for(int day=24;day<24+34*8;day+=8)put(layout::CompetitionCalendar+uint32_t(day*2),1); + RuntimePolicy policy{2030,2,90,1,{{layout::FreeKickTakerLabel,"Test label"}}}; + save_runtime_image(LeImage::from_bytes(layout::GermanDataSegmentBegin,data),policy,root/"source.dat"); + const auto runtime=read_file(root/"source.dat"); + bundle(root/kCommunityBundle,{{"RUNTIME.DAT",runtime},{"OBMHPROF.INI",bytes("[OpenBMH]\nprofile=classic-plus-1\n")},{"KB/20.VGA",bytes("synthetic picture")}}); + std::filesystem::remove(root/"source.dat"); + check(!std::filesystem::exists(root/"BMH.exe"),"synthetic bundle accidentally requires an executable"); + check(resource_exists(root/"kb"/"20.vga") && read_file(root/"kb"/"20.vga")==bytes("synthetic picture"),"case-insensitive resource read"); + check(load_data_profile(root).extended(),"bundled profile metadata not found"); + const auto image=load_le(root/"BMH.exe"); + check(image.expanded_profile() && image.runtime_policy(),"data-only image did not bind the extended profile"); + check(!image.contains(layout::CalendarDateSetup) && !image.contains(layout::ForeignBadgeGateInstruction),"machine-code segment retained"); + check(calendar_epoch_from_executable(image).year==2030,"calendar still depends on original instructions"); + check(image.cstring(layout::FreeKickTakerLabel)=="Test label","resolved auxiliary label lost"); + check(image.u16(layout::TeamsWithSquads)==164,"standalone runtime roster dimensions differ"); + // Loose user-created files remain writable and are not replaced by bundle data. + {std::ofstream out(root/"user.FBM");out<<"user save";} + check(read_file(root/"user.FBM")==bytes("user save"),"bundle swallowed a loose user save"); + for(int kind=0;kind<2;++kind) { + const auto bad=root/("bad-"+std::to_string(kind));std::filesystem::create_directory(bad); + bundle(bad/kCommunityBundle,kind==0?std::vector>{{"../escape",{1}}}: + std::vector>{{"DUP",{1}},{"dup",{2}}}); + bool rejected=false;try{resource_exists(bad/"anything");}catch(const FormatError&){rejected=true;} + check(rejected,"unsafe or duplicate resource accepted"); + } + auto broken=runtime;broken.pop_back(); + {std::ofstream out(root/"broken.dat",std::ios::binary);out.write(reinterpret_cast(broken.data()),std::streamsize(broken.size()));} + bool rejected=false;try{load_le(root/"broken.dat");}catch(const FormatError&){rejected=true;} + check(rejected,"truncated runtime labels accepted"); + std::filesystem::remove_all(root); + std::cout<<"Standalone data policies, bundled resources, paths and loose saves passed\n";return 0; +}catch(const std::exception& e){std::cerr<=10_000_000: + destination.unlink() + raise ValueError('Community ZIP exceeds the 10 MB budget') + print(json.dumps({'file':str(destination),'bytes':destination.stat().st_size,'sha256':hashlib.sha256(destination.read_bytes()).hexdigest(),'source_included':False})) + +if __name__=='__main__': + p=argparse.ArgumentParser(description=__doc__);p.add_argument('--program',type=Path,required=True);p.add_argument('--data',type=Path,required=True);p.add_argument('--out',type=Path,required=True);p.add_argument('--version',required=True);a=p.parse_args();create(a.program,a.data,a.out,a.version) diff --git a/tools/poc/bundle_community.py b/tools/poc/bundle_community.py new file mode 100644 index 0000000..cf5ff6a --- /dev/null +++ b/tools/poc/bundle_community.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-3.0-only +"""Prepare a compact data-only community directory from private supplied content.""" +import argparse +import hashlib +import json +from pathlib import Path +import subprocess +import zipfile + +def build(source, destination, exporter): + if destination.exists(): + raise ValueError('Destination must not exist') + source=source.resolve();destination.mkdir(parents=True) + runtime=destination/'RUNTIME.DAT' + try: + exe=next(p for p in source.iterdir() if p.name.upper()=='BMH.EXE') + subprocess.run([str(exporter.resolve()),str(exe),str(runtime)],check=True) + data=runtime.read_bytes() + if data[:8]!=b'OBMHDAT1':raise ValueError('Exporter did not produce a data-only runtime') + # Only resources actually used by OpenBMH; no DOS programs, manuals, backups or unused original graphics formats. + names=('M3D.MAN','TEXTD.BLK','M3.DAT','M3.MEN','M3PC.COL','OBMHPROF.INI') + entries={'RUNTIME.DAT':data} + for name in names: + file=next(p for p in source.iterdir() if p.name.upper()==name) + entries[name]=file.read_bytes() + for directory in ('KB','W','F','S'): + root=next(p for p in source.iterdir() if p.name.upper()==directory) + for file in sorted(root.iterdir()): + if not file.is_file():continue + if directory in ('KB','W') and file.suffix.upper()!='.VGA':continue + if directory=='F' and file.name.upper() not in ('BMH2.FNT','BMH3.FNT'):continue + if directory=='S' and file.name not in ('2000','2001','2002','2003'):continue + entries[directory+'/'+file.name.upper()]=file.read_bytes() + receipt={'format':'openbmh-community-1','files':{name:hashlib.sha256(value).hexdigest() for name,value in entries.items()}, + 'executable_code_included':False,'profile':'classic-plus-1'} + entries['PACKAGE.json']=(json.dumps(receipt,indent=2)+'\n').encode() + archive=destination/'COMMUNITY.OBD' + with zipfile.ZipFile(archive,'w',zipfile.ZIP_DEFLATED,compresslevel=9) as z: + for name,value in entries.items():z.writestr(name,value) + report=source/'IMPORT-REPORT.json' + if report.exists():(destination/report.name).write_bytes(report.read_bytes()) + print(json.dumps({'bundle_bytes':archive.stat().st_size,'uncompressed_resource_bytes':sum(map(len,entries.values())), + 'resource_count':len(entries),'runtime_bytes':len(data),'bundle_sha256':hashlib.sha256(archive.read_bytes()).hexdigest()})) + finally:runtime.unlink(missing_ok=True) + +if __name__=='__main__': + p=argparse.ArgumentParser(description=__doc__);p.add_argument('--source',type=Path,required=True);p.add_argument('--out',type=Path,required=True);p.add_argument('--exporter',type=Path,required=True);a=p.parse_args();build(a.source,a.out,a.exporter) diff --git a/tools/poc/export_runtime.cpp b/tools/poc/export_runtime.cpp new file mode 100644 index 0000000..af23137 --- /dev/null +++ b/tools/poc/export_runtime.cpp @@ -0,0 +1,18 @@ +// SPDX-License-Identifier: GPL-3.0-only +#include "formats/le.hpp" +#include "formats/executable_layout.hpp" +#include "formats/badge_policy.hpp" +#include "game/calendar.hpp" +#include "game/environment.hpp" +#include +int main(int argc,char**argv)try { + if(argc!=3)throw std::runtime_error("usage: export_community_runtime "); + const auto image=bmh::load_le(argv[1]); + const auto epoch=bmh::calendar_epoch_from_executable(image); + bmh::install_environment_rules(image); + bmh::RuntimePolicy policy{uint32_t(epoch.year),uint32_t(epoch.weekday),uint32_t(bmh::original_badge_gate(image)),uint32_t(bmh::environment_rules_supported()),{}}; + for(auto address:{bmh::layout::FreeKickTakerLabel,bmh::layout::PenaltyTakerLabel,bmh::layout::CornerTakerLabel,bmh::layout::AutomaticTakersLabel}) + policy.labels.emplace(address,image.cstring(address)); + bmh::save_runtime_image(image,policy,argv[2]); + std::cout<<"Data-only runtime exported; no DOS header, loader or code objects\n";return 0; +}catch(const std::exception& e){std::cerr<{html.escape(initials)}' image=Image.open(io.BytesIO(cairosvg.svg2png(bytestring=svg.encode()))).convert('RGBA') - foreground = [(r,g,b) for r,g,b,a in image.getdata() if a >= 128] - contrast_adjusted = bool(foreground) and sum(max(pixel) for pixel in foreground)/len(foreground) < 60 - if contrast_adjusted: - alpha=image.getchannel('A');image=Image.new('RGBA',image.size,(238,244,240,0));image.putalpha(alpha) + contrast_adjusted = False # Black silhouettes remain readable on the Original bright background. image=ImageOps.contain(image,(36,36),method=Image.Resampling.LANCZOS) - canvas=Image.new('RGBA',(38,38));canvas.alpha_composite(image,((38-image.width)//2,(38-image.height)//2)) + canvas=backdrop_image.copy();canvas.alpha_composite(image,((38-image.width)//2,(38-image.height)//2)) pixels=bytearray() for r,g,b,a in canvas.getdata(): - if a<128:pixels.append(0);continue key=(r,g,b) - if key not in cache:cache[key]=min(range(1,256),key=lambda n:sum((x-y)**2 for x,y in zip(rgb[n],key))) + if key not in cache:cache[key]=min(range(256),key=lambda n:sum((x-y)**2 for x,y in zip(rgb[n],key))) pixels.append(cache[key]) dst=sheet(cell//8) for y in range(38):dst[y*304+(cell%8)*38:y*304+(cell%8)*38+38]=pixels[y*38:(y+1)*38] badges[index]=(badges[index]&~511)|cell - crest_report.append({'club':club['id'],'runtime_team':index,'cell':cell,'asset':club.get('crest'),'source':asset.get('source'),'license':asset.get('license'),'fictional':fictional,'contrast_adjusted':contrast_adjusted,'last_decode_error':error}) + crest_report.append({'club':club['id'],'runtime_team':index,'cell':cell,'asset':club.get('crest'),'source':asset.get('source'),'license':asset.get('license'),'fictional':fictional,'contrast_adjusted':contrast_adjusted,'background':'original-crest-texture','last_decode_error':error}) db['badges']=words(badges) # Every Original player word and all German starting rosters remain unchanged. original_db = read_master(master) diff --git a/tools/poc/test_prepare_international.py b/tools/poc/test_prepare_international.py index be9036f..5684841 100644 --- a/tools/poc/test_prepare_international.py +++ b/tools/poc/test_prepare_international.py @@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-3.0-only """Synthetic encoder/reader tests; no Original game content required.""" import unittest -from prepare_international import decode_vga, encode_vga, legacy_name, read_master, write_master, words, repeated_native_images +from prepare_international import decode_vga, encode_vga, legacy_name, read_master, write_master, words, repeated_native_images, crest_background_from_samples class ProfileDataTests(unittest.TestCase): def test_master_identity_and_sizes(self): @@ -37,6 +37,14 @@ class ProfileDataTests(unittest.TestCase): found=repeated_native_images(clubs,assets,lambda path: {'shared':b'placeholder','real':b'individual-crest'}[path]) self.assertEqual(found,{hashlib.sha256(b'placeholder').hexdigest()}) + def test_original_background_recovers_texture(self): + texture=bytes(92+(i%4) for i in range(38*38)) + samples=[bytearray(texture) for _ in range(7)] + for j, sample in enumerate(samples): + for i in range(j*100,j*100+80):sample[i]=0 + self.assertEqual(crest_background_from_samples(samples),texture) + self.assertNotIn(0,crest_background_from_samples(samples)) + def test_original_encoding(self): self.assertEqual(legacy_name('Müller'),b'M}LLER') self.assertEqual(legacy_name('Sørloth'),b'SORLOTH')