mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-05 16:55:32 +01:00
ba17623970
- An include is put into the list of files of the tape when it is opened, whether it gives tokens or not: an include of includes, an empty one and one that failed to open now make the cache old when they change or appear (parser_tape::writer::file). - The terrain directive puts its .txtf and .btf files, and the .sbt it names, into the list (cParser::recordFile), and file.binary.terrain is one of the settings of the cache, so switching it or editing a terrain file records the tape anew. - The cache keeps the Rainsted marks in its name: $td.scn holds other trainsets and time than td.scn, and gets a cache of its own. - The state of a file is looked up in the case it has on the disk, as the parser opens it, so a case-sensitive file system follows changes.
157 lines
5.6 KiB
C++
157 lines
5.6 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"
|
|
#include "utilities/utilities.h"
|
|
#include "utilities/utilities.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. the parser names the files in lowercase and
|
|
// opens them in the case they have on a case-sensitive file system, and so is the file looked at here
|
|
std::pair<std::uint64_t, std::uint64_t> file_state(std::string const &Name) {
|
|
std::error_code error;
|
|
std::filesystem::path path{Name};
|
|
if (!std::filesystem::exists(path, error)) {
|
|
if (auto const cased{FileCased(path)}; !cased.empty())
|
|
path = cased;
|
|
}
|
|
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) {
|
|
// the Rainsted marks stay: a scenario written with them holds other trainsets and time than the one without
|
|
auto name{Scenariofile};
|
|
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(hash, Settings.file_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, std::string const &Tape, std::vector<std::string> const &Files, std::uint64_t const Engine, load_settings const &Settings) {
|
|
auto const &text{Tape};
|
|
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(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, std::string &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::size_t total{0};
|
|
for (auto const &entry : cache.package())
|
|
total += entry.tag == tape_tag ? entry.raw_size : 0;
|
|
text.reserve(total);
|
|
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::vector<std::string> files;
|
|
std::size_t entries{0};
|
|
if (!parser_tape::check(text, files, entries))
|
|
return "the tape doesn't read";
|
|
if (cache.header().source_hash != source_hash(files, Settings))
|
|
return "the sources or the settings changed";
|
|
Tape = std::move(text);
|
|
return {};
|
|
}
|
|
|
|
} // namespace world_logic_cache
|