Build 0.2 native launcher and portable Classic runtime (by Codex)
This commit is contained in:
1 parent
3a021fc160
commit
2552528176
84 files changed
+5004
-197
No files matched your search
+29
-1
@@ -1,6 +1,8 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#include "formats/executable_layout.hpp"
|
||||
#include "game/highscore.hpp"
|
||||
#include "formats/native_state.hpp"
|
||||
#include "formats/native_file.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <bit>
|
||||
@@ -186,11 +188,25 @@ HighscoreTable decode_highscore_table(const HighscoreBytes& bytes) {
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
std::filesystem::path highscore_file(const std::filesystem::path& base, uint16_t signature, const std::string& prefix) {
|
||||
std::filesystem::path highscore_file(const std::filesystem::path &base, uint16_t signature,
|
||||
const std::string &prefix, bool native) {
|
||||
if (!signature) throw std::invalid_argument("highscore: signature0 has no ranked file");
|
||||
if (native)
|
||||
return base / ("highscore-" + std::to_string(signature) + ".obscore");
|
||||
return base / (prefix + std::to_string(signature));
|
||||
}
|
||||
HighscoreTable load_highscore_table(const std::filesystem::path& file) {
|
||||
if (file.extension() == ".obscore") {
|
||||
const auto data = read_file(file);
|
||||
const auto j = cli::parse_json(std::string(data.begin(), data.end()));
|
||||
if (!j.object() || !j.object()->contains("schema") || !j.object()->at("schema").string() ||
|
||||
*j.object()->at("schema").string() != "classic-highscore@1.0" || !j.object()->contains("payload"))
|
||||
throw FormatError("Invalid native highscore");
|
||||
const auto raw = decode_native_bytes(j.object()->at("payload"), 360);
|
||||
HighscoreBytes bytes;
|
||||
std::copy(raw.begin(), raw.end(), bytes.begin());
|
||||
return decode_highscore_table(bytes);
|
||||
}
|
||||
std::ifstream in(file, std::ios::binary);
|
||||
if (!in) {
|
||||
HighscoreTable empty{};
|
||||
@@ -203,6 +219,18 @@ HighscoreTable load_highscore_table(const std::filesystem::path& file) {
|
||||
}
|
||||
bool save_highscore_table(const std::filesystem::path& file, const HighscoreTable& rows) {
|
||||
const auto bytes = encode_highscore_table(rows);
|
||||
if (file.extension() == ".obscore") {
|
||||
try {
|
||||
content::write_atomic(file,
|
||||
cli::dump_json(cli::Json::Object{
|
||||
{"schema", "classic-highscore@1.0"},
|
||||
{"payload", encode_native_bytes(Bytes(bytes.begin(), bytes.end()))}}),
|
||||
true);
|
||||
return true;
|
||||
} catch (const std::exception &) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
std::ofstream out(file, std::ios::binary | std::ios::trunc);
|
||||
if (!out) return false;
|
||||
out.write(reinterpret_cast<const char*>(bytes.data()), bytes.size());
|
||||
|
||||
@@ -53,8 +53,8 @@ HighscoreProjection make_highscore_projection(const GameState& g, HighscoreTable
|
||||
HighscoreBytes encode_highscore_table(const HighscoreTable& rows);
|
||||
HighscoreTable decode_highscore_table(const HighscoreBytes& bytes);
|
||||
// The caller supplies the configured installation base and Original TEXTD889 prefix.
|
||||
std::filesystem::path highscore_file(const std::filesystem::path& base,
|
||||
uint16_t signature, const std::string& prefix = "high/h.");
|
||||
std::filesystem::path highscore_file(const std::filesystem::path &base, uint16_t signature,
|
||||
const std::string &prefix = "high/h.", bool native = false);
|
||||
HighscoreTable load_highscore_table(const std::filesystem::path& file);
|
||||
bool save_highscore_table(const std::filesystem::path& file, const HighscoreTable& rows);
|
||||
|
||||
|
||||
@@ -70,6 +70,13 @@ public:
|
||||
}
|
||||
void set_pointer(uint32_t a, uint32_t address) { set32(a, address == 0 ? 0 : address + kGuestDataBase); }
|
||||
|
||||
const std::map<uint32_t, Bytes> &blocks() const { return blocks_; }
|
||||
static StaticMemory from_blocks(std::map<uint32_t, Bytes> blocks) {
|
||||
StaticMemory memory;
|
||||
memory.blocks_ = std::move(blocks);
|
||||
return memory;
|
||||
}
|
||||
|
||||
private:
|
||||
static std::string hex(uint32_t v) {
|
||||
char b[16];
|
||||
|
||||
@@ -20,6 +20,10 @@ public:
|
||||
void srand(uint32_t seed) { seed_ = seed; }
|
||||
uint32_t seed() const { return seed_; }
|
||||
uint64_t calls() const { return calls_; }
|
||||
void restore(uint32_t seed, uint64_t calls) {
|
||||
seed_ = seed;
|
||||
calls_ = calls;
|
||||
}
|
||||
int rand() {
|
||||
calls_++;
|
||||
if (draw_hook) draw_hook(1);
|
||||
@@ -47,6 +51,10 @@ public:
|
||||
int32_t seed() const { return seed_; }
|
||||
void set_seed(int32_t seed) { seed_ = seed; }
|
||||
uint64_t calls() const { return calls_; }
|
||||
void restore(int32_t seed, uint64_t calls) {
|
||||
seed_ = seed;
|
||||
calls_ = calls;
|
||||
}
|
||||
int32_t range(int32_t lo, int32_t hi) {
|
||||
calls_++;
|
||||
if (draw_hook) draw_hook(0);
|
||||
|
||||
@@ -166,3 +166,60 @@ std::pair<int,int> RealityGrid::position(int figure) const {
|
||||
}
|
||||
|
||||
} // namespace bmh
|
||||
|
||||
namespace bmh {
|
||||
cli::Json RealityGrid::checkpoint() const {
|
||||
cli::Json::Array planes, positions;
|
||||
for (const auto &plane : occupied_) {
|
||||
cli::Json::Array cells;
|
||||
for (auto b : plane)
|
||||
cells.emplace_back(int(b));
|
||||
planes.emplace_back(std::move(cells));
|
||||
}
|
||||
for (const auto &[x, y] : positions_)
|
||||
positions.emplace_back(cli::Json::Array{x, y});
|
||||
return cli::Json::Object{
|
||||
{"planes", std::move(planes)},
|
||||
{"positions", std::move(positions)},
|
||||
{"ball", cli::Json::Array{ball_.first, ball_.second}},
|
||||
{"previous_ball", cli::Json::Array{previous_ball_.first, previous_ball_.second}}};
|
||||
}
|
||||
void RealityGrid::restore_checkpoint(const cli::Json &value) {
|
||||
const auto field = [&](const char *name) -> const cli::Json & {
|
||||
if (!value.object() || !value.object()->contains(name))
|
||||
throw FormatError("native Reality grid field missing");
|
||||
return value.object()->at(name);
|
||||
};
|
||||
const auto array = [](const cli::Json &j, size_t size) -> const cli::Json::Array & {
|
||||
if (!j.array() || j.array()->size() != size)
|
||||
throw FormatError("invalid native Reality grid dimensions");
|
||||
return *j.array();
|
||||
};
|
||||
const auto integer = [](const cli::Json &j) -> int {
|
||||
if (!j.integer() || *j.integer() < INT32_MIN || *j.integer() > INT32_MAX)
|
||||
throw FormatError("invalid native Reality coordinate");
|
||||
return int(*j.integer());
|
||||
};
|
||||
RealityGrid restored;
|
||||
const auto &planes = array(field("planes"), 2);
|
||||
for (size_t p = 0; p < 2; ++p) {
|
||||
const auto &cells = array(planes[p], 0x478);
|
||||
for (size_t i = 0; i < cells.size(); ++i) {
|
||||
const int n = integer(cells[i]);
|
||||
if (n < 0 || n > 255)
|
||||
throw FormatError("invalid native Reality cell");
|
||||
restored.occupied_[p][i] = uint8_t(n);
|
||||
}
|
||||
}
|
||||
const auto pair = [&](const cli::Json &j) {
|
||||
const auto &xy = array(j, 2);
|
||||
return std::pair<int, int>{integer(xy[0]), integer(xy[1])};
|
||||
};
|
||||
const auto &positions = array(field("positions"), 24);
|
||||
for (size_t i = 0; i < 24; ++i)
|
||||
restored.positions_[i] = pair(positions[i]);
|
||||
restored.ball_ = pair(field("ball"));
|
||||
restored.previous_ball_ = pair(field("previous_ball"));
|
||||
*this = std::move(restored);
|
||||
}
|
||||
} // namespace bmh
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <utility>
|
||||
|
||||
#include "formats/le.hpp"
|
||||
#include "cli/json.hpp"
|
||||
|
||||
namespace bmh {
|
||||
|
||||
@@ -59,15 +60,17 @@ RealityPath reality_goal_path(int x, int y, int goal_x);
|
||||
// either player plane. This is runtime scene state, not a Native save extension.
|
||||
class RealityGrid {
|
||||
public:
|
||||
bool place(int figure, int x, int y, int side);
|
||||
void clear(int x, int y, int side); // caller clears old occupancy before a move
|
||||
void reset_occupancy(); // Source initialization preserves old figure/ball data
|
||||
bool operator==(const RealityGrid&) const = default;
|
||||
void stage_position(int figure, int x, int y); // afb0c/afb23 before occupancy is committed
|
||||
uint8_t occupant(int x, int y, int side) const;
|
||||
std::pair<int, int> position(int figure) const;
|
||||
std::pair<int, int> ball() const { return ball_; }
|
||||
std::pair<int, int> previous_ball() const { return previous_ball_; }
|
||||
cli::Json checkpoint() const;
|
||||
void restore_checkpoint(const cli::Json &);
|
||||
bool place(int figure, int x, int y, int side);
|
||||
void clear(int x, int y, int side); // caller clears old occupancy before a move
|
||||
void reset_occupancy(); // Source initialization preserves old figure/ball data
|
||||
bool operator==(const RealityGrid &) const = default;
|
||||
void stage_position(int figure, int x, int y); // afb0c/afb23 before occupancy is committed
|
||||
uint8_t occupant(int x, int y, int side) const;
|
||||
std::pair<int, int> position(int figure) const;
|
||||
std::pair<int, int> ball() const { return ball_; }
|
||||
std::pair<int, int> previous_ball() const { return previous_ball_; }
|
||||
private:
|
||||
std::array<std::array<uint8_t, 0x478>, 2> occupied_{};
|
||||
std::array<std::pair<int, int>, 24> positions_{};
|
||||
|
||||
+3
-3
@@ -27,7 +27,7 @@ void put32(Bytes& d, size_t at, uint32_t x) {
|
||||
|
||||
// DeskDate2(date, buffer, 0): "TT.MM.JJJJ" of a day counter (day 0 = 1 July 2026; a
|
||||
// four-year leap cycle that starts with the third year).
|
||||
std::string date_text(uint32_t counter) {
|
||||
std::string date_text(uint32_t counter, int start_year) {
|
||||
static const int kDays[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
||||
int cycle = 3, years = 0, month = 7;
|
||||
while (counter > uint32_t(364 + (cycle == 4))) {
|
||||
@@ -45,7 +45,7 @@ std::string date_text(uint32_t counter) {
|
||||
if (++month == 13) month = 1, ++years;
|
||||
}
|
||||
char buf[16];
|
||||
std::snprintf(buf, sizeof buf, "%02d.%02d.%04d", day, month, 2026 + years);
|
||||
std::snprintf(buf, sizeof buf, "%02d.%02d.%04d", day, month, start_year + years);
|
||||
return buf;
|
||||
}
|
||||
|
||||
@@ -133,7 +133,7 @@ Bytes save_header(const GameState& g) {
|
||||
const int managers = g.memory.u8(::bmh::layout::ManagerCount);
|
||||
Bytes h;
|
||||
h.push_back(uint8_t(managers));
|
||||
const std::string date = date_text(g.date);
|
||||
const std::string date = date_text(g.date, g.display_start_year);
|
||||
for (size_t i = 0; i < 11; i++) h.push_back(i < date.size() ? uint8_t(date[i]) : 0);
|
||||
Bytes list(0xa0, 0);
|
||||
for (int m = 0; m < managers && m < 4; m++) {
|
||||
|
||||
@@ -174,6 +174,7 @@ struct GameState {
|
||||
// 3 (stadiums, *ba360): not used by the engine, kept for writing savegames.
|
||||
std::vector<uint8_t> master_database_metadata, referee_names, stadium_names;
|
||||
std::vector<uint16_t> player_cards; // *PlayerCards, per player: card counters per competition
|
||||
int display_start_year = 2026; // Cosmetic Classic origin; does not alter scheduling.
|
||||
uint32_t date = 0; // CalendarDay: day counter
|
||||
// DaysIntoSeason (day of the season, the calendar index) moves with the day counter within
|
||||
// a season: season_day() = date - season_start.
|
||||
|
||||
Reference in new issue
Block a user