mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-27 22:55:16 +01:00
* fix(scene): write the region cache through a temporary file, reject a damaged one
The static geometry cache of a scenery (<scenery>.sbt) was written in place,
and only its 8-byte header was checked before it was used. A write cut short
(a crash, the process killed, the test mode's real time watchdog) left a
truncated file which every later start loaded as it was: the sections up to
the cut, then whatever the reads past the end produced, with the section
index used unchecked and the section size read and ignored.
- basic_region::serialize() writes <scenery>.sbt.tmp and renames it over the
final name only when complete; a failed write is logged and the temporary
file removed, the previous file stays.
- basic_region::is_scene() checks the whole layout: header, section count,
every section index within the region and present once, and the section
sizes adding up to the length of the file. A cut-short file is turned down
there ("Bad file: ... ignored"), so the scenery loads from the text and the
cache is written anew at the end of the load.
- basic_region::deserialize() checks the layout again, reads each section out
of its own data (sectionsize bytes) and requires the section to use exactly
that; sections are restored apart from the region and taken over only when
the whole file was read, so a damaged file leaves nothing of itself behind.
If the content is damaged although the layout holds, the default .sbt (the
game's own copy of the text) is removed so the next start rebuilds it.
- A vertex count the rest of the data can't hold, a shape count running past
the data, and a string cut short by the end of the data
(sn_utils::d_str() looped forever there) now stop the read instead of
allocating or looping on.
- The layout check and the write through a temporary file are in
scene/regionfile.{h,cpp}, in the core library with sn_utils, and have unit
tests (test_region_file.cpp).
- run.py also removes a <case>.sbt.tmp left by a run cut short.
The file format is unchanged.
* refactor(scene): the region file code without Sonar issues
write_through_temporary takes the writer as a template parameter, the
messages are built with std::format, the reading of one section moves
into a helper (one break per loop), and the test drops chained
assignments and implicit lambda captures. No change in behaviour.
131 lines
4.6 KiB
C++
131 lines
4.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 "scene/regionfile.h"
|
|
#include "scene/sn_utils.h"
|
|
|
|
#include <filesystem>
|
|
#include <format>
|
|
#include <fstream>
|
|
#include <vector>
|
|
|
|
namespace scene::region_file {
|
|
|
|
std::string
|
|
check_layout( std::istream &Input, std::uint32_t const Header, std::uint32_t const Version, std::uint32_t const Sectionlimit ) {
|
|
|
|
Input.clear();
|
|
Input.seekg( 0, std::ios::end );
|
|
auto const end { Input.tellg() };
|
|
Input.seekg( 0, std::ios::beg );
|
|
if( ( end < 0 ) || ( false == Input.good() ) ) {
|
|
return "can't be read";
|
|
}
|
|
auto const filesize { static_cast<std::uint64_t>( end ) };
|
|
|
|
auto const headermain { sn_utils::ld_uint32( Input ) };
|
|
if( auto const headertype { sn_utils::ld_uint32( Input ) }; ( false == Input.good() ) || ( headermain != Header ) || ( headertype != Version ) ) {
|
|
return "is of either unrecognized type or version";
|
|
}
|
|
auto sectioncount { sn_utils::ld_uint32( Input ) };
|
|
if( false == Input.good() ) {
|
|
return "ends before the section count";
|
|
}
|
|
if( sectioncount > Sectionlimit ) {
|
|
return std::format( "holds {} sections, more than the {} a region has", sectioncount, Sectionlimit );
|
|
}
|
|
std::vector<bool> present( Sectionlimit, false );
|
|
std::uint64_t position { 3 * sizeof( std::uint32_t ) };
|
|
std::uint32_t sectionnumber { 0 };
|
|
while( sectionnumber < sectioncount ) {
|
|
if( filesize - position < section_header_size ) {
|
|
return std::format( "ends after {} of its {} sections", sectionnumber, sectioncount );
|
|
}
|
|
std::uint32_t index;
|
|
std::uint32_t size;
|
|
if( auto const error { read_section_header( Input, Sectionlimit, index, size ) }; false == error.empty() ) {
|
|
return error;
|
|
}
|
|
if( true == present[ index ] ) {
|
|
return std::format( "holds section {} more than once", index );
|
|
}
|
|
present[ index ] = true;
|
|
if( filesize - position < size ) {
|
|
return std::format(
|
|
"ends inside section {} ({} of {}), {} of its {} bytes are there", index, sectionnumber + 1, sectioncount, filesize - position, size );
|
|
}
|
|
position += size;
|
|
Input.seekg( static_cast<std::streamoff>( position ), std::ios::beg );
|
|
if( false == Input.good() ) {
|
|
return std::format( "can't be read past section {}", index );
|
|
}
|
|
++sectionnumber;
|
|
}
|
|
if( position != filesize ) {
|
|
return std::format( "has {} bytes past its last section", filesize - position );
|
|
}
|
|
return {};
|
|
}
|
|
|
|
std::string
|
|
read_section_header( std::istream &Input, std::uint32_t const Sectionlimit, std::uint32_t &Index, std::uint32_t &Size ) {
|
|
|
|
Index = sn_utils::ld_uint32( Input );
|
|
Size = sn_utils::ld_uint32( Input );
|
|
if( false == Input.good() ) {
|
|
return "ends inside a section header";
|
|
}
|
|
if( Index >= Sectionlimit ) {
|
|
return std::format( "holds section {}, out of the {} a region has", Index, Sectionlimit );
|
|
}
|
|
if( Size < section_header_size ) {
|
|
return std::format( "gives section {} the size of {} bytes, less than its header takes", Index, Size );
|
|
}
|
|
return {};
|
|
}
|
|
|
|
std::string
|
|
open_temporary( std::string const &Filename, std::ofstream &Output ) {
|
|
|
|
Output.open( Filename + ".tmp", std::ios::binary | std::ios::trunc );
|
|
if( Output.fail() ) {
|
|
return "can't write to file \"" + Filename + ".tmp\"";
|
|
}
|
|
return {};
|
|
}
|
|
|
|
std::string
|
|
finish_temporary( std::string const &Filename, std::ofstream &Output, std::string Error ) {
|
|
|
|
if( ( true == Error.empty() ) && ( Output.fail() ) ) {
|
|
Error = "can't write to file \"" + Filename + ".tmp\"";
|
|
}
|
|
Output.close();
|
|
if( ( true == Error.empty() ) && ( Output.fail() ) ) {
|
|
Error = "can't write to file \"" + Filename + ".tmp\"";
|
|
}
|
|
std::filesystem::path const temporaryfile { Filename + ".tmp" };
|
|
std::error_code result;
|
|
if( false == Error.empty() ) {
|
|
std::filesystem::remove( temporaryfile, result );
|
|
return Error;
|
|
}
|
|
// the finished file replaces the previous version in one go, so an interrupted write doesn't leave a broken one behind
|
|
std::filesystem::rename( temporaryfile, Filename, result );
|
|
if( result ) {
|
|
Error = "can't replace file \"" + Filename + "\": " + result.message();
|
|
std::filesystem::remove( temporaryfile, result );
|
|
return Error;
|
|
}
|
|
return {};
|
|
}
|
|
|
|
} // scene::region_file
|