mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-03 17:04:52 +01:00
93399bd4a1
Step 2a of docs/world-format-eu07.md (#516, section 7): the container of the cache, one file per package and layer. A header (magic and type as EU07_FILEHEADER, the versions, the body, package, layer, tile level, count of levels of detail, and the engine and source hashes), a table of sections, the name table, a directory of 40-byte entries sorted by tile id and level of detail, and the tile blocks at multiples of 4 KB, packed with zstd in frames of 64 KB and checked by a CRC-32. A block unpacks to tagged chunks; a reader skips what it doesn't know, a section of a newer minor version included. Everything is little-endian and read field by field. The writer gives the same bytes for the same contents. Nothing in the game uses it yet. Tests: the CRC against zlib's value, a file written and read back (header, names, directory, every block), a large block in frames, the same bytes twice, what the writer refuses, and broken files: another type or major version, cut short, a block not matching its CRC; a newer minor version still opens.
177 lines
6.4 KiB
C++
177 lines
6.4 KiB
C++
#include "stdafx.h"
|
|
#include "doctest.h"
|
|
#include "world/worldcache.h"
|
|
|
|
#include <cstring>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
// the cache file of the new world format (docs/world-format-eu07.md, section 7): the header, the name table, the directory of tiles
|
|
// and the tile blocks packed with zstd, checked by CRC and split into tagged chunks
|
|
using namespace world_cache;
|
|
using world_geometry::tile_id;
|
|
|
|
namespace {
|
|
|
|
std::vector<std::byte> bytes_of(std::string const &Text) {
|
|
std::vector<std::byte> bytes(Text.size());
|
|
std::memcpy(bytes.data(), Text.data(), Text.size());
|
|
return bytes;
|
|
}
|
|
|
|
// a block of the size, its bytes not repeating much, so that zstd doesn't shrink it to nothing
|
|
std::vector<std::byte> noise(std::size_t const Size, std::uint32_t Seed) {
|
|
std::vector<std::byte> bytes(Size);
|
|
for (auto &value : bytes) {
|
|
Seed = Seed * 1'664'525u + 1'013'904'223u;
|
|
value = static_cast<std::byte>(Seed >> 24);
|
|
}
|
|
return bytes;
|
|
}
|
|
|
|
file_contents sample() {
|
|
file_contents contents;
|
|
contents.header.body = world_geometry::body::plane();
|
|
contents.header.package = 1;
|
|
contents.header.layer = layer_kind::surroundings;
|
|
contents.header.lod_count = 2;
|
|
contents.header.engine_hash = 0x1122'3344'5566'7788ull;
|
|
contents.header.source_hash = 0x99AA'BBCC'DDEE'FF00ull;
|
|
contents.names = {"", "linia053_wrzosy", "models/tree.t3d", "zażółć"};
|
|
auto const a{world_geometry::plane_tile_at({}, 13)};
|
|
auto const b{a.edge_neighbours()[1]};
|
|
// out of order on purpose: the writer sorts them
|
|
contents.tiles.push_back({b, 0, 0, -3.5f, 40.25f, {{four_cc('I', 'N', 'S', 'T'), 0, bytes_of("instances")}, {four_cc('X', 'Y', 'Z', 'W'), 7, bytes_of("unknown")}}});
|
|
contents.tiles.push_back({a, 1, 0, 0.0f, 12.0f, {{four_cc('V', 'P', 'O', 'S'), 0, noise(200'000, 7)}}});
|
|
contents.tiles.push_back({a, 0, 0, -1.0f, 1.0f, {{four_cc('T', 'I', 'N', 'F'), 0, bytes_of("a")}, {four_cc('E', 'M', 'P', 'T'), 0, {}}}});
|
|
contents.tiles.push_back({b, 1, entry_flags::empty, 0.0f, 0.0f, {}});
|
|
return contents;
|
|
}
|
|
|
|
std::string written(file_contents const &Contents) {
|
|
std::ostringstream output;
|
|
REQUIRE(write(output, Contents).empty());
|
|
return output.str();
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST_SUITE("world cache") {
|
|
|
|
TEST_CASE("the CRC-32 is zlib's") {
|
|
auto const text{bytes_of("123456789")};
|
|
CHECK(crc32(text.data(), text.size()) == 0xCBF4'3926u);
|
|
CHECK(crc32(nullptr, 0) == 0u);
|
|
}
|
|
|
|
TEST_CASE("a cache file is written and read back: header, names, directory and blocks") {
|
|
auto const contents{sample()};
|
|
auto const file{written(contents)};
|
|
std::istringstream input{file};
|
|
reader cache;
|
|
REQUIRE(cache.open(input).empty());
|
|
CHECK(cache.header() == contents.header);
|
|
CHECK(cache.names() == contents.names);
|
|
CHECK(cache.current(contents.header.engine_hash, contents.header.source_hash));
|
|
CHECK_FALSE(cache.current(contents.header.engine_hash, contents.header.source_hash + 1));
|
|
REQUIRE(cache.directory().size() == 4);
|
|
// sorted by tile and level of detail, the blocks at multiples of 4 KB
|
|
for (std::size_t k = 1; k < cache.directory().size(); ++k) {
|
|
auto const &previous{cache.directory()[k - 1]};
|
|
auto const &entry{cache.directory()[k]};
|
|
CHECK((previous.tile.raw() < entry.tile.raw() || (previous.tile == entry.tile && previous.lod < entry.lod)));
|
|
}
|
|
for (auto const &entry : cache.directory())
|
|
if ((entry.flags & entry_flags::empty) == 0)
|
|
CHECK(entry.offset % block_alignment == 0);
|
|
CHECK(file.size() % block_alignment == 0);
|
|
for (auto const &tile : contents.tiles) {
|
|
auto const entry{cache.find(tile.tile, tile.lod)};
|
|
REQUIRE(entry.has_value());
|
|
CHECK(entry->y_min == tile.y_min);
|
|
CHECK(entry->y_max == tile.y_max);
|
|
std::vector<chunk> chunks;
|
|
REQUIRE(cache.read_block(input, *entry, chunks).empty());
|
|
CHECK(chunks == tile.chunks);
|
|
}
|
|
CHECK_FALSE(cache.find(contents.tiles[0].tile, 5).has_value());
|
|
CHECK_FALSE(cache.find(world_geometry::plane_tile_at({}, 12), 0).has_value());
|
|
}
|
|
|
|
TEST_CASE("a large block is packed in frames of 64 KB and shrinks; the same contents give the same bytes") {
|
|
auto contents{sample()};
|
|
auto const repeated{std::vector<std::byte>(300'000, std::byte{0x5A})};
|
|
contents.tiles[1].chunks.push_back({four_cc('I', 'D', 'X', '4'), 0, repeated});
|
|
auto const file{written(contents)};
|
|
CHECK(file == written(contents));
|
|
std::istringstream input{file};
|
|
reader cache;
|
|
REQUIRE(cache.open(input).empty());
|
|
auto const entry{cache.find(contents.tiles[1].tile, contents.tiles[1].lod)};
|
|
REQUIRE(entry.has_value());
|
|
CHECK(entry->raw_size > 500'000);
|
|
CHECK(entry->stored_size < entry->raw_size);
|
|
std::vector<chunk> chunks;
|
|
REQUIRE(cache.read_block(input, *entry, chunks).empty());
|
|
CHECK(chunks == contents.tiles[1].chunks);
|
|
}
|
|
|
|
TEST_CASE("the writer refuses what the file can't hold") {
|
|
auto contents{sample()};
|
|
contents.tiles.push_back(contents.tiles[0]);
|
|
std::ostringstream output;
|
|
CHECK_FALSE(write(output, contents).empty()); // a tile repeated
|
|
contents = sample();
|
|
contents.tiles[0].lod = 2;
|
|
CHECK_FALSE(write(output, contents).empty()); // beyond the count of levels of detail
|
|
contents = sample();
|
|
contents.tiles[0].tile = tile_id{};
|
|
CHECK_FALSE(write(output, contents).empty()); // no tile
|
|
contents = sample();
|
|
contents.tiles[3].chunks.push_back({1, 0, {}});
|
|
CHECK_FALSE(write(output, contents).empty()); // an empty tile with chunks
|
|
}
|
|
|
|
TEST_CASE("a broken cache file doesn't open, and a broken block doesn't read") {
|
|
auto const file{written(sample())};
|
|
reader cache;
|
|
SUBCASE("not a cache file") {
|
|
auto broken{file};
|
|
broken[4] = 'X';
|
|
std::istringstream input{broken};
|
|
CHECK_FALSE(cache.open(input).empty());
|
|
}
|
|
SUBCASE("another major version") {
|
|
auto broken{file};
|
|
broken[8] = 2;
|
|
std::istringstream input{broken};
|
|
CHECK_FALSE(cache.open(input).empty());
|
|
}
|
|
SUBCASE("a newer minor version still reads") {
|
|
auto newer{file};
|
|
newer[10] = 7;
|
|
std::istringstream input{newer};
|
|
CHECK(cache.open(input).empty());
|
|
CHECK(cache.header().minor == 7);
|
|
}
|
|
SUBCASE("cut short") {
|
|
for (auto const length : {std::size_t{10}, std::size_t{70}, std::size_t{200}}) {
|
|
std::istringstream input{file.substr(0, length)};
|
|
CHECK_FALSE(cache.open(input).empty());
|
|
}
|
|
}
|
|
SUBCASE("a block that doesn't match its CRC") {
|
|
std::istringstream input{file};
|
|
REQUIRE(cache.open(input).empty());
|
|
auto const entry{cache.directory().front()};
|
|
auto broken{file};
|
|
broken[static_cast<std::size_t>(entry.offset)] ^= 0x40;
|
|
std::istringstream damaged{broken};
|
|
std::vector<chunk> chunks;
|
|
CHECK_FALSE(cache.read_block(damaged, entry, chunks).empty());
|
|
}
|
|
}
|
|
|
|
} // TEST_SUITE
|