diff --git a/simulation/simulationstateserializer.cpp b/simulation/simulationstateserializer.cpp index 449a11aec..bec32f107 100644 --- a/simulation/simulationstateserializer.cpp +++ b/simulation/simulationstateserializer.cpp @@ -63,6 +63,90 @@ std::string catenary_file( cParser const &Input ) { } // namespace +namespace { + +// the transformations a statement is read with +world_logic_cache::context +transformations_of( scene::scratch_data::location_data const &Location ) { + + world_logic_cache::context context; + context.has_offset = ( false == Location.offset.empty() ); + if( context.has_offset ) { + context.offset_x = Location.offset.top().x; + context.offset_y = Location.offset.top().y; + context.offset_z = Location.offset.top().z; + } + context.has_scale = ( false == Location.scale.empty() ); + if( context.has_scale ) { + context.scale_x = Location.scale.top().x; + context.scale_y = Location.scale.top().y; + context.scale_z = Location.scale.top().z; + } + context.rotation_x = Location.rotation.x; + context.rotation_y = Location.rotation.y; + context.rotation_z = Location.rotation.z; + return context; +} + +// sets the transformations a statement was read with, for a statement read out of the order of the file +void +restore_transformations( scene::scratch_data::location_data &Location, world_logic_cache::context const &Context ) { + + Location.offset = {}; + if( Context.has_offset ) { + Location.offset.emplace( Context.offset_x, Context.offset_y, Context.offset_z ); + } + Location.scale = {}; + if( Context.has_scale ) { + Location.scale.emplace( Context.scale_x, Context.scale_y, Context.scale_z ); + } + Location.rotation = { Context.rotation_x, Context.rotation_y, Context.rotation_z }; +} + +// the order to replay the statements in, asked for by EU07_WORLD_REPLAY_ORDER: "file", the order of the file, by seeks on the tape; +// "regions", the statements of the whole package first, then those of each region; "regions-first", the regions first. none with no variable or no table: then the tape +// plays through as it was recorded +std::vector +replay_order( world_logic_cache::statement_table const &Table ) { + + std::vector order; + auto const *asked { std::getenv( "EU07_WORLD_REPLAY_ORDER" ) }; + if( asked == nullptr || Table.statements.empty() ) { + return order; + } + std::string_view const mode { asked }; + if( mode == "file" ) { + for( std::size_t index = 0; index < Table.statements.size(); ++index ) { + order.push_back( index ); + } + } + else if( mode == "regions" || mode == "regions-first" ) { + // the statements of the whole package ahead of the regions, or after them + std::vector package; + std::map> regions; + for( std::size_t index = 0; index < Table.statements.size(); ++index ) { + if( Table.statements[ index ].region == 0 ) { + package.push_back( index ); + } + else { + regions[ Table.statements[ index ].region ].push_back( index ); + } + } + if( mode == "regions" ) { + order = package; + } + for( auto const &[ region, indices ] : regions ) { + order.insert( order.end(), indices.begin(), indices.end() ); + } + if( mode == "regions-first" ) { + order.insert( order.end(), package.begin(), package.end() ); + } + } + return order; +} + +} // namespace + // the hash of this build for the logic cache: the tokens the loader asks for may change with any version std::uint64_t state_serializer::logic_cache_engine() { @@ -89,10 +173,18 @@ state_serializer::make_deserializer_state( std::string const &Scenariofile, bool } auto const cachefile { FileOnDisk( world_logic_cache::file_name( Global.asCurrentSceneryPath, Scenariofile ) ) }; std::string tape; - if( auto const reason { world_logic_cache::read( cachefile, logic_cache_engine(), settings, tape ) }; true == reason.empty() ) { + world_logic_cache::statement_table table; + std::vector files; + if( auto const reason { world_logic_cache::read( cachefile, logic_cache_engine(), settings, tape, table, files ) }; true == reason.empty() ) { WriteLog( "Logic cache current: \"" + cachefile + "\", the scenery is replayed from it" ); auto state { std::make_shared( Scenariofile, std::move( tape ) ) }; state->cachesettings = std::move( settings ); + state->order = replay_order( table ); + if( false == state->order.empty() ) { + WriteLog( "Logic cache: " + std::to_string( state->order.size() ) + " statements replayed in the order of EU07_WORLD_REPLAY_ORDER=" + std::getenv( "EU07_WORLD_REPLAY_ORDER" ) ); + } + state->replaytable = std::move( table ); + state->tapefiles = std::move( files ); return state; } else { @@ -214,6 +306,52 @@ state_serializer::deserialize_begin( std::string const &Scenariofile ) { } // continues deserialization for given context, amount limited by time, returns true if needs to be called again +// the statement the keyword begins: the deserializer of the keyword, the catenary, the default skin of its suspensions, or a fault +void +state_serializer::deserialize_statement( deserializer_state &State, std::string const &Token ) { + + auto &Input { State.input }; + auto lookup = State.functionmap.find( Token ); + if( lookup != State.functionmap.end() ) { + lookup->second(); + } + else if( Token == "catenary" ) { + // sieć trakcyjna: plik z podwieszeniami, "catenary endcatenary" + catenary_scene_load( Input ); + } + else if( Token == "skin" ) { + // domyślna tekstura podwieszeń sieci z tego pliku: plik sieci z dyrektywy "catenary" dołączony jako warstwa + catenary_scene_skin( Input, catenary_file( Input ) ); + } + else { + ErrorLog( "Bad scenario: unexpected token \"" + Token + "\" defined in file \"" + Input.Name() + "\" (line " + std::to_string( Input.Line() - 1 ) + ")" ); + } +} + +// replays the statements of the tape in the order of the state, each from its own place on the tape with the transformations it was +// read with. returns: true if it has to be called again, false once every statement was read +bool +state_serializer::deserialize_ordered( deserializer_state &State, std::chrono::steady_clock::time_point const Started ) { + + auto const &statements { State.replaytable.statements }; + while( State.ordercursor < State.order.size() ) { + auto const index { State.order[ State.ordercursor++ ] }; + auto const &item { statements[ index ] }; + // the statement ends where the next one in the file begins, the last one ahead of the end mark of the tape + auto const end { index + 1 < statements.size() ? statements[ index + 1 ].offset : State.tape.size() - 1 }; + State.replayer.seek( static_cast( item.offset ), static_cast( end ), item.start, State.tapefiles ); + restore_transformations( State.scratchpad.location, State.replaytable.contexts[ item.context ] ); + if( auto const token { State.input.getToken() }; false == token.empty() ) { + deserialize_statement( State, token ); + } + if( std::chrono::duration_cast( std::chrono::steady_clock::now() - Started ).count() >= 200 ) { + Application.set_progress( static_cast( 100 * State.ordercursor / State.order.size() ), static_cast( 100 * State.ordercursor / State.order.size() ) ); + return true; + } + } + return false; +} + bool state_serializer::deserialize_continue(std::shared_ptr state) { cParser &Input = state->input; @@ -221,57 +359,37 @@ state_serializer::deserialize_continue(std::shared_ptr state // deserialize content from the provided input auto timelast { std::chrono::steady_clock::now() }; + if( false == state->order.empty() ) { + // the statements of the tape one by one, in the order asked for (EU07_WORLD_REPLAY_ORDER) + if( true == deserialize_ordered( *state, timelast ) ) { + return true; + } + } + else { // where the statement begins on the tape being recorded, and where its first node went auto statementstart { state->recording ? state->recorder.position() : 0 }; + auto statementstate { state->recording ? state->recorder.last_state() : parser_tape::state {} }; std::string token { Input.getToken() }; while( false == token.empty() ) { world_logic_cache::context context; if( true == state->recording ) { simulation::Region->take_insert(); - // the transformations the statement is read with - auto const &location { Scratchpad.location }; - context.has_offset = ( false == location.offset.empty() ); - if( context.has_offset ) { - context.offset_x = location.offset.top().x; - context.offset_y = location.offset.top().y; - context.offset_z = location.offset.top().z; - } - context.has_scale = ( false == location.scale.empty() ); - if( context.has_scale ) { - context.scale_x = location.scale.top().x; - context.scale_y = location.scale.top().y; - context.scale_z = location.scale.top().z; - } - context.rotation_x = location.rotation.x; - context.rotation_y = location.rotation.y; - context.rotation_z = location.rotation.z; - } - auto lookup = state->functionmap.find( token ); - if( lookup != state->functionmap.end() ) { - lookup->second(); - } - else if( token == "catenary" ) { - // sieć trakcyjna: plik z podwieszeniami, "catenary endcatenary" - catenary_scene_load( Input ); - } - else if( token == "skin" ) { - // domyślna tekstura podwieszeń sieci z tego pliku: plik sieci z dyrektywy "catenary" dołączony jako warstwa - catenary_scene_skin( Input, catenary_file( Input ) ); - } - else { - ErrorLog( "Bad scenario: unexpected token \"" + token + "\" defined in file \"" + Input.Name() + "\" (line " + std::to_string( Input.Line() - 1 ) + ")" ); + context = transformations_of( Scratchpad.location ); } + deserialize_statement( *state, token ); if( true == state->recording ) { world_logic_cache::statement item; item.offset = statementstart; + item.start = statementstate; if( auto const location { simulation::Region->take_insert() }; location.has_value() ) { item.region = world_logic_cache::statement_region( location->x, location->z ); } item.context = state->statements.context_index( context ); state->statements.statements.push_back( item ); statementstart = state->recorder.position(); + statementstate = state->recorder.last_state(); } auto timenow = std::chrono::steady_clock::now(); @@ -282,6 +400,7 @@ state_serializer::deserialize_continue(std::shared_ptr state token = Input.getToken(); } + } if( false == Scratchpad.initialized ) { // manually perform scenario initialization diff --git a/simulation/simulationstateserializer.h b/simulation/simulationstateserializer.h index 7e67bc9b7..21a5bb904 100644 --- a/simulation/simulationstateserializer.h +++ b/simulation/simulationstateserializer.h @@ -23,6 +23,12 @@ struct deserializer_state { parser_tape::writer recorder; // the statements of the scenery on the tape being recorded, with their regions world_logic_cache::statement_table statements; + // replaying statement by statement in another order than the tape's (EU07_WORLD_REPLAY_ORDER): the table of the cache, the names + // of the files of the tape, the order and the place in it + world_logic_cache::statement_table replaytable; + std::vector tapefiles; + std::vector order; + std::size_t ordercursor { 0 }; std::string tape; parser_tape::reader replayer; bool recording { false }; @@ -75,6 +81,10 @@ private: static std::uint64_t logic_cache_engine(); // the state of a scenery load: from its files, or from the tape of its logic cache (world.binary) static std::shared_ptr make_deserializer_state( std::string const &Scenariofile, bool const Binaryterrain ); + // the statement the keyword begins + void deserialize_statement( deserializer_state &State, std::string const &Token ); + // replays the statements of the tape in the order of the state + bool deserialize_ordered( deserializer_state &State, std::chrono::steady_clock::time_point const Started ); // restores class data from provided stream void deserialize_area( cParser &Input, scene::scratch_data &Scratchpad ); void deserialize_isolated( cParser &Input, scene::scratch_data &Scratchpad ); diff --git a/utilities/parsertape.cpp b/utilities/parsertape.cpp index 768c9f8bb..ef4dc0fbd 100644 --- a/utilities/parsertape.cpp +++ b/utilities/parsertape.cpp @@ -10,6 +10,8 @@ http://mozilla.org/MPL/2.0/. #include "stdafx.h" #include "utilities/parsertape.h" +#include + namespace parser_tape { namespace { @@ -227,13 +229,21 @@ std::string const &reader::file_name(std::uint32_t const Index) const { 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(entry_kind::file)) { - m_files.emplace_back(input.text()); + 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(entry_kind::end)) { @@ -260,6 +270,16 @@ void reader::decode() { 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 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; diff --git a/utilities/parsertape.h b/utilities/parsertape.h index a6030fac2..83becbf84 100644 --- a/utilities/parsertape.h +++ b/utilities/parsertape.h @@ -71,6 +71,8 @@ public: std::size_t entries() const { return m_entries; } // how many bytes the tape has so far: the place of the entry written next std::size_t position() const { return m_bytes.size(); } + // the state after the token written last: what a reader has there + state const &last_state() const { return m_previous; } // the names of the files the tape refers to, in the order of their indices std::vector const &files() const { return m_files; } @@ -96,6 +98,9 @@ public: std::string_view next_text() const { return m_nexttext; } // moves past the next entry; past a token its state becomes the current one void pop(); + // moves to the entry at the offset, with the state a reader has there and the names of all the files of the tape (check() gives + // them), and reads only up to the end offset; for replaying the statements of a tape out of their order + void seek(std::size_t Offset, std::size_t End, state const &State, std::vector const &Files); // the state after the token popped last, or before the first one state const ¤t() const { return m_current; } std::string const &file_name(std::uint32_t Index) const; @@ -112,6 +117,10 @@ private: std::string_view m_bytes; std::size_t m_position{0}; + // the end of the part being read, the end of the bytes unless seek() set it + std::size_t m_end{static_cast(-1)}; + // the table of files was given whole by seek(): the file entries met on the way add nothing to it + bool m_fulltable{false}; std::vector m_files; entry_kind m_next{entry_kind::end}; std::string_view m_nexttext; diff --git a/world/worldlogiccache.cpp b/world/worldlogiccache.cpp index e03fc4df2..4aad646ac 100644 --- a/world/worldlogiccache.cpp +++ b/world/worldlogiccache.cpp @@ -131,6 +131,41 @@ bool get_context(std::string_view const Bytes, std::size_t &Position, context &C && get_bits(Bytes, Position, Context.rotation_x) && get_bits(Bytes, Position, Context.rotation_y) && get_bits(Bytes, Position, Context.rotation_z); } +std::uint64_t zigzag(std::int64_t const Value) { + return (static_cast(Value) << 1) ^ static_cast(Value >> 63); +} + +std::int64_t unzigzag(std::uint64_t const Raw) { + return static_cast(Raw >> 1) ^ -static_cast(Raw & 1); +} + +void put_tape_state(std::string &Output, parser_tape::state const &State) { + put_varint(Output, State.file); + put_varint(Output, State.line); + put_varint(Output, zigzag(State.line_main)); + put_varint(Output, zigzag(State.begin)); + put_varint(Output, zigzag(State.end)); + Output.push_back(State.layer_file ? 1 : 0); +} + +bool get_tape_state(std::string_view const Bytes, std::size_t &Position, parser_tape::state &State) { + std::uint64_t file{0}; + std::uint64_t line{0}; + std::uint64_t main{0}; + std::uint64_t begin{0}; + std::uint64_t end{0}; + if (!get_varint(Bytes, Position, file) || !get_varint(Bytes, Position, line) || !get_varint(Bytes, Position, main) || !get_varint(Bytes, Position, begin) + || !get_varint(Bytes, Position, end) || Position >= Bytes.size() || file > 0xFFFF'FFFFULL) + return false; + State.file = static_cast(file); + State.line = static_cast(line); + State.line_main = static_cast(unzigzag(main)); + State.begin = static_cast(unzigzag(begin)); + State.end = static_cast(unzigzag(end)); + State.layer_file = Bytes[Position++] != 0; + return true; +} + bool get_statement(std::string_view const Bytes, std::size_t &Position, std::uint64_t &Offset, std::size_t const Contexts, statement &Item) { std::uint64_t delta{0}; std::uint64_t index{0}; @@ -139,7 +174,7 @@ bool get_statement(std::string_view const Bytes, std::size_t &Position, std::uin Offset += delta; Item.offset = Offset; Item.context = static_cast(index); - return true; + return get_tape_state(Bytes, Position, Item.start); } } // namespace @@ -161,6 +196,7 @@ std::string encode_statements(statement_table const &Table) { put_varint(bytes, item.offset - previous); put_varint(bytes, item.region); put_varint(bytes, item.context); + put_tape_state(bytes, item.start); previous = item.offset; } return bytes; @@ -258,6 +294,12 @@ std::string write(std::string const &Filename, std::string const &Tape, std::vec } std::string read(std::string const &Filename, std::uint64_t const Engine, load_settings const &Settings, std::string &Tape) { + statement_table statements; + std::vector files; + return read(Filename, Engine, Settings, Tape, statements, files); +} + +std::string read(std::string const &Filename, std::uint64_t const Engine, load_settings const &Settings, std::string &Tape, statement_table &Statements, std::vector &Files) { std::ifstream input{std::filesystem::path{Filename}, std::ios::binary}; if (!input) return "no cache"; @@ -274,12 +316,18 @@ std::string read(std::string const &Filename, std::uint64_t const Engine, load_s total += entry.tag == tape_tag ? entry.raw_size : 0; text.reserve(total); std::vector part; + statement_table statements; for (auto const &entry : cache.package()) { - if (entry.tag != tape_tag) + if (entry.tag != tape_tag && entry.tag != statements_tag) continue; if (auto const error{cache.read_package_block(input, entry, part)}; !error.empty()) return error; - text.append(reinterpret_cast(part.data()), part.size()); + if (entry.tag == tape_tag) { + text.append(reinterpret_cast(part.data()), part.size()); + } + else if (!decode_statements(std::string_view{reinterpret_cast(part.data()), part.size()}, statements)) { + return "the statement table doesn't read"; + } } std::vector files; std::size_t entries{0}; @@ -288,6 +336,8 @@ std::string read(std::string const &Filename, std::uint64_t const Engine, load_s if (cache.header().source_hash != source_hash(files, Settings)) return "the sources or the settings changed"; Tape = std::move(text); + Statements = std::move(statements); + Files = std::move(files); return {}; } diff --git a/world/worldlogiccache.h b/world/worldlogiccache.h index 554dfc433..56bd23d13 100644 --- a/world/worldlogiccache.h +++ b/world/worldlogiccache.h @@ -36,6 +36,8 @@ struct statement { std::uint64_t region{0}; // the index of the transformations the statement was read with, in the list of contexts std::uint32_t context{0}; + // the state of the tape at the start of the statement, for a reader moved there (parser_tape::reader::seek) + parser_tape::state start; friend bool operator==(statement const &, statement const &) = default; }; @@ -106,5 +108,7 @@ std::string write(std::string const &Filename, std::string const &Tape, std::vec // reads the bytes of the tape of the cache, if the cache is there and current: of this engine, whole, and made from the files and // settings as they are now. returns the empty string when the tape can be replayed, the reason why not otherwise std::string read(std::string const &Filename, std::uint64_t Engine, load_settings const &Settings, std::string &Tape); +// as above, with the statement table of the cache and the names of the files of the tape, both empty when the cache has no table +std::string read(std::string const &Filename, std::uint64_t Engine, load_settings const &Settings, std::string &Tape, statement_table &Statements, std::vector &Files); } // namespace world_logic_cache