small refactor and void library

Signed-off-by: Aleksei (Faumaray) Selikhov <contact@faumaray.com>
This commit is contained in:
faumaray committed 2026-12-02 19:16:31 +03:00
1 parent 74b89ae3ca
commit 083b84e11a
12 files changed
+646 -343

No files matched your search

+23 -14
View File
@@ -1,10 +1,15 @@
#include "settlement/core/world.hpp"
#include <Void/Binary/Reader.hpp>
#include <Void/Binary/Writer.hpp>
#include <Void/Core/Hash.hpp>
#include <algorithm>
#include <bit>
#include <array>
#include <istream>
#include <limits>
#include <ostream>
#include <span>
#include <stdexcept>
#include <string_view>
#include <type_traits>
@@ -32,15 +37,15 @@ public:
if constexpr (std::is_enum_v<T>) {
field(static_cast<std::underlying_type_t<T>>(value));
} else {
using Bits = std::make_unsigned_t<T>;
std::uint64_t bits = std::bit_cast<Bits>(value);
for (std::size_t i = 0; i < sizeof(T); ++i) {
const auto byte = static_cast<std::uint8_t>(bits);
std::array<char, sizeof(T)> bytes;
Void::Binary::Writer writer(std::as_writable_bytes(std::span{bytes}));
require_save(writer.writeLittle(value).has_value(), "Could not encode a save field.");
// Preserve the stream prefix if writing stops partway through a field.
for (char byte : bytes) {
require_save(bytes_ < max_save_bytes, "Save exceeds the 128 MiB limit.");
++bytes_;
stream_.put(std::bit_cast<char>(byte));
stream_.put(byte);
require_save(static_cast<bool>(stream_), "Could not write the save.");
bits >>= 8;
}
}
}
@@ -62,19 +67,22 @@ public:
field(bits, payload);
value = static_cast<T>(bits);
} else {
std::uint64_t bits = 0;
for (std::size_t i = 0; i < sizeof(T); ++i) {
std::array<char, sizeof(T)> bytes;
for (char& byte : bytes) {
const int next = stream_.get();
require_save(next != std::char_traits<char>::eof(), "Save is incomplete or unreadable.");
const auto byte = static_cast<std::uint8_t>(next);
byte = std::char_traits<char>::to_char_type(next);
if (payload) {
require_save(bytes_ < max_save_bytes, "Save exceeds the 128 MiB limit.");
++bytes_;
if (legacy_footer_) legacy_digest_ = (legacy_digest_ ^ byte) * 1099511628211ULL;
}
bits |= static_cast<std::uint64_t>(byte) << (8 * i);
}
value = std::bit_cast<T>(static_cast<std::make_unsigned_t<T>>(bits));
const auto wire = std::as_bytes(std::span{bytes});
if (payload && legacy_footer_) legacy_digest_ = Void::Hash::fnv1a64_update(legacy_digest_, wire);
Void::Binary::Reader reader(wire);
const auto decoded = reader.readLittle<T>();
require_save(decoded.has_value(), "Could not decode a save field.");
value = *decoded;
}
}
@@ -95,8 +103,9 @@ public:
private:
std::istream& stream_;
std::size_t bytes_ = 0;
// Legacy footers cover the signature and version, which are read first.
bool legacy_footer_ = true;
std::uint64_t legacy_digest_ = 14695981039346656037ULL;
std::uint64_t legacy_digest_ = Void::Hash::fnv1a64_offset_basis;
};
template<class Archive, class Vector, class Visit>