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.cpp
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

275 lines
7.2 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/sn_utils.h"
#include <bit>
// sanity checks
static_assert(std::numeric_limits<double>::is_iec559, "IEEE754 required");
static_assert(sizeof(float) == 4, "Float must be 4 bytes");
static_assert(sizeof(double) == 8, "Double must be 8 bytes");
static_assert(-1 == ~0, "Two's complement required");
// deserialize little endian uint16
uint16_t sn_utils::ld_uint16(std::istream &s)
{
std::array<uint8_t, 2> buf{};
s.read(reinterpret_cast<char *>(buf.data()), buf.size());
auto v = static_cast<uint16_t>(buf[1] << 8 | buf[0]);
return v;
}
// deserialize little endian uint32
uint32_t sn_utils::ld_uint32(std::istream &s)
{
std::array<uint8_t, 4> buf{};
s.read(reinterpret_cast<char *>(buf.data()), buf.size());
uint32_t v = buf[3] << 24 | buf[2] << 16 | buf[1] << 8 | buf[0];
return v;
}
// deserialize little endian int32
int32_t sn_utils::ld_int32(std::istream &s)
{
std::array<uint8_t, 4> buf{};
s.read(reinterpret_cast<char *>(buf.data()), buf.size());
uint32_t v = buf[3] << 24 | buf[2] << 16 | buf[1] << 8 | buf[0];
return static_cast<int32_t>(v);
}
// deserialize little endian uint64
uint64_t sn_utils::ld_uint64(std::istream &s)
{
std::array<uint8_t, 8> buf{};
s.read(reinterpret_cast<char *>(buf.data()), buf.size());
uint64_t v = (uint64_t)buf[7] << 56 | (uint64_t)buf[6] << 48 |
(uint64_t)buf[5] << 40 | (uint64_t)buf[4] << 32 |
(uint64_t)buf[3] << 24 | (uint64_t)buf[2] << 16 |
(uint64_t)buf[1] << 8 | (uint64_t)buf[0];
return v;
}
// deserialize little endian int64
int64_t sn_utils::ld_int64(std::istream &s)
{
std::array<uint8_t, 8> buf{};
s.read(reinterpret_cast<char *>(buf.data()), buf.size());
uint64_t v = (uint64_t)buf[7] << 56 | (uint64_t)buf[6] << 48 |
(uint64_t)buf[5] << 40 | (uint64_t)buf[4] << 32 |
(uint64_t)buf[3] << 24 | (uint64_t)buf[2] << 16 |
(uint64_t)buf[1] << 8 | (uint64_t)buf[0];
return static_cast<int64_t>(v);
}
// deserialize little endian ieee754 float32
float sn_utils::ld_float32(std::istream &s)
{
std::array<uint8_t, 4> buf{};
s.read(reinterpret_cast<char *>(buf.data()), buf.size());
uint32_t v = buf[3] << 24 | buf[2] << 16 | buf[1] << 8 | buf[0];
return std::bit_cast<float>(v);
}
// deserialize little endian ieee754 float64
double sn_utils::ld_float64(std::istream &s)
{
std::array<uint8_t, 8> buf{};
s.read(reinterpret_cast<char *>(buf.data()), buf.size());
uint64_t v = (uint64_t)buf[7] << 56 | (uint64_t)buf[6] << 48 |
(uint64_t)buf[5] << 40 | (uint64_t)buf[4] << 32 |
(uint64_t)buf[3] << 24 | (uint64_t)buf[2] << 16 |
(uint64_t)buf[1] << 8 | (uint64_t)buf[0];
return std::bit_cast<double>(v);
}
// deserialize null-terminated string
std::string sn_utils::d_str(std::istream &s)
{
std::string r;
r.reserve(32);
char c{};
while (true)
{
// a string cut short by the end of the data ends there too
if (!s.read(&c, 1) || c == 0)
break;
r.push_back(c);
}
return r;
}
// number of bytes left to read in the stream, 0 if it failed or can't tell
std::uint64_t sn_utils::remaining(std::istream &s)
{
auto const position{s.tellg()};
if (position < 0)
return 0;
s.seekg(0, std::ios::end);
auto const end{s.tellg()};
s.seekg(position);
return end > position ? static_cast<std::uint64_t>(end - position) : 0;
}
bool sn_utils::d_bool(std::istream& s)
{
return ld_uint16(s) == 1;
}
glm::dvec3 sn_utils::d_dvec3(std::istream& s)
{
return {
ld_float64(s),
ld_float64(s),
ld_float64(s) };
}
glm::vec3 sn_utils::d_vec3( std::istream& s )
{
return {
ld_float32(s),
ld_float32(s),
ld_float32(s) };
}
glm::vec4 sn_utils::d_vec4( std::istream& s )
{
return {
ld_float32(s),
ld_float32(s),
ld_float32(s),
ld_float32(s) };
}
uint8_t sn_utils::d_uint8( std::istream& s ) {
uint8_t buf;
s.read((char*)&buf, 1);
return buf;
}
void sn_utils::ls_uint16(std::ostream &s, uint16_t v)
{
std::array<uint8_t, 2> buf{};
buf[0] = static_cast<uint8_t>(v);
buf[1] = v >> 8;
s.write(reinterpret_cast<char const *>(buf.data()), buf.size());
}
void sn_utils::ls_uint32(std::ostream &s, uint32_t v)
{
std::array<uint8_t, 4> buf{};
buf[0] = static_cast<uint8_t>(v);
buf[1] = static_cast<uint8_t>(v >> 8);
buf[2] = static_cast<uint8_t>(v >> 16);
buf[3] = v >> 24;
s.write(reinterpret_cast<char const *>(buf.data()), buf.size());
}
void sn_utils::ls_int32(std::ostream &s, int32_t v)
{
std::array<uint8_t, 4> buf{};
buf[0] = static_cast<uint8_t>(v);
buf[1] = static_cast<uint8_t>(v >> 8);
buf[2] = static_cast<uint8_t>(v >> 16);
buf[3] = v >> 24;
s.write(reinterpret_cast<char const *>(buf.data()), buf.size());
}
void sn_utils::ls_uint64(std::ostream &s, uint64_t v)
{
std::array<uint8_t, 8> buf{};
buf[0] = static_cast<uint8_t>(v);
buf[1] = static_cast<uint8_t>(v >> 8);
buf[2] = static_cast<uint8_t>(v >> 16);
buf[3] = static_cast<uint8_t>(v >> 24);
buf[4] = static_cast<uint8_t>(v >> 32);
buf[5] = static_cast<uint8_t>(v >> 40);
buf[6] = static_cast<uint8_t>(v >> 48);
buf[7] = v >> 56;
s.write(reinterpret_cast<char const *>(buf.data()), buf.size());
}
void sn_utils::ls_int64(std::ostream &s, int64_t v)
{
std::array<uint8_t, 8> buf{};
buf[0] = static_cast<uint8_t>(v);
buf[1] = static_cast<uint8_t>(v >> 8);
buf[2] = static_cast<uint8_t>(v >> 16);
buf[3] = static_cast<uint8_t>(v >> 24);
buf[4] = static_cast<uint8_t>(v >> 32);
buf[5] = static_cast<uint8_t>(v >> 40);
buf[6] = static_cast<uint8_t>(v >> 48);
buf[7] = v >> 56;
s.write(reinterpret_cast<char const *>(buf.data()), buf.size());
}
void sn_utils::ls_float32(std::ostream &s, float t)
{
auto v = std::bit_cast<uint32_t>(t);
std::array<uint8_t, 4> buf{};
buf[0] = static_cast<uint8_t>(v);
buf[1] = static_cast<uint8_t>(v >> 8);
buf[2] = static_cast<uint8_t>(v >> 16);
buf[3] = v >> 24;
s.write(reinterpret_cast<char const *>(buf.data()), buf.size());
}
void sn_utils::ls_float64(std::ostream &s, double t)
{
auto v = std::bit_cast<uint64_t>(t);
std::array<uint8_t, 8> buf{};
buf[0] = static_cast<uint8_t>(v);
buf[1] = static_cast<uint8_t>(v >> 8);
buf[2] = static_cast<uint8_t>(v >> 16);
buf[3] = static_cast<uint8_t>(v >> 24);
buf[4] = static_cast<uint8_t>(v >> 32);
buf[5] = static_cast<uint8_t>(v >> 40);
buf[6] = static_cast<uint8_t>(v >> 48);
buf[7] = v >> 56;
s.write(reinterpret_cast<char const *>(buf.data()), buf.size());
}
void sn_utils::s_uint8(std::ostream &s, uint8_t v)
{
s.write((char*)&v, 1);
}
void sn_utils::s_str(std::ostream &s, std::string const &v)
{
const char* buf = v.c_str();
s.write(buf, v.size() + 1);
}
void sn_utils::s_bool(std::ostream &s, bool v)
{
ls_uint16(
s,
true == v ? 1 : 0 );
}
void sn_utils::s_dvec3(std::ostream &s, glm::dvec3 const &v)
{
ls_float64(s, v.x);
ls_float64(s, v.y);
ls_float64(s, v.z);
}
void sn_utils::s_vec3(std::ostream &s, glm::vec3 const &v)
{
ls_float32(s, v.x);
ls_float32(s, v.y);
ls_float32(s, v.z);
}
void sn_utils::s_vec4(std::ostream &s, glm::vec4 const &v)
{
ls_float32(s, v.x);
ls_float32(s, v.y);
ls_float32(s, v.z);
ls_float32(s, v.w);
}