diff --git a/docs/bsp-validation.md b/docs/bsp-validation.md index ceebda5..e9c4a95 100644 --- a/docs/bsp-validation.md +++ b/docs/bsp-validation.md @@ -51,7 +51,12 @@ values are unsigned. Every range is checked without overflow. samples are its null lighting data plus the offset, so only offset 0 is accepted (the unlit `cz_worldmap.bsp` gives every face 0). - Mark surfaces reference faces. -- Models reference their head node; a clipping hull's head is a clipnode, a negative +- A non-world model with zero faces and zero visible leaves may have drawing head -1. + Some maps end their model lump with a marker whose only populated fields are the first + word and an origin.z of 9999. Callers can use `UtilAssetsIntegrity_IsBSPModelMarker` + to retain its slot with neutral bounds and avoid interpreting the first word as a float. + Surface queries on that hull have no node to visit. Other models reference a drawing node. + A clipping hull's head is a clipnode, a negative contents, or the clipnode count; visible leaves are below the leaf count; faces are in range. Compilers give a model without clipping brushes the next model's first clipnode, which after the last model with clipnodes is the count (stock `c2a2a.bsp`, diff --git a/include/Interface/UtilAssetsIntegrityBSPModel.h b/include/Interface/UtilAssetsIntegrityBSPModel.h new file mode 100644 index 0000000..58c46c2 --- /dev/null +++ b/include/Interface/UtilAssetsIntegrityBSPModel.h @@ -0,0 +1,22 @@ +#pragma once + +#include +#include +#include + +//A complete 64-byte disk model is required. Some maps append a marker with an opaque first word, +//origin.z = 9999.0f (little endian), and every other field zero. Its first word is not a bound. +inline bool UtilAssetsIntegrity_IsBSPModelMarker(const void* record, std::size_t index, std::size_t count) +{ + if (!index || index + 1 != count) + return false; + + constexpr std::size_t originZ = 8 * sizeof(float); + constexpr std::size_t heads = originZ + sizeof(float); + constexpr std::uint8_t zeros[64]{}; + constexpr std::uint8_t tag[] = {0x00, 0x3c, 0x1c, 0x46}; + const auto* bytes = static_cast(record); + return std::memcmp(bytes + originZ, tag, sizeof(tag)) == 0 && + std::memcmp(bytes + sizeof(float), zeros, originZ - sizeof(float)) == 0 && + std::memcmp(bytes + heads, zeros, sizeof(zeros) - heads) == 0; +} diff --git a/src/BSPValidation.h b/src/BSPValidation.h index c2ea493..0d456d6 100644 --- a/src/BSPValidation.h +++ b/src/BSPValidation.h @@ -1,6 +1,7 @@ #pragma once #include "TextureAssetValidation.h" +#include #include #include #include @@ -488,6 +489,8 @@ class CBSPValidation for (size_t i = 0; i < Count(ModelLump, ModelBytes); ++i) { const auto model = Record(ModelLump, i, ModelBytes); + if (UtilAssetsIntegrity_IsBSPModelMarker(m_pData + model, i, Count(ModelLump, ModelBytes))) + continue; //Mins, maxs and origin. if (!Finite(model, Axes * 3)) return Bad("non-finite model bounds", Reason::InvalidFormat); @@ -498,10 +501,16 @@ class CBSPValidation return Bad("inverted model bounds", Reason::InvalidFormat); } - if (!Index(ReadInt32(model + ModelHeadNodeOffset), nodes)) + //A faceless inline model may omit its drawing tree while other hulls still have collision. + constexpr int contentsEmpty = -1; + const auto headNode = ReadInt32(model + ModelHeadNodeOffset); + const auto visibleLeaves = ReadInt32(model + ModelVisLeafsOffset); + const auto faceCount = ReadInt32(model + ModelFaceCountOffset); + const bool emptyDrawingHull = i != 0 && headNode == contentsEmpty && !visibleLeaves && !faceCount; + if (!emptyDrawingHull && !Index(headNode, nodes)) return Bad("model head node out of range"); - //A negative clipping head is the contents of an empty hull. Compilers also give a model without clipping + //A negative clipping head is a terminal contents value. Compilers also give a model without clipping //brushes the next model's first clipnode, which is the clipnode count after the last one with clipnodes: //stock maps (c2a2a, de_inferno_cz) have such models, which callers must not trace. for (int hull = 1; hull < Hulls; ++hull) @@ -512,10 +521,10 @@ class CBSPValidation } //A model's visible leaves exclude leaf 0. - if (!Index(ReadInt32(model + ModelVisLeafsOffset), leaves)) + if (!Index(visibleLeaves, leaves)) return Bad("model visible leaves out of range"); - if (!Span(ReadInt32(model + ModelFirstFaceOffset), ReadInt32(model + ModelFaceCountOffset), faces)) + if (!Span(ReadInt32(model + ModelFirstFaceOffset), faceCount, faces)) return Bad("model faces out of range"); } diff --git a/tests/BSPTests.h b/tests/BSPTests.h index 47b69cb..8a40307 100644 --- a/tests/BSPTests.h +++ b/tests/BSPTests.h @@ -475,6 +475,21 @@ void CheckBSPs(IUtilAssetsIntegrity& api) } //Models. + { + F f; + F::Model marker{}; + marker.origin[2] = 9999.0f; + marker.mins[0] = std::numeric_limits::quiet_NaN(); + f.Append(F::Models, marker); + expect(Reason::OK, f, "BSP trailing marker with an opaque first word"); + marker.headNodes[1] = -1; + f.Set(F::Models, marker, 1); + expect(Reason::InvalidFormat, f, "BSP actual model still requires finite bounds"); + marker.headNodes[1] = 0; + f.Set(F::Models, marker, 1); + f.Append(F::Models, F::Model{}); + expect(Reason::InvalidFormat, f, "BSP marker must be last"); + } const auto model = [](F& f, void (*change)(F::Model&)) { auto value = f.Get(F::Models); change(value); @@ -487,6 +502,25 @@ void CheckBSPs(IUtilAssetsIntegrity& api) model(f, [](F::Model& value) { value.headNodes[0] = -1; }); expect(Reason::OutOfBound, f, "BSP negative model head node"); } + { + F f; + F::Model inlineModel{}; + inlineModel.headNodes[0] = -1; + inlineModel.headNodes[2] = -2; // CONTENTS_SOLID + inlineModel.headNodes[3] = -3; // CONTENTS_WATER + f.Append(F::Models, inlineModel); + expect(Reason::OK, f, "BSP empty drawing hull with independent clipping heads"); + inlineModel.faceCount = 1; + f.Set(F::Models, inlineModel, 1); + expect(Reason::OutOfBound, f, "BSP faces without a drawing tree"); + inlineModel.faceCount = 0; + inlineModel.visLeaves = 1; + f.Set(F::Models, inlineModel, 1); + expect(Reason::OutOfBound, f, "BSP visible leaves without a drawing tree"); + inlineModel.visLeaves = 0; + f.Set(F::Models, inlineModel); + expect(Reason::OutOfBound, f, "BSP world without a drawing tree"); + } { F f; //The clipnode count is the head compilers give a model without clipping brushes.