mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-03-22 15:05:03 +01:00
build 171031: reduced footprint for binary region terrain file
This commit is contained in:
@@ -13,6 +13,17 @@ Copyright (C) 2007-2014 Maciej Cierniak
|
||||
*/
|
||||
#include "stdafx.h"
|
||||
#include "mctools.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#ifndef WIN32
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#ifdef WIN32
|
||||
#define stat _stat
|
||||
#endif
|
||||
|
||||
#include "Globals.h"
|
||||
|
||||
/*================================================*/
|
||||
@@ -275,8 +286,18 @@ extract_value( bool &Variable, std::string const &Key, std::string const &Input,
|
||||
}
|
||||
}
|
||||
|
||||
bool FileExists( std::string const &Filename ) {
|
||||
bool
|
||||
FileExists( std::string const &Filename ) {
|
||||
|
||||
std::ifstream file( Filename );
|
||||
return( true == file.is_open() );
|
||||
}
|
||||
|
||||
// returns time of last modification for specified file
|
||||
__time64_t
|
||||
last_modified( std::string const &Filename ) {
|
||||
|
||||
struct stat filestat;
|
||||
if( ::stat( Filename.c_str(), &filestat ) == 0 ) { return filestat.st_mtime; }
|
||||
else { return 0; }
|
||||
}
|
||||
|
||||
@@ -158,3 +158,6 @@ bool
|
||||
extract_value( bool &Variable, std::string const &Key, std::string const &Input, std::string const &Default );
|
||||
|
||||
bool FileExists( std::string const &Filename );
|
||||
|
||||
// returns time of last modification for specified file
|
||||
__time64_t last_modified( std::string const &Filename );
|
||||
45
scene.cpp
45
scene.cpp
@@ -224,6 +224,11 @@ basic_cell::deserialize( std::istream &Input ) {
|
||||
while( itemcount-- ) {
|
||||
m_lines.emplace_back( lines_node().deserialize( Input ) );
|
||||
}
|
||||
// cell activation flag
|
||||
m_active = (
|
||||
( false == m_shapesopaque.empty() )
|
||||
|| ( false == m_shapestranslucent.empty() )
|
||||
|| ( false == m_lines.empty() ) );
|
||||
}
|
||||
|
||||
// adds provided shape to the cell
|
||||
@@ -877,18 +882,27 @@ basic_region::serialize( std::string const &Scenariofile ) const {
|
||||
|
||||
std::ofstream output { filename, std::ios::binary };
|
||||
|
||||
// region file version 0
|
||||
// region file version 1
|
||||
// header: EU07SBT + version (0-255)
|
||||
sn_utils::ls_uint32( output, MAKE_ID4( 'E', 'U', '0', '7' ) );
|
||||
sn_utils::ls_uint32( output, MAKE_ID4( 'S', 'B', 'T', 0 ) );
|
||||
sn_utils::ls_uint32( output, MAKE_ID4( 'S', 'B', 'T', 1 ) );
|
||||
// sections
|
||||
// TBD, TODO: build table of sections and file offsets, if we postpone section loading until they're within range
|
||||
for( auto section : m_sections ) {
|
||||
// length of section data, followed by section data (if any)
|
||||
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 ); }
|
||||
else {
|
||||
sn_utils::ls_uint32( output, 0 ); }
|
||||
++sectionindex;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -907,7 +921,7 @@ basic_region::deserialize( std::string const &Scenariofile ) {
|
||||
if( false == FileExists( filename ) ) {
|
||||
return false;
|
||||
}
|
||||
// region file version 0
|
||||
// region file version 1
|
||||
// file type and version check
|
||||
std::ifstream input( filename, std::ios::binary );
|
||||
|
||||
@@ -915,20 +929,21 @@ basic_region::deserialize( std::string const &Scenariofile ) {
|
||||
uint32_t headertype { sn_utils::ld_uint32( input ) };
|
||||
|
||||
if( ( headermain != MAKE_ID4( 'E', 'U', '0', '7' )
|
||||
|| ( headertype != MAKE_ID4( 'S', 'B', 'T', 0 ) ) ) ) {
|
||||
|| ( headertype != MAKE_ID4( 'S', 'B', 'T', 1 ) ) ) ) {
|
||||
// wrong file type
|
||||
ErrorLog( "Bad file: \"" + filename + "\" is of either unrecognized type or version" );
|
||||
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
|
||||
for( auto §ion : m_sections ) {
|
||||
// length of section data, followed by section data (if any)
|
||||
// section count
|
||||
auto sectioncount { sn_utils::ld_uint32( input ) };
|
||||
while( sectioncount-- ) {
|
||||
// section index, followed by section data size, followed by section data
|
||||
auto *§ion { m_sections[ sn_utils::ld_uint32( input ) ] };
|
||||
auto const sectionsize { sn_utils::ld_uint32( input ) };
|
||||
if( sectionsize != 0 ) {
|
||||
section = new basic_section();
|
||||
section->deserialize( input );
|
||||
}
|
||||
section = new basic_section();
|
||||
section->deserialize( input );
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
@@ -80,7 +80,11 @@ shape_node::shapenode_data::serialize( std::ostream &Output ) const {
|
||||
// vertex count, followed by vertex data
|
||||
sn_utils::ls_uint32( Output, vertices.size() );
|
||||
for( auto const &vertex : vertices ) {
|
||||
vertex.serialize( Output );
|
||||
basic_vertex(
|
||||
glm::vec3{ vertex.position - origin },
|
||||
vertex.normal,
|
||||
vertex.texture )
|
||||
.serialize( Output );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,8 +109,12 @@ shape_node::shapenode_data::deserialize( std::istream &Input ) {
|
||||
// NOTE: geometry handle is acquired during geometry creation
|
||||
// vertex data
|
||||
vertices.resize( sn_utils::ld_uint32( Input ) );
|
||||
basic_vertex localvertex;
|
||||
for( auto &vertex : vertices ) {
|
||||
vertex.deserialize( Input );
|
||||
localvertex.deserialize( Input );
|
||||
vertex.position = origin + glm::dvec3{ localvertex.position };
|
||||
vertex.normal = localvertex.normal;
|
||||
vertex.texture = localvertex.texture;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -456,7 +464,11 @@ lines_node::linesnode_data::serialize( std::ostream &Output ) const {
|
||||
// vertex count, followed by vertex data
|
||||
sn_utils::ls_uint32( Output, vertices.size() );
|
||||
for( auto const &vertex : vertices ) {
|
||||
vertex.serialize( Output );
|
||||
basic_vertex(
|
||||
glm::vec3{ vertex.position - origin },
|
||||
vertex.normal,
|
||||
vertex.texture )
|
||||
.serialize( Output );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -477,8 +489,12 @@ lines_node::linesnode_data::deserialize( std::istream &Input ) {
|
||||
// NOTE: geometry handle is acquired during geometry creation
|
||||
// vertex data
|
||||
vertices.resize( sn_utils::ld_uint32( Input ) );
|
||||
basic_vertex localvertex;
|
||||
for( auto &vertex : vertices ) {
|
||||
vertex.deserialize( Input );
|
||||
localvertex.deserialize( Input );
|
||||
vertex.position = origin + glm::dvec3{ localvertex.position };
|
||||
vertex.normal = localvertex.normal;
|
||||
vertex.texture = localvertex.texture;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -39,15 +39,22 @@ state_manager::deserialize( std::string const &Scenariofile ) {
|
||||
// TODO: check first for presence of serialized binary files
|
||||
// if this fails, fall back on the legacy text format
|
||||
scene::scratch_data importscratchpad;
|
||||
importscratchpad.binary.terrain = Region->deserialize( Scenariofile );
|
||||
if( Scenariofile != "$.scn" ) {
|
||||
// compilation to binary file isn't supported for rainsted-created overrides
|
||||
importscratchpad.binary.terrain = Region->deserialize( Scenariofile );
|
||||
}
|
||||
// NOTE: for the time being import from text format is a given, since we don't have full binary serialization
|
||||
cParser scenarioparser( Scenariofile, cParser::buffer_FILE, Global::asCurrentSceneryPath, Global::bLoadTraction );
|
||||
|
||||
if( false == scenarioparser.ok() ) { return false; }
|
||||
|
||||
deserialize( scenarioparser, importscratchpad );
|
||||
// if we didn't find usable binary version of the scenario files, create them now for future use
|
||||
if( false == importscratchpad.binary.terrain ) { Region->serialize( Scenariofile ); }
|
||||
if( ( false == importscratchpad.binary.terrain )
|
||||
&& ( Scenariofile != "$.scn" ) ) {
|
||||
// if we didn't find usable binary version of the scenario files, create them now for future use
|
||||
// as long as the scenario file wasn't rainsted-created base file override
|
||||
Region->serialize( Scenariofile );
|
||||
}
|
||||
|
||||
Global::iPause &= ~0x10; // koniec pauzy wczytywania
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user