mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-27 21:45: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.
78 lines
3.6 KiB
C++
78 lines
3.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/.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <cstdint>
|
|
#include <exception>
|
|
#include <fstream>
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
// the parts of the region file (<scenery>.sbt, the static geometry of a scenery cached by basic_region) which need
|
|
// nothing of the scene: checking how a file is laid out, and writing it so that an interrupted write leaves no
|
|
// cut-short file behind. kept apart so the unit tests can check them
|
|
namespace scene::region_file {
|
|
|
|
// layout of the file: the header (two uint32: the file type and its version), the section count (uint32), then for
|
|
// each section its index (uint32), its size (uint32, counted from the start of the index, so it covers the index,
|
|
// the size itself and the section data) and the section data. all numbers little endian
|
|
|
|
// size of the index and the size of a section, the least a section can take
|
|
std::uint32_t constexpr section_header_size { 2 * sizeof( std::uint32_t ) };
|
|
|
|
// checks the layout of the file held by the stream, read from its start: the header has to match the provided type
|
|
// and version, the sections have to have indices below Sectionlimit, each one no more than once, and their sizes have
|
|
// to add up to the length of the file. the section data itself isn't read. returns: the empty string if the layout
|
|
// holds, the reason why not otherwise
|
|
std::string
|
|
check_layout( std::istream &Input, std::uint32_t const Header, std::uint32_t const Version, std::uint32_t const Sectionlimit );
|
|
|
|
// reads the index and the size of the next section, and checks them: the index has to be below Sectionlimit, and the
|
|
// section no smaller than its own index and size. returns: the empty string if both were read and are sound, the
|
|
// reason why not otherwise
|
|
std::string
|
|
read_section_header( std::istream &Input, std::uint32_t const Sectionlimit, std::uint32_t &Index, std::uint32_t &Size );
|
|
|
|
// opens the temporary file write_through_temporary() writes into. returns: the empty string on success, the reason of
|
|
// the failure otherwise
|
|
std::string
|
|
open_temporary( std::string const &Filename, std::ofstream &Output );
|
|
|
|
// closes the temporary file write_through_temporary() wrote into, and puts it in place of the file, unless Error holds
|
|
// the reason why the writing failed. the temporary file is removed on a failure. returns: the empty string on success,
|
|
// the reason of the failure otherwise
|
|
std::string
|
|
finish_temporary( std::string const &Filename, std::ofstream &Output, std::string Error );
|
|
|
|
// writes a file with the provided function (taking the std::ostream to write into) into a temporary file next to it
|
|
// (the name with .tmp added), which then replaces the file in one go. if the writing fails or is cut short, the
|
|
// previous file (or none) stays in place, rather than a partial one; the temporary file is removed when the failure is
|
|
// noticed. returns: the empty string on success, the reason of the failure otherwise
|
|
template <typename Writer_>
|
|
std::string
|
|
write_through_temporary( std::string const &Filename, Writer_ const &Write ) {
|
|
|
|
std::ofstream output;
|
|
if( auto const openerror { open_temporary( Filename, output ) }; false == openerror.empty() ) {
|
|
return openerror;
|
|
}
|
|
std::string error;
|
|
try {
|
|
Write( output );
|
|
output.flush();
|
|
}
|
|
catch( std::exception const &Exception ) {
|
|
error = "can't write to file \"" + Filename + ".tmp\": " + Exception.what();
|
|
}
|
|
return finish_temporary( Filename, output, std::move( error ) );
|
|
}
|
|
|
|
} // scene::region_file
|