Build 0.2 native launcher and portable Classic runtime (by Codex)

This commit is contained in:
Codex committed 2026-10-18 11:28:51 +02:00
1 parent 3a021fc160
commit 2552528176
84 files changed
+5004 -197

No files matched your search

+63
View File
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: GPL-3.0-only
#include "content/digest.hpp"
#include <array>
#include <bit>
#include <cstdint>
#include <vector>
namespace bmh::content {
std::string sha256(std::string_view input) {
constexpr std::array<uint32_t, 64> k{
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
std::array<uint32_t, 8> h{0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
std::vector<uint8_t> data(input.begin(), input.end());
const uint64_t bits = uint64_t(data.size()) * 8;
data.push_back(0x80);
while (data.size() % 64 != 56)
data.push_back(0);
for (int i = 7; i >= 0; --i)
data.push_back(uint8_t(bits >> (8 * i)));
for (size_t offset = 0; offset < data.size(); offset += 64) {
std::array<uint32_t, 64> w{};
for (size_t i = 0; i < 16; ++i)
for (size_t j = 0; j < 4; ++j)
w[i] = (w[i] << 8) | data[offset + 4 * i + j];
for (size_t i = 16; i < 64; ++i) {
const auto a = w[i - 15], b = w[i - 2];
w[i] = w[i - 16] + (std::rotr(a, 7) ^ std::rotr(a, 18) ^ (a >> 3)) + w[i - 7] +
(std::rotr(b, 17) ^ std::rotr(b, 19) ^ (b >> 10));
}
auto [a, b, c, d, e, f, g, v] = h;
for (size_t i = 0; i < 64; ++i) {
const auto t = v + (std::rotr(e, 6) ^ std::rotr(e, 11) ^ std::rotr(e, 25)) +
((e & f) ^ (~e & g)) + k[i] + w[i];
const auto u =
(std::rotr(a, 2) ^ std::rotr(a, 13) ^ std::rotr(a, 22)) + ((a & b) ^ (a & c) ^ (b & c));
v = g;
g = f;
f = e;
e = d + t;
d = c;
c = b;
b = a;
a = t + u;
}
const std::array<uint32_t, 8> work{a, b, c, d, e, f, g, v};
for (size_t i = 0; i < 8; ++i)
h[i] += work[i];
}
constexpr char digits[] = "0123456789abcdef";
std::string result;
for (auto word : h)
for (int i = 7; i >= 0; --i)
result += digits[(word >> (4 * i)) & 15];
return result;
}
} // namespace bmh::content
+7
View File
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include <string>
#include <string_view>
namespace bmh::content {
std::string sha256(std::string_view data);
}
+123
View File
@@ -0,0 +1,123 @@
// SPDX-License-Identifier: GPL-3.0-only
#include "content/package.hpp"
#include "import/detail.hpp"
#include <chrono>
#include <fstream>
namespace bmh::content {
namespace fs = std::filesystem;
using namespace obw::detail;
namespace {
constexpr const char *inventory = "x-classic/seal.json";
O hashes(const Documents &docs) {
O result;
for (const auto &[name, value] : docs)
if (name != inventory) {
if (!safe_path(name))
fail("invalid content entry");
result[name] = sha256(value);
}
return result;
}
std::string identity(const Documents &docs) {
auto entries = hashes(docs);
entries.erase("x-classic/inventory.json");
auto manifest = parse(docs, "manifest.json");
manifest.object()->erase("generator");
manifest.object()->erase("title");
entries["manifest.json"] = sha256(cli::dump_json(manifest));
return sha256(cli::dump_json(entries));
}
} // namespace
Documents read_archive(const fs::path &file) {
if (fs::is_symlink(fs::symlink_status(file)) || !fs::is_regular_file(file) ||
fs::file_size(file) > 128ull * 1024 * 1024)
fail("invalid native archive");
const auto input = read_file(file);
mz_zip_archive zip{};
if (!mz_zip_reader_init_mem(&zip, input.data(), input.size(), 0))
fail("invalid ZIP archive");
struct End {
mz_zip_archive *zip;
~End() { mz_zip_reader_end(zip); }
} end{&zip};
Documents out;
uint64_t total = 0;
const auto count = mz_zip_reader_get_num_files(&zip);
if (count > 8192)
fail("too many archive entries");
for (mz_uint i = 0; i < count; ++i) {
mz_zip_archive_file_stat stat{};
if (!mz_zip_reader_file_stat(&zip, i, &stat))
fail("cannot read entry metadata");
if (stat.m_is_directory)
continue;
const std::string name = stat.m_filename;
if (!safe_path(name) || stat.m_uncomp_size > 32ull * 1024 * 1024 ||
(total += stat.m_uncomp_size) > 128ull * 1024 * 1024)
fail("unsafe or oversized archive entry");
std::string value(size_t(stat.m_uncomp_size), '\0');
if (!mz_zip_reader_extract_to_mem(&zip, i, value.data(), value.size(), 0))
fail("damaged ZIP entry");
if (!out.emplace(name, std::move(value)).second)
fail("duplicate archive entry");
}
return out;
}
void write_archive(const Documents &out, const fs::path &file, bool overwrite) {
mz_zip_archive zip{};
if (!mz_zip_writer_init_heap(&zip, 0, 0))
fail("cannot initialize ZIP writer");
struct End {
mz_zip_archive *zip;
~End() { mz_zip_writer_end(zip); }
} end{&zip};
uint64_t total = 0;
if (out.size() > 8192)
fail("too many archive entries");
for (const auto &[name, data] : out) {
if (!safe_path(name) || data.size() > 32ull * 1024 * 1024 ||
(total += data.size()) > 128ull * 1024 * 1024)
fail("invalid archive size or path");
if (!mz_zip_writer_add_mem(&zip, name.c_str(), data.data(), data.size(), MZ_BEST_SPEED))
fail("cannot add ZIP entry");
}
void *buffer = nullptr;
size_t size = 0;
if (!mz_zip_writer_finalize_heap_archive(&zip, &buffer, &size))
fail("cannot finalize ZIP archive");
struct Free {
void *data;
~Free() { mz_free(data); }
} free{buffer};
write_atomic(file, std::string_view(static_cast<const char *>(buffer), size), overwrite);
}
std::string seal(Documents &docs) {
obw::validate(docs);
auto entries = hashes(docs);
const auto id = identity(docs);
put(docs, inventory,
O{{"schema", "classic-seal@1.0"}, {"content_id", id}, {"entries", std::move(entries)}});
return id;
}
std::string verify(const Documents &docs) {
const auto stored = parse(docs, inventory);
if (str(get(stored, "schema")) != "classic-seal@1.0")
fail("unsupported Classic seal");
const auto actual = hashes(docs);
const auto encoded = cli::dump_json(actual);
if (cli::dump_json(get(stored, "entries")) != encoded || str(get(stored, "content_id")) != identity(docs))
fail("Classic contents were changed; import the installation again");
return identity(docs);
}
std::shared_ptr<const Classic> open(const fs::path &path) {
auto docs = read_archive(path);
const auto id = verify(docs);
auto value = std::make_shared<Classic>(obw::load_classic(docs));
value->content_id = id;
const auto manifest = parse(docs, "manifest.json");
value->start_year = std::stoi(str(get(manifest, "start_date")).substr(0, 4));
value->executable.set_display_start_year(value->start_year);
value->documents = std::move(docs);
return value;
}
} // namespace bmh::content
+15
View File
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include "import/classic.hpp"
#include "content/digest.hpp"
#include "formats/native_file.hpp"
namespace bmh::content {
using Documents = obw::Documents;
// Generic bounded ZIP documents; format-specific validation belongs to its reader.
Documents read_archive(const std::filesystem::path &file);
void write_archive(const Documents &, const std::filesystem::path &, bool overwrite = false);
// Sealed Classic contents: hashes cover every entry, including the original importer inventory.
std::string seal(Documents &);
std::string verify(const Documents &);
std::shared_ptr<const Classic> open(const std::filesystem::path &);
} // namespace bmh::content
+262
View File
@@ -0,0 +1,262 @@
// SPDX-License-Identifier: GPL-3.0-only
#include "content/save.hpp"
#include "import/detail.hpp"
#include "game/save.hpp"
#include <limits>
#include <algorithm>
#include <bit>
namespace bmh::content {
using namespace obw::detail;
namespace {
J rng(uint32_t seed, uint64_t calls) { return O{{"seed", int64_t(seed)}, {"calls", std::to_string(calls)}}; }
uint64_t calls(const J &value) {
const auto s = str(get(value, "calls"));
size_t end = 0;
uint64_t n;
try {
n = std::stoull(s, &end);
} catch (...) {
fail("invalid random draw counter");
}
if (end != s.size() || s.empty() ||
!std::all_of(s.begin(), s.end(), [](unsigned char c) { return c >= '0' && c <= '9'; }))
fail("invalid random draw counter");
return n;
}
uint32_t seed(const J &value) {
const auto n = num(get(value, "seed"));
if (n < 0 || n > UINT32_MAX)
fail("invalid random seed");
return uint32_t(n);
}
Documents world_of(const Documents &docs) {
Documents world;
for (const auto &[name, value] : docs)
if (!name.starts_with("state/"))
world[name] = value;
world["manifest.json"] = docs.at("state/world-manifest.json");
return world;
}
} // namespace
namespace {
template <class T> J vector_json(const std::vector<T> &values) {
A rows;
for (auto value : values)
rows.emplace_back(int64_t(value));
return rows;
}
template <class T> void vector_json(const J &j, std::vector<T> &out) {
if (!j.array() || j.array()->size() > 1000000)
fail("invalid checkpoint array");
for (const auto &row : *j.array()) {
const auto n = num(row);
if (n < int64_t(std::numeric_limits<T>::min()) || n > int64_t(std::numeric_limits<T>::max()))
fail("checkpoint array value out of range");
out.push_back(T(n));
}
}
J group_json(const MatchGroup &g) {
O out;
#define FIELD(name) out[#name] = vector_json(g.name);
FIELD(desc)
FIELD(fixtures) FIELD(results) FIELD(cup_results) FIELD(cup_other_leg) FIELD(records) FIELD(end_minute)
FIELD(spectators) FIELD(log)
#undef FIELD
out["cup_addresses"] = A{int64_t(g.cup_result_address[0]), int64_t(g.cup_result_address[1])};
out["phase"] = g.phase;
out["ended"] = int64_t(g.ended);
out["kickoff_void_mask"] = int64_t(g.kickoff_void_mask);
return out;
}
MatchGroup group_json(const J &in) {
MatchGroup g;
#define FIELD(name) vector_json(get(in, #name), g.name);
FIELD(desc)
FIELD(fixtures) FIELD(results) FIELD(cup_results) FIELD(cup_other_leg) FIELD(records) FIELD(end_minute)
FIELD(spectators) FIELD(log)
#undef FIELD
const auto &at = arr(get(in, "cup_addresses"));
if (at.size() != 2)
fail("invalid cup addresses");
for (size_t i = 0; i < 2; ++i) {
const auto n = num(at[i]);
if (n < 0 || n > UINT32_MAX)
fail("invalid cup address");
g.cup_result_address[i] = uint32_t(n);
}
const auto phase = num(get(in, "phase")), ended = num(get(in, "ended")),
mask = num(get(in, "kickoff_void_mask"));
if (phase < 0 || phase > 1 || ended < 0 || ended > UINT32_MAX || mask < 0 || mask > UINT32_MAX)
fail("invalid match checkpoint");
g.phase = int(phase);
g.ended = uint32_t(ended);
g.kickoff_void_mask = uint32_t(mask);
if ((g.desc.size() != 0xc0 && g.desc.size() != 0xd0) || g.fixtures.size() % 2 || g.matches() > 32 ||
g.records.size() != size_t(g.matches()) * 18 || g.end_minute.size() != size_t(g.matches()) ||
g.spectators.size() != size_t(g.matches()))
fail("invalid match dimensions");
return g;
}
J groups_json(const std::vector<MatchGroup> &groups) {
A rows;
for (const auto &group : groups)
rows.push_back(group_json(group));
return rows;
}
std::vector<MatchGroup> groups_json(const J &j) {
std::vector<MatchGroup> groups;
const auto &rows = arr(j);
if (rows.size() > 6)
fail("too many match groups");
for (const auto &row : rows)
groups.push_back(group_json(row));
return groups;
}
} // namespace
J driver_checkpoint(const TurnDriver &d) {
return O{{"schema", "classic-driver@1.0"},
{"stage", int(d.stage)},
{"calendar_entry", d.calendar_entry},
{"manager_position", d.manager_position},
{"next_event_id", std::to_string(d.next_event_id)},
{"loaded_matchday_resume", d.loaded_matchday_resume},
{"suppress_remaining_pre_match_offers", d.suppress_remaining_pre_match_offers},
{"national_nomination_desk", d.national_nomination_desk},
{"enable_national_nomination_desks", d.enable_national_nomination_desks},
{"groups", groups_json(d.groups)},
{"referee_source", vector_json(d.referee_source)},
{"referees", vector_json(d.referees)},
{"last_results", O{{"valid", d.last_results.valid},
{"date", int64_t(d.last_results.date)},
{"calendar_entry", d.last_results.calendar_entry},
{"matchday", d.last_results.matchday},
{"groups", groups_json(d.last_results.groups)},
{"tournament_name", vector_json(d.last_results.tournament_name)}}}};
}
void restore_driver(TurnDriver &d, const J &j) {
if (str(get(j, "schema")) != "classic-driver@1.0")
fail("unsupported driver checkpoint");
const auto stage = num(get(j, "stage"));
const auto position = num(get(j, "manager_position"));
const auto entry = num(get(j, "calendar_entry"));
if (stage < 0 || stage > 2 || position < 0 || position > 4 || entry < 0 || entry > UINT16_MAX)
fail("invalid saved desk cursor");
d.stage = TurnDriver::Stage(stage);
d.manager_position = int(position);
d.calendar_entry = uint16_t(entry);
const auto flag = [](const J &value, const char *name) {
const auto &b = get(value, name);
if (!b.boolean())
fail("invalid driver flag");
return *b.boolean();
};
d.loaded_matchday_resume = flag(j, "loaded_matchday_resume");
d.suppress_remaining_pre_match_offers = flag(j, "suppress_remaining_pre_match_offers");
d.national_nomination_desk = flag(j, "national_nomination_desk");
d.enable_national_nomination_desks = flag(j, "enable_national_nomination_desks");
J counter = O{{"calls", get(j, "next_event_id")}};
d.next_event_id = calls(counter);
d.groups = groups_json(get(j, "groups"));
d.referee_source.clear();
d.referees.clear();
vector_json(get(j, "referee_source"), d.referee_source);
vector_json(get(j, "referees"), d.referees);
const auto &last = get(j, "last_results");
d.last_results.valid = flag(last, "valid");
const auto date = num(get(last, "date")), calendar = num(get(last, "calendar_entry")),
matchday = num(get(last, "matchday"));
if (date < 0 || date > UINT32_MAX || calendar < 0 || calendar > UINT16_MAX || matchday < 0 ||
matchday > UINT16_MAX)
fail("invalid result snapshot");
d.last_results.date = uint32_t(date);
d.last_results.calendar_entry = uint16_t(calendar);
d.last_results.matchday = uint16_t(matchday);
d.last_results.groups = groups_json(get(last, "groups"));
d.last_results.tournament_name.clear();
vector_json(get(last, "tournament_name"), d.last_results.tournament_name);
}
void save_career(const std::filesystem::path &path, const Classic &content, const GameState &state,
const WatcomRandom &random, const GameRandom &game, const TurnDriver &driver, bool overwrite,
const J &session) {
if (driver.stage != TurnDriver::Stage::Empty && driver.stage != TurnDriver::Stage::MatchdayDesks &&
driver.stage != TurnDriver::Stage::SeasonEndDesk)
fail("save unavailable outside a manager desk");
Documents docs = content.documents;
if (verify(docs) != content.content_id)
fail("invalid content identity");
docs["state/world-manifest.json"] = docs.at("manifest.json");
auto manifest = parse(docs, "manifest.json");
(*manifest.object())["kind"] = "save";
(*manifest.object())["save_profile"] = "classic-career-v1";
(*manifest.object())["content_id"] = content.content_id;
put(docs, "manifest.json", std::move(manifest));
put(docs, "state/career.json", encode_native_state(state));
put(docs, "state/rng.json",
O{{"schema", "classic-rng@1.0"},
{"watcom", rng(random.seed(), random.calls())},
{"game", rng(uint32_t(game.seed()), game.calls())}});
put(docs, "state/session.json", session);
put(docs, "state/driver.json", driver_checkpoint(driver));
put(docs, "state/information.json", encode_native_bytes(save_header(state)));
O checks;
for (const auto &[name, value] : docs)
if (name.starts_with("state/"))
checks[name] = sha256(value);
put(docs, "state/integrity.json", O{{"schema", "save-integrity@1.0"}, {"entries", std::move(checks)}});
write_archive(docs, path, overwrite);
}
SavedCareer load_career(const std::filesystem::path &path, const Classic &content) {
const auto docs = read_archive(path);
const auto manifest = parse(docs, "manifest.json");
if (str(get(manifest, "kind")) != "save" || str(get(manifest, "save_profile")) != "classic-career-v1" ||
str(get(manifest, "content_id")) != content.content_id)
fail("save belongs to another mod or format");
const auto integrity = parse(docs, "state/integrity.json");
if (str(get(integrity, "schema")) != "save-integrity@1.0")
fail("unsupported save integrity");
O checks;
for (const auto &[name, value] : docs)
if (name.starts_with("state/") && name != "state/integrity.json")
checks[name] = sha256(value);
if (cli::dump_json(get(integrity, "entries")) != cli::dump_json(checks))
fail("damaged native save");
const auto world = world_of(docs);
if (verify(world) != content.content_id)
fail("save world contents changed");
SavedCareer out;
out.state = decode_native_state(parse(docs, "state/career.json"));
if (out.state.players.size() != content.database.players.size() ||
out.state.club_ids.size() != content.database.club_names.size() ||
out.state.manager_records.size() != 5 * 0xb0c)
fail("native state does not match Classic dimensions");
const auto generators = parse(docs, "state/rng.json");
if (str(get(generators, "schema")) != "classic-rng@1.0")
fail("unsupported RNG checkpoint");
const auto &w = get(generators, "watcom");
const auto &g = get(generators, "game");
out.random.restore(seed(w), calls(w));
out.game.restore(std::bit_cast<int32_t>(seed(g)), calls(g));
out.session = parse(docs, "state/session.json");
out.driver = parse(docs, "state/driver.json");
out.header = decode_native_bytes(parse(docs, "state/information.json"), 172);
return out;
}
Bytes save_information(const std::filesystem::path &path) {
const auto docs = read_archive(path);
return decode_native_bytes(parse(docs, "state/information.json"), 172);
}
Bytes load_preferences(const std::filesystem::path &path, const char *kind, size_t size) {
const auto b = read_file(path);
const auto root = cli::parse_json(std::string(b.begin(), b.end()));
if (str(get(root, "schema")) != std::string("classic-") + kind + "@1.0")
fail("unsupported preferences");
return decode_native_bytes(get(root, "payload"), size);
}
void save_preferences(const std::filesystem::path &path, const char *kind, const Bytes &b, bool overwrite) {
write_atomic(path,
cli::dump_json(O{{"schema", std::string("classic-") + kind + "@1.0"},
{"payload", encode_native_bytes(b)}}) +
"\n",
overwrite);
}
} // namespace bmh::content
+23
View File
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include "content/package.hpp"
#include "formats/native_state.hpp"
#include "game/turn_driver.hpp"
namespace bmh::content {
struct SavedCareer {
GameState state;
WatcomRandom random;
GameRandom game;
cli::Json driver, session;
Bytes header;
};
void save_career(const std::filesystem::path &, const Classic &, const GameState &, const WatcomRandom &,
const GameRandom &, const TurnDriver &, bool overwrite, const cli::Json &session = {});
SavedCareer load_career(const std::filesystem::path &, const Classic &);
Bytes save_information(const std::filesystem::path &);
cli::Json driver_checkpoint(const TurnDriver &);
void restore_driver(TurnDriver &, const cli::Json &);
// Native settings/layout files retain the validated Classic bytes with a typed envelope.
Bytes load_preferences(const std::filesystem::path &, const char *kind, size_t size);
void save_preferences(const std::filesystem::path &, const char *kind, const Bytes &, bool overwrite);
} // namespace bmh::content