Files
aevolution/src/world/codec.cpp
T

525 lines
21 KiB
C++

#include "chunk_state.hpp"
#include <algorithm>
#include <array>
#include <limits>
#include <utility>
namespace evo::world {
namespace {
constexpr std::uint64_t snapshot_magic = 0x31504e5357564541ULL;
constexpr std::uint64_t journal_magic = 0x314c4e4a57564541ULL;
std::uint64_t checksum(std::span<const std::uint8_t> data) noexcept {
std::uint64_t h = 14695981039346656037ULL;
for (const auto byte : data) {
h = (h ^ byte) * 1099511628211ULL;
}
return h;
}
struct Reader {
std::span<const std::uint8_t> data;
std::size_t at{};
bool ok{true};
std::uint64_t get(unsigned width) noexcept {
if (!ok || width > data.size() - at) {
ok = false;
return 0;
}
std::uint64_t value = 0;
for (unsigned i = 0; i < width; ++i) {
value |= static_cast<std::uint64_t>(data[at++]) << (8U * i);
}
return value;
}
std::uint32_t u32() noexcept {
return static_cast<std::uint32_t>(get(4));
}
std::int64_t signed64() noexcept {
const auto value = get(8);
return value <= static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max())
? static_cast<std::int64_t>(value)
: -1 - static_cast<std::int64_t>(~value);
}
ChunkAddress address() noexcept {
return {signed64(), signed64(), signed64()};
}
};
struct Writer {
WorldBytes data;
explicit Writer(std::size_t bytes) {
data.reserve(bytes);
}
void put(std::uint64_t value, unsigned width) {
for (unsigned i = 0; i < width; ++i) {
data.push_back(static_cast<std::uint8_t>(value & 255U));
value >>= 8U;
}
}
void address(ChunkAddress a) {
put(static_cast<std::uint64_t>(a.x), 8);
put(static_cast<std::uint64_t>(a.y), 8);
put(static_cast<std::uint64_t>(a.z), 8);
}
void header(std::uint64_t magic, WorldIdentity id) {
put(magic, 8);
put(1, 4);
put(0, 4);
put(id.world, 8);
put(id.frame, 8);
}
WorldBytes finish() {
const auto length = data.size() + 8;
for (unsigned i = 0; i < 4; ++i) {
data[12 + i] = static_cast<std::uint8_t>((length >> (8U * i)) & 255U);
}
const auto hash = checksum(data);
put(hash, 8);
return std::move(data);
}
};
std::expected<Reader, WorldCodecError> open(std::span<const std::uint8_t> bytes,
std::uint64_t magic, std::size_t maximum,
WorldIdentity id) noexcept {
if (bytes.size() < 40 || bytes.size() > maximum) {
return std::unexpected{WorldCodecError::bounds};
}
Reader r{bytes.first(bytes.size() - 8)};
if (r.get(8) != magic) {
return std::unexpected{WorldCodecError::format};
}
if (r.u32() != 1) {
return std::unexpected{WorldCodecError::version};
}
if (r.u32() != bytes.size()) {
return std::unexpected{WorldCodecError::bounds};
}
Reader tail{bytes.last(8)};
if (checksum(r.data) != tail.get(8)) {
return std::unexpected{WorldCodecError::checksum};
}
const WorldIdentity stored{r.get(8), r.get(8)};
if (id.world == 0 || id.frame == 0 || stored != id) {
return std::unexpected{WorldCodecError::identity};
}
return r;
}
void write_request(Writer& w, const ExcavationRequest& r) {
w.put(r.command, 8);
w.put(r.digest, 8);
w.put(r.destination, 4);
w.put(r.cuts.size(), 4);
for (const auto& cut : r.cuts) {
w.address(cut.chunk);
w.put(cut.local.x, 4);
w.put(cut.local.y, 4);
w.put(cut.local.z, 4);
w.put(cut.expected_revision, 8);
w.put(cut.removed_fill, 4);
}
}
std::expected<ExcavationRequest, WorldCodecError>
read_request(Reader& r, std::array<CellCut, 64>& cuts) noexcept {
ExcavationRequest request{};
request.command = r.get(8);
request.digest = r.get(8);
request.destination = r.u32();
const auto count = r.u32();
if (!r.ok || count == 0 || count > 64 ||
static_cast<std::size_t>(count) * 48 > r.data.size() - r.at) {
return std::unexpected{WorldCodecError::bounds};
}
for (std::size_t i = 0; i < count; ++i) {
cuts[i] = {r.address(), {r.u32(), r.u32(), r.u32()}, r.get(8), r.u32()};
}
request.cuts = std::span{cuts}.first(count);
const auto hash = excavation_digest(request);
if (!r.ok || !hash || *hash != request.digest) {
return std::unexpected{WorldCodecError::state};
}
return request;
}
void write_receipt(Writer& w, const EditReceipt& r) {
for (const auto amount : r.removed) {
w.put(amount.value, 8);
}
w.put(r.chunk_count, 4);
for (std::size_t i = 0; i < r.chunk_count; ++i) {
w.address(r.chunks[i].address);
w.put(r.chunks[i].revision, 8);
}
}
std::uint64_t covered(std::span<const std::uint8_t> bytes) noexcept {
Reader r{bytes.subspan(72, 8)};
return r.get(8); // only after successful snapshot restore
}
std::uint64_t fingerprint(std::span<const std::uint8_t> bytes) noexcept {
Reader r{bytes.last(8)};
return r.get(8); // only after envelope validation
}
} // namespace
std::expected<WorldBytes, WorldCodecError> ChunkStore::snapshot(WorldIdentity identity) const {
if (identity.world == 0 || identity.frame == 0) {
return std::unexpected{WorldCodecError::identity};
}
std::array<std::size_t, max_slots> order{};
std::size_t count = 0;
for (std::size_t i = 0; i < slots_; ++i) {
if (entries_[i].cells) {
order[count++] = i;
}
}
std::ranges::sort(std::span{order}.first(count),
[&](auto a, auto b) { return entries_[a].address < entries_[b].address; });
std::array<std::size_t, retained_commands> history{};
std::size_t records = 0, length = 132 + count * (33 + 16384);
for (std::size_t i = 0; i < retained_commands; ++i) {
if (history_[i].receipt.command != 0) {
history[records++] = i;
length += 44 + 48 * history_[i].count + 32 * history_[i].receipt.chunk_count;
}
}
std::ranges::sort(std::span{history}.first(records), [&](auto a, auto b) {
return history_[a].receipt.command < history_[b].receipt.command;
});
if (length > max_world_snapshot_bytes) {
return std::unexpected{WorldCodecError::bounds};
}
Writer w{length};
w.header(snapshot_magic, identity);
w.put(descriptor_.seed, 8);
w.put(descriptor_.coordinate_version, 4);
w.put(descriptor_.generator_version, 4);
w.put(descriptor_.material_version, 4);
w.put(descriptor_.edge, 4);
w.put(slots_, 4);
w.put(count, 4);
w.put(revision_, 8);
w.put(last_command_, 8);
w.put(committed_edits_, 8);
for (const auto cap : debris_capacity_.materials) {
w.put(cap.value, 8);
}
for (const auto amount : debris_) {
w.put(amount.value, 8);
}
w.put(records, 4);
for (std::size_t i = 0; i < count; ++i) {
const auto& entry = entries_[order[i]];
w.address(entry.address);
w.put(entry.revision, 8);
w.put(entry.dirty ? 1 : 0, 1);
if (!entry.cells) {
return std::unexpected{WorldCodecError::state};
}
for (std::uint32_t j = 0; j < base_chunk_cells; ++j) {
const auto cell = *entry.cells->read(j);
w.put(cell.material().value, 2);
w.put(cell.fill(), 2);
}
}
for (std::size_t i = 0; i < records; ++i) {
const auto& h = history_[history[i]];
write_request(w, {h.receipt.command, h.destination, std::span{h.cuts}.first(h.count),
h.receipt.digest});
write_receipt(w, h.receipt);
}
return w.finish();
}
std::expected<std::unique_ptr<ChunkStore>, WorldCodecError>
ChunkStore::restore(std::span<const std::uint8_t> bytes, WorldIdentity identity,
std::size_t budget) {
const auto envelope = open(bytes, snapshot_magic, max_world_snapshot_bytes, identity);
if (!envelope) {
return std::unexpected{envelope.error()};
}
auto r = *envelope;
const BaseDescriptor descriptor{r.get(8), r.u32(), r.u32(), r.u32(), r.u32()};
const auto slots = r.u32(), count = r.u32();
const auto revision = r.get(8), command = r.get(8), commits = r.get(8);
const DebrisCapacity capacity{{VolumeUnits{r.get(8)}, VolumeUnits{r.get(8)}}};
const std::array<VolumeUnits, 2> debris{VolumeUnits{r.get(8)}, VolumeUnits{r.get(8)}};
const auto records = r.u32();
if (!r.ok || slots == 0 || slots > max_slots || count > slots || records > retained_commands ||
static_cast<std::size_t>(count) * (33 + 16384) > r.data.size() - r.at) {
return std::unexpected{WorldCodecError::bounds};
}
if (records != std::min<std::uint64_t>(commits, retained_commands) || commits > command ||
((commits == 0) != (command == 0)) || debris[0].value > capacity.materials[0].value ||
debris[1].value > capacity.materials[1].value) {
return std::unexpected{WorldCodecError::state};
}
auto made = create(descriptor, slots, budget, capacity);
if (!made) {
return std::unexpected{made.error() == ChunkError::memory_budget
? WorldCodecError::budget
: WorldCodecError::version};
}
auto& store = **made;
std::array<std::uint64_t, 2> removed{};
for (std::size_t i = 0; i < count; ++i) {
auto& entry = store.entries_[i];
entry.address = r.address();
entry.revision = r.get(8);
const auto dirty = r.get(1);
const auto origin = join({entry.address, {}}, base_chunk_edge);
if (!r.ok || !origin || dirty > 1 || entry.revision == 0 || entry.revision > revision ||
(i > 0 && entry.address <= store.entries_[i - 1].address)) {
return std::unexpected{WorldCodecError::state};
}
for (std::size_t j = 0; j < i; ++j) {
if (store.entries_[j].revision == entry.revision) {
return std::unexpected{WorldCodecError::state};
}
}
entry.dirty = dirty == 1;
std::array<std::uint32_t, base_chunk_cells> words{};
for (std::uint32_t j = 0; j < base_chunk_cells; ++j) {
const auto material = static_cast<std::uint16_t>(r.get(2));
const auto fill = static_cast<std::uint32_t>(r.get(2));
const auto cell = CellSample::create({material}, fill);
const auto local = *local_from_ordinal(j, base_chunk_edge);
const auto base = *base_sample(
descriptor, {origin->x + local.x, origin->y + local.y, origin->z + local.z});
if (!r.ok || !cell || (material != 0 && material != base.material().value) ||
fill > base.fill() || (!entry.dirty && *cell != base)) {
return std::unexpected{WorldCodecError::state};
}
if (base.material().value != 0) {
removed[base.material().value - 1] += base.fill() - fill;
}
words[j] = material | (fill << 16U);
}
const auto payload = CompactCells::required_bytes(words);
if (payload > budget - store.bytes_) {
return std::unexpected{WorldCodecError::budget};
}
entry.cells.emplace(CompactCells::encode(words));
store.bytes_ += payload;
++store.size_;
}
if (removed[0] != debris[0].value || removed[1] != debris[1].value) {
return std::unexpected{WorldCodecError::state};
}
std::uint64_t prior_command = 0, prior_revision = 0;
for (std::size_t i = 0; i < records; ++i) {
auto& h = store.history_[i];
const auto request = read_request(r, h.cuts);
if (!request) {
return std::unexpected{request.error()};
}
h.count = request->cuts.size();
h.destination = request->destination;
h.receipt.command = request->command;
h.receipt.digest = request->digest;
h.receipt.removed = {VolumeUnits{r.get(8)}, VolumeUnits{r.get(8)}};
h.receipt.chunk_count = r.u32();
if (!r.ok || h.receipt.chunk_count == 0 || h.receipt.chunk_count > 4 ||
request->command <= prior_command || request->command > command) {
return std::unexpected{WorldCodecError::state};
}
std::size_t expected_chunks = 0;
std::array<std::uint64_t, 2> amounts{};
for (std::size_t j = 0; j < h.count; ++j) {
const auto& cut = h.cuts[j];
const auto at = join({cut.chunk, cut.local}, base_chunk_edge);
const auto base = *base_sample(descriptor, *at);
if (base.material().value == 0) {
return std::unexpected{WorldCodecError::state};
}
amounts[base.material().value - 1] += cut.removed_fill;
if (j == 0 || cut.chunk != h.cuts[j - 1].chunk) {
if (expected_chunks >= h.receipt.chunk_count) {
return std::unexpected{WorldCodecError::state};
}
auto& edited = h.receipt.chunks[expected_chunks];
edited = {r.address(), r.get(8)};
if (edited.address != cut.chunk || edited.revision <= prior_revision ||
edited.revision > revision ||
(expected_chunks > 0 &&
edited.revision != h.receipt.chunks[expected_chunks - 1].revision + 1)) {
return std::unexpected{WorldCodecError::state};
}
++expected_chunks;
}
}
if (!r.ok || expected_chunks != h.receipt.chunk_count ||
amounts[0] != h.receipt.removed[0].value || amounts[1] != h.receipt.removed[1].value) {
return std::unexpected{WorldCodecError::state};
}
for (const auto& cut : request->cuts) {
if (cut.expected_revision >= h.receipt.chunks[0].revision) {
return std::unexpected{WorldCodecError::state};
}
}
prior_revision = h.receipt.chunks[h.receipt.chunk_count - 1].revision;
prior_command = request->command;
}
if (!r.ok || r.at != r.data.size() || prior_command != command) {
return std::unexpected{WorldCodecError::state};
}
// Verify history against current geometry and subsequent requests without storing
// an unbounded undo world. At most 4096 retained cuts participate in these scans.
for (std::size_t i = 0; i < records; ++i) {
const auto& h = store.history_[i];
for (std::size_t j = 0; j < h.receipt.chunk_count; ++j) {
const auto edited = h.receipt.chunks[j];
const auto token = store.view(edited.address);
if (!token || !store.entries_[token->slot_].dirty) {
return std::unexpected{WorldCodecError::state};
}
auto next_revision = token->revision();
for (std::size_t later = i + 1; later < records; ++later) {
const auto& next = store.history_[later];
bool found = false;
for (std::size_t k = 0; k < next.count; ++k) {
if (next.cuts[k].chunk == edited.address) {
next_revision = next.cuts[k].expected_revision;
found = true;
break;
}
}
if (found) {
break;
}
}
if (next_revision != edited.revision) {
return std::unexpected{WorldCodecError::state};
}
for (std::size_t k = 0; k < count; ++k) {
if (store.entries_[k].revision == edited.revision &&
store.entries_[k].address != edited.address) {
return std::unexpected{WorldCodecError::state};
}
}
}
for (std::size_t j = 0; j < h.count; ++j) {
const auto& cut = h.cuts[j];
const auto token = store.view(cut.chunk);
if (!token) {
return std::unexpected{WorldCodecError::state};
}
const auto current = store.read(*token, cut.local);
if (!current) {
return std::unexpected{WorldCodecError::state};
}
std::uint64_t undo = current->fill();
for (std::size_t later = i; later < records; ++later) {
const auto& next = store.history_[later];
for (std::size_t k = 0; k < next.count; ++k) {
if (next.cuts[k].chunk == cut.chunk && next.cuts[k].local == cut.local) {
undo += next.cuts[k].removed_fill;
}
}
}
const auto base =
*base_sample(descriptor, *join({cut.chunk, cut.local}, base_chunk_edge));
if (undo > base.fill()) {
return std::unexpected{WorldCodecError::state};
}
}
}
std::array<std::uint64_t, 2> recorded_removed{};
for (std::size_t i = 0; i < records; ++i) {
for (std::size_t m = 0; m < 2; ++m) {
recorded_removed[m] += store.history_[i].receipt.removed[m].value;
}
}
for (std::size_t m = 0; m < 2; ++m) {
if (recorded_removed[m] > debris[m].value ||
(commits <= retained_commands && recorded_removed[m] != debris[m].value)) {
return std::unexpected{WorldCodecError::state};
}
}
if (commits > revision) {
return std::unexpected{WorldCodecError::state};
}
store.revision_ = revision;
store.last_command_ = command;
store.committed_edits_ = commits;
store.debris_ = debris;
store.history_next_ = records % retained_commands;
return std::move(*made);
}
std::expected<WorldBytes, WorldCodecError>
make_edit_journal(std::span<const std::uint8_t> base, WorldIdentity identity,
std::span<const ExcavationRequest> requests, std::size_t budget) {
if (requests.size() > 64) {
return std::unexpected{WorldCodecError::bounds};
}
auto store = ChunkStore::restore(base, identity, budget);
if (!store) {
return std::unexpected{store.error()};
}
std::size_t length = 68;
for (const auto& r : requests) {
if (r.cuts.size() > 64) {
return std::unexpected{WorldCodecError::bounds};
}
length += 24 + 48 * r.cuts.size();
}
Writer w{length};
w.header(journal_magic, identity);
w.put(fingerprint(base), 8);
w.put(covered(base), 8);
w.put(requests.size(), 4);
auto last = covered(base);
for (const auto& r : requests) {
if (r.command <= last || !(**store).excavate(r)) {
return std::unexpected{WorldCodecError::edit};
}
write_request(w, r);
last = r.command;
}
const auto final = (**store).snapshot(identity);
if (!final) {
return std::unexpected{final.error()};
}
w.put(fingerprint(*final), 8);
return w.finish();
}
std::expected<std::unique_ptr<ChunkStore>, WorldCodecError>
replay_edit_journal(std::span<const std::uint8_t> base, std::span<const std::uint8_t> journal,
WorldIdentity identity, std::size_t budget) {
const auto envelope = open(journal, journal_magic, max_world_journal_bytes, identity);
if (!envelope) {
return std::unexpected{envelope.error()};
}
auto r = *envelope;
const auto previous = r.get(8), last = r.get(8);
const auto count = r.u32();
if (!r.ok || count > 64 || static_cast<std::size_t>(count) * 72 + 8 > r.data.size() - r.at) {
return std::unexpected{WorldCodecError::bounds};
}
auto store = ChunkStore::restore(base, identity, budget);
if (!store) {
return std::unexpected{store.error()};
}
if (previous != fingerprint(base) || last != covered(base)) {
return std::unexpected{WorldCodecError::predecessor};
}
auto prior = last;
for (std::size_t i = 0; i < count; ++i) {
std::array<CellCut, 64> cuts{};
const auto request = read_request(r, cuts);
if (!request) {
return std::unexpected{request.error()};
}
if (request->command <= prior || !(**store).excavate(*request)) {
return std::unexpected{WorldCodecError::edit};
}
prior = request->command;
}
const auto expected = r.get(8);
if (!r.ok || r.at != r.data.size()) {
return std::unexpected{WorldCodecError::format};
}
const auto final = (**store).snapshot(identity);
if (!final) {
return std::unexpected{final.error()};
}
if (fingerprint(*final) != expected) {
return std::unexpected{WorldCodecError::state};
}
return std::move(*store);
}
} // namespace evo::world