From c6341aa4f90f0e2a4cfbe7909b80a1b9f9c536d8 Mon Sep 17 00:00:00 2001 From: Codex Date: Sun, 1 Nov 2026 19:38:29 +0100 Subject: [PATCH] (by Codex) Import verified Diamond 3.7 native save containers --- CMakeLists.txt | 9 ++++++ src/app/career_session.cpp | 3 +- src/formats/native_save_version.hpp | 20 ++++++++++++ src/ui/desk_state.cpp | 4 +-- src/ui/preparation.cpp | 3 +- src/ui/save_load.cpp | 4 ++- tests/native_save_import_tests.cpp | 43 ++++++++++++++++++++++++++ tests/native_save_version_tests.cpp | 47 +++++++++++++++++++++++++++++ 8 files changed, 128 insertions(+), 5 deletions(-) create mode 100644 src/formats/native_save_version.hpp create mode 100644 tests/native_save_import_tests.cpp create mode 100644 tests/native_save_version_tests.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d2d3632..79ee023 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1189,3 +1189,12 @@ add_test(NAME bank_robbery_month_tests COMMAND bank_robbery_month_tests) add_executable(monthly_dividend_tests tests/monthly_dividend_tests.cpp) target_link_libraries(monthly_dividend_tests PRIVATE bmhui) add_test(NAME monthly_dividend_tests COMMAND monthly_dividend_tests) + +add_executable(native_save_version_tests tests/native_save_version_tests.cpp) +target_link_libraries(native_save_version_tests PRIVATE bmhcore) +add_test(NAME native_save_version_tests COMMAND native_save_version_tests) +add_executable(native_save_import_tests tests/native_save_import_tests.cpp ${BMH_APP_CONTROLLER_SOURCE} ${BMH_APP_SESSION_SDL_SOURCE}) +target_link_libraries(native_save_import_tests PRIVATE bmhui openbmh_audio ${BMH_SDL_TARGET}) +set(BMH_TEST_NATIVE_SAVE_IMPORT_INPUT "" CACHE FILEPATH "External native save for import compatibility; not distributed") +bmh_add_data_test(NAME native_save_import_tests REQUIRED_OPTIONS BMH_TEST_NATIVE_SAVE_IMPORT_INPUT + COMMAND native_save_import_tests "${BMH_DATA}" "${BMH_TEST_NATIVE_SAVE_IMPORT_INPUT}") diff --git a/src/app/career_session.cpp b/src/app/career_session.cpp index 2303aba..babb8c1 100644 --- a/src/app/career_session.cpp +++ b/src/app/career_session.cpp @@ -1,4 +1,5 @@ // SPDX-License-Identifier: GPL-3.0-only +#include "formats/native_save_version.hpp" #include "formats/badge_policy.hpp" #include "game/calendar.hpp" #include "formats/executable_layout.hpp" @@ -586,7 +587,7 @@ CareerSession::CareerSession(const std::filesystem::path& data, uint32_t watcom_ session_id_ = next_session_id.fetch_add(1, std::memory_order_relaxed); initialize_club_modal_runtime(); const Savegame file = load_savegame(save_path); - if (!file.checksum_ok || file.version != "V2.24") throw std::runtime_error("savegame: unsupported or damaged FBM"); + require_native_savegame(file); const DbCounts counts{exe_.u32(::bmh::layout::CareerSession_DataDirectory_RuntimeField), exe_.u32(::bmh::layout::CareerSession_DataDirectory_RuntimeField3), exe_.u32(::bmh::layout::CareerSession_DataDirectory_RuntimeField2), exe_.u32(::bmh::layout::CareerSession_DataDirectory_RuntimeField4)}; const SaveState save(file.stream, counts); g_ = GameState::from_save(save, counts); diff --git a/src/formats/native_save_version.hpp b/src/formats/native_save_version.hpp new file mode 100644 index 0000000..00ae626 --- /dev/null +++ b/src/formats/native_save_version.hpp @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-3.0-only +#pragma once +#include "formats/fbm.hpp" +#include + +namespace bmh { +// Verified German V2.24 state layout: the supplied Diamond 3.7 changes its +// container label, while retaining the same block order and database counts. +// Expanded careers have their own layout and must not accept these markers. +inline bool supported_native_save_version(std::string_view version, bool expanded = false) { + return expanded ? version == "O1.1" : version == "V2.24" || version == "V3.7"; +} +inline void require_native_savegame(const Savegame& save, bool expanded = false) { + if (!save.checksum_ok) throw FormatError("savegame: FBM checksum mismatch"); + if (!supported_native_save_version(save.version, expanded)) + throw FormatError("savegame: unsupported FBM version " + save.version); + // Callers still parse the complete state using the installed edition's + // exact DbCounts; a recognized marker never bypasses layout validation. +} +} diff --git a/src/ui/desk_state.cpp b/src/ui/desk_state.cpp index 1bb0bd5..3ac6295 100644 --- a/src/ui/desk_state.cpp +++ b/src/ui/desk_state.cpp @@ -1,4 +1,5 @@ // SPDX-License-Identifier: GPL-3.0-only +#include "formats/native_save_version.hpp" #include "formats/badge_policy.hpp" #include "formats/executable_layout.hpp" #include "ui/desk_state.hpp" @@ -690,8 +691,7 @@ DeskState desk_from_game(const GameState& g, const LeImage& exe, uint64_t revisi DeskState load_reference_desk(const std::filesystem::path& path, const LeImage& exe) { auto fbm = load_savegame(path); - if (!fbm.checksum_ok) throw FormatError("desk: FBM checksum mismatch"); - if (fbm.version != "V2.24") throw FormatError("desk: unsupported FBM version"); + require_native_savegame(fbm); DbCounts counts{exe.u32(::bmh::layout::CareerSession_DataDirectory_RuntimeField), exe.u32(::bmh::layout::CareerSession_DataDirectory_RuntimeField3), exe.u32(::bmh::layout::CareerSession_DataDirectory_RuntimeField2), exe.u32(::bmh::layout::CareerSession_DataDirectory_RuntimeField4)}; SaveState save(fbm.stream, counts); auto d = desk_from_save(save, exe); diff --git a/src/ui/preparation.cpp b/src/ui/preparation.cpp index 9e3d3aa..39bd0ae 100644 --- a/src/ui/preparation.cpp +++ b/src/ui/preparation.cpp @@ -1,4 +1,5 @@ // SPDX-License-Identifier: GPL-3.0-only +#include "formats/native_save_version.hpp" #include "formats/executable_layout.hpp" #include "ui/preparation.hpp" @@ -93,7 +94,7 @@ PreparationState preparation_from_game(const GameState& game, const LeImage& exe PreparationState load_preparation_reference(const std::filesystem::path& path, const LeImage& exe) { const auto fbm = load_savegame(path); - if (!fbm.checksum_ok || fbm.version != "V2.24") throw FormatError("preparation: unsupported/damaged FBM"); + require_native_savegame(fbm); const DbCounts counts{exe.u32(::bmh::layout::CareerSession_DataDirectory_RuntimeField), exe.u32(::bmh::layout::CareerSession_DataDirectory_RuntimeField3), exe.u32(::bmh::layout::CareerSession_DataDirectory_RuntimeField2), exe.u32(::bmh::layout::CareerSession_DataDirectory_RuntimeField4)}; const SaveState save(fbm.stream, counts); PreparationState result; diff --git a/src/ui/save_load.cpp b/src/ui/save_load.cpp index e11a1c6..8fc93fb 100644 --- a/src/ui/save_load.cpp +++ b/src/ui/save_load.cpp @@ -1,4 +1,5 @@ // SPDX-License-Identifier: GPL-3.0-only +#include "formats/native_save_version.hpp" #include "formats/executable_layout.hpp" #include "ui/save_load.hpp" @@ -185,7 +186,8 @@ bool SaveLoadScreen::info_press(int x, int y) { try { const auto saved = load_savegame(entries_.at(size_t(selected_)).path); const auto& h = saved.plain_header; - if (!saved.checksum_ok || saved.version != "V2.24" || h.size() < 172 || h[0] < 1 || h[0] > 4) + require_native_savegame(saved); + if (h.size() < 172 || h[0] < 1 || h[0] > 4) throw FormatError("save/load: invalid native information header"); const auto field = [&](size_t at, size_t count) { const auto end = h.begin() + at + count; diff --git a/tests/native_save_import_tests.cpp b/tests/native_save_import_tests.cpp new file mode 100644 index 0000000..5d38a72 --- /dev/null +++ b/tests/native_save_import_tests.cpp @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: GPL-3.0-only +#include "app/controller.hpp" +#include "formats/native_save_version.hpp" +#include "game/save.hpp" +#include +#include +#include +using namespace bmh;using namespace bmh::ui; +namespace {void check(bool value,const char* why){if(!value)throw std::runtime_error(why);}} +int main(int argc,char** argv)try { + if(argc<3||!*argv[1]||!*argv[2])return 77; + const std::filesystem::path data=argv[1],input=argv[2];const auto unchanged=read_file(input); + const auto container=load_savegame(input);require_native_savegame(container); + app::CareerSession imported(data,713,input);const auto desk=imported.desktop_projection(1); + check(desk.manager_slot>=0&&!desk.manager_name.empty(),"unchanged external save did not produce an actual career desk"); + const auto exe=load_le(asset_path(data,"BMH.exe")); + check(load_reference_desk(input,exe).manager_slot==desk.manager_slot,"read-only desktop importer rejects an accepted version"); + check(load_preparation_reference(input,exe).desktop.manager_slot==desk.manager_slot,"preparation importer rejects an accepted version"); + const auto directory=std::filesystem::temp_directory_path()/("openbmh-native-import-"+std::to_string(std::chrono::steady_clock::now().time_since_epoch().count())); + std::filesystem::create_directories(directory); + try { + std::filesystem::copy_file(input,directory/"IMPORT.FBM"); + SaveLoadScreen browser(data,directory,SaveLoadMode::load,desk.manager_slot,1,Image(320,240)); + check(browser.info_press(80,45)&&browser.info_open(),"load-list information rejects the accepted original save"); + StartupFlow flow(data,directory);std::unique_ptr session;uint64_t revision=0; + openbmh::audio::Playback playback;std::optional audio; + flow.click(214,215); + check(flow.screen_name()=="save_load","setup did not open the actual game browser"); + const auto request=flow.send_key("ENTER");check(request.request==FlowRequest::file_action,"browser did not emit the original save load request"); + const auto result=app::serve_flow_request(flow,request,session,data,directory,713,revision,playback,audio,false); + check(result.empty()&&session&&flow.screen_name()=="desktop","shared production controller rejects the original version"); + const auto output=directory/"ROUNDTRIP.FBM";std::string error; + check(imported.save_game(output,desk.manager_slot,false,error),"regular session save rejected the imported original career"); + const auto saved=load_savegame(output);require_native_savegame(saved); + app::CareerSession reloaded(data,911,output); + check(reloaded.desktop_projection(1).manager_slot==desk.manager_slot&& + save_stream(reloaded.state())==save_stream(imported.state()), + "ordinary save/reload changed the imported career's native state"); + check(read_file(input)==unchanged,"original input save was changed"); + std::filesystem::remove_all(directory); + }catch(...){std::filesystem::remove_all(directory);throw;} + std::cout<<"Unchanged external "< +#include +#include +#include +using namespace bmh; +namespace { +void check(bool value,const char* why){if(!value)throw std::runtime_error(why);} +templatevoid rejects(F function,const char* message){bool rejected=false;try{function();}catch(const FormatError&){rejected=true;}check(rejected,message);} +} +int main()try { + GameState state;state.manager_records.resize(5*0xb0c);state.manager_teams={0};state.club_ids={0};state.club_names={'Q',0}; + state.memory.add_block(layout::ManagerCount,1);state.memory.set8(layout::ManagerCount,1); + WatcomRandom random(713);const auto original=make_savegame(state,random); + for(const char* version:{"V2.24","V3.7"}) { + auto saved=original;saved.version=version;require_native_savegame(saved); + const SaveState parsed(saved.stream);check(!parsed.blocks().empty(),"recognized synthetic native layout does not parse"); + auto truncated=saved;truncated.stream.pop_back(); + rejects([&]{require_native_savegame(truncated);(void)SaveState(truncated.stream);},"recognized version bypassed strict state/trailer validation"); + auto counts=DbCounts{};++counts.players; + rejects([&]{require_native_savegame(saved);(void)SaveState(saved.stream,counts);},"recognized version bypassed installed database dimensions"); + } + for(const char* version:{"V2.20","V3.70","v3.7","V9.99","O1.1","O1.2",""}) { + auto saved=original;saved.version=version; + rejects([&]{require_native_savegame(saved);},"unknown/different-layout native version was accepted"); + } + check(supported_native_save_version("O1.1",true)&&!supported_native_save_version("V2.24",true)&& + !supported_native_save_version("V3.7",true)&&!supported_native_save_version("V2.20",true), + "expanded layout accepted a classic/English marker or rejected its own version"); + auto damaged=original;damaged.checksum_ok=false; + try{require_native_savegame(damaged);throw std::runtime_error("checksum failure was accepted");} + catch(const FormatError& error){check(std::string(error.what()).find("checksum mismatch")!=std::string::npos,"checksum failure was confused with version support");} + auto unsupported=original;unsupported.version="V2.20"; + try{require_native_savegame(unsupported);throw std::runtime_error("different-layout version was accepted");} + catch(const FormatError& error){check(std::string(error.what()).find("unsupported FBM version V2.20")!=std::string::npos,"version rejection lost its actual marker");} + const auto path=std::filesystem::temp_directory_path()/("openbmh-version-"+std::to_string(std::chrono::steady_clock::now().time_since_epoch().count())+".FBM"); + auto version37=original;version37.version="V3.7";write_savegame(path,version37);const auto loaded=load_savegame(path); + require_native_savegame(loaded);check(loaded.version=="V3.7"&&loaded.stream==original.stream,"synthetic V3.7 container roundtrip changed its state"); + auto bytes=read_file(path);bytes.back()^=1;{std::ofstream file(path,std::ios::binary|std::ios::trunc);file.write(reinterpret_cast(bytes.data()),std::streamsize(bytes.size()));} + const auto corrupt=load_savegame(path);std::filesystem::remove(path); + rejects([&]{require_native_savegame(corrupt);},"actual corrupt container checksum was accepted"); + std::cout<<"Known native save versions, separate expanded layout, strict dimensions/trailers and checksum/version diagnostics passed\n";return 0; +}catch(const std::exception& error){std::cerr<