18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 21:45:16 +01:00
Files
maszyna/scene/sn_utils.h
jerrrrycho 3cbb529d16 fix(scene): write the region cache through a temporary file, reject a damaged one (#351)
* 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.
2026-11-20 16:32:32 +01:00

40 lines
1.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 <string>
class sn_utils
{
public:
static uint16_t ld_uint16(std::istream&);
static uint32_t ld_uint32(std::istream&);
static int32_t ld_int32(std::istream&);
static uint64_t ld_uint64(std::istream&);
static int64_t ld_int64(std::istream&);
static float ld_float32(std::istream&);
static double ld_float64(std::istream&);
static uint8_t d_uint8(std::istream&);
static std::string d_str(std::istream&);
static bool d_bool(std::istream&);
static glm::dvec3 d_dvec3(std::istream&);
static glm::vec3 d_vec3(std::istream&);
static glm::vec4 d_vec4(std::istream&);
// number of bytes left to read in the stream, 0 if it failed or can't tell
static std::uint64_t remaining(std::istream&);
static void ls_uint16(std::ostream&, uint16_t);
static void ls_uint32(std::ostream&, uint32_t);
static void ls_int32(std::ostream&, int32_t);
static void ls_uint64(std::ostream&, uint64_t);
static void ls_int64(std::ostream&, int64_t);
static void ls_float32(std::ostream&, float);
static void ls_float64(std::ostream&, double);
static void s_uint8(std::ostream&, uint8_t);
static void s_str(std::ostream&, std::string const &);
static void s_bool(std::ostream&, bool);
static void s_dvec3(std::ostream&, glm::dvec3 const &);
static void s_vec3(std::ostream&, glm::vec3 const &);
static void s_vec4(std::ostream&, glm::vec4 const &);
};