18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 12:25:27 +01:00
Files
maszyna/world/worldlogiccache.cpp
T
maj00r 30f3d70da9 feat(world): the logic cache of a scenery, recorded on the first load and replayed while current (world.binary)
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.
2026-12-02 22:20:40 +01:00

148 lines
5.1 KiB
C++

/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#include "stdafx.h"
#include "world/worldlogiccache.h"
#include <algorithm>
#include <array>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include "scene/regionfile.h"
namespace world_logic_cache {
namespace {
// the bytes of a number, little-endian, for the hash
std::array<char, 8> bytes_of(std::uint64_t const Value) {
std::array<char, 8> bytes{};
for (std::size_t k = 0; k < bytes.size(); ++k)
bytes[k] = static_cast<char>((Value >> (8 * k)) & 0xFFu);
return bytes;
}
std::uint64_t mix(std::uint64_t const Hash, std::uint64_t const Value) {
auto const bytes{bytes_of(Value)};
return fnv1a(std::string_view{bytes.data(), bytes.size()}, Hash);
}
// the size and the time of the last change of the file; all ones when it isn't there
std::pair<std::uint64_t, std::uint64_t> file_state(std::string const &Name) {
std::error_code error;
std::filesystem::path const path{Name};
auto const size{std::filesystem::file_size(path, error)};
if (error)
return {~std::uint64_t{0}, ~std::uint64_t{0}};
auto const time{std::filesystem::last_write_time(path, error)};
if (error)
return {size, ~std::uint64_t{0}};
return {size, static_cast<std::uint64_t>(time.time_since_epoch().count())};
}
} // namespace
std::string file_name(std::string const &Sceneryfolder, std::string const &Scenariofile) {
auto name{Scenariofile};
auto const marks{name.find_first_not_of('$')};
name.erase(0, std::min(marks, name.size()));
if (auto const dot{name.rfind('.')}; dot != std::string::npos && name.find_first_of("/\\", dot) == std::string::npos)
name.erase(dot);
return Sceneryfolder + name + ".logic.eu07";
}
std::uint64_t fnv1a(std::string_view const Bytes, std::uint64_t Hash) {
for (auto const byte : Bytes) {
Hash ^= static_cast<std::uint64_t>(static_cast<unsigned char>(byte));
Hash *= 0x0000'0100'0000'01B3ull;
}
return Hash;
}
std::uint64_t engine_hash(std::string_view const Version) {
return fnv1a(Version);
}
std::uint64_t source_hash(std::vector<std::string> const &Files, load_settings const &Settings) {
auto hash{fnv1a("eu07 logic cache")};
for (auto const &name : Files) {
if (name.empty())
continue;
hash = mix(fnv1a(name, hash), name.size());
auto const [size, time]{file_state(name)};
hash = mix(mix(hash, size), time);
}
hash = mix(hash, Settings.load_traction ? 1 : 0);
hash = mix(hash, Settings.binary_terrain ? 1 : 0);
hash = mix(fnv1a(Settings.prepend, hash), Settings.prepend.size());
hash = mix(fnv1a(Settings.trainset_overrides, hash), Settings.trainset_overrides.size());
return hash;
}
std::string write(std::string const &Filename, parser_tape::tape const &Tape, std::uint64_t const Engine, load_settings const &Settings) {
std::ostringstream bytes;
parser_tape::write(bytes, Tape);
auto const text{bytes.str()};
world_cache::file_contents contents;
contents.header.body = world_geometry::body::plane();
contents.header.layer = world_cache::layer_kind::logic;
contents.header.tile_level = world_geometry::logic_region_level;
contents.header.engine_hash = Engine;
contents.header.source_hash = source_hash(Tape.files, Settings);
for (std::size_t start = 0; start < text.size(); start += tape_part_size) {
auto const length{std::min(tape_part_size, text.size() - start)};
world_cache::package_block block;
block.tag = tape_tag;
block.data.resize(length);
std::memcpy(block.data.data(), text.data() + start, length);
contents.package.push_back(std::move(block));
}
// a failure throws, so the temporary file is removed rather than put in place
return scene::region_file::write_through_temporary(Filename, [&contents](std::ostream &Output) {
if (auto const failure{world_cache::write(Output, contents)}; !failure.empty())
throw std::runtime_error{failure};
});
}
std::string read(std::string const &Filename, std::uint64_t const Engine, load_settings const &Settings, parser_tape::tape &Tape) {
std::ifstream input{std::filesystem::path{Filename}, std::ios::binary};
if (!input)
return "no cache";
world_cache::reader cache;
if (auto const error{cache.open(input)}; !error.empty())
return error;
if (cache.header().layer != world_cache::layer_kind::logic || cache.header().body.kind != world_geometry::body_kind::plane)
return "not a cache of the logic on the plane";
if (cache.header().engine_hash != Engine)
return "made by another build";
std::string text;
std::vector<std::byte> part;
for (auto const &entry : cache.package()) {
if (entry.tag != tape_tag)
continue;
if (auto const error{cache.read_package_block(input, entry, part)}; !error.empty())
return error;
text.append(reinterpret_cast<char const *>(part.data()), part.size());
}
std::istringstream bytes{text};
parser_tape::tape tape;
if (!parser_tape::read(bytes, tape))
return "the tape doesn't read";
if (cache.header().source_hash != source_hash(tape.files, Settings))
return "the sources or the settings changed";
Tape = std::move(tape);
return {};
}
} // namespace world_logic_cache