18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 18:14:53 +01:00
Files
maszyna/utilities/parsertape.cpp
T
maj00r 4657928af2 feat(world): the statements of the logic tape replayed one by one, in another order than the file's, for the check of D4
Toward loading the logic by regions (docs/world-format-eu07.md, section 13, D4). The statement table keeps the
state of the tape at the start of each statement, the tape reader can move to a statement and read only to its
end (parser_tape::reader::seek, with the names of all the files of the tape), and the logic cache gives the
table back. With EU07_WORLD_REPLAY_ORDER set, a scenery replayed from its cache goes statement by statement,
each from its place on the tape with the transformations it was read with: "file" in the order of the file,
"regions" the statements of the whole package ahead of the regions, "regions-first" the regions first. A check
for the developers; without the variable the tape plays through as before.

The loop of deserialize_continue gives its dispatch to deserialize_statement(), and the transformations go
through transformations_of() and restore_transformations().

Checked in the game (--test 120 s, seed 1, world.binary 1):
- "file": the physics logs of td.scn and Glinojeck.scn the same bit for bit as from the files, so the moves on
  the tape and the restored transformations give the very same scenery;
- "regions-first": the same bit for bit too, for both; the error logs hold the same lines in another order (the
  models load in another order);
- "regions": td.scn ends up without vehicles, its trainsets read before the tracks they stand on. The first
  thing D4 has to settle: the statements of the whole package that refer to the regions go after them.
2026-12-03 00:56:49 +01:00

305 lines
9.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 "utilities/parsertape.h"
#include <algorithm>
namespace parser_tape {
namespace {
constexpr std::uint8_t tape_version{2};
// the fields of a token's state which differ from the state before it
constexpr std::uint8_t changed_file{1};
constexpr std::uint8_t changed_line{2};
constexpr std::uint8_t changed_line_main{4};
constexpr std::uint8_t changed_begin{8};
constexpr std::uint8_t changed_end{16};
constexpr std::uint8_t layer_file_bit{32};
void put_varint(std::string &Output, std::uint64_t Value) {
while (Value >= 0x80) {
Output.push_back(static_cast<char>((Value & 0x7F) | 0x80));
Value >>= 7;
}
Output.push_back(static_cast<char>(Value));
}
void put_signed(std::string &Output, std::int64_t const Value) {
// zigzag: the small differences of either sign stay small
put_varint(Output, (static_cast<std::uint64_t>(Value) << 1) ^ static_cast<std::uint64_t>(Value >> 63));
}
void put_string(std::string &Output, std::string_view const Text) {
put_varint(Output, Text.size());
Output.append(Text);
}
// reads the encoded values off the bytes; any read past their end marks the whole reading broken
class decoder {
public:
decoder(std::string_view const Bytes, std::size_t &Position) : m_bytes{Bytes}, m_position{Position} {}
bool ok() const { return m_ok; }
std::uint8_t byte() {
if (m_position >= m_bytes.size()) {
m_ok = false;
return 0;
}
return static_cast<std::uint8_t>(m_bytes[m_position++]);
}
std::uint64_t varint() {
std::uint64_t value{0};
for (int shift = 0; shift < 64 && m_ok; shift += 7) {
auto const next{byte()};
value |= static_cast<std::uint64_t>(next & 0x7F) << shift;
if ((next & 0x80) == 0)
return value;
}
m_ok = false;
return 0;
}
std::int64_t signed_value() {
auto const raw{varint()};
return static_cast<std::int64_t>(raw >> 1) ^ -static_cast<std::int64_t>(raw & 1);
}
std::string_view text() {
auto const length{varint()};
if (!m_ok || length > m_bytes.size() - m_position) {
m_ok = false;
return {};
}
auto const result{m_bytes.substr(m_position, static_cast<std::size_t>(length))};
m_position += static_cast<std::size_t>(length);
return result;
}
private:
std::string_view m_bytes;
std::size_t &m_position;
bool m_ok{true};
};
void put_full_state(std::string &Output, state const &State) {
put_varint(Output, State.line);
put_signed(Output, State.line_main);
put_signed(Output, State.begin);
put_signed(Output, State.end);
Output.push_back(State.layer_file ? 1 : 0);
}
state get_full_state(decoder &Input) {
state result;
result.line = static_cast<std::size_t>(Input.varint());
result.line_main = static_cast<int>(Input.signed_value());
result.begin = static_cast<std::streamoff>(Input.signed_value());
result.end = static_cast<std::streamoff>(Input.signed_value());
result.layer_file = Input.byte() != 0;
return result;
}
// the state of a token, as its differences from the state before
void put_state(std::string &Output, state const &State, state const &Previous, std::size_t const Length) {
std::uint8_t flags{0};
if (State.file != Previous.file)
flags |= changed_file;
if (State.line != Previous.line)
flags |= changed_line;
if (State.line_main != Previous.line_main)
flags |= changed_line_main;
if (State.begin != Previous.end)
flags |= changed_begin;
if (State.end != State.begin + static_cast<std::streamoff>(Length))
flags |= changed_end;
if (State.layer_file)
flags |= layer_file_bit;
Output.push_back(static_cast<char>(flags));
if ((flags & changed_file) != 0)
put_varint(Output, State.file);
if ((flags & changed_line) != 0)
put_signed(Output, static_cast<std::int64_t>(State.line) - static_cast<std::int64_t>(Previous.line));
if ((flags & changed_line_main) != 0)
put_signed(Output, static_cast<std::int64_t>(State.line_main) - static_cast<std::int64_t>(Previous.line_main));
if ((flags & changed_begin) != 0)
put_signed(Output, static_cast<std::int64_t>(State.begin - Previous.end));
if ((flags & changed_end) != 0)
put_signed(Output, static_cast<std::int64_t>(State.end - State.begin));
}
state get_state(decoder &Input, state const &Previous, std::size_t const Length, std::size_t const Files) {
auto const flags{Input.byte()};
state result{Previous};
result.begin = Previous.end;
result.layer_file = (flags & layer_file_bit) != 0;
if ((flags & changed_file) != 0) {
// an index beyond the table of files is a broken tape, which the reader tells by the index
auto const file{Input.varint()};
result.file = file < Files ? static_cast<std::uint32_t>(file) : static_cast<std::uint32_t>(Files);
}
if ((flags & changed_line) != 0)
result.line = static_cast<std::size_t>(static_cast<std::int64_t>(Previous.line) + Input.signed_value());
if ((flags & changed_line_main) != 0)
result.line_main = static_cast<int>(Previous.line_main + Input.signed_value());
if ((flags & changed_begin) != 0)
result.begin = Previous.end + static_cast<std::streamoff>(Input.signed_value());
result.end = result.begin + static_cast<std::streamoff>(Length);
if ((flags & changed_end) != 0)
result.end = result.begin + static_cast<std::streamoff>(Input.signed_value());
return result;
}
} // namespace
void writer::begin(std::string_view const File, state const &Start) {
m_bytes.clear();
m_files.clear();
m_fileindices.clear();
m_entries = 0;
m_bytes.push_back(static_cast<char>(tape_version));
// the state first, then the name of its file as the first file entry, index 0
put_full_state(m_bytes, Start);
auto start{Start};
start.file = file_index(File);
m_previous = start;
}
std::uint32_t writer::file_index(std::string_view const Name) {
auto const [found, added]{m_fileindices.try_emplace(std::string{Name}, static_cast<std::uint32_t>(m_files.size()))};
if (added) {
m_files.emplace_back(Name);
m_bytes.push_back(static_cast<char>(entry_kind::file));
put_string(m_bytes, Name);
}
return found->second;
}
void writer::token(std::string_view const Token, std::string_view const File, state State) {
State.file = file_index(File);
m_bytes.push_back(static_cast<char>(entry_kind::token));
put_string(m_bytes, Token);
put_state(m_bytes, State, m_previous, Token.size());
m_previous = State;
++m_entries;
}
void writer::side_effect(entry_kind const Kind) {
m_bytes.push_back(static_cast<char>(Kind));
++m_entries;
}
void writer::text(std::string_view const Text) {
m_bytes.push_back(static_cast<char>(entry_kind::text));
put_string(m_bytes, Text);
++m_entries;
}
std::string writer::finish() {
m_bytes.push_back(static_cast<char>(entry_kind::end));
return std::move(m_bytes);
}
reader::reader(std::string_view const Bytes) : m_bytes{Bytes} {
decoder input{m_bytes, m_position};
if (input.byte() != tape_version) {
m_broken = true;
return;
}
// the name of the file of the start comes right after it, as the first file entry
m_current = get_full_state(input);
if (!input.ok()) {
m_broken = true;
return;
}
decode();
}
std::string const &reader::file_name(std::uint32_t const Index) const {
static std::string const none;
return Index < m_files.size() ? m_files[Index] : none;
}
void reader::decode() {
m_next = entry_kind::end;
m_nexttext = {};
if (m_position >= m_end) {
// the end of the part seek() set
return;
}
while (!m_broken && m_position < m_bytes.size()) {
decoder input{m_bytes, m_position};
auto const kind{input.byte()};
if (kind == static_cast<std::uint8_t>(entry_kind::file)) {
auto const name{input.text()};
if (!m_fulltable)
m_files.emplace_back(name);
if (!input.ok())
m_broken = true;
if (m_position >= m_end)
return;
continue;
}
if (kind > static_cast<std::uint8_t>(entry_kind::end)) {
m_broken = true;
return;
}
m_next = static_cast<entry_kind>(kind);
if (m_next == entry_kind::end) {
// the end mark has to be the last byte
m_broken = m_position != m_bytes.size();
return;
}
if (m_next == entry_kind::token || m_next == entry_kind::text)
m_nexttext = input.text();
if (m_next == entry_kind::token)
m_nextstate = get_state(input, m_current, m_nexttext.size(), m_files.size());
if (!input.ok() || (m_next == entry_kind::token && m_nextstate.file >= m_files.size())) {
m_broken = true;
m_next = entry_kind::end;
}
return;
}
if (!m_broken)
m_broken = true; // the bytes ran out before the end mark
}
void reader::seek(std::size_t const Offset, std::size_t const End, state const &State, std::vector<std::string> const &Files) {
m_position = std::min(Offset, m_bytes.size());
m_end = std::min(End, m_bytes.size());
m_current = State;
m_files = Files;
m_fulltable = true;
m_broken = false;
decode();
}
void reader::pop() {
if (m_next == entry_kind::end)
return;
if (m_next == entry_kind::token)
m_current = m_nextstate;
decode();
}
bool check(std::string_view const Bytes, std::vector<std::string> &Files, std::size_t &Entries) {
reader tape{Bytes};
Entries = 0;
while (tape.next_kind() != entry_kind::end) {
tape.pop();
++Entries;
}
if (tape.broken())
return false;
Files = tape.files();
return true;
}
} // namespace parser_tape