mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-03 12:25:27 +01:00
30f3d70da9
Step 2d of docs/world-format-eu07.md (#516). With world.binary 1 in eu07.ini (off by default; then nothing is written and nothing changes) the loader looks for <scenery>.logic.eu07 in the folder of the sceneries: - current: made by this build (GIT_HASH and SRC_DATE), from the files the tape names as they are now (names, sizes, times of the last change) and with the same load settings (traction loaded, the binary terrain file standing in for the terrain includes, the text put ahead of the scenery, the trainset overrides): the scenery is replayed from the tape through the same parsers, with no file opened and no include expanded; - not there or not current: the scenery is read from its files as before, the parser records its tape, and the cache is written once the scenery is loaded, through a temporary file. The log says which of the two. Never in the editor, which keeps track of the files of the nodes, nor for a Rainsted override ($.scn). The cache file gains package blocks, data of the whole package rather than of a tile; the tape goes into them in parts of at most 64 MB until the logic is split into regions (step 3). The SBT check moved ahead of the parser, unchanged, as the cache depends on it. Not checked yet in the game: that a run from the replayed scenery gives the same physicslog bit for bit. Tests: the cache file name, FNV-1a, package blocks written and read back, and the logic cache read back while current and refused for another build, other settings or a changed included file.
76 lines
3.3 KiB
C++
76 lines
3.3 KiB
C++
#include "stdafx.h"
|
|
#include "doctest.h"
|
|
#include "scratch.h"
|
|
#include "utilities/parser.h"
|
|
#include "world/worldlogiccache.h"
|
|
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
// the cache of the logic of a scenery (world/worldlogiccache.h): the tape of the parser, in package blocks of a cache file, read
|
|
// back only while the build, the files and the settings are those it was made from
|
|
|
|
TEST_CASE("the logic cache is named after the scenario file, without the Rainsted marks") {
|
|
CHECK(world_logic_cache::file_name("scenery/", "linia053_wrzosy.scn") == "scenery/linia053_wrzosy.logic.eu07");
|
|
CHECK(world_logic_cache::file_name("scenery/", "$$wrzosy.scn") == "scenery/wrzosy.logic.eu07");
|
|
CHECK(world_logic_cache::file_name("scenery/", "noextension") == "scenery/noextension.logic.eu07");
|
|
}
|
|
|
|
TEST_CASE("the FNV-1a hash is the standard one") {
|
|
CHECK(world_logic_cache::fnv1a("") == 0xCBF2'9CE4'8422'2325ull);
|
|
CHECK(world_logic_cache::fnv1a("a") == 0xAF63'DC4C'8601'EC8Cull);
|
|
}
|
|
|
|
TEST_CASE("a cache file holds package blocks next to the tiles") {
|
|
world_cache::file_contents contents;
|
|
contents.header.layer = world_cache::layer_kind::logic;
|
|
contents.package.push_back({world_logic_cache::tape_tag, std::vector<std::byte>(100'000, std::byte{7})});
|
|
contents.package.push_back({world_logic_cache::tape_tag, std::vector<std::byte>(10, std::byte{9})});
|
|
std::ostringstream output;
|
|
REQUIRE(world_cache::write(output, contents).empty());
|
|
std::istringstream input{output.str()};
|
|
world_cache::reader cache;
|
|
REQUIRE(cache.open(input).empty());
|
|
REQUIRE(cache.package().size() == 2);
|
|
std::vector<std::byte> data;
|
|
for (std::size_t k = 0; k < 2; ++k) {
|
|
REQUIRE(cache.read_package_block(input, cache.package()[k], data).empty());
|
|
CHECK(data == contents.package[k].data);
|
|
}
|
|
}
|
|
|
|
TEST_CASE("the logic cache is read back while it's current, and refused when the build, a file or a setting changed") {
|
|
test::scratch_folder const folder{"logic_cache"};
|
|
folder.write("main.scn", "node 1 0 a model 0 0 0 none 1 endmodel\ninclude part.inc end\n");
|
|
folder.write("part.inc", "node 2 0 b model 0 0 0 none 2 endmodel\n");
|
|
parser_tape::tape tape;
|
|
{
|
|
cParser input{"main.scn", cParser::buffer_FILE, folder.path()};
|
|
input.startRecording(tape);
|
|
while (!input.getToken<std::string>().empty()) {
|
|
}
|
|
}
|
|
REQUIRE(tape.files.size() >= 2);
|
|
world_logic_cache::load_settings settings;
|
|
auto const cachefile{folder.path("main.logic.eu07")};
|
|
constexpr std::uint64_t engine{0x1234};
|
|
REQUIRE(world_logic_cache::write(cachefile, tape, engine, settings).empty());
|
|
|
|
parser_tape::tape back;
|
|
CHECK(world_logic_cache::read(cachefile, engine, settings, back).empty());
|
|
CHECK(back == tape);
|
|
|
|
CHECK_FALSE(world_logic_cache::read(cachefile, engine + 1, settings, back).empty());
|
|
auto other{settings};
|
|
other.load_traction = false;
|
|
CHECK_FALSE(world_logic_cache::read(cachefile, engine, other, back).empty());
|
|
other = settings;
|
|
other.trainset_overrides = "3=trainset x endtrainset;";
|
|
CHECK_FALSE(world_logic_cache::read(cachefile, engine, other, back).empty());
|
|
// an included file changed
|
|
folder.write("part.inc", "node 2 0 b model 0 0 0 none 22 endmodel\n");
|
|
CHECK_FALSE(world_logic_cache::read(cachefile, engine, settings, back).empty());
|
|
CHECK_FALSE(world_logic_cache::read(folder.path("missing.logic.eu07"), engine, settings, back).empty());
|
|
}
|