Load standalone community data without DOS programs and match Original crest backgrounds (by Codex)
Prepare verified release / draft (push) Canceled after 0s

This commit is contained in:
Codex committed 2026-10-31 11:43:25 +01:00
1 parent 3044b95b44
commit 1771cb5599
22 files changed
+454 -19

No files matched your search

+1
View File
@@ -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 &&
+3
View File
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-only
#include "formats/binary.hpp"
#include "formats/resource_bundle.hpp"
#include <fstream>
#include <iterator>
@@ -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<char>(in), {});
+4 -2
View File
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-only
#include "formats/data_profile.hpp"
#include "formats/resource_bundle.hpp"
#include <fstream>
#include <numeric>
#include <set>
@@ -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();
+43
View File
@@ -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<const char*>(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;i<labels;++i) {
if(at+8>raw.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);
+11
View File
@@ -11,11 +11,18 @@
#include <cstdint>
#include <filesystem>
#include <string>
#include <map>
#include <optional>
#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<uint32_t,std::string> 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<RuntimePolicy> 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
+92
View File
@@ -0,0 +1,92 @@
// SPDX-License-Identifier: GPL-3.0-only
#include "formats/resource_bundle.hpp"
#include "miniz.h"
#include <algorithm>
#include <cctype>
#include <fstream>
#include <iterator>
#include <map>
#include <memory>
#include <mutex>
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<std::string,mz_uint> 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<char>(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;i<count;++i) {
mz_zip_archive_file_stat st{};
if(!mz_zip_reader_file_stat(&zip,i,&st))throw FormatError("invalid community directory");
if(st.m_is_directory)continue;
const std::string name=st.m_filename;
const std::filesystem::path relative(name);
if(name.empty()||name.find('\\')!=std::string::npos||name.find(':')!=std::string::npos||relative.is_absolute())
throw FormatError("unsafe community resource name");
for(const auto& part:relative)if(part==".."||part==".")throw FormatError("unsafe community resource path");
total+=st.m_uncomp_size;
if(st.m_uncomp_size>32*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> bundle; };
std::map<std::filesystem::path,Cached> cache;
std::pair<std::shared_ptr<Bundle>,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<Bundle>(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<Bytes> 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;
}
}
+10
View File
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include "formats/binary.hpp"
#include <optional>
namespace bmh {
inline constexpr const char* kCommunityBundle = "COMMUNITY.OBD";
// Read-only package lookup. Loose user saves always remain outside the bundle.
std::optional<Bytes> bundled_resource(const std::filesystem::path& path);
bool resource_exists(const std::filesystem::path& path);
}
+1
View File
@@ -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};
+1
View File
@@ -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; }
+1 -1
View File
@@ -14,7 +14,7 @@ std::optional<std::filesystem::path> 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());
+13 -5
View File
@@ -11,6 +11,8 @@
#include <system_error>
#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<fs::path> {
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<fs::path> 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))
+2
View File
@@ -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 <algorithm>
@@ -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)) {