mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-07-22 16:19:19 +02:00
Add binary scenery format (eu7 v3) with async baking, replacing SBT
Text scenery component files (.scn/.inc/.scm/.ctr) are compiled into per-file binary twins (.scnb/.incb/.scmb/.ctrb), handled transparently at the cParser layer: a fresh twin (mtime-checked, version-matched) is replayed instead of re-tokenizing text; otherwise the text is parsed and a twin is compiled alongside it for next time. Format details: - per-file string interning: keywords/paths stored once, referenced by varint index (so node/endmodel/... are not repeated as text) - numeric tokens stored as 8-byte IEEE doubles, not ASCII - includes kept as references with parameters; random sets stored verbatim and re-evaluated on every load (choice not frozen at compile time) - twin writing offloaded to a bounded thread pool so baking overlaps scene construction instead of blocking the load The legacy terrain-only .sbt path is removed; terrain now loads as ordinary scenery content. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
120
scene/scene.cpp
120
scene/scene.cpp
@@ -24,9 +24,6 @@ http://mozilla.org/MPL/2.0/.
|
||||
|
||||
namespace scene {
|
||||
|
||||
std::string const EU07_FILEEXTENSION_REGION { ".sbt" };
|
||||
std::uint32_t const EU07_FILEHEADER { MAKE_ID4( 'E','U','0','7' ) };
|
||||
std::uint32_t const EU07_FILEVERSION_REGION { MAKE_ID4( 'S', 'B', 'T', '2' ) };
|
||||
std::map<std::string, basic_node *> Hierarchy;
|
||||
|
||||
// potentially activates event handler with the same name as provided node, and within handler activation range
|
||||
@@ -1105,123 +1102,6 @@ basic_region::update_traction( TDynamicObject *Vehicle, int const Pantographinde
|
||||
}
|
||||
}
|
||||
|
||||
// checks whether specified file is a valid region data file
|
||||
bool
|
||||
basic_region::is_scene( std::string const &Scenariofile ) const {
|
||||
|
||||
auto filename { Scenariofile };
|
||||
while( filename[ 0 ] == '$' ) {
|
||||
// trim leading $ char rainsted utility may add to the base name for modified .scn files
|
||||
filename.erase( 0, 1 );
|
||||
}
|
||||
erase_extension( filename );
|
||||
filename = Global.asCurrentSceneryPath + filename;
|
||||
filename += EU07_FILEEXTENSION_REGION;
|
||||
|
||||
if( false == FileExists( filename ) ) {
|
||||
return false;
|
||||
}
|
||||
// file type and version check
|
||||
std::ifstream input( filename, std::ios::binary );
|
||||
|
||||
uint32_t headermain{ sn_utils::ld_uint32( input ) };
|
||||
uint32_t headertype{ sn_utils::ld_uint32( input ) };
|
||||
|
||||
if( ( headermain != EU07_FILEHEADER
|
||||
|| ( headertype != EU07_FILEVERSION_REGION ) ) ) {
|
||||
// wrong file type
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// stores content of the class in file with specified name
|
||||
void
|
||||
basic_region::serialize( std::string const &Scenariofile ) const {
|
||||
|
||||
auto filename { Scenariofile };
|
||||
while( filename[ 0 ] == '$' ) {
|
||||
// trim leading $ char rainsted utility may add to the base name for modified .scn files
|
||||
filename.erase( 0, 1 );
|
||||
}
|
||||
erase_extension( filename );
|
||||
filename = Global.asCurrentSceneryPath + filename;
|
||||
filename += EU07_FILEEXTENSION_REGION;
|
||||
|
||||
std::ofstream output { filename, std::ios::binary };
|
||||
|
||||
// region file version 1
|
||||
// header: EU07SBT + version (0-255)
|
||||
sn_utils::ls_uint32( output, EU07_FILEHEADER );
|
||||
sn_utils::ls_uint32( output, EU07_FILEVERSION_REGION );
|
||||
// sections
|
||||
// TBD, TODO: build table of sections and file offsets, if we postpone section loading until they're within range
|
||||
std::uint32_t sectioncount { 0 };
|
||||
for( auto *section : m_sections ) {
|
||||
if( section != nullptr ) {
|
||||
++sectioncount;
|
||||
}
|
||||
}
|
||||
// section count, followed by section data
|
||||
sn_utils::ls_uint32( output, sectioncount );
|
||||
std::uint32_t sectionindex { 0 };
|
||||
for( auto *section : m_sections ) {
|
||||
// section data: section index, followed by length of section data, followed by section data
|
||||
if( section != nullptr ) {
|
||||
sn_utils::ls_uint32( output, sectionindex );
|
||||
section->serialize( output ); }
|
||||
++sectionindex;
|
||||
}
|
||||
}
|
||||
|
||||
// restores content of the class from file with specified name. returns: true on success, false otherwise
|
||||
bool
|
||||
basic_region::deserialize( std::string const &Scenariofile ) {
|
||||
|
||||
auto filename { Scenariofile };
|
||||
while( filename[ 0 ] == '$' ) {
|
||||
// trim leading $ char rainsted utility may add to the base name for modified .scn files
|
||||
filename.erase( 0, 1 );
|
||||
}
|
||||
erase_extension( filename );
|
||||
filename = Global.asCurrentSceneryPath + filename;
|
||||
filename += EU07_FILEEXTENSION_REGION;
|
||||
|
||||
if( false == FileExists( filename ) ) {
|
||||
Global.file_binary_terrain_state = false;
|
||||
return false;
|
||||
}
|
||||
// region file version 1
|
||||
// file type and version check
|
||||
std::ifstream input( filename, std::ios::binary );
|
||||
|
||||
uint32_t headermain { sn_utils::ld_uint32( input ) };
|
||||
uint32_t headertype { sn_utils::ld_uint32( input ) };
|
||||
|
||||
if( ( headermain != EU07_FILEHEADER
|
||||
|| ( headertype != EU07_FILEVERSION_REGION ) ) ) {
|
||||
// wrong file type
|
||||
WriteLog( "Bad file: \"" + filename + "\" is of either unrecognized type or version" );
|
||||
return false;
|
||||
}
|
||||
// sections
|
||||
// TBD, TODO: build table of sections and file offsets, if we postpone section loading until they're within range
|
||||
// section count
|
||||
auto sectioncount { sn_utils::ld_uint32( input ) };
|
||||
while( sectioncount-- ) {
|
||||
// section index, followed by section data size, followed by section data
|
||||
auto const sectionindex { sn_utils::ld_uint32( input ) };
|
||||
auto const sectionsize { sn_utils::ld_uint32( input ) };
|
||||
if( m_sections[ sectionindex ] == nullptr ) {
|
||||
m_sections[ sectionindex ] = new basic_section();
|
||||
}
|
||||
m_sections[ sectionindex ]->deserialize( input );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// sends content of the class in legacy (text) format to provided stream
|
||||
void
|
||||
basic_region::export_as_text( std::ostream &Output ) const {
|
||||
|
||||
@@ -37,12 +37,6 @@ int constexpr EU07_REGIONSIDESECTIONCOUNT = 500; // number of sections along a s
|
||||
|
||||
struct scratch_data {
|
||||
|
||||
struct binary_data {
|
||||
|
||||
bool terrain{ false };
|
||||
bool terrain_included{false};
|
||||
} binary;
|
||||
|
||||
struct location_data {
|
||||
|
||||
std::stack<glm::dvec3> offset;
|
||||
@@ -68,7 +62,6 @@ struct scratch_data {
|
||||
} trainset;
|
||||
|
||||
std::string name;
|
||||
std::string terrain_name;
|
||||
|
||||
bool initialized { false };
|
||||
bool time_initialized { false };
|
||||
@@ -386,15 +379,6 @@ public:
|
||||
// legacy method, updates sounds around camera
|
||||
void
|
||||
update_sounds();
|
||||
// checks whether specified file is a valid region data file
|
||||
bool
|
||||
is_scene( std::string const &Scenariofile ) const;
|
||||
// stores content of the class in file with specified name
|
||||
void
|
||||
serialize( std::string const &Scenariofile ) const;
|
||||
// restores content of the class from file with specified name. returns: true on success, false otherwise
|
||||
bool
|
||||
deserialize( std::string const &Scenariofile );
|
||||
// sends content of the class in legacy (text) format to provided stream
|
||||
void
|
||||
export_as_text( std::ostream &Output ) const;
|
||||
|
||||
317
scene/scenerybinary.cpp
Normal file
317
scene/scenerybinary.cpp
Normal file
@@ -0,0 +1,317 @@
|
||||
/*
|
||||
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 "scene/scenerybinary.h"
|
||||
|
||||
#include "scene/sn_utils.h"
|
||||
#include "utilities/utilities.h" // ToLower
|
||||
#include "utilities/Logs.h"
|
||||
|
||||
#include <unordered_map>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include <queue>
|
||||
#include <functional>
|
||||
#include <fstream>
|
||||
|
||||
namespace scene {
|
||||
|
||||
namespace {
|
||||
|
||||
// LEB128 unsigned varint: 1 byte for values < 128 (covers most string-table indices),
|
||||
// keeping keyword/index references compact.
|
||||
void write_varint( std::ostream &Output, std::uint64_t Value ) {
|
||||
do {
|
||||
std::uint8_t byte = Value & 0x7F;
|
||||
Value >>= 7;
|
||||
if( Value != 0 ) { byte |= 0x80; }
|
||||
Output.put( static_cast<char>( byte ) );
|
||||
} while( Value != 0 );
|
||||
}
|
||||
|
||||
std::uint64_t read_varint( std::istream &Input ) {
|
||||
std::uint64_t value = 0;
|
||||
int shift = 0;
|
||||
while( shift < 64 ) {
|
||||
int const c = Input.get();
|
||||
if( c == std::char_traits<char>::eof() ) { break; }
|
||||
value |= ( static_cast<std::uint64_t>( c & 0x7F ) << shift );
|
||||
if( ( c & 0x80 ) == 0 ) { break; }
|
||||
shift += 7;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
// builds the per-file string table while interning, returning each string's index
|
||||
class string_interner {
|
||||
public:
|
||||
std::uint32_t intern( std::string const &Text ) {
|
||||
auto const it = m_lookup.find( Text );
|
||||
if( it != m_lookup.end() ) { return it->second; }
|
||||
auto const index = static_cast<std::uint32_t>( m_table.size() );
|
||||
m_lookup.emplace( Text, index );
|
||||
m_table.emplace_back( Text );
|
||||
return index;
|
||||
}
|
||||
std::vector<std::string> const &table() const { return m_table; }
|
||||
private:
|
||||
std::unordered_map<std::string, std::uint32_t> m_lookup;
|
||||
std::vector<std::string> m_table;
|
||||
};
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
void
|
||||
scenery_binary_writer::add_token( std::string Token ) {
|
||||
scenery_entry entry;
|
||||
entry.type = scenery_entry_type::token;
|
||||
entry.text = std::move( Token );
|
||||
m_entries.emplace_back( std::move( entry ) );
|
||||
}
|
||||
|
||||
void
|
||||
scenery_binary_writer::add_number( double Value ) {
|
||||
scenery_entry entry;
|
||||
entry.type = scenery_entry_type::number;
|
||||
entry.number = Value;
|
||||
m_entries.emplace_back( std::move( entry ) );
|
||||
}
|
||||
|
||||
void
|
||||
scenery_binary_writer::add_include( std::vector<std::string> Fileexpr, std::vector<std::string> Params ) {
|
||||
scenery_entry entry;
|
||||
entry.type = scenery_entry_type::include;
|
||||
entry.fileexpr = std::move( Fileexpr );
|
||||
entry.params = std::move( Params );
|
||||
m_entries.emplace_back( std::move( entry ) );
|
||||
}
|
||||
|
||||
bool
|
||||
scenery_binary_writer::write( std::ostream &Output, scenery_file_kind Kind ) const {
|
||||
|
||||
// first pass: intern every string the entries reference so keywords/paths that
|
||||
// repeat are stored once and referenced by index
|
||||
string_interner interner;
|
||||
std::vector<std::uint32_t> tokenindex; // index per token entry, in order
|
||||
std::vector<std::vector<std::uint32_t>> fileexpridx, paramidx; // per include entry
|
||||
for( auto const &entry : m_entries ) {
|
||||
if( entry.type == scenery_entry_type::token ) {
|
||||
tokenindex.emplace_back( interner.intern( entry.text ) );
|
||||
}
|
||||
else if( entry.type == scenery_entry_type::include ) {
|
||||
std::vector<std::uint32_t> fe, pe;
|
||||
for( auto const &token : entry.fileexpr ) { fe.emplace_back( interner.intern( token ) ); }
|
||||
for( auto const ¶m : entry.params ) { pe.emplace_back( interner.intern( param ) ); }
|
||||
fileexpridx.emplace_back( std::move( fe ) );
|
||||
paramidx.emplace_back( std::move( pe ) );
|
||||
}
|
||||
}
|
||||
|
||||
// header: magic + kind + flags
|
||||
sn_utils::ls_uint32( Output, SCENERYBINARY_MAGIC );
|
||||
sn_utils::s_uint8( Output, static_cast<std::uint8_t>( Kind ) );
|
||||
sn_utils::ls_uint32( Output, 0 ); // flags, reserved
|
||||
|
||||
// string table
|
||||
auto const &table = interner.table();
|
||||
sn_utils::ls_uint32( Output, static_cast<std::uint32_t>( table.size() ) );
|
||||
for( auto const &text : table ) {
|
||||
sn_utils::s_str( Output, text );
|
||||
}
|
||||
|
||||
// entries
|
||||
sn_utils::ls_uint32( Output, static_cast<std::uint32_t>( m_entries.size() ) );
|
||||
std::size_t tokencursor = 0, includecursor = 0;
|
||||
for( auto const &entry : m_entries ) {
|
||||
sn_utils::s_uint8( Output, static_cast<std::uint8_t>( entry.type ) );
|
||||
if( entry.type == scenery_entry_type::include ) {
|
||||
auto const &fe = fileexpridx[ includecursor ];
|
||||
auto const &pe = paramidx[ includecursor ];
|
||||
++includecursor;
|
||||
write_varint( Output, fe.size() );
|
||||
for( auto idx : fe ) { write_varint( Output, idx ); }
|
||||
write_varint( Output, pe.size() );
|
||||
for( auto idx : pe ) { write_varint( Output, idx ); }
|
||||
}
|
||||
else if( entry.type == scenery_entry_type::number ) {
|
||||
sn_utils::ls_float64( Output, entry.number );
|
||||
}
|
||||
else {
|
||||
write_varint( Output, tokenindex[ tokencursor++ ] );
|
||||
}
|
||||
}
|
||||
|
||||
return ( false == Output.fail() );
|
||||
}
|
||||
|
||||
bool
|
||||
scenery_binary_reader::open( std::istream &Input ) {
|
||||
|
||||
m_entries.clear();
|
||||
|
||||
auto const magic { sn_utils::ld_uint32( Input ) };
|
||||
if( magic != SCENERYBINARY_MAGIC ) {
|
||||
// unrecognized type or incompatible version
|
||||
return false;
|
||||
}
|
||||
m_kind = static_cast<scenery_file_kind>( sn_utils::d_uint8( Input ) );
|
||||
sn_utils::ld_uint32( Input ); // flags, reserved
|
||||
|
||||
// string table
|
||||
std::vector<std::string> table;
|
||||
auto tablesize { sn_utils::ld_uint32( Input ) };
|
||||
table.reserve( tablesize );
|
||||
while( tablesize-- ) {
|
||||
table.emplace_back( sn_utils::d_str( Input ) );
|
||||
}
|
||||
// resolves an interned index back to its string, guarding against corruption
|
||||
auto const resolve = [ & ]( std::uint64_t index ) -> std::string {
|
||||
return ( index < table.size() ) ? table[ static_cast<std::size_t>( index ) ] : std::string();
|
||||
};
|
||||
|
||||
auto count { sn_utils::ld_uint32( Input ) };
|
||||
m_entries.reserve( count );
|
||||
while( count-- ) {
|
||||
scenery_entry entry;
|
||||
entry.type = static_cast<scenery_entry_type>( sn_utils::d_uint8( Input ) );
|
||||
if( entry.type == scenery_entry_type::include ) {
|
||||
auto expr { read_varint( Input ) };
|
||||
entry.fileexpr.reserve( expr );
|
||||
while( expr-- ) {
|
||||
entry.fileexpr.emplace_back( resolve( read_varint( Input ) ) );
|
||||
}
|
||||
auto params { read_varint( Input ) };
|
||||
entry.params.reserve( params );
|
||||
while( params-- ) {
|
||||
entry.params.emplace_back( resolve( read_varint( Input ) ) );
|
||||
}
|
||||
}
|
||||
else if( entry.type == scenery_entry_type::number ) {
|
||||
entry.number = sn_utils::ld_float64( Input );
|
||||
}
|
||||
else {
|
||||
entry.text = resolve( read_varint( Input ) );
|
||||
}
|
||||
m_entries.emplace_back( std::move( entry ) );
|
||||
}
|
||||
|
||||
return ( false == Input.fail() );
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
// minimal bounded thread pool for offloading twin serialization/writing
|
||||
class bake_pool {
|
||||
public:
|
||||
bake_pool() {
|
||||
unsigned const workers = std::max( 2u, std::min( 8u, std::thread::hardware_concurrency() ) );
|
||||
for( unsigned i = 0; i < workers; ++i ) {
|
||||
m_threads.emplace_back( [ this ] { run(); } );
|
||||
}
|
||||
}
|
||||
~bake_pool() {
|
||||
{ std::unique_lock<std::mutex> lock( m_mutex ); m_stop = true; }
|
||||
m_wake.notify_all();
|
||||
for( auto &thread : m_threads ) { if( thread.joinable() ) { thread.join(); } }
|
||||
}
|
||||
void enqueue( std::function<void()> Task ) {
|
||||
{ std::unique_lock<std::mutex> lock( m_mutex ); m_tasks.emplace( std::move( Task ) ); ++m_pending; }
|
||||
m_wake.notify_one();
|
||||
}
|
||||
void wait_idle() {
|
||||
std::unique_lock<std::mutex> lock( m_mutex );
|
||||
m_idle.wait( lock, [ this ] { return m_pending == 0; } );
|
||||
}
|
||||
private:
|
||||
void run() {
|
||||
for( ;; ) {
|
||||
std::function<void()> task;
|
||||
{
|
||||
std::unique_lock<std::mutex> lock( m_mutex );
|
||||
m_wake.wait( lock, [ this ] { return m_stop || ( false == m_tasks.empty() ); } );
|
||||
if( m_stop && m_tasks.empty() ) { return; }
|
||||
task = std::move( m_tasks.front() );
|
||||
m_tasks.pop();
|
||||
}
|
||||
task();
|
||||
{
|
||||
std::unique_lock<std::mutex> lock( m_mutex );
|
||||
if( --m_pending == 0 ) { m_idle.notify_all(); }
|
||||
}
|
||||
}
|
||||
}
|
||||
std::vector<std::thread> m_threads;
|
||||
std::queue<std::function<void()>> m_tasks;
|
||||
std::mutex m_mutex;
|
||||
std::condition_variable m_wake;
|
||||
std::condition_variable m_idle;
|
||||
bool m_stop { false };
|
||||
std::size_t m_pending { 0 };
|
||||
};
|
||||
|
||||
bake_pool &pool() {
|
||||
static bake_pool instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
// results recorded by worker threads, drained and logged on the main thread
|
||||
std::mutex g_resultmutex;
|
||||
std::vector<std::string> g_resultlog; // success lines, in completion order
|
||||
std::vector<std::string> g_resultfail; // failure paths
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
void
|
||||
scenerybinary_write_async( std::unique_ptr<scenery_binary_writer> Writer, std::string Path, scenery_file_kind Kind ) {
|
||||
// std::function requires a copyable target, so move the writer into a shared_ptr
|
||||
std::shared_ptr<scenery_binary_writer> writer { std::move( Writer ) };
|
||||
pool().enqueue( [ writer, path = std::move( Path ), Kind ] {
|
||||
std::ofstream output( path, std::ios::binary );
|
||||
bool const ok = output.good() && writer->write( output, Kind );
|
||||
output.flush();
|
||||
std::lock_guard<std::mutex> lock( g_resultmutex );
|
||||
if( ok ) {
|
||||
g_resultlog.emplace_back( "Compiled binary scenery: " + path + " (" + std::to_string( writer->entry_count() ) + " entries)" );
|
||||
}
|
||||
else {
|
||||
g_resultfail.emplace_back( path );
|
||||
}
|
||||
} );
|
||||
}
|
||||
|
||||
void
|
||||
scenerybinary_wait_all() {
|
||||
pool().wait_idle();
|
||||
std::lock_guard<std::mutex> lock( g_resultmutex );
|
||||
for( auto const &line : g_resultlog ) { WriteLog( line ); }
|
||||
for( auto const &path : g_resultfail ) { ErrorLog( "Failed to write binary scenery \"" + path + "\"" ); }
|
||||
g_resultlog.clear();
|
||||
g_resultfail.clear();
|
||||
}
|
||||
|
||||
std::string
|
||||
scenerybinary_extension_for( std::string const &Sourcefile ) {
|
||||
|
||||
auto const lower { ToLower( Sourcefile ) };
|
||||
if( lower.size() >= 4 && lower.compare( lower.size() - 4, 4, ".inc" ) == 0 ) {
|
||||
return SCENERYBINARY_EXT_INC;
|
||||
}
|
||||
if( lower.size() >= 4 && lower.compare( lower.size() - 4, 4, ".scm" ) == 0 ) {
|
||||
return SCENERYBINARY_EXT_SCM;
|
||||
}
|
||||
if( lower.size() >= 4 && lower.compare( lower.size() - 4, 4, ".ctr" ) == 0 ) {
|
||||
return SCENERYBINARY_EXT_CTR;
|
||||
}
|
||||
// default: treat as a top-level scenario file (.scn)
|
||||
return SCENERYBINARY_EXT_SCN;
|
||||
}
|
||||
|
||||
} // namespace scene
|
||||
136
scene/scenerybinary.h
Normal file
136
scene/scenerybinary.h
Normal file
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
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/.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <iosfwd>
|
||||
#include <memory>
|
||||
|
||||
#include "utilities/utilities.h" // MAKE_ID4
|
||||
|
||||
// Binary, modular scenery container ("eu7" format, version 1).
|
||||
//
|
||||
// Replaces the legacy text formats (.scn / .scm / .inc) and the old terrain-only
|
||||
// .sbt blob. The mapping is one-to-one and modular: every text file compiles to
|
||||
// exactly one binary twin and includes are preserved as references, so the binary
|
||||
// twin of one file points to the binary twins of the files it includes.
|
||||
// route.scn -> route.scnb
|
||||
// piece.inc -> piece.incb
|
||||
// mesh.scm -> mesh.scmb
|
||||
//
|
||||
// A binary twin stores the source file's own, fully resolved content (comments
|
||||
// stripped, parameters and random sets resolved) as an ordered list of entries.
|
||||
// Numeric tokens are stored as 8-byte IEEE doubles (so coordinates, ranges etc. are
|
||||
// genuinely binary, not ASCII). All string tokens (keywords like "node"/"endmodel",
|
||||
// names, paths) are interned into a per-file string table and referenced from the
|
||||
// entries by a small varint index, so a keyword that occurs thousands of times is
|
||||
// stored once. An "include" entry marks the spot where the source file pulled in
|
||||
// another file, carrying the (interned) include filename expression and parameters.
|
||||
//
|
||||
// The binary is consumed transparently at the cParser file layer: opening a file
|
||||
// whose binary twin exists serves tokens from the twin instead of tokenizing text,
|
||||
// and an include entry triggers the same include machinery as the text path (which
|
||||
// recursively prefers the included file's own binary twin). The scenery
|
||||
// deserializer is unaware of the format and needs no changes; the file's identity
|
||||
// (cParser::Name()), node grouping per .inc and parameter substitution are all
|
||||
// preserved exactly as in the text path.
|
||||
|
||||
namespace scene {
|
||||
|
||||
// "eu7" + format version, little-endian via MAKE_ID4 ('e','u','7',<ver>).
|
||||
// v2 introduced typed numeric entries (f64); v3 interns string tokens into a per-file
|
||||
// table referenced by varint indices. bumping the version invalidates older twins so
|
||||
// they are recompiled rather than misread.
|
||||
constexpr std::uint32_t SCENERYBINARY_MAGIC { MAKE_ID4( 'e', 'u', '7', 3 ) };
|
||||
|
||||
// file extension of the binary twin, derived from source kind
|
||||
std::string const SCENERYBINARY_EXT_SCN { ".scnb" };
|
||||
std::string const SCENERYBINARY_EXT_INC { ".incb" };
|
||||
std::string const SCENERYBINARY_EXT_SCM { ".scmb" };
|
||||
std::string const SCENERYBINARY_EXT_CTR { ".ctrb" };
|
||||
|
||||
// which text format the binary file was compiled from
|
||||
enum class scenery_file_kind : std::uint8_t {
|
||||
scn = 0,
|
||||
inc = 1,
|
||||
scm = 2,
|
||||
};
|
||||
|
||||
// kind of a stored entry
|
||||
enum class scenery_entry_type : std::uint8_t {
|
||||
token = 0, // a non-numeric resolved token (name/path/keyword), stored as a string
|
||||
include = 1, // an include directive: pulls in another file with parameters
|
||||
number = 2, // a numeric token, stored as an 8-byte IEEE double
|
||||
};
|
||||
|
||||
// a single ordered element of a file's resolved content
|
||||
struct scenery_entry {
|
||||
scenery_entry_type type { scenery_entry_type::token };
|
||||
// token value (token entries only)
|
||||
std::string text;
|
||||
// numeric value (number entries only)
|
||||
double number { 0.0 };
|
||||
// include only: the raw filename expression as written in the source, i.e. either
|
||||
// a single filename token or a random set "[ a b c ]" (including the brackets).
|
||||
// kept verbatim so the random choice is re-evaluated on every replay rather than
|
||||
// frozen at compile time.
|
||||
std::vector<std::string> fileexpr;
|
||||
// include only: directive parameters
|
||||
std::vector<std::string> params;
|
||||
};
|
||||
|
||||
// accumulates a file's entries and writes them, with a header, to a stream
|
||||
class scenery_binary_writer {
|
||||
|
||||
public:
|
||||
void add_token( std::string Token );
|
||||
// stores a numeric token as a typed double
|
||||
void add_number( double Value );
|
||||
// Fileexpr is the verbatim filename expression (single token or random set)
|
||||
void add_include( std::vector<std::string> Fileexpr, std::vector<std::string> Params );
|
||||
std::size_t entry_count() const { return m_entries.size(); }
|
||||
// serializes header + entries to the stream. returns false on stream failure.
|
||||
bool write( std::ostream &Output, scenery_file_kind Kind ) const;
|
||||
|
||||
private:
|
||||
std::vector<scenery_entry> m_entries;
|
||||
};
|
||||
|
||||
// reads a binary scenery file fully into memory and exposes its entries in order
|
||||
class scenery_binary_reader {
|
||||
|
||||
public:
|
||||
// validates magic/version and loads every entry. returns false if the stream is
|
||||
// not a valid current-version binary scenery file.
|
||||
bool open( std::istream &Input );
|
||||
|
||||
scenery_file_kind kind() const { return m_kind; }
|
||||
std::vector<scenery_entry> const &entries() const { return m_entries; }
|
||||
|
||||
private:
|
||||
scenery_file_kind m_kind { scenery_file_kind::scn };
|
||||
std::vector<scenery_entry> m_entries;
|
||||
};
|
||||
|
||||
// maps a source scenery filename to the extension of its binary twin
|
||||
std::string scenerybinary_extension_for( std::string const &Sourcefile );
|
||||
|
||||
// Asynchronous baking: serializing and writing a twin is pure, self-contained work
|
||||
// (it only needs the writer's collected entries), so it is offloaded to a small thread
|
||||
// pool and overlaps with the rest of scene construction instead of blocking the load.
|
||||
// Takes ownership of the writer.
|
||||
void scenerybinary_write_async( std::unique_ptr<scenery_binary_writer> Writer, std::string Path, scenery_file_kind Kind );
|
||||
// Blocks until every queued async write has finished, then emits their log lines on the
|
||||
// calling (main) thread. Call once the scenario has finished loading.
|
||||
void scenerybinary_wait_all();
|
||||
|
||||
} // namespace scene
|
||||
Reference in New Issue
Block a user