Complete the Modern main menu and persistent mod library (by Codex)
This commit is contained in:
1 parent
ab4a962a39
commit
6bcd95d668
21 files changed
+2043
-248
No files matched your search
@@ -0,0 +1,404 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#include "modern/ui/mod_library.hpp"
|
||||
#include "editor/images.hpp"
|
||||
#include "import/obw.hpp"
|
||||
#include "import/classic.hpp"
|
||||
#include "content/package.hpp"
|
||||
#include "formats/executable_layout.hpp"
|
||||
#include "launch/archive.hpp"
|
||||
#include "launch/mods.hpp"
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <stdexcept>
|
||||
using namespace bmh;
|
||||
using namespace bmh::modern_ui;
|
||||
namespace fixture {
|
||||
using J = bmh::cli::Json;
|
||||
// Generated records and effective tables reused from the public OBW test.
|
||||
|
||||
void check(bool condition, const std::string& message) {
|
||||
if (!condition) throw std::runtime_error(message);
|
||||
}
|
||||
template<class F> void rejects(F&& action, const std::string& message) {
|
||||
bool rejected = false;
|
||||
try { action(); } catch (const std::exception&) { rejected = true; }
|
||||
check(rejected, message);
|
||||
}
|
||||
void word(Bytes& data, size_t at, uint32_t value, size_t width = 2) {
|
||||
for (size_t i = 0; i < width; ++i) data.at(at + i) = uint8_t(value >> (i * 8));
|
||||
}
|
||||
void append(Bytes& data, uint32_t value, size_t width = 2) {
|
||||
for (size_t i = 0; i < width; ++i) data.push_back(uint8_t(value >> (i * 8)));
|
||||
}
|
||||
void save(const fs::path& path, const Bytes& bytes) {
|
||||
std::ofstream out(path, std::ios::binary);
|
||||
out.write(reinterpret_cast<const char*>(bytes.data()), std::streamsize(bytes.size()));
|
||||
check(bool(out), "write synthetic fixture");
|
||||
}
|
||||
|
||||
Database database(bool english) {
|
||||
Database db;
|
||||
db.club_names.resize(419);
|
||||
for (size_t i = 0; i < db.club_names.size(); ++i) db.club_names[i] = "Synthetic team " + std::to_string(i);
|
||||
db.club_names[0] = "Test M}nchen";
|
||||
db.club_names[357] = english ? "ENGLAND" : "DEUTSCHLAND";
|
||||
db.club_names[358] = english ? "GERMANY" : "SPANIEN";
|
||||
db.club_names[359] = english ? "ITALY" : "ENGLAND";
|
||||
db.club_names[360] = "FRANCE";
|
||||
db.club_names[361] = english ? "SPAIN" : "ITALIEN";
|
||||
if (english) db.club_names[150] = " ";
|
||||
db.players.resize(2574);
|
||||
for (size_t i = 0; i < db.players.size(); ++i)
|
||||
db.players[i] = {"Synthetic Player " + std::to_string(i), 60u | (90u << 7) | (80u << 14), 0x321, (6u << 21) | uint32_t(i % 4)};
|
||||
db.players[0].name = "T{st \"Name\"";
|
||||
db.players[1].w368 |= 1u << 27;
|
||||
db.raw_player_bonus_values.resize(3146);
|
||||
db.raw_player_bonus_values[1] = 128 | 17; // nationality 33, plus a bonus of 17
|
||||
db.raw_database_metadata.resize(100);
|
||||
db.raw_club_auxiliary_words.resize(168 * 2);
|
||||
db.raw_club_badge_selectors.resize(419 * 2);
|
||||
db.raw_foreign_club_strength.resize(251, 60);
|
||||
db.raw_league_auxiliary_bytes.resize(189);
|
||||
db.raw_referee_properties.resize(50);
|
||||
db.raw_club_kit_selectors.resize(419);
|
||||
db.raw_club_short_name_offsets.resize(168);
|
||||
db.squad_list.resize((3096 - 4) * 2 + 108);
|
||||
for (size_t i = 0; i < db.squad_list.size(); i += 2) word(db.squad_list, i, 4095);
|
||||
word(db.squad_list, 0, 0x8000); word(db.squad_list, 2, 1);
|
||||
word(db.squad_list, 90 * 24 * 2, 2); // first foreign squad, 16-slot stride
|
||||
word(db.squad_list, (2928 - 1) * 2, 3); // last squad
|
||||
db.referees = {"Synthetic Referee"};
|
||||
db.stadiums = {"Synthetic Stadium"};
|
||||
const auto pool = [](const std::vector<std::string>& names, Bytes& offsets) {
|
||||
Bytes result;
|
||||
for (const auto& name : names) {
|
||||
append(offsets, uint32_t(result.size()));
|
||||
result.insert(result.end(), name.begin(), name.end()); result.push_back(0);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
db.pools[0] = pool(db.club_names, db.raw_club_name_offsets);
|
||||
std::vector<std::string> names;
|
||||
for (const auto& player : db.players) names.push_back(player.name);
|
||||
db.pools[1] = pool(names, db.raw_player_name_offsets);
|
||||
Bytes unused;
|
||||
db.pools[2] = pool(db.referees, unused); db.pools[3] = pool(db.stadiums, unused);
|
||||
return db;
|
||||
}
|
||||
|
||||
LeImage executable(bool english) {
|
||||
constexpr uint32_t base = 0x10000;
|
||||
Bytes bytes(layout::GermanImageEnd - base);
|
||||
const auto put = [&](uint32_t address, uint32_t value, size_t width = 1) { word(bytes, address - base, value, width); };
|
||||
const int sizes[] = {english ? 20 : 18, english ? 20 : 18, english ? 20 : 18, english ? 20 : 18, english ? 10 : 18, 12, 12, 12, 12};
|
||||
for (uint32_t i = 0; i < 9; ++i) {
|
||||
put(layout::LeagueSizes + i, uint32_t(sizes[i]));
|
||||
put(layout::LeagueCalendarBits + i, english && i == 4 ? 31 : i);
|
||||
}
|
||||
if (!english) { put(layout::CareerSession_InstallLeagueSizes_RuntimeField3 + 4, 'A'); put(layout::CareerSession_InstallLeagueSizes_RuntimeField3 + 6, 'B'); }
|
||||
const std::vector<std::pair<int, uint32_t>> bindings = english ?
|
||||
std::vector<std::pair<int, uint32_t>>{{4, layout::EnglishFourTeamFixtureTable}, {6, layout::EnglishSixTeamFixtureTable},
|
||||
{12, layout::EnglishTwelveTeamFixtureTable}, {20, layout::EnglishTwentyTeamFixtureTable}} :
|
||||
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 = layout::GermanImageEnd - 4096; // synthetic data storage, outside pointer tables
|
||||
for (const auto& [teams, address] : bindings) {
|
||||
put(address, cursor, 4); ++cursor;
|
||||
std::vector<int> order;
|
||||
for (int i = 0; i < teams; ++i) order.push_back(i);
|
||||
for (int round = 1; round < teams; ++round) {
|
||||
put(address + uint32_t(round * 4), cursor, 4);
|
||||
for (int i = 0; i < teams / 2; ++i) {
|
||||
put(cursor++, uint32_t('a' + order[size_t(i)]));
|
||||
put(cursor++, uint32_t('a' + order[size_t(teams - 1 - i)]));
|
||||
}
|
||||
++cursor;
|
||||
std::rotate(order.begin() + 1, order.end() - 1, order.end());
|
||||
}
|
||||
}
|
||||
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));
|
||||
}
|
||||
|
||||
Bytes serialize(const Database& db) {
|
||||
Bytes bytes;
|
||||
for (const auto& pool : db.pools) append(bytes, uint32_t(pool.size()));
|
||||
const auto add = [&](const Bytes& data) { bytes.insert(bytes.end(), data.begin(), data.end()); };
|
||||
add(db.raw_player_bonus_values);
|
||||
for (const auto& pool : db.pools) add(pool);
|
||||
for (const auto* table : {&db.raw_club_name_offsets, &db.raw_player_name_offsets, &db.raw_database_metadata,
|
||||
&db.raw_club_auxiliary_words, &db.raw_club_badge_selectors, &db.raw_foreign_club_strength,
|
||||
&db.raw_league_auxiliary_bytes, &db.raw_referee_properties, &db.raw_club_kit_selectors, &db.raw_club_short_name_offsets, &db.squad_list}) add(*table);
|
||||
for (int table = 0; table < 3; ++table)
|
||||
for (const auto& player : db.players) append(bytes, table == 0 ? player.w35c : table == 1 ? player.w368 : player.w364, 4);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
} // namespace fixture
|
||||
|
||||
namespace {
|
||||
void require(bool condition, const char *message) { if (!condition) throw std::runtime_error(message); }
|
||||
template<class F> void rejected(F action, const char *message) {
|
||||
bool failed = false; try { action(); } catch (const std::exception &) { failed = true; }
|
||||
require(failed, message);
|
||||
}
|
||||
void write(const fs::path &root, const std::string &name, const Json &value) {
|
||||
fs::create_directories((root / name).parent_path()); editor::write_atomic(root / name, value.dump());
|
||||
}
|
||||
Json tree(const fs::path &root) {
|
||||
std::map<std::string, std::string> result;
|
||||
if (fs::exists(root)) for (const auto &file : fs::recursive_directory_iterator(root))
|
||||
if (file.is_regular_file() && !file.is_symlink()) result[editor::utf8(file.path().lexically_relative(root))] = content::sha256(editor::read_file(file.path()));
|
||||
return Json(result);
|
||||
}
|
||||
struct Fixture {
|
||||
fs::path root;
|
||||
Fixture() : root(fs::temp_directory_path() / editor::new_id("openbmh-ui-mods-test-")) { fs::create_directory(root); }
|
||||
~Fixture() { std::error_code error; fs::remove_all(root, error); }
|
||||
};
|
||||
void modern(const fs::path &path) {
|
||||
write(path, "manifest.json", {{"format", "openbmh-world"}, {"kind", "world"}, {"format_version", "1.1"},
|
||||
{"id", "world:synthetic-mod-library"}, {"title", "Synthetic world"}, {"version", "2.7"},
|
||||
{"description", "Synthetic modern library fixture"}, {"start_date", "2032-07-01"},
|
||||
{"requires", {"editor:world-v1"}}, {"x-editor", {{"profile", "editor-world-v1"}}}});
|
||||
write(path, "world/nations.json", {{"items", {{{"id", "nation:de"}, {"name", "Synthetic nation"}}}}});
|
||||
write(path, "world/competitions.json", {{"items", {{{"id", "competition:test"}, {"name", "Synthetic league"}, {"type", "league"}, {"nation", "nation:de"}}}}});
|
||||
write(path, "world/clubs.json", {{"items", {{{"id", "club:test"}, {"name", "Synthetic club"}, {"competition", "competition:test"}, {"nation", "nation:de"}}}}});
|
||||
write(path, "world/players.json", {{"items", Json::array()}});
|
||||
write(path, "assets/index.json", {{"items", Json::array()}});
|
||||
}
|
||||
fs::path classic(const fs::path &root, bool english) {
|
||||
const auto input = root / (english ? "synthetic-en" : "synthetic-de");
|
||||
fs::create_directories(input);
|
||||
const auto db = fixture::database(english);
|
||||
const auto exe = fixture::executable(english);
|
||||
TextDb texts; texts.strings.resize(TEXT_COUNT, "Synthetic text");
|
||||
const obw::Options options{"2032-07-01", "Synthetic Classic"};
|
||||
auto docs = obw::convert_database(db, texts, exe, english, options);
|
||||
const auto raw_db = fixture::serialize(db);
|
||||
constexpr char digits[] = "0123456789abcdef";
|
||||
std::string encoded; for (auto value : raw_db) { encoded += digits[value >> 4]; encoded += digits[value & 15]; }
|
||||
docs["x-legacy/source-database.json"] = Json{{"schema", "legacy-source@1.0"}, {"name", english ? "M3E.MAN" : "M3D.MAN"}, {"encoding", "hex"}, {"bytes", encoded}}.dump();
|
||||
Bytes raw_text(7); for (const auto &value : texts.strings) { raw_text.insert(raw_text.end(), value.begin(), value.end()); raw_text.push_back(0); }
|
||||
fixture::save(input / (english ? "TEXTE.BLK" : "TEXTD.BLK"), raw_text);
|
||||
fixture::save(input / "M3.MEN", Bytes(864, 7)); fixture::save(input / "M3.DAT", Bytes(1408, 9));
|
||||
fixture::save(input / "M3PC.COL", Bytes(768, 31));
|
||||
for (const auto *name : {"KB", "W", "F", "S"}) fs::create_directory(input / name);
|
||||
Bytes font(37, 0); fixture::word(font, 0, 1); font[3] = 2; fixture::word(font, 4, 1); font[8] = font[9] = 'A'; fixture::word(font, 16, 1); font[28] = 0x80; fixture::word(font, 31, 1); fixture::word(font, 35, 1);
|
||||
fixture::save(input / "F/BMH2.FNT", font); fixture::save(input / "F/BMH3.FNT", font);
|
||||
const 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"}) fixture::save(input / "S" / name, bank);
|
||||
const auto image = [&](const fs::path &path) {
|
||||
Bytes bytes(8, 0), rle;
|
||||
for (int i = 0; i < 320 * 240; 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); fixture::word(bytes, 0, 320); fixture::word(bytes, 2, 240); fixture::word(bytes, 4, uint32_t(rle.size()), 4);
|
||||
bytes.insert(bytes.end(), rle.begin(), rle.end()); fixture::save(path, bytes);
|
||||
};
|
||||
image(input / "KB/0.VGA"); image(input / "KB/29.VGA"); image(input / "W/0.VGA");
|
||||
obw::complete_classic(docs, input, db, exe, exe, english, options);
|
||||
content::seal(docs);
|
||||
const auto package = root / (english ? "classic-en.obw" : "classic-de.obw");
|
||||
content::write_archive(docs, package);
|
||||
return package;
|
||||
}
|
||||
void lifecycle() {
|
||||
Fixture fixture;
|
||||
const auto source = fixture.root / "source"; modern(source);
|
||||
const auto portable = fixture.root / "portable";
|
||||
UiModLibrary library(portable);
|
||||
require(library.snapshot().entries.empty() && !library.snapshot().active && !fs::exists(portable), "Empty library has implicit active mod or writes on read");
|
||||
auto plan = library.inspect(source);
|
||||
require(!fs::exists(portable), "Inspect or cancellation created library data");
|
||||
require(plan.metadata.type == "Modern" && plan.metadata.source == "OpenBMH" && plan.metadata.version == "2.7" &&
|
||||
plan.metadata.description == "Synthetic modern library fixture" && plan.metadata.file_count == 6 && plan.metadata.bytes > 0,
|
||||
"Modern metadata is not derived from actual package contents");
|
||||
require(plan.metadata.path.empty() && plan.metadata.id.empty(), "Import plan falsely claims installed membership");
|
||||
for (const std::string &name : std::vector<std::string>{"../outside", "CON.txt", "bad:name", ".hidden", "", std::string(65, 'x')})
|
||||
rejected([&] { library.commit(plan, name); }, "Invalid portable mod name accepted");
|
||||
require(!fs::exists(portable), "Invalid import name changed library");
|
||||
require(UiModLibrary::name_key("Öffnung") == UiModLibrary::name_key("o\xcc\x88" "FFNUNG") &&
|
||||
UiModLibrary::name_key("Straße") == UiModLibrary::name_key("STRASSE"), "Mod Unicode casefold differs from file UI");
|
||||
std::string unicode; for (int i = 0; i < 64; ++i) unicode += "Ä";
|
||||
require(UiModLibrary::clean_name(unicode) == unicode, "64 German Unicode characters rejected");
|
||||
plan.metadata.type = "Classic"; plan.source_token = "forged display token";
|
||||
const auto first = library.commit(plan, "Öffnung");
|
||||
require(first.valid() && first.type == "Modern" && first.path == library.root() / "mods" / editor::path("Öffnung"), "Commit trusted mutable display metadata or returned wrong directory");
|
||||
require(!library.snapshot().active, "Installation automatically activated a mod");
|
||||
const auto before_duplicate = tree(portable);
|
||||
rejected([&] { library.commit(plan, "o\xcc\x88" "FFNUNG"); }, "Casefold duplicate import accepted");
|
||||
require(tree(portable) == before_duplicate, "Duplicate import changed existing library");
|
||||
auto selected = library.snapshot(); library.activate(first, selected.selection_token);
|
||||
selected = UiModLibrary(portable).snapshot();
|
||||
require(selected.active == first.id && std::count_if(selected.entries.begin(), selected.entries.end(), [](const auto &r) { return r.active; }) == 1, "Active selection not exactly one after restart");
|
||||
const auto second = library.commit(library.inspect(source), "Second");
|
||||
rejected([&] { library.activate(second, before_duplicate.dump()); }, "Stale selection token accepted");
|
||||
require(library.snapshot().active == first.id, "Rejected activation changed active mod");
|
||||
selected = library.snapshot(); library.activate(second, selected.selection_token);
|
||||
require(UiModLibrary(portable).snapshot().active == second.id, "Replacement activation not persisted");
|
||||
selected = library.snapshot(); library.deactivate(selected.selection_token);
|
||||
require(!UiModLibrary(portable).snapshot().active, "Explicit no-active selection not persisted");
|
||||
selected = library.snapshot(); library.activate(first, selected.selection_token);
|
||||
fs::create_directories(portable / "userdata" / first.content_id);
|
||||
editor::write_atomic(portable / "userdata" / first.content_id / "keep.obs", "synthetic personal data");
|
||||
selected = library.snapshot();
|
||||
const auto renamed = library.rename(first, "ÖFFNUNG", selected.selection_token);
|
||||
require(renamed.valid() && library.snapshot().active == renamed.id && fs::exists(renamed.path / "content.obw"), "Case-only rename lost active selection");
|
||||
rejected([&] { library.remove(first, library.snapshot().selection_token); }, "Old membership token survived rename");
|
||||
const auto foreign = UiModLibrary(fixture.root / "other").commit(library.inspect(source), "Foreign");
|
||||
const auto stable = tree(portable);
|
||||
rejected([&] { library.remove(foreign, library.snapshot().selection_token); }, "Foreign library membership accepted");
|
||||
require(tree(portable) == stable, "Foreign deletion changed local files");
|
||||
selected = library.snapshot(); library.remove(renamed, selected.selection_token);
|
||||
require(!library.snapshot().active && fs::exists(portable / "userdata" / first.content_id / "keep.obs"), "Active deletion failed to clear selection or removed userdata");
|
||||
const auto before_stale = tree(portable);
|
||||
const auto stale = library.inspect(source);
|
||||
auto manifest = Json::parse(editor::read_file(source / "manifest.json")); manifest["description"] = "Changed after inspect";
|
||||
write(source, "manifest.json", manifest);
|
||||
rejected([&] { library.commit(stale, "Stale"); }, "Changed inspected source accepted");
|
||||
require(tree(portable) == before_stale, "Stale source import changed library");
|
||||
const auto changing = library.inspect(source);
|
||||
rejected([&] { library.commit(changing, "Changed during staging", [&](const std::string &progress) {
|
||||
if (progress.find("übernommen") != progress.npos) { manifest["description"] = "Changed during final progress"; write(source, "manifest.json", manifest); }
|
||||
}); }, "Source changed during staging was published");
|
||||
require(tree(portable) == before_stale, "Failed staged import left named or temporary library data");
|
||||
fs::create_directory(portable / ".ui-mod-lock");
|
||||
rejected([&] { library.deactivate(library.snapshot().selection_token); }, "Busy mutation ignored transaction lock");
|
||||
fs::remove(portable / ".ui-mod-lock");
|
||||
require(tree(portable) == before_stale, "Busy mutation changed library files");
|
||||
fs::create_directories(portable / "mods/Broken"); editor::write_atomic(portable / "mods/Broken/content.obw", "synthetic damaged archive");
|
||||
auto rows = library.snapshot().entries;
|
||||
auto broken = std::find_if(rows.begin(), rows.end(), [](const auto &row) { return row.name == "Broken"; });
|
||||
require(broken != rows.end() && !broken->valid(), "Invalid mod disappeared rather than showing an error row");
|
||||
rejected([&] { library.activate(*broken, library.snapshot().selection_token); }, "Invalid mod was activated");
|
||||
library.remove(*broken, library.snapshot().selection_token);
|
||||
require(!fs::exists(portable / "mods/Broken"), "Invalid owned mod cannot be removed safely");
|
||||
fs::create_directories(portable / "mods/BadProfile");
|
||||
fs::copy_file(second.path / "content.obw", portable / "mods/BadProfile/content.obw");
|
||||
editor::write_atomic(portable / "mods/BadProfile/mod.json", "{broken profile");
|
||||
rows = library.snapshot().entries;
|
||||
const auto bad_profile = std::find_if(rows.begin(), rows.end(), [](const auto &row) { return row.name == "BadProfile"; });
|
||||
require(bad_profile != rows.end() && !bad_profile->valid() && !bad_profile->token.empty(), "Malformed mod profile lost selectable membership token");
|
||||
library.remove(*bad_profile, library.snapshot().selection_token);
|
||||
fs::create_directory(portable / "mods" / std::string(130, 'x'));
|
||||
const auto installed_despite_error = library.commit(library.inspect(source), "Alongside error row");
|
||||
require(installed_despite_error.valid(), "Foreign non-normalizable error row blocked all imports");
|
||||
const auto selection_before_io = library.snapshot();
|
||||
fs::rename(portable / ".ui-active-mod.json", portable / "selection-preserved.json");
|
||||
fs::create_directory(portable / ".ui-active-mod.json");
|
||||
const auto blocked = tree(portable);
|
||||
rejected([&] { library.rename(second, "Blocked selection IO", selection_before_io.selection_token); }, "Selection IO failure did not reject mutation");
|
||||
require(tree(portable) == blocked && fs::exists(second.path), "Selection IO failure partially renamed/deleted a mod");
|
||||
fs::remove(portable / ".ui-active-mod.json");
|
||||
fs::rename(portable / "selection-preserved.json", portable / ".ui-active-mod.json");
|
||||
std::error_code symlink_error;
|
||||
fs::create_directory_symlink(second.path, portable / "mods/Linked", symlink_error);
|
||||
if (!symlink_error) {
|
||||
rows = library.snapshot().entries; const auto linked = std::find_if(rows.begin(), rows.end(), [](const auto &row) { return row.name == "Linked"; });
|
||||
require(linked != rows.end() && !linked->valid(), "Linked mod not reported as unsafe");
|
||||
rejected([&] { library.activate(*linked, library.snapshot().selection_token); }, "Linked member activated");
|
||||
rejected([&] { library.remove(*linked, library.snapshot().selection_token); }, "Deletion followed linked member");
|
||||
fs::remove(portable / "mods/Linked");
|
||||
}
|
||||
}
|
||||
void package_kinds() {
|
||||
Fixture fixture; const auto source = fixture.root / "world"; modern(source);
|
||||
const auto archive = fixture.root / "modern.obw";
|
||||
editor::ArchiveStore::open(source)->save(archive, {}, {});
|
||||
fs::copy_file(archive, fixture.root / "modern.zip");
|
||||
UiModLibrary library(fixture.root / "portable");
|
||||
require(library.inspect(fixture.root / "modern.zip").metadata.type == "Modern", "Modern ZIP unsupported");
|
||||
fs::create_directory(fixture.root / "hinted"); fs::copy_file(archive, fixture.root / "hinted/content.obw");
|
||||
write(fixture.root / "hinted", "mod.json", {{"schema", "openbmh-mod@1.0"}, {"type", "Classic"}});
|
||||
require(library.inspect(fixture.root / "hinted").metadata.type == "Modern", "Untrusted mod profile forced Classic classification");
|
||||
auto bad = Json::parse(editor::read_file(source / "manifest.json")); bad["requires"] = {"runtime:unknown-v9"};
|
||||
write(source, "manifest.json", bad);
|
||||
rejected([&] { library.inspect(source); }, "Unknown world requirement treated as supported");
|
||||
for (bool english : {false, true}) {
|
||||
const auto file = classic(fixture.root, english);
|
||||
const auto plan = library.inspect(file);
|
||||
require(plan.metadata.type == "Classic" && plan.metadata.source == (english ? "FTLD" : "BMH") && plan.metadata.content_id.size() == 64 && plan.metadata.file_count > 10 && plan.metadata.bytes > 1000, "Validated synthetic Classic metadata missing or guessed");
|
||||
const auto installed = library.commit(plan, english ? "English Classic" : "German Classic");
|
||||
require(installed.valid() && content::open(installed.path / "content.obw")->english == english, "Committed Classic package is not sealed/validated");
|
||||
require(!fs::exists(installed.path / "mod.json") &&
|
||||
std::distance(fs::directory_iterator(installed.path), fs::directory_iterator()) == 1,
|
||||
"Classic installation violated the content-only launcher contract");
|
||||
const auto launcher_rows = launch::ModLibrary(library.root()).list();
|
||||
const auto launcher_mod = std::find_if(launcher_rows.begin(), launcher_rows.end(), [&](const auto &row) { return row.name == installed.name; });
|
||||
require(launcher_mod != launcher_rows.end() && launcher_mod->error.empty() &&
|
||||
launcher_mod->type == "Classic" && launcher_mod->content_id == installed.content_id,
|
||||
"Existing0.2 launcher rejects the UI-imported sealed Classic mod");
|
||||
const auto old = tree(library.root());
|
||||
editor::ArchiveStore::open(file)->save(fixture.root / "tampered.obw", {{"x-classic/desktop-seed.bin", std::string(864, 'x')}}, {});
|
||||
rejected([&] { library.inspect(fixture.root / "tampered.obw"); }, "Unsealed/tampered Classic import accepted");
|
||||
require(tree(library.root()) == old, "Rejected Classic package changed library");
|
||||
const auto legacy_name = std::string(80, english ? 'E' : 'G');
|
||||
fs::create_directory(library.root() / "mods" / legacy_name);
|
||||
fs::copy_file(file, library.root() / "mods" / legacy_name / "content.obw");
|
||||
auto rows = library.snapshot().entries;
|
||||
const auto legacy = std::find_if(rows.begin(), rows.end(), [&](const auto &r) { return r.name == legacy_name; });
|
||||
require(legacy != rows.end() && legacy->valid(), "Existing0.2 content-only mod layout/name rejected");
|
||||
library.activate(*legacy, library.snapshot().selection_token);
|
||||
require(UiModLibrary(library.root()).snapshot().active == legacy_name, "Legacy active name not restart-safe");
|
||||
const auto renamed = library.rename(*legacy, english ? "English legacy" : "German legacy", library.snapshot().selection_token);
|
||||
require(renamed.valid() && library.snapshot().active == renamed.id, "Legacy mod cannot be renamed safely");
|
||||
}
|
||||
const auto original = fixture.root / "unsupported-original"; fs::create_directory(original);
|
||||
editor::write_atomic(original / "M3.EXE", "MZ synthetic unsupported original");
|
||||
const auto before = tree(library.root());
|
||||
rejected([&] { library.inspect(original); }, "Unsupported original edition treated as playable");
|
||||
require(tree(library.root()) == before, "Original inspect changed library before confirmation");
|
||||
}
|
||||
void write_fixtures(const fs::path &requested) {
|
||||
if (fs::is_symlink(fs::symlink_status(requested)) ||
|
||||
(fs::exists(requested) && (!fs::is_directory(requested) || !fs::is_empty(requested))))
|
||||
throw std::runtime_error("Fixture destination must be a new or empty directory");
|
||||
const auto output = fs::weakly_canonical(fs::absolute(requested));
|
||||
fs::create_directories(output);
|
||||
modern(output / "modern-directory");
|
||||
editor::ArchiveStore::open(output / "modern-directory")->save(output / "modern.obw", {}, {});
|
||||
fs::copy_file(output / "modern.obw", output / "modern.zip");
|
||||
fs::create_directory(output / "modern-mod");
|
||||
fs::copy_file(output / "modern.obw", output / "modern-mod/content.obw");
|
||||
write(output / "modern-mod", "mod.json", {{"schema", "openbmh-mod@1.0"}, {"type", "Modern"}});
|
||||
const auto german = classic(output, false), english = classic(output, true);
|
||||
require(!content::open(german)->english && content::open(english)->english,
|
||||
"Synthetic fixture Classic editions failed validation");
|
||||
write(output, "fixtures.json", {{"schema", "openbmh-synthetic-mod-fixtures@1"},
|
||||
{"proprietary_data", false}, {"engine_started", false},
|
||||
{"modern_directory", "modern-directory"}, {"modern_obw", "modern.obw"},
|
||||
{"modern_zip", "modern.zip"}, {"modern_mod_directory", "modern-mod"},
|
||||
{"classic_german", "classic-de.obw"}, {"classic_english", "classic-en.obw"},
|
||||
{"note", "synthetic-de/en are generation inputs, not complete original installations"},
|
||||
{"file_hashes", tree(output)}});
|
||||
std::cout << "Wrote synthetic Modern and sealed Classic DE/EN fixtures to " << editor::utf8(output) << '\n';
|
||||
}
|
||||
} // namespace
|
||||
int main(int argc, char **argv) {
|
||||
try {
|
||||
const auto args = launch::utf8_arguments(argc, argv);
|
||||
if (args.size() == 3 && args[1] == "--write-fixtures") {
|
||||
write_fixtures(editor::path(args[2])); return 0;
|
||||
}
|
||||
if (args.size() != 1) throw std::runtime_error("usage: modern_mods_ui_tests [--write-fixtures DIR]");
|
||||
lifecycle(); package_kinds(); std::cout << "PASS native mod library: staged import, Modern/sealed Classic metadata, 0/1 active persistence, Unicode conflicts, stale guards, ownership, rollback and retained userdata\n"; return 0;
|
||||
}
|
||||
catch (const std::exception &e) { std::cerr << e.what() << '\n'; return 1; }
|
||||
}
|
||||
Reference in new issue
Block a user