diff --git a/.gitmodules b/.gitmodules index b9c60f72a..6137eb9d2 100644 --- a/.gitmodules +++ b/.gitmodules @@ -39,3 +39,4 @@ [submodule "ref/meshoptimizer"] path = ref/meshoptimizer url = https://github.com/zeux/meshoptimizer.git + shallow = true diff --git a/docs/eu07m-format.md b/docs/eu07m-format.md index d16bf9914..78d2c6930 100644 --- a/docs/eu07m-format.md +++ b/docs/eu07m-format.md @@ -223,7 +223,10 @@ go w EU07M: - `iFlags`, zakres widoczności i kolory zostają w węźle i w slocie materiału. - `TP_FREESPOTLIGHT` → `LGHT`; `Light_On##` / `Light_Off##` → `LSLT`; `smokesource_*` → `EMIT`; animacja `anim:` → `LANM`; `TP_TEXT` → `LTXT`; `TP_STARS` → `STAR`. -- Indeksy są zapisywane jako `u16`, gdy prymityw ma mniej niż 65 536 wierzchołków. +- Indeksy są zapisywane jako `u16`, gdy prymityw ma co najwyżej 65 536 wierzchołków (indeksy 0–65 535). +- Bity fazy rysowania (0x30) zgadnięte przy wczytaniu E3D z materiału nie trafiają do `legacy_flags` ani do flag prymitywu: + zgaduje je na nowo gra, z prawdziwym materiałem. Model pojazdu konwertuje się z `.e3d`, który robi z niego gra, nie z `.t3d`. +- Wymienna skóra `-k` w przebiegu przezroczystym (bit k−1 flag E3D) ma w `MATL` flagę `translucent`. Pozostałe polecenia: `eu07 -eu07m dump ` (zawartość jako tekst) i `eu07 -eu07m validate ` (sprawdzenie pliku wraz z hashem zawartości). diff --git a/model/Model3d.cpp b/model/Model3d.cpp index bbd927c6d..6b2230c64 100644 --- a/model/Model3d.cpp +++ b/model/Model3d.cpp @@ -27,6 +27,8 @@ Copyright (C) 2001-2004 Marcin Wozniak, Maciej Czapkiewicz and others #include "utilities/wholefile.h" #include +#include +#include //--------------------------------------------------------------------------- @@ -1801,6 +1803,27 @@ glm::vec3 TSubModel::offset(float const Geometrytestoffsetthreshold) const namespace { +// modelformat eu07m: an .eu07m older than the .e3d / .t3d it was made from isn't used, the model was changed after it was +// converted (the hash of the source in its header would need the source read whole to be compared) +bool eu07m_up_to_date(std::string const &Eu07m, std::initializer_list const Sources) +{ + std::error_code error; + auto const made{std::filesystem::last_write_time(Eu07m, error)}; + if (error) + return false; + for (auto const &source : Sources) + { + if (source.empty()) + continue; + if (auto const changed{std::filesystem::last_write_time(source, error)}; (false == bool(error)) && (changed > made)) + { + WriteLog("Model \"" + source + "\" is newer than its \"" + Eu07m + "\", which isn't used", logtype::model); + return false; + } + } + return true; +} + // Fazy szczegółowości nazwane "_LOD" (części sieci trakcyjnej) dostają zakresy widoczności tutaj, // żeby nie trzeba było wpisywać ich w modelach: kolejne fazy zmieniają się na 20, 40, 100, 300 i 400 m, // a ostatnia faza, jaką część ma, zostaje widoczna do 400 m (drobnica: model mieszczący się w promieniu 0,5 m @@ -1902,7 +1925,8 @@ bool TModel3d::LoadFromFile(std::string const &FileName, bool dynamic) Init(); assign_lod_ranges(Root, name); } - else if (auto const eu07mfile{Global.modelformat_eu07m ? FileCased(name + ".eu07m") : std::string{}}; (false == eu07mfile.empty()) && LoadFromEU07M(eu07mfile, dynamic)) + else if (auto const eu07mfile{Global.modelformat_eu07m ? FileCased(name + ".eu07m") : std::string{}}; + (false == eu07mfile.empty()) && eu07m_up_to_date(eu07mfile, {binaryfile, textfile}) && LoadFromEU07M(eu07mfile, dynamic)) { // modelformat eu07m: the .eu07m made from the model takes its place; one which doesn't load leaves the .e3d / .t3d to go on asBinary = ""; @@ -2775,6 +2799,7 @@ void TSubModel::BinInit(TSubModel *s, float4x4 *m, std::vector *t, float opacity = mat->get_or_guess_opacity(); // set phase flag based on material opacity + m_passguessed = true; if (opacity == 0.0f) iFlags |= 0x20; // translucent else diff --git a/model/Model3d.h b/model/Model3d.h index b6df14651..2024424dc 100644 --- a/model/Model3d.h +++ b/model/Model3d.h @@ -103,6 +103,9 @@ public: // bit 13: =1 wystarczy przesunięcie zamiast mnożenia macierzy (trzy jedynki) // bit 14: =1 wymagane przechowanie macierzy (animacje) // bit 15: =1 wymagane przechowanie macierzy (transform niejedynkowy) + // the pass bits (0x30) weren't in the file but guessed on load from the material (BinInit); a converter keeps them out, + // so the guess is made again with the real material where the model is drawn + bool m_passguessed{false}; union { // transform, nie każdy submodel musi mieć float4x4 *fMatrix = nullptr; // pojedyncza precyzja wystarcza diff --git a/model/eu07m/eu07m_build.cpp b/model/eu07m/eu07m_build.cpp index bae52d126..09d725a2a 100644 --- a/model/eu07m/eu07m_build.cpp +++ b/model/eu07m/eu07m_build.cpp @@ -12,6 +12,7 @@ http://mozilla.org/MPL/2.0/. #include "model/eu07m/eu07m_build.h" #include +#include #include #include @@ -58,11 +59,8 @@ std::unordered_map matrix_lookup(model const &Model) glm::mat4 node_transform(model const &Model, std::uint32_t const Node, std::unordered_map const &Matrices) { auto const &node{Model.nodes[Node]}; if ((node.flags & node_flags::matrix) != 0) { - if (auto const lookup{Matrices.find(Node)}; lookup != Matrices.end()) { - glm::mat4 matrix; - std::memcpy(&matrix[0][0], Model.transforms[lookup->second].matrix.data(), sizeof(float) * 16); - return matrix; - } + if (auto const lookup{Matrices.find(Node)}; lookup != Matrices.end()) + return glm::make_mat4(Model.transforms[lookup->second].matrix.data()); } return compose(node); } diff --git a/model/eu07m/eu07m_convert.cpp b/model/eu07m/eu07m_convert.cpp index b689eb9e2..3e6ac0676 100644 --- a/model/eu07m/eu07m_convert.cpp +++ b/model/eu07m/eu07m_convert.cpp @@ -22,10 +22,13 @@ http://mozilla.org/MPL/2.0/. #include "utilities/wholefile.h" #include +#include #include #include #include +#include #include +#include namespace eu07m { @@ -67,8 +70,16 @@ class converter { model m_model; bool m_userdata{false}; std::vector m_stars; // the nodes whose STAR components wait for their meshes + std::unordered_map m_nodeof; // the node made of each submodel }; +// the pass bits of the submodel as they come from the model file: the ones BinInit guessed from a material are left out, so +// where the model is drawn the guess is made again with the real material (the converter loads through the null renderer, +// whose materials all guess opaque) +std::uint32_t file_flags(TSubModel const &Submodel) { + return Submodel.m_passguessed ? Submodel.iFlags & ~0x30u : Submodel.iFlags; +} + bool converter::any_userdata(TSubModel const *First) const { for (auto const *submodel{First}; submodel != nullptr; submodel = submodel->Next) { if (submodel->m_geometry.handle != null_handle && false == GfxRenderer->UserData(submodel->m_geometry.handle).empty()) @@ -99,7 +110,8 @@ void converter::node(TSubModel &Submodel, std::int32_t const Parent) { auto &record{m_model.nodes.back()}; record.name = Submodel.pName.empty() ? no_index : m_model.intern(Submodel.pName); record.parent = Parent; - record.legacy_flags = Submodel.iFlags; + record.legacy_flags = file_flags(Submodel); + m_nodeof.try_emplace(&Submodel, index); record.range_min_sq = Submodel.fSquareMinDist; record.range_max_sq = Submodel.fSquareMaxDist; if ((Submodel.iFlags & 0x4000) != 0) @@ -170,8 +182,12 @@ bool converter::has_geometry(TSubModel const &Submodel) { std::uint32_t converter::material(TSubModel const &Submodel) { material_record record; - if (Submodel.m_material < 0) + if (Submodel.m_material < 0) { record.replaceable = std::max(Submodel.m_material, -4); + // the pass of a replaceable skin: bit k-1 of the E3D flags for the skin -k + if ((Submodel.iFlags & (1u << (-record.replaceable - 1))) != 0) + record.flags |= material_flags::translucent; + } else if (false == Submodel.m_materialname.empty()) record.name = m_model.intern(Submodel.m_materialname); record.selfillum = Submodel.fLight; @@ -239,7 +255,8 @@ void converter::geometry(TSubModel const &Submodel, std::uint32_t const Node) { primitive.node = Node; primitive.material = material(Submodel); auto const kind{Submodel.eType == TP_STARS ? 0u : static_cast(Submodel.eType)}; - primitive.flags = kind | ((Submodel.iFlags & 0x10) != 0 ? primitive_flags::opaque : 0u) | ((Submodel.iFlags & 0x20) != 0 ? primitive_flags::translucent : 0u); + auto const flags{file_flags(Submodel)}; + primitive.flags = kind | ((flags & 0x10) != 0 ? primitive_flags::opaque : 0u) | ((flags & 0x20) != 0 ? primitive_flags::translucent : 0u); vertices(Submodel, vertexdata, GfxRenderer->UserData(Submodel.m_geometry.handle)); if (auto const &indexdata{GfxRenderer->Indices(Submodel.m_geometry.handle)}; false == indexdata.empty()) indices(Submodel, indexdata, primitive); @@ -259,22 +276,30 @@ void converter::geometry(TSubModel const &Submodel, std::uint32_t const Node) { void converter::light_slots() { // the numbered lights the lights event of the scenery switches (TModel3d::instance_parts) + // the submodel the game finds by the name (GetFromName, which looks at the following submodels before the children), so + // with two of one name the same one counts + auto const found{[this](std::string const &Name) -> std::optional { + auto const *submodel{m_source.GetFromName(Name)}; + if (submodel == nullptr) + return std::nullopt; + auto const lookup{m_nodeof.find(submodel)}; + return lookup == m_nodeof.end() ? std::nullopt : std::optional{lookup->second}; + }}; for (int slot = 0; slot < 8; ++slot) { - auto const on{find_nodes(m_model, std::format("Light_On{:02}", slot))}; - auto const off{find_nodes(m_model, std::format("Light_Off{:02}", slot))}; - if (on.empty() && off.empty()) + auto const on{found(std::format("Light_On{:02}", slot))}; + auto const off{found(std::format("Light_Off{:02}", slot))}; + if (false == on.has_value() && false == off.has_value()) continue; payload::light_slot record; record.slot = static_cast(slot); - // as GetFromName, the first submodel of the name counts - record.on_count = on.empty() ? 0 : 1; - record.off_count = off.empty() ? 0 : 1; + record.on_count = on.has_value() ? 1 : 0; + record.off_count = off.has_value() ? 1 : 0; payload_writer writer; writer.put(record); - if (false == on.empty()) - writer.put(on.front()); - if (false == off.empty()) - writer.put(off.front()); + if (on.has_value()) + writer.put(*on); + if (off.has_value()) + writer.put(*off); m_model.add_component(component::light_slot, target_kind::model, 0, writer.data()); } } @@ -304,10 +329,18 @@ bool load_source(std::string const &Input, bool const Dynamic, TModel3d &Source, return true; } -void start_tools(int const Argc, char *Argv[]) { - Global.iConvertModels = Argc > 6 ? std::stoi(Argv[6]) : 0; +bool start_tools(int const Argc, char *Argv[]) { + Global.iConvertModels = 0; + if (Argc > 6) { + auto const flags{std::string_view{Argv[6]}}; + if (auto const [end, error]{std::from_chars(flags.data(), flags.data() + flags.size(), Global.iConvertModels)}; error != std::errc{} || end != flags.data() + flags.size()) { + std::cerr << std::format("the convertmodels flags \"{}\" aren't a number\n", flags); + return false; + } + } Global.iWriteLogEnabled = 2; GfxRenderer = gfx_renderer_factory::get_instance()->create("null"); + return true; } // the differences between two loaded models, submodel by submodel in the order of the tree @@ -446,7 +479,8 @@ int command_diff(int const Argc, char *Argv[]) { std::cerr << "usage: -eu07m diff [dynamic 0|1]\n"; return 2; } - start_tools(Argc, Argv); + if (false == start_tools(Argc, Argv)) + return 2; auto const dynamic{Argc > 5 && std::string_view{Argv[5]} == "1"}; TModel3d source; TModel3d converted; @@ -473,7 +507,14 @@ int command_convert(int const Argc, char *Argv[]) { std::string const input{Argv[3]}; std::string const output{Argv[4]}; auto const dynamic{Argc > 5 && std::string_view{Argv[5]} == "1"}; - start_tools(Argc, Argv); + if (dynamic && extension_of(input) != ".e3d") { + // the game prepares a vehicle's text model only after the vehicle marked its animated parts (WillBeAnimated); made here + // at once, their vertices would be turned into the frame of the model instead of keeping their matrices + std::cerr << "a vehicle's model is converted from the .e3d the game makes of it (convertmodels), not from its .t3d\n"; + return 2; + } + if (false == start_tools(Argc, Argv)) + return 2; TModel3d source; if (false == load_source(input, dynamic, source)) return 1; diff --git a/model/eu07m/eu07m_io.cpp b/model/eu07m/eu07m_io.cpp index 915bf5e4e..cbef66292 100644 --- a/model/eu07m/eu07m_io.cpp +++ b/model/eu07m/eu07m_io.cpp @@ -15,12 +15,14 @@ http://mozilla.org/MPL/2.0/. #include "meshoptimizer.h" #include "zstd.h" +#include #include #include #include #include #include #include +#include namespace eu07m { @@ -339,6 +341,8 @@ bool section_bytes::strings(std::vector &Output, std::string &Error // the largest section unpacked, so a broken file can't make the reader take all the memory constexpr std::uint64_t section_size_limit{std::uint64_t{1} << 30}; +// and the most all the sections of a model may unpack to together +constexpr std::uint64_t model_size_limit{std::uint64_t{2} << 30}; class file_reader { public: @@ -375,6 +379,8 @@ bool file_reader::read_header(file_header &Header) { return fail(std::format("the model has version {}.{}, this program reads version {}", Header.major, Header.minor, version_major)); if (Header.file_size > m_file.size()) return fail(std::format("the file is cut short: {} bytes of {}", m_file.size(), Header.file_size)); + if (Header.file_size < sizeof(file_header)) + return fail(std::format("the file claims {} bytes, less than its header", Header.file_size)); m_file = m_file.first(Header.file_size); if (m_options.verify_hash && hash_bytes(m_file.subspan(sizeof(file_header))) != Header.content_hash) return fail("the content of the file doesn't match its hash"); @@ -388,15 +394,19 @@ bool file_reader::read_entries(file_header const &Header, std::vector seen; + std::unordered_set seen; + seen.reserve(Entries.size()); + std::uint64_t total{0}; for (auto const &entry : Entries) { if (entry.offset > m_file.size() || entry.stored_size > m_file.size() - entry.offset) return fail(std::format("section {} lies outside the file", fourcc_name(entry.id))); if (entry.size > section_size_limit) return fail(std::format("section {} claims {} bytes", fourcc_name(entry.id), entry.size)); - if (std::ranges::find(seen, entry.id) != seen.end()) + if (false == seen.insert(entry.id).second) return fail(std::format("section {} appears twice", fourcc_name(entry.id))); - seen.push_back(entry.id); + total += entry.size; + if (total > model_size_limit) + return fail(std::format("the sections claim more than {} bytes together", model_size_limit)); } return true; } @@ -521,11 +531,18 @@ bool file_reader::read_streams(std::vector const &Sections, model std::vector streams; std::ranges::copy_if(Sections, std::back_inserter(streams), [](section_entry const &Entry) { return is_vertex_stream(Entry.id); }); std::vector errors(streams.size()); + // a stream which can't be had (its memory, say) is an error of the file like the others: an exception left in a thread + // of its own would end the program auto const unpack_one{[this, &streams, &errors, &Model](std::size_t const Index) { - file_reader worker{m_file, m_options}; - bool handled{false}; - if (false == worker.vertex_stream(streams[Index], Model, handled)) - errors[Index] = std::move(worker.m_error); + try { + file_reader worker{m_file, m_options}; + bool handled{false}; + if (false == worker.vertex_stream(streams[Index], Model, handled)) // NOSONAR: S2259 false positive, worker is a local object and no pointer is null here + errors[Index] = std::move(worker.m_error); + } + catch (std::exception const &Error) { + errors[Index] = std::format("section {} can't be unpacked: {}", fourcc_name(streams[Index].id), Error.what()); + } }}; { std::vector workers; @@ -541,6 +558,18 @@ bool file_reader::read_streams(std::vector const &Sections, model return true; } +// the sections this reader reads (dispatch() below) +constexpr std::array known_sections{section::strings, section::name_index, section::nodes, section::transforms, + section::meshes, section::primitives, section::positions, section::attributes, + section::skin_weights, section::skin_weights_wide, section::userdata, section::bindings, + section::indices16, section::indices32, section::materials, section::skins, + section::skin_joints, section::vertex_groups, section::vertex_group_items, section::vertex_group_weights, + section::components, section::component_data, section::bounds, section::edit}; + +bool known_section(std::uint32_t const Id) { + return std::ranges::find(known_sections, Id) != known_sections.end(); +} + bool file_reader::dispatch(section_entry const &Entry, section_bytes const &Section, model &Model) { std::string error; bool known{true}; @@ -601,6 +630,17 @@ read_result file_reader::run(model &Model) { for (auto const &entry : sections) { if (is_vertex_stream(entry.id)) continue; + if (false == known_section(entry.id)) { + // a section this reader doesn't know: one the model needs is refused before its codec is looked at; one it can + // do without is passed over before it is unpacked, unless the tools keep it to write it back, and always when it + // is in the codec of the streams, which this reader unpacks only for the streams it knows + if ((entry.flags & section_flags::required) != 0) { + Model = model{}; + return {false, std::format("the model needs section {}, which this program doesn't know", fourcc_name(entry.id))}; + } + if (false == m_options.keep_unknown || (entry.flags & section_flags::meshopt) != 0) + continue; + } std::span bytes; if (false == unpack(entry, storage, bytes) || false == dispatch(entry, section_bytes{entry.id, bytes}, Model)) { Model = model{}; @@ -842,11 +882,16 @@ void validator::node(std::uint32_t const Index, node_record const &Node) { void validator::nodes() { for (std::uint32_t idx = 0; idx < m_model.nodes.size() && false == full(); ++idx) node(idx, m_model.nodes[idx]); - // a node within the subtree of another has it as an ancestor: the subtrees nest + // the subtrees nest, and the parent of each node is the innermost node whose subtree holds it, so the tree the ranges + // give is the one the parents give + std::vector open; for (std::uint32_t idx = 0; idx < m_model.nodes.size() && false == full(); ++idx) { - auto const parent{m_model.nodes[idx].parent}; - if (parent >= 0 && parent < static_cast(idx) && idx >= m_model.nodes[parent].subtree_end) - fault(std::format("node {} lies outside the subtree of its parent", idx)); + while (false == open.empty() && m_model.nodes[open.back()].subtree_end <= idx) + open.pop_back(); + auto const holder{open.empty() ? std::int64_t{-1} : std::int64_t{open.back()}}; + if (m_model.nodes[idx].parent != holder) + fault(std::format("node {} has parent {}, but lies in the subtree of node {}", idx, m_model.nodes[idx].parent, holder)); + open.push_back(idx); } } diff --git a/model/eu07m/eu07m_load.cpp b/model/eu07m/eu07m_load.cpp index c517b6d81..987f25191 100644 --- a/model/eu07m/eu07m_load.cpp +++ b/model/eu07m/eu07m_load.cpp @@ -15,6 +15,7 @@ http://mozilla.org/MPL/2.0/. #include "model/eu07m/eu07m_io.h" #include "rendering/renderer.h" #include "utilities/Logs.h" +#include "utilities/utilities.h" #include "utilities/wholefile.h" #include @@ -22,7 +23,9 @@ http://mozilla.org/MPL/2.0/. #include #include #include +#include #include +#include #include #include #include @@ -334,12 +337,24 @@ void submodel_builder::fill_geometry() { // the biggest first, so the threads end at about the same time std::ranges::sort(m_jobs, std::ranges::greater{}, [](geometry_job const &Job) { return Job.primitive->vertex_count; }); std::atomic next{0}; - auto const work{[this, &next] { - while (true) { - auto const index{next.fetch_add(1)}; - if (index >= m_jobs.size()) - break; - fill(m_jobs[index]); + // the first exception of a worker (no memory for the vertices) is thrown again here, after all of them ended: left in a + // thread of its own it would end the program instead of the model falling back to its .e3d + std::exception_ptr failure; + std::mutex failurelock; + auto const work{[this, &next, &failure, &failurelock] { + try { + while (true) { + auto const index{next.fetch_add(1)}; + if (index >= m_jobs.size()) + break; + fill(m_jobs[index]); + } + } + catch (...) { + std::scoped_lock lock{failurelock}; + if (failure == nullptr) + failure = std::current_exception(); + next = m_jobs.size(); } }}; { @@ -349,6 +364,8 @@ void submodel_builder::fill_geometry() { workers.emplace_back(work); work(); } // the workers join here + if (failure != nullptr) + std::rethrow_exception(failure); } } // namespace @@ -362,8 +379,14 @@ bool TModel3d::LoadFromEU07M(std::string const &FileName, bool const dynamic) { } eu07m::model model; // the game has no use for the sections it doesn't know - if (auto const result{eu07m::read(std::as_bytes(std::span{file.data.get(), file.size}), model, {false, false})}; false == result.ok) { - ErrorLog("Bad model: \"" + FileName + "\" isn't a valid EU07M file: " + result.error, logtype::model); + try { + if (auto const result{eu07m::read(std::as_bytes(std::span{file.data.get(), file.size}), model, {false, false})}; false == result.ok) { + ErrorLog("Bad model: \"" + FileName + "\" isn't a valid EU07M file: " + result.error, logtype::model); + return false; + } + } + catch (std::exception const &Error) { + ErrorLog("Bad model: \"" + FileName + "\" can't be read: " + Error.what(), logtype::model); return false; } if (model.nodes.empty()) { @@ -381,9 +404,21 @@ bool TModel3d::LoadFromEU07M(std::string const &FileName, bool const dynamic) { iSubModelsCount = static_cast(model.nodes.size()); Root = new TSubModel[model.nodes.size()]; submodel_builder builder{model, Textures, Names, Matrices}; - for (std::uint32_t idx = 0; idx < model.nodes.size(); ++idx) - builder.build(idx, Root[idx]); - builder.fill_geometry(); + try { + for (std::uint32_t idx = 0; idx < model.nodes.size(); ++idx) + builder.build(idx, Root[idx]); + builder.fill_geometry(); + } + catch (std::exception const &Error) { + // nothing of the model is kept, so its .e3d / .t3d is loaded in its place + SafeDeleteArray(Root); + iSubModelsCount = 0; + Textures.clear(); + Names.clear(); + Matrices.clear(); + ErrorLog("Bad model: \"" + FileName + "\" can't be built: " + Error.what(), logtype::model); + return false; + } // the skins, with their joints as the submodels of the nodes m_skins.clear(); for (auto const &record : model.skins) { diff --git a/tests/unit/test_eu07m.cpp b/tests/unit/test_eu07m.cpp index 1d71b7cf3..5fac0167e 100644 --- a/tests/unit/test_eu07m.cpp +++ b/tests/unit/test_eu07m.cpp @@ -300,6 +300,17 @@ TEST_CASE("eu07m: broken files are refused with a reason, and leave the model em rehash(File); }, "don't hold together"}, + {"a header that claims fewer bytes than itself", + [](std::vector &File) { put_at(File, offsetof(eu07m::file_header, file_size), std::uint64_t{32}); }, + "less than its header"}, + {"a node in the subtree of another node than its parent", + [§ion](std::vector &File) { + // the body (node 1) reaches over the hose (node 2), whose parent is the root + put_at(File, section(eu07m::section::nodes).offset + sizeof(eu07m::table_header) + sizeof(eu07m::node_record) + offsetof(eu07m::node_record, subtree_end), + std::uint32_t{5}); + rehash(File); + }, + "lies in the subtree of node 1"}, {"a record too short for its table", [§ion](std::vector &File) { put_at(File, section(eu07m::section::primitives).offset + 4, std::uint32_t{20}); @@ -506,13 +517,49 @@ TEST_CASE("eu07m: the streams in the codecs of meshoptimizer, with and without z CHECK(loaded.nodes.empty()); } +TEST_CASE("eu07m: a section the reader doesn't know is passed over unless the tools keep it") +{ + auto const file{sample_file()}; + eu07m::model kept; + REQUIRE(eu07m::read(file, kept, {true, true}).ok); + CHECK(kept.unknown.size() == 1); + eu07m::model skipped; + auto const result{eu07m::read(file, skipped, {true, false})}; + REQUIRE_MESSAGE(result.ok, result.error); + CHECK(skipped.unknown.empty()); + kept.unknown.clear(); + CHECK(eu07m::dump(skipped) == eu07m::dump(kept)); +} + +TEST_CASE("eu07m: an unknown section in the codec of the streams doesn't stop the reading, unless the model needs it") +{ + // a later version may put a new optional stream in the codec; this one passes it over, the tools too + auto file{sample_file(false)}; + auto const offset{entry_offset(file, eu07m::fourcc("ZZZZ"))}; + auto entry{get_at(file, offset)}; + entry.flags |= eu07m::section_flags::meshopt; + put_at(file, offset, entry); + rehash(file); + eu07m::model loaded; + auto const result{eu07m::read(file, loaded, {true, true})}; + REQUIRE_MESSAGE(result.ok, result.error); + CHECK(loaded.unknown.empty()); + // one the model needs is refused as unknown, not for its codec + entry.flags |= eu07m::section_flags::required; + put_at(file, offset, entry); + rehash(file); + auto const required{eu07m::read(file, loaded, {true, true})}; + CHECK_FALSE(required.ok); + CHECK(required.error.find("doesn't know") != std::string::npos); +} + TEST_CASE("eu07m: a file with random bytes changed is read or refused, never more") { // the sample, too small for the codecs, and the grid, in them for (auto const variant : {0, 1, 2}) { auto const compress{variant == 1}; auto const good{variant < 2 ? sample_file(compress) : eu07m::write(grid_model(), {true, 9, true})}; - std::mt19937 random{static_cast(7 + variant * 4)}; + std::mt19937 random{static_cast(7 + variant * 4)}; // NOSONAR: seeded on purpose, the test changes the same bytes every run; nothing secret std::uniform_int_distribution position{0, good.size() - 1}; std::uniform_int_distribution value{0, 255}; for (int attempt = 0; attempt < 3000; ++attempt) { @@ -533,7 +580,7 @@ TEST_CASE("eu07m: a file with random bytes changed is read or refused, never mor TEST_CASE("eu07m: the tangent frame survives its packing") { - std::mt19937 random{3}; + std::mt19937 random{3}; // NOSONAR: seeded on purpose, the test packs the same frames every run; nothing secret std::uniform_real_distribution coordinate{-1.f, 1.f}; for (int attempt = 0; attempt < 2000; ++attempt) { glm::vec3 normal{coordinate(random), coordinate(random), coordinate(random)};