mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 18:47:06 +01:00
1638fc919b
- pack_folder() writes a store of the old blocks and the new ones together before the models, then the models, then the store of the new blocks alone: a failure on any model (a rename refused, the disk full) leaves models packed against either store, all of which load. Before, models 1..k referred to blocks no store on the disk had. - write_file() closes the stream before it checks it (what sat in its buffer is written by the close) and removes the .tmp of a failed write. - family_store() opens the store of a folder again when its size or time changed (packed anew while the game runs): its blocks are read from the file by the offsets of the index, which then belonged to another file. A store which can't be opened is told in the log once, with the reason, instead of every model of the folder reporting a missing block. - The game finds a block by its hash and checks its counts; the bytes are hashed again only with verify_hash, as the tools read (insert_blocks(..., Verify)), so a load no longer hashes every block twice. - The index of a store is checked without the offset and the size wrapping, and a block may unpack to 1 GiB at most.
496 lines
20 KiB
C++
496 lines
20 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 "model/eu07m/eu07m_store.h"
|
|
|
|
#include "utilities/Logs.h"
|
|
#include "zstd.h"
|
|
|
|
#include <algorithm>
|
|
#include <cstring>
|
|
#include <format>
|
|
#include <fstream>
|
|
#include <map>
|
|
#include <mutex>
|
|
|
|
namespace eu07m {
|
|
|
|
namespace {
|
|
|
|
// the head of a store file
|
|
struct store_header {
|
|
std::array<char, 4> magic{'E', 'U', '7', 'P'};
|
|
std::uint16_t major{1};
|
|
std::uint16_t minor{0};
|
|
std::uint32_t block_count{0};
|
|
std::uint32_t flags{0};
|
|
std::uint64_t index_offset{0};
|
|
std::uint64_t reserved{0};
|
|
};
|
|
static_assert(sizeof(store_header) == 32);
|
|
|
|
constexpr std::uint32_t entry_zstd{0x1};
|
|
constexpr int store_level{9};
|
|
constexpr std::uint64_t second_seed{0x84222325cbf29ce4ull};
|
|
|
|
// the vertex streams of the model, in the order of the bits of a block, each with its bit
|
|
template <typename Model_, typename Visit_> void each_stream(Model_ &Model, Visit_ &&Visit) {
|
|
Visit(block_streams::positions, Model.positions);
|
|
Visit(block_streams::attributes, Model.vertex_attributes);
|
|
Visit(block_streams::weights, Model.weights);
|
|
Visit(block_streams::weights_wide, Model.weights_wide);
|
|
Visit(block_streams::userdata, Model.vertex_userdata);
|
|
Visit(block_streams::bindings, Model.bindings);
|
|
}
|
|
|
|
// the size of an element of the vertex stream of the bit
|
|
std::size_t element_size(std::uint32_t const Bit) {
|
|
switch (Bit) {
|
|
case block_streams::positions: return sizeof(position);
|
|
case block_streams::attributes: return sizeof(attributes);
|
|
case block_streams::weights: return sizeof(skin_weights);
|
|
case block_streams::weights_wide: return sizeof(skin_weights_wide);
|
|
case block_streams::userdata: return sizeof(userdata);
|
|
case block_streams::bindings: return sizeof(vertex_binding);
|
|
default: return 0;
|
|
}
|
|
}
|
|
|
|
constexpr std::array<std::uint32_t, 6> stream_bits{block_streams::positions, block_streams::attributes, block_streams::weights, block_streams::weights_wide, block_streams::userdata,
|
|
block_streams::bindings};
|
|
|
|
// where the data of the stream of the bit starts in the block (the streams before it, then the indices for a bit past them all)
|
|
std::size_t stream_offset(block_header const &Header, std::uint32_t const Bit) {
|
|
std::size_t offset{sizeof(block_header)};
|
|
for (auto const other : stream_bits) {
|
|
if (other < Bit && (Header.streams & other) != 0)
|
|
offset += std::size_t{Header.vertex_count} * element_size(other);
|
|
}
|
|
return offset;
|
|
}
|
|
|
|
// the largest block unpacked, so a broken index can't make the reader take all the memory
|
|
constexpr std::uint32_t block_size_limit{std::uint32_t{1} << 30};
|
|
|
|
// whether the index of the store fits in a file of Size bytes and is sorted
|
|
bool valid_index(std::vector<store_entry> const &Index, std::uint64_t const Size) {
|
|
auto const fits{[Size](store_entry const &Entry) { return Entry.offset <= Size && Entry.stored_size <= Size - Entry.offset && Entry.size <= block_size_limit; }};
|
|
return std::ranges::all_of(Index, fits) && std::ranges::is_sorted(Index, {}, &store_entry::hash);
|
|
}
|
|
|
|
bool valid_header(store_header const &Header, std::uint64_t const Size) {
|
|
return Header.magic == store_header{}.magic && Header.major == 1 && Header.index_offset <= Size && (Size - Header.index_offset) / sizeof(store_entry) >= Header.block_count;
|
|
}
|
|
|
|
bool indexed(primitive_record const &Primitive) {
|
|
return (Primitive.flags & primitive_flags::indexed) != 0;
|
|
}
|
|
|
|
std::uint32_t index_size(primitive_record const &Primitive) {
|
|
if (false == indexed(Primitive))
|
|
return 0;
|
|
return (Primitive.flags & primitive_flags::index32) != 0 ? 4u : 2u;
|
|
}
|
|
|
|
bool overlap(std::uint64_t const First, std::uint64_t const Count, std::uint64_t const OtherFirst, std::uint64_t const OtherCount) {
|
|
return (Count > 0) && (OtherCount > 0) && (First < OtherFirst + OtherCount) && (OtherFirst < First + Count);
|
|
}
|
|
|
|
template <typename Type_, std::size_t Extent_> void append(std::vector<std::byte> &Output, std::span<Type_, Extent_> const Values) {
|
|
auto const bytes{std::as_bytes(Values)};
|
|
Output.insert(Output.end(), bytes.begin(), bytes.end());
|
|
}
|
|
|
|
// a piece of a stream which the store keeps: where it goes in the whole stream and its bytes
|
|
struct piece {
|
|
std::size_t offset{0}; // in elements
|
|
std::span<std::byte const> data;
|
|
};
|
|
|
|
// the whole stream from the part the file kept and the pieces of the store, put in at their places (sorted by them)
|
|
template <typename Type_> bool rebuild(std::vector<Type_> &Stream, std::vector<piece> const &Pieces, std::string_view const Name, std::string &Error) {
|
|
std::vector<Type_> result;
|
|
std::size_t kept{0};
|
|
for (auto const &part : Pieces) {
|
|
if (part.offset < result.size() || (part.data.size() % sizeof(Type_)) != 0 || kept + (part.offset - result.size()) > Stream.size()) {
|
|
Error = std::format("the pieces of the geometry store don't fit in stream {}", Name);
|
|
return false;
|
|
}
|
|
auto const gap{part.offset - result.size()};
|
|
result.insert(result.end(), Stream.begin() + static_cast<std::ptrdiff_t>(kept), Stream.begin() + static_cast<std::ptrdiff_t>(kept + gap));
|
|
kept += gap;
|
|
auto const count{part.data.size() / sizeof(Type_)};
|
|
auto const first{result.size()};
|
|
result.resize(first + count);
|
|
std::memcpy(static_cast<void *>(result.data() + first), part.data.data(), part.data.size());
|
|
}
|
|
result.insert(result.end(), Stream.begin() + static_cast<std::ptrdiff_t>(kept), Stream.end());
|
|
Stream = std::move(result);
|
|
return true;
|
|
}
|
|
|
|
// the stream without the ranges (sorted, apart)
|
|
template <typename Type_> void cut(std::vector<Type_> &Stream, std::vector<std::pair<std::size_t, std::size_t>> const &Ranges) {
|
|
if (Stream.empty() || Ranges.empty())
|
|
return;
|
|
std::vector<Type_> result;
|
|
result.reserve(Stream.size());
|
|
std::size_t next{0};
|
|
for (auto const &[first, count] : Ranges) {
|
|
if (first > Stream.size())
|
|
break;
|
|
result.insert(result.end(), Stream.begin() + static_cast<std::ptrdiff_t>(next), Stream.begin() + static_cast<std::ptrdiff_t>(first));
|
|
next = std::min(first + count, Stream.size());
|
|
}
|
|
result.insert(result.end(), Stream.begin() + static_cast<std::ptrdiff_t>(next), Stream.end());
|
|
Stream = std::move(result);
|
|
}
|
|
|
|
// a block of a reference read and checked against it and its primitive
|
|
struct block_view {
|
|
block_header header;
|
|
std::vector<std::byte> bytes;
|
|
primitive_record primitive;
|
|
};
|
|
|
|
bool read_block(model const &Model, geometry_reference_record const &Reference, block_source const &Source, bool const Verify, block_view &View, std::string &Error) {
|
|
if (Reference.primitive >= Model.primitives.size()) {
|
|
Error = std::format("a geometry reference names primitive {}, which the model hasn't", Reference.primitive);
|
|
return false;
|
|
}
|
|
View.primitive = Model.primitives[Reference.primitive];
|
|
if ((Source == nullptr) || false == Source(Reference, View.bytes)) {
|
|
Error = std::format("the geometry store hasn't the block of primitive {}", Reference.primitive);
|
|
return false;
|
|
}
|
|
// the block is found by its hash; its bytes are hashed again only when the content of the file is checked too (verify_hash),
|
|
// which the game doesn't do, as the store is read from the same disk as the model
|
|
if (View.bytes.size() < sizeof(block_header) || (Verify && block_hash(View.bytes) != Reference.hash)) {
|
|
Error = std::format("the block of primitive {} in the geometry store isn't the one the model refers to", Reference.primitive);
|
|
return false;
|
|
}
|
|
std::memcpy(&View.header, View.bytes.data(), sizeof(block_header));
|
|
auto const &primitive{View.primitive};
|
|
auto const indices{indexed(primitive) ? primitive.index_count : 0u};
|
|
if (View.header.vertex_count != primitive.vertex_count || View.header.vertex_count != Reference.vertex_count || View.header.index_count != indices ||
|
|
View.header.index_count != Reference.index_count || View.header.index_size != index_size(primitive)) {
|
|
Error = std::format("the block of primitive {} in the geometry store has other counts than the primitive", Reference.primitive);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// the pieces of the blocks for one vertex stream; false when a block lacks a stream the model has
|
|
template <typename Type_>
|
|
bool vertex_pieces(std::vector<block_view> const &Blocks, std::uint32_t const Bit, bool const Present, std::vector<piece> &Pieces, std::string &Error) {
|
|
for (auto const &block : Blocks) {
|
|
if ((block.header.streams & Bit) == 0) {
|
|
if (Present) {
|
|
Error = "a block of the geometry store lacks a vertex stream the model has";
|
|
return false;
|
|
}
|
|
continue;
|
|
}
|
|
auto const offset{stream_offset(block.header, Bit)};
|
|
auto const size{std::size_t{block.header.vertex_count} * sizeof(Type_)};
|
|
if (offset + size > block.bytes.size()) {
|
|
Error = "a block of the geometry store is cut short";
|
|
return false;
|
|
}
|
|
Pieces.push_back({block.primitive.base_vertex, std::span{block.bytes}.subspan(offset, size)});
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool index_pieces(std::vector<block_view> const &Blocks, std::uint32_t const Size, std::vector<piece> &Pieces, std::string &Error) {
|
|
for (auto const &block : Blocks) {
|
|
if (block.header.index_size != Size || block.header.index_count == 0)
|
|
continue;
|
|
auto const offset{stream_offset(block.header, 0x80000000u)};
|
|
auto const size{std::size_t{block.header.index_count} * Size};
|
|
if (offset + size > block.bytes.size()) {
|
|
Error = "a block of the geometry store is cut short";
|
|
return false;
|
|
}
|
|
Pieces.push_back({block.primitive.first_index, std::span{block.bytes}.subspan(offset, size)});
|
|
}
|
|
std::ranges::sort(Pieces, {}, &piece::offset);
|
|
return true;
|
|
}
|
|
|
|
std::vector<std::byte> pack_block(std::span<std::byte const> Block, std::uint32_t &Flags) {
|
|
std::vector<std::byte> packed(ZSTD_compressBound(Block.size()));
|
|
auto const size{ZSTD_compress(packed.data(), packed.size(), Block.data(), Block.size(), store_level)};
|
|
if (ZSTD_isError(size) || size + size / 8 >= Block.size()) {
|
|
Flags = 0;
|
|
return {Block.begin(), Block.end()};
|
|
}
|
|
packed.resize(size);
|
|
Flags = entry_zstd;
|
|
return packed;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
bool shareable(model const &Model, std::uint32_t const Primitive) {
|
|
if (Primitive >= Model.primitives.size())
|
|
return false;
|
|
auto const &primitive{Model.primitives[Primitive]};
|
|
if (primitive.vertex_count == 0 || std::uint64_t{primitive.base_vertex} + primitive.vertex_count > Model.vertex_count())
|
|
return false;
|
|
for (std::uint32_t idx = 0; idx < Model.primitives.size(); ++idx) {
|
|
if (idx == Primitive)
|
|
continue;
|
|
auto const &other{Model.primitives[idx]};
|
|
if (overlap(primitive.base_vertex, primitive.vertex_count, other.base_vertex, other.vertex_count))
|
|
return false;
|
|
if (indexed(primitive) && index_size(other) == index_size(primitive) && overlap(primitive.first_index, primitive.index_count, other.first_index, other.index_count))
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
std::vector<std::byte> make_block(model const &Model, primitive_record const &Primitive) {
|
|
block_header header;
|
|
header.vertex_count = Primitive.vertex_count;
|
|
header.index_count = indexed(Primitive) ? Primitive.index_count : 0u;
|
|
header.index_size = index_size(Primitive);
|
|
auto const vertices{Model.vertex_count()};
|
|
each_stream(Model, [&header, vertices](std::uint32_t const Bit, auto const &Stream) {
|
|
if (false == Stream.empty() && Stream.size() == vertices)
|
|
header.streams |= Bit;
|
|
});
|
|
std::vector<std::byte> block;
|
|
append(block, std::span{&header, 1});
|
|
each_stream(Model, [&header, &block, &Primitive](std::uint32_t const Bit, auto const &Stream) {
|
|
if ((header.streams & Bit) != 0)
|
|
append(block, std::span{Stream}.subspan(Primitive.base_vertex, Primitive.vertex_count));
|
|
});
|
|
if (header.index_size == 2)
|
|
append(block, std::span{Model.indices16}.subspan(Primitive.first_index, Primitive.index_count));
|
|
else if (header.index_size == 4)
|
|
append(block, std::span{Model.indices32}.subspan(Primitive.first_index, Primitive.index_count));
|
|
return block;
|
|
}
|
|
|
|
block_id block_hash(std::span<std::byte const> const Block) {
|
|
return {hash_bytes(Block), hash_bytes(Block, second_seed)};
|
|
}
|
|
|
|
bool insert_blocks(model &Model, block_source const &Source, std::string &Error, bool const Verify) {
|
|
if (Model.geometry_references.empty())
|
|
return true;
|
|
std::vector<block_view> blocks;
|
|
blocks.reserve(Model.geometry_references.size());
|
|
for (auto const &reference : Model.geometry_references) {
|
|
if (false == read_block(Model, reference, Source, Verify, blocks.emplace_back(), Error))
|
|
return false;
|
|
}
|
|
std::ranges::sort(blocks, {}, [](block_view const &Block) { return Block.primitive.base_vertex; });
|
|
auto ok{true};
|
|
each_stream(Model, [&blocks, &Error, &ok](std::uint32_t const Bit, auto &Stream) {
|
|
using element = typename std::remove_cvref_t<decltype(Stream)>::value_type;
|
|
auto const present{false == Stream.empty() || std::ranges::any_of(blocks, [Bit](block_view const &Block) { return (Block.header.streams & Bit) != 0; })};
|
|
if (false == ok || false == present)
|
|
return;
|
|
std::vector<piece> pieces;
|
|
ok = vertex_pieces<element>(blocks, Bit, present, pieces, Error) && rebuild(Stream, pieces, "of vertices", Error);
|
|
});
|
|
if (false == ok)
|
|
return false;
|
|
std::vector<piece> narrow;
|
|
std::vector<piece> wide;
|
|
return index_pieces(blocks, 2, narrow, Error) && rebuild(Model.indices16, narrow, "IDX2", Error) && index_pieces(blocks, 4, wide, Error) &&
|
|
rebuild(Model.indices32, wide, "IDX4", Error);
|
|
}
|
|
|
|
model remove_referenced(model const &Model) {
|
|
model result{Model};
|
|
std::vector<std::pair<std::size_t, std::size_t>> vertices;
|
|
std::vector<std::pair<std::size_t, std::size_t>> narrow;
|
|
std::vector<std::pair<std::size_t, std::size_t>> wide;
|
|
for (auto const &reference : Model.geometry_references) {
|
|
if (reference.primitive >= Model.primitives.size())
|
|
continue;
|
|
auto const &primitive{Model.primitives[reference.primitive]};
|
|
vertices.emplace_back(primitive.base_vertex, primitive.vertex_count);
|
|
if (index_size(primitive) == 2)
|
|
narrow.emplace_back(primitive.first_index, primitive.index_count);
|
|
else if (index_size(primitive) == 4)
|
|
wide.emplace_back(primitive.first_index, primitive.index_count);
|
|
}
|
|
std::ranges::sort(vertices);
|
|
std::ranges::sort(narrow);
|
|
std::ranges::sort(wide);
|
|
each_stream(result, [&vertices](std::uint32_t, auto &Stream) { cut(Stream, vertices); });
|
|
cut(result.indices16, narrow);
|
|
cut(result.indices32, wide);
|
|
return result;
|
|
}
|
|
|
|
std::vector<std::byte> write_store(std::vector<std::vector<std::byte>> const &Blocks) {
|
|
std::map<block_id, std::vector<std::byte> const *> unique;
|
|
for (auto const &block : Blocks)
|
|
unique.try_emplace(block_hash(block), &block);
|
|
std::vector<std::byte> file(sizeof(store_header));
|
|
std::vector<store_entry> index;
|
|
index.reserve(unique.size());
|
|
for (auto const &[hash, block] : unique) {
|
|
store_entry entry;
|
|
entry.hash = hash;
|
|
entry.offset = file.size();
|
|
entry.size = static_cast<std::uint32_t>(block->size());
|
|
auto const packed{pack_block(*block, entry.flags)};
|
|
entry.stored_size = static_cast<std::uint32_t>(packed.size());
|
|
file.insert(file.end(), packed.begin(), packed.end());
|
|
index.push_back(entry);
|
|
}
|
|
store_header header;
|
|
header.block_count = static_cast<std::uint32_t>(index.size());
|
|
header.index_offset = file.size();
|
|
append(file, std::span<store_entry const>{index});
|
|
std::memcpy(file.data(), &header, sizeof(header));
|
|
return file;
|
|
}
|
|
|
|
bool geometry_store::open(std::filesystem::path const &Path, std::string &Error) {
|
|
m_path = Path;
|
|
m_data.clear();
|
|
m_index.clear();
|
|
std::ifstream file{Path, std::ios::binary | std::ios::ate};
|
|
if (false == file.good()) {
|
|
Error = "can't read the geometry store";
|
|
return false;
|
|
}
|
|
auto const size{static_cast<std::uint64_t>(file.tellg())};
|
|
store_header header;
|
|
file.seekg(0);
|
|
if (size < sizeof(header) || false == bool(file.read(reinterpret_cast<char *>(&header), sizeof(header))) || false == valid_header(header, size)) {
|
|
Error = "the geometry store isn't one, or is of a later version";
|
|
return false;
|
|
}
|
|
m_index.resize(header.block_count);
|
|
file.seekg(static_cast<std::streamoff>(header.index_offset));
|
|
if (false == bool(file.read(reinterpret_cast<char *>(m_index.data()), static_cast<std::streamsize>(m_index.size() * sizeof(store_entry))))) {
|
|
m_index.clear();
|
|
Error = "the index of the geometry store is cut short";
|
|
return false;
|
|
}
|
|
if (false == valid_index(m_index, size)) {
|
|
m_index.clear();
|
|
Error = "the index of the geometry store is out of order or out of the file";
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool geometry_store::open(std::vector<std::byte> Data, std::string &Error) {
|
|
m_path.clear();
|
|
m_index.clear();
|
|
m_data = std::move(Data);
|
|
store_header header;
|
|
if (m_data.size() < sizeof(header)) {
|
|
Error = "the geometry store is cut short";
|
|
return false;
|
|
}
|
|
std::memcpy(&header, m_data.data(), sizeof(header));
|
|
if (false == valid_header(header, m_data.size())) {
|
|
Error = "the geometry store isn't one, or is of a later version";
|
|
return false;
|
|
}
|
|
m_index.resize(header.block_count);
|
|
std::memcpy(m_index.data(), m_data.data() + header.index_offset, m_index.size() * sizeof(store_entry));
|
|
if (false == valid_index(m_index, m_data.size())) {
|
|
m_index.clear();
|
|
Error = "the index of the geometry store is out of order or out of the file";
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool geometry_store::stored(store_entry const &Entry, std::vector<std::byte> &Bytes) const {
|
|
Bytes.resize(Entry.stored_size);
|
|
if (false == m_data.empty()) {
|
|
std::memcpy(Bytes.data(), m_data.data() + Entry.offset, Entry.stored_size);
|
|
return true;
|
|
}
|
|
std::ifstream file{m_path, std::ios::binary};
|
|
file.seekg(static_cast<std::streamoff>(Entry.offset));
|
|
return bool(file.read(reinterpret_cast<char *>(Bytes.data()), static_cast<std::streamsize>(Bytes.size())));
|
|
}
|
|
|
|
bool geometry_store::block(block_id const &Hash, std::vector<std::byte> &Block) const {
|
|
auto const found{std::ranges::lower_bound(m_index, Hash, {}, &store_entry::hash)};
|
|
if (found == m_index.end() || found->hash != Hash)
|
|
return false;
|
|
std::vector<std::byte> bytes;
|
|
if (false == stored(*found, bytes))
|
|
return false;
|
|
if ((found->flags & entry_zstd) == 0) {
|
|
Block = std::move(bytes);
|
|
return Block.size() == found->size;
|
|
}
|
|
Block.resize(found->size);
|
|
auto const size{ZSTD_decompress(Block.data(), Block.size(), bytes.data(), bytes.size())};
|
|
return (false == ZSTD_isError(size)) && size == found->size;
|
|
}
|
|
|
|
std::vector<block_id> geometry_store::blocks() const {
|
|
std::vector<block_id> result;
|
|
result.reserve(m_index.size());
|
|
std::ranges::transform(m_index, std::back_inserter(result), &store_entry::hash);
|
|
return result;
|
|
}
|
|
|
|
block_source store_source(std::shared_ptr<geometry_store const> Store) {
|
|
if (Store == nullptr)
|
|
return {};
|
|
return [Store = std::move(Store)](geometry_reference_record const &Reference, std::vector<std::byte> &Block) { return Store->block(Reference.hash, Block); };
|
|
}
|
|
|
|
std::shared_ptr<geometry_store const> family_store(std::filesystem::path const &ModelFile) {
|
|
// the store of a folder as it was opened: a file which changed since (packed anew while the game runs) is opened again, as
|
|
// its blocks are read from the file by the offsets of the index
|
|
struct cached {
|
|
std::shared_ptr<geometry_store const> store;
|
|
bool exists{false};
|
|
std::uintmax_t size{0};
|
|
std::filesystem::file_time_type time{};
|
|
};
|
|
static std::mutex mutex;
|
|
static std::map<std::filesystem::path, cached> stores;
|
|
auto path{ModelFile.parent_path() / store_file_name};
|
|
std::error_code error;
|
|
if (auto canonical{std::filesystem::weakly_canonical(path, error)}; false == bool(error))
|
|
path = std::move(canonical);
|
|
cached state;
|
|
state.exists = std::filesystem::exists(path, error);
|
|
if (state.exists) {
|
|
state.size = std::filesystem::file_size(path, error);
|
|
state.time = std::filesystem::last_write_time(path, error);
|
|
}
|
|
std::scoped_lock lock{mutex};
|
|
if (auto const found{stores.find(path)};
|
|
found != stores.end() && found->second.exists == state.exists && found->second.size == state.size && found->second.time == state.time)
|
|
return found->second.store;
|
|
if (state.exists) {
|
|
auto store{std::make_shared<geometry_store>()};
|
|
if (std::string reason; store->open(path, reason))
|
|
state.store = std::move(store);
|
|
else // once for the folder: its models which refer to the store then can't be read
|
|
ErrorLog("Bad model: the geometry store \"" + path.string() + "\" can't be used: " + reason, logtype::model);
|
|
}
|
|
stores.insert_or_assign(path, state);
|
|
return state.store;
|
|
}
|
|
|
|
} // namespace eu07m
|