diff --git a/README.md b/README.md index a52420f..adeb8b6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # UtilAssetsIntegrity -A Windows x86 utility DLL for checking GoldSrc StudioModel assets and indexed-color BMP images. +A Windows x86 utility DLL for checking GoldSrc StudioModel assets, BSP brush files and indexed-color BMP images. Originally part of [MetaHookSv](https://github.com/MetaHookSv/MetaHookSv/tree/main/PluginLibs/UtilAssetsIntegrity). The initial migration preserved the original behavior. Studio validation has since @@ -23,7 +23,7 @@ scripts\build-UtilAssetsIntegrity-x86-Debug.bat Each script configures, builds, runs CTest, installs, and repeats the public-interface smoke tests against the installed DLLs. Output is under `build/x86/` and `install/x86/`. Tests dynamically load the DLL, obtain its factory, -and check StudioModel and BMP behavior without running a game. +and check StudioModel, BMP and BSP behavior without running a game. Local source trees can override automatic downloads through CMake cache arguments or environment variables. Explicit source trees are read-only inputs; CMake cache arguments @@ -60,6 +60,10 @@ using the interface and do not delete the instance. It accepts the last frame's interpolation read up to `UTIL_ASSETS_INTEGRITY_SEQUENCE_GROUP_TAIL_BYTES` past the group, as stock group files need; callers that animate the group keep that many zeroed bytes after it. +- `CheckBSP` checks a version 30 brush file in the given lump layout (GoldSrc or Blue + Shift) before an engine parses it: lump ranges, every cross-reference, light samples, + visibility rows and the node graphs. `MaxLeafs` bounds the leaf count with the + caller's PVS capacity. See [BSP validation](docs/bsp-validation.md). - `Check8bitBMP` checks for FreeImage's indexed-color classification and applies `MaxWidth`, `MaxHeight` and `MaxSize` when a result object is supplied. - `MaxSize` is the decoded pixel count, not the input file size. All three limits diff --git a/README.zh-CN.md b/README.zh-CN.md index d94d53a..e1c63c6 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -1,6 +1,6 @@ # UtilAssetsIntegrity -用于检查 GoldSrc StudioModel 和索引色 BMP 图像的 Windows x86 工具 DLL。 +用于检查 GoldSrc StudioModel、BSP 地图文件和索引色 BMP 图像的 Windows x86 工具 DLL。 源码来自 [MetaHookSv](https://github.com/MetaHookSv/MetaHookSv/tree/main/PluginLibs/UtilAssetsIntegrity), 保留 `CreateInterface("UtilAssetsIntegrityAPI_001", ...)` 的 vtable 和结果布局; Studio 校验已修正边界错误并补充结构与动画检查,详见[校验更新](docs/studio-validation.md)。 @@ -39,6 +39,9 @@ scripts\build-UtilAssetsIntegrity-x86-Debug.bat - `CheckStudioSequenceGroup` 依据主模型检查 IDSQ 文件的动画数据。原版序列组文件需要 最后一帧的插值前瞻读取越过文件末尾,因此允许其最多越过 `UTIL_ASSETS_INTEGRITY_SEQUENCE_GROUP_TAIL_BYTES` 字节;对序列组做动画的调用方需在数据后保留同样多的零字节。 +- `CheckBSP` 按调用方给出的 lump 布局(GoldSrc 或 Blue Shift),在引擎解析之前检查版本 30 的 + 地图文件:lump 范围、所有交叉引用、光照采样、可见性行和节点图。`MaxLeafs` 以调用方的 PVS + 容量限制叶子数。详见 [BSP 校验](docs/bsp-validation.md)。 - `Check8bitBMP` 使用 FreeImage 的索引色分类检查图像,传入结果对象时应用 `MaxWidth`、`MaxHeight` 和 `MaxSize` 限制。 - `MaxSize` 指解码后的像素数量,三项限制默认为零,检查非空图像前需要设置; diff --git a/docs/bsp-validation.md b/docs/bsp-validation.md new file mode 100644 index 0000000..ceebda5 --- /dev/null +++ b/docs/bsp-validation.md @@ -0,0 +1,107 @@ +# BSP validation + +`CheckBSP(buf, bufSize, layout, result)` checks a version 30 brush file before any of +it is parsed. It is appended to `UtilAssetsIntegrityAPI_001` after +`CheckStudioSequenceGroup()`, so the existing vtable slots and result layouts stay +unchanged. The input is borrowed synchronously and the DLL keeps none of it. + +The guarantee is about the GoldSrc engine's loader and the code that reads what it +loads: for a file that passes, `Mod_LoadBrushModel`, `CalcSurfaceExtents`, +`Mod_SetParent`, `Mod_MakeHull0`, `Mod_SetupSubmodel`, the hull traces, +`RecursiveLightPoint`, `R_BuildLightMap` and `CM_DecompressPVS` stay inside the file's +records and terminate. Records are read at their fixed little-endian offsets, so the +validator includes neither an SDK's nor an engine's `bspfile.h`, and an engine's +enlarged `MAX_MAP_*` capacities do not change it. + +## Inputs + +- `layout`: `UtilAssetsIntegrityBSPLayout::GoldSrc`, or `BlueShift` for Blue Shift's + brush files, which swap the entities and planes entries of the lump table. The file + does not record which it uses; engines pick it by game, so the caller passes it. Any + other value is `InvalidFormat`. +- `result->MaxLeafs`: the leaf records the caller's PVS buffers hold (an engine's + `MAX_MAP_LEAFS`). More leaves is `SizeTooLarge`. As with the BMP limits, the limit + starts at zero and is enforced; a null result pointer skips it and all diagnostics. + +## Checks + +Fields are read as the engine reads them: `dface_t` and `dclipnode_t` shorts and the +node children are signed; node face ranges, leaf mark surface ranges and mark surface +values are unsigned. Every range is checked without overflow. + +- Header: at least 124 bytes, at most `INT_MAX`, version 30. Every lump lies inside + the file, and the fixed-size lumps hold whole records. Models, nodes and leaves are + not empty (the engine reads model 0, its head node and leaf 0 unconditionally). +- Planes, vertices, texture vectors and model bounds are finite; model mins do not + exceed maxs. +- Edges reference vertices; surface edges reference edges, and `INT_MIN` is refused. +- Texture lump: the offset table and every present miptex (offset `-1` is absent) lie + inside the lump; names are terminated; dimensions are 1..16384. An embedded miptex + (`offsets[0] != 0`) is 16 aligned and holds its four mip levels, its color count and + a palette of at least 256 colors, the size `GL_LoadTexture` reads. WAD textures are + read by name, outside this check. +- Texture infos reference a miptex when the file has a texture lump. +- Faces reference a plane, a texture info and at least one surface edge in range. + Extents are computed as `CalcSurfaceExtents` computes them, keeping its float + extremes and its shorts. A face without `TEX_SPECIAL` may not exceed 512 texels. + Unless the light offset is `-1`, it is not negative, and its samples (one 3-byte + luxel per 16 texels plus one, per style before the first 255) lie inside the lighting + lump. Only special faces over 512 are exempt: a missing texture clears a special + face's flags, after which its samples are read. Without a lighting lump the engine's + 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 + 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`, + `de_inferno_cz.bsp` and `dod_jagd.bsp` have such models): callers must not trace a + hull whose head is the count. +- Nodes reference a plane, children nodes or leaves (`-1 - child`), and faces in range. +- Leaves have negative contents, which `Mod_SetParent` and `Mod_MakeHull0` use to tell + them from nodes, and mark surfaces in range. A visibility offset other than `-1` + decodes a whole row as `CM_DecompressPVS` does: at least once, with every byte inside + the visibility lump, until it reaches `(visleafs + 7) / 8` bytes for model 0's visible + leaves, the leaf count `Mod_SetupSubmodel` gives the world. Compilers write longer + rows (hlvis pads them to 64 leaves; the stock Opposing Force and many Counter-Strike + maps hold a bit for every leaf), and the engine does not clip the run of zeros that + crosses its row end, so a run may end anywhere up to the longer of those two rows. + Within `MaxLeafs`, that stays inside the caller's `MAX_MAP_LEAFS / 8` buffers; callers + that decode into rows sized by the visible leaves must clip the run. Without a + visibility lump, as for lighting, only offset 0 is accepted, which the engine reads + as seeing every leaf. +- Clipnodes reference a plane and children clipnodes; negative children are contents. +- Graphs: the node and clipnode graphs are acyclic and no deeper than 1024 levels, + bounding the engine's recursion. Clipnode subtrees may be shared, as compilers merge + identical clipnodes. The drawing tree below node 0, which `Mod_SetParent` recurses, + reaches every node once. + +Failures fill `ReasonStr` with a `BSP:` diagnostic: `SizeTooSmall` and +`VersionMismatch` for the header, `SizeTooLarge` for the size and leaf limits, +`InvalidFormat` for record sizes, non-finite values, extents and graph shapes, and +`OutOfBound` for references and ranges. + +## Left to the caller + +- WAD textures, which are loaded by name from outside the file. +- Terminating the entities: Condition Zero: Deleted Scenes ends them with a space, so + the lump is not required to hold a zero; callers copy it with a terminator. +- Engine policy that depends on how it draws a face, such as `GL_SubdivideSurface`'s + 64-edge limit for warped faces, chosen by texture name. +- The meaning of entity key values. + +## Tests + +`tests/BSPTests.h` builds a complete one-face world lump by lump and checks it through +the real DLL, then breaks each rule above one field at a time, including both +layouts. The smoke runner's optional directory argument also scans every `.bsp` below +it, using the Blue Shift layout below a `bshift` directory. + +## Stock map scan + +Scanning 496 brush files of an installed Half-Life (valve, bshift, gearbox, cstrike, +czero, czeror, dod and several mods) accepted all but two `!Src.bsp` files in `czero` +and `trinity`, whose surface extents exceed 512; the engine's `CalcSurfaceExtents` +refuses them too. The relaxed rules above (clipping heads at the count, runs past the +engine row, offset 0 without lighting or visibility, unterminated entities) each come +from stock files this scan first rejected. diff --git a/include/Interface/IUtilAssetsIntegrity.h b/include/Interface/IUtilAssetsIntegrity.h index 455569e..e01e980 100644 --- a/include/Interface/IUtilAssetsIntegrity.h +++ b/include/Interface/IUtilAssetsIntegrity.h @@ -49,6 +49,28 @@ class UtilAssetsIntegrityCheckResult_BMP : public UtilAssetsIntegrityCheckResult size_t MaxSize; }; +//Order of a BSP file's lump table. Blue Shift's brush files swap the entities and planes entries; the file does not say +//which it uses, so the caller passes the layout it loads the file with. +enum class UtilAssetsIntegrityBSPLayout +{ + GoldSrc = 0, + BlueShift, +}; + +class UtilAssetsIntegrityCheckResult_BSP : public UtilAssetsIntegrityCheckResult +{ +public: + UtilAssetsIntegrityCheckResult_BSP() : UtilAssetsIntegrityCheckResult() + { + MaxLeafs = 0; + } + +public: + //Leaf records the caller's PVS buffers hold (an engine's MAX_MAP_LEAFS). As with the BMP limits, zero is enforced + //and a null result pointer skips the limit. + size_t MaxLeafs; +}; + class IUtilAssetsIntegrity : public IBaseInterface { public: @@ -67,6 +89,15 @@ class IUtilAssetsIntegrity : public IBaseInterface const void* group, size_t groupBytes, UtilAssetsIntegrityCheckResult_StudioModel* result) = 0; + + //Checks a version 30 BSP before any of it is parsed: every lump range and record size, every cross-reference as the + //GoldSrc engine interprets it, the light samples and visibility rows it decodes, and that its node and clipnode + //graphs are acyclic and shallow enough to recurse. Appended after the existing methods, as above. + virtual UtilAssetsIntegrityCheckReason CheckBSP( + const void* buf, + size_t bufSize, + UtilAssetsIntegrityBSPLayout layout, + UtilAssetsIntegrityCheckResult_BSP* result) = 0; }; //The interface is published only through EXPOSE_SINGLE_INTERFACE; callers resolve diff --git a/memory/project_overview.md b/memory/project_overview.md index 0004b3c..c08d37c 100644 --- a/memory/project_overview.md +++ b/memory/project_overview.md @@ -22,6 +22,26 @@ That method lets the last frame's interpolation read end up to Half-Life group file needs; callers that animate a group keep that many zeroed bytes after it, and main models stay checked strictly. +BSP brush files are checked by `CheckBSP()`, appended after `CheckStudioSequenceGroup()` +and implemented in `src/BSPValidation.h`. It validates a version 30 file before an engine +parses it, reading every field as the GoldSrc engine reads it (signed `dface_t` and +clipnode shorts, unsigned node face, leaf mark and mark surface values): lump ranges and +record sizes, all cross-references, embedded miptex data, the light samples +`RecursiveLightPoint`/`R_BuildLightMap` read, every visibility row decoded as +`CM_DecompressPVS` decodes it (row length from model 0's `visleafs`; a run may end at the +longer compiled row, padded to 64 leaves or covering every leaf), and acyclic node and +clipnode graphs at most 1024 levels deep, with the drawing tree below node 0 unshared. +A scan of 496 installed brush files accepted all but two the engine also refuses; the +stock files it first rejected shaped the relaxed rules (clipping heads at the clipnode +count, runs past the engine row, offset 0 without lighting or visibility, unterminated +entities), as the sequence-group tail did for Studio validation. +The caller passes the lump layout (`UtilAssetsIntegrityBSPLayout`, GoldSrc or Blue Shift) +and its PVS capacity in `UtilAssetsIntegrityCheckResult_BSP::MaxLeafs`. See +`docs/bsp-validation.md` for the rules and what stays with the caller (WAD textures, +draw-dependent limits such as the 64-edge warp polygon, terminating the entities, not +tracing a hull whose head is the clipnode count, clipping visibility runs to rows sized +by the visible leaves). + The remainder of this note is the **historical initial-migration snapshot**. Its statements about preserved buggy behavior, missing validation and byte-identical sources describe that snapshot, not the current implementation. In particular, a @@ -196,11 +216,14 @@ license terms stay in their upstream repositories. Consumers of - `src/UtilAssetsIntegrity.cpp`, `src/dllmain.cpp` — implementation and DLL entry point. - `include/Interface/IUtilAssetsIntegrity.h` — public header (shipped with the sources, not installed). +- `src/StudioModelValidation.h`, `src/BSPValidation.h` — the Studio and BSP validators. - `tests/SmokeTests.cpp` — factory, singleton and validation smoke tests (CTest). +- `tests/StudioModelTests.h`, `tests/BSPTests.h` — Studio and BSP regressions run by the smoke tests. - `CMakeLists.txt`, `cmake/Dependencies.cmake` — build and dependency pinning. - `scripts/build-UtilAssetsIntegrity-x86-{Debug,Release}.bat` — configure/build/test/install entry points. - `docs/build.md` — build commands, dependency inputs, verification gates and migration records. +- `docs/studio-validation.md`, `docs/bsp-validation.md` — the current validation guarantees. - `README.md`, `README.zh-CN.md` — documentation. ## Build and verification diff --git a/src/BSPValidation.h b/src/BSPValidation.h new file mode 100644 index 0000000..952b704 --- /dev/null +++ b/src/BSPValidation.h @@ -0,0 +1,797 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +//Validates a version 30 BSP as the GoldSrc engine reads it (Mod_LoadBrushModel, CalcSurfaceExtents, Mod_SetParent, the +//hull traces, RecursiveLightPoint, R_BuildLightMap and CM_DecompressPVS), before any of it is parsed. Records are read +//at their fixed little-endian offsets, so neither an SDK's nor an engine's bspfile.h and MAX_MAP_* capacities are used. +class CBSPValidation +{ +public: + using Reason = UtilAssetsIntegrityCheckReason; + using Layout = UtilAssetsIntegrityBSPLayout; + + CBSPValidation(const void* pData, size_t size, Layout layout, UtilAssetsIntegrityCheckResult_BSP* pResult) : + m_pData(static_cast(pData)), + m_Size(size), + m_Layout(layout), + m_pResult(pResult) + { + if (m_pResult) + m_pResult->ReasonStr[0] = 0; + } + + Reason Check() + { + if (!m_pData || m_Size < HeaderBytes) + return Fail("truncated header", Reason::SizeTooSmall); + + if (m_Size > INT_MAX) + return Fail("file exceeds signed lump offsets", Reason::SizeTooLarge); + + if (m_Layout != Layout::GoldSrc && m_Layout != Layout::BlueShift) + return Fail("unknown lump layout", Reason::InvalidFormat); + + if (ReadInt32(0) != Version) + return Fail("expected BSP version 30", Reason::VersionMismatch); + + auto code = Lumps(); + if (code != Reason::OK) + return code; + + if (m_pResult && Count(LeafLump, LeafBytes) > m_pResult->MaxLeafs) + return Fail("more leaves than the caller's PVS capacity", Reason::SizeTooLarge); + + if (!Geometry() || + !Textures() || + !TexInfos() || + !Faces() || + !MarkSurfaces() || + !Models() || + !Nodes() || + !Leaves() || + !ClipNodes() || + !Trees()) + { + return m_Reason; + } + + return Reason::OK; + } + +private: + static constexpr std::int32_t Version = 30; + static constexpr size_t LumpCount = 15; + static constexpr size_t HeaderBytes = sizeof(std::int32_t) + LumpCount * 2 * sizeof(std::int32_t); + + //Lumps by meaning; for Blue Shift the entities and planes come from each other's table entry. + static constexpr size_t EntityLump = 0; + static constexpr size_t PlaneLump = 1; + static constexpr size_t TextureLump = 2; + static constexpr size_t VertexLump = 3; + static constexpr size_t VisibilityLump = 4; + static constexpr size_t NodeLump = 5; + static constexpr size_t TexInfoLump = 6; + static constexpr size_t FaceLump = 7; + static constexpr size_t LightingLump = 8; + static constexpr size_t ClipNodeLump = 9; + static constexpr size_t LeafLump = 10; + static constexpr size_t MarkSurfaceLump = 11; + static constexpr size_t EdgeLump = 12; + static constexpr size_t SurfEdgeLump = 13; + static constexpr size_t ModelLump = 14; + + static constexpr size_t PlaneBytes = 20; + static constexpr size_t VertexBytes = 12; + static constexpr size_t NodeBytes = 24; + static constexpr size_t TexInfoBytes = 40; + static constexpr size_t FaceBytes = 20; + static constexpr size_t ClipNodeBytes = 8; + static constexpr size_t LeafBytes = 28; + static constexpr size_t MarkSurfaceBytes = 2; + static constexpr size_t EdgeBytes = 4; + static constexpr size_t SurfEdgeBytes = 4; + static constexpr size_t ModelBytes = 64; + static constexpr size_t MiptexBytes = 40; + + static constexpr size_t NodeChildrenOffset = 4; + static constexpr size_t NodeFirstFaceOffset = 20; + static constexpr size_t NodeFaceCountOffset = 22; + static constexpr size_t ClipChildrenOffset = 4; + static constexpr size_t TexInfoMiptexOffset = 32; + static constexpr size_t TexInfoFlagsOffset = 36; + static constexpr size_t FaceFirstEdgeOffset = 4; + static constexpr size_t FaceEdgeCountOffset = 8; + static constexpr size_t FaceTexInfoOffset = 10; + static constexpr size_t FaceStylesOffset = 12; + static constexpr size_t FaceLightOffset = 16; + static constexpr size_t LeafVisOffset = 4; + static constexpr size_t LeafFirstMarkOffset = 20; + static constexpr size_t LeafMarkCountOffset = 22; + static constexpr size_t ModelMaxsOffset = 12; + static constexpr size_t ModelHeadNodeOffset = 36; + static constexpr size_t ModelVisLeafsOffset = 52; + static constexpr size_t ModelFirstFaceOffset = 56; + static constexpr size_t ModelFaceCountOffset = 60; + static constexpr size_t MiptexWidthOffset = 16; + static constexpr size_t MiptexHeightOffset = 20; + static constexpr size_t MiptexPixelsOffset = 24; + static constexpr size_t MiptexNameBytes = 16; + + static constexpr int Axes = 3; + static constexpr int TextureAxes = 2; + static constexpr int TextureVecs = 4; + static constexpr int Hulls = 4; + static constexpr int LightStyles = 4; + static constexpr int NoLightStyle = 255; + static constexpr int LightSample = 3; + static constexpr int LuxelSize = 16; + static constexpr int TexSpecial = 1; + static constexpr int AbsentOffset = -1; + static constexpr int MaxTextureSide = 16384; + static constexpr int TextureAlign = 16; + static constexpr size_t PaletteColors = 256; + static constexpr size_t PaletteCountLen = sizeof(std::uint16_t); + //A face whose extents exceed this either errors in CalcSurfaceExtents or is TEX_SPECIAL, which has no lightmap. + static constexpr int MaxSurfaceExtent = 512; + //CalcSurfaceExtents' starting extremes, kept as its floats. + static constexpr float ExtentsInitialMin = 999999.0f; + static constexpr float ExtentsInitialMax = -99999.0f; + //Texture coordinates beyond this make floor(coordinate / 16) overflow int. + static constexpr double MaxTextureCoordinate = 268435456.0; + //Mod_SetParent, the hull traces and the light point recurse once per level; stock maps stay far below this. + static constexpr int MaxTreeDepth = 1024; + + Reason Fail(const char* text, Reason reason = Reason::OutOfBound) + { + if (m_pResult) + std::snprintf(m_pResult->ReasonStr, sizeof(m_pResult->ReasonStr), "BSP: %s.", text); + + m_Reason = reason; + return reason; + } + + bool Bad(const char* text, Reason reason = Reason::OutOfBound) + { + Fail(text, reason); + return false; + } + + static bool Range(size_t offset, size_t count, size_t stride, size_t limit) + { + return offset <= limit && stride && count <= (limit - offset) / stride; + } + + template + T Read(size_t offset) const + { + T value; + std::memcpy(&value, m_pData + offset, sizeof(value)); + return value; + } + + std::int32_t ReadInt32(size_t offset) const { return Read(offset); } + std::int16_t ReadInt16(size_t offset) const { return Read(offset); } + std::uint16_t ReadUInt16(size_t offset) const { return Read(offset); } + float ReadFloat(size_t offset) const { return Read(offset); } + + size_t Count(size_t lump, size_t recordBytes) const + { + return m_Lengths[lump] / recordBytes; + } + + //File offset of record index of a lump, which Lumps() has sized. + size_t Record(size_t lump, size_t index, size_t recordBytes) const + { + return m_Offsets[lump] + index * recordBytes; + } + + static bool Index(std::int64_t value, size_t count) + { + return value >= 0 && static_cast(value) < count; + } + + //[first, first + count) within total, both read as the engine reads them. + static bool Span(std::int64_t first, std::int64_t count, size_t total) + { + return first >= 0 && count >= 0 && Range(static_cast(first), static_cast(count), 1, total); + } + + bool Finite(size_t offset, int floats) const + { + for (int i = 0; i < floats; ++i) + { + if (!std::isfinite(ReadFloat(offset + i * sizeof(float)))) + return false; + } + + return true; + } + + Reason Lumps() + { + for (size_t i = 0; i < LumpCount; ++i) + { + const auto entry = sizeof(std::int32_t) + i * 2 * sizeof(std::int32_t); + const auto offset = ReadInt32(entry); + const auto length = ReadInt32(entry + sizeof(std::int32_t)); + + if (offset < 0 || length < 0 || !Range(static_cast(offset), static_cast(length), 1, m_Size)) + return Fail("lump outside file"); + + m_Offsets[i] = static_cast(offset); + m_Lengths[i] = static_cast(length); + } + + if (m_Layout == Layout::BlueShift) + { + std::swap(m_Offsets[EntityLump], m_Offsets[PlaneLump]); + std::swap(m_Lengths[EntityLump], m_Lengths[PlaneLump]); + } + + //Mod_LoadBmodel's "funny lump size". + const size_t records[][2] = { + {PlaneLump, PlaneBytes}, + {VertexLump, VertexBytes}, + {NodeLump, NodeBytes}, + {TexInfoLump, TexInfoBytes}, + {FaceLump, FaceBytes}, + {ClipNodeLump, ClipNodeBytes}, + {LeafLump, LeafBytes}, + {MarkSurfaceLump, MarkSurfaceBytes}, + {EdgeLump, EdgeBytes}, + {SurfEdgeLump, SurfEdgeBytes}, + {ModelLump, ModelBytes}, + }; + + for (const auto& record : records) + { + if (m_Lengths[record[0]] % record[1]) + return Fail("lump size is not a whole number of records", Reason::InvalidFormat); + } + + //Model 0 is the world; the engine reads its head node and leaf 0 unconditionally. + if (!Count(ModelLump, ModelBytes) || !Count(NodeLump, NodeBytes) || !Count(LeafLump, LeafBytes)) + return Fail("missing model, node or leaf", Reason::InvalidFormat); + + return Reason::OK; + } + + bool Geometry() + { + for (size_t i = 0; i < Count(PlaneLump, PlaneBytes); ++i) + { + if (!Finite(Record(PlaneLump, i, PlaneBytes), Axes + 1)) + return Bad("non-finite plane", Reason::InvalidFormat); + } + + const auto vertices = Count(VertexLump, VertexBytes); + for (size_t i = 0; i < vertices; ++i) + { + if (!Finite(Record(VertexLump, i, VertexBytes), Axes)) + return Bad("non-finite vertex", Reason::InvalidFormat); + } + + const auto edges = Count(EdgeLump, EdgeBytes); + for (size_t i = 0; i < edges; ++i) + { + const auto offset = Record(EdgeLump, i, EdgeBytes); + if (ReadUInt16(offset) >= vertices || ReadUInt16(offset + sizeof(std::uint16_t)) >= vertices) + return Bad("edge vertex out of range"); + } + + for (size_t i = 0; i < Count(SurfEdgeLump, SurfEdgeBytes); ++i) + { + const std::int64_t edge = ReadInt32(Record(SurfEdgeLump, i, SurfEdgeBytes)); + if (!Index(edge < 0 ? -edge : edge, edges)) + return Bad("surface edge out of range"); + } + + return true; + } + + //Mod_LoadTextures copies an embedded miptex's palette from after its four mip levels and its color count, and + //GL_LoadTexture reads a whole 256-color palette there. Names are used as strings, embedded or not. + bool Textures() + { + const auto length = m_Lengths[TextureLump]; + if (!length) + return true; + + const auto base = m_Offsets[TextureLump]; + if (length < sizeof(std::int32_t)) + return Bad("truncated texture lump"); + + const auto count = ReadInt32(base); + if (count < 0 || !Range(sizeof(std::int32_t), static_cast(count), sizeof(std::int32_t), length)) + return Bad("texture offset table outside lump"); + + m_Miptexes = static_cast(count); + + for (size_t i = 0; i < m_Miptexes; ++i) + { + const auto offset = ReadInt32(base + sizeof(std::int32_t) + i * sizeof(std::int32_t)); + if (offset == AbsentOffset) + continue; + + if (offset < 0 || !Range(static_cast(offset), 1, MiptexBytes, length)) + return Bad("miptex outside lump"); + + const auto miptex = base + static_cast(offset); + if (!std::memchr(m_pData + miptex, 0, MiptexNameBytes)) + return Bad("unterminated miptex name"); + + const auto width = Read(miptex + MiptexWidthOffset); + const auto height = Read(miptex + MiptexHeightOffset); + if (!width || !height || width > MaxTextureSide || height > MaxTextureSide) + return Bad("invalid miptex dimensions", Reason::InvalidFormat); + + //Textures without embedded pixels come from the WADs by name. + if (!Read(miptex + MiptexPixelsOffset)) + continue; + + if ((width % TextureAlign) || (height % TextureAlign)) + return Bad("miptex is not 16 aligned", Reason::InvalidFormat); + + //Four mip levels: 1 + 1/4 + 1/16 + 1/64 of the pixels, as the engine's width * height / 64 * 85. + const auto pixelBytes = std::uint64_t(width) * height / 64 * 85; + const auto paletteCount = miptex + MiptexBytes + pixelBytes; + const auto end = base + length; + if (paletteCount > end || PaletteCountLen > end - paletteCount) + return Bad("miptex pixels outside lump"); + + const auto colors = Read(static_cast(paletteCount)); + const auto bytes = std::uint64_t(colors < PaletteColors ? PaletteColors : colors) * LightSample; + if (bytes > end - paletteCount - PaletteCountLen) + return Bad("miptex palette outside lump"); + } + + return true; + } + + bool TexInfos() + { + for (size_t i = 0; i < Count(TexInfoLump, TexInfoBytes); ++i) + { + const auto offset = Record(TexInfoLump, i, TexInfoBytes); + if (!Finite(offset, TextureAxes * TextureVecs)) + return Bad("non-finite texture vectors", Reason::InvalidFormat); + + //Without a texture lump every face gets the missing texture. + if (m_Lengths[TextureLump] && !Index(ReadInt32(offset + TexInfoMiptexOffset), m_Miptexes)) + return Bad("texinfo miptex out of range"); + } + + return true; + } + + //CalcSurfaceExtents, which keeps texturemins and extents as shorts. + bool FaceExtents(size_t face, size_t texInfo, std::int16_t (&extents)[TextureAxes]) + { + float mins[TextureAxes] = {ExtentsInitialMin, ExtentsInitialMin}; + float maxs[TextureAxes] = {ExtentsInitialMax, ExtentsInitialMax}; + + const auto firstEdge = ReadInt32(face + FaceFirstEdgeOffset); + const auto edgeCount = ReadInt16(face + FaceEdgeCountOffset); + + for (int i = 0; i < edgeCount; ++i) + { + const auto edge = ReadInt32(Record(SurfEdgeLump, static_cast(firstEdge) + i, SurfEdgeBytes)); + const auto record = Record(EdgeLump, static_cast(edge >= 0 ? edge : -edge), EdgeBytes); + const auto vertex = ReadUInt16(record + (edge >= 0 ? 0 : sizeof(std::uint16_t))); + const auto point = Record(VertexLump, vertex, VertexBytes); + + for (int axis = 0; axis < TextureAxes; ++axis) + { + const auto vec = texInfo + axis * TextureVecs * sizeof(float); + const auto value = static_cast(ReadFloat(point)) * ReadFloat(vec) + + static_cast(ReadFloat(point + sizeof(float))) * ReadFloat(vec + sizeof(float)) + + static_cast(ReadFloat(point + 2 * sizeof(float))) * ReadFloat(vec + 2 * sizeof(float)) + + static_cast(ReadFloat(vec + 3 * sizeof(float))); + + if (!(std::fabs(value) <= MaxTextureCoordinate)) + return Bad("texture coordinate out of range", Reason::InvalidFormat); + + const auto coordinate = static_cast(value); + if (coordinate < mins[axis]) + mins[axis] = coordinate; + if (coordinate > maxs[axis]) + maxs[axis] = coordinate; + } + } + + for (int axis = 0; axis < TextureAxes; ++axis) + { + const auto luxelMin = static_cast(std::floor(mins[axis] / LuxelSize)); + const auto luxelMax = static_cast(std::ceil(maxs[axis] / LuxelSize)); + extents[axis] = static_cast((luxelMax - luxelMin) * LuxelSize); + } + + return true; + } + + bool Faces() + { + const auto planes = Count(PlaneLump, PlaneBytes); + const auto texInfos = Count(TexInfoLump, TexInfoBytes); + const auto surfEdges = Count(SurfEdgeLump, SurfEdgeBytes); + const auto lighting = m_Lengths[LightingLump]; + + for (size_t i = 0; i < Count(FaceLump, FaceBytes); ++i) + { + const auto face = Record(FaceLump, i, FaceBytes); + const auto edges = ReadInt16(face + FaceEdgeCountOffset); + + if (!Index(ReadInt16(face), planes) || + !Index(ReadInt16(face + FaceTexInfoOffset), texInfos)) + { + return Bad("face plane or texinfo out of range"); + } + + if (edges < 1 || !Span(ReadInt32(face + FaceFirstEdgeOffset), edges, surfEdges)) + return Bad("face edges out of range"); + + const auto texInfo = Record(TexInfoLump, static_cast(ReadInt16(face + FaceTexInfoOffset)), TexInfoBytes); + std::int16_t extents[TextureAxes]; + if (!FaceExtents(face, texInfo, extents)) + return false; + + const bool bSpecial = (ReadInt32(texInfo + TexInfoFlagsOffset) & TexSpecial) != 0; + const bool bLarge = extents[0] > MaxSurfaceExtent || extents[1] > MaxSurfaceExtent; + if (!bSpecial && bLarge) + return Bad("surface extents exceed 512", Reason::InvalidFormat); + + const auto lightOffset = ReadInt32(face + FaceLightOffset); + if (lightOffset == AbsentOffset) + continue; + + if (lightOffset < 0) + return Bad("negative light offset"); + + //Without lighting the engine's samples are the null lightdata plus the offset: only 0 is null, which reads as + //unlit (the unlit cz_worldmap gives every face 0). + if (!lighting) + { + if (lightOffset) + return Bad("light offset without lighting"); + + continue; + } + + //Large special faces are drawn tiled and never sampled. Any other face may be, by RecursiveLightPoint + //and R_BuildLightMap, even if it is special: a missing texture clears its flags. + if (bLarge) + continue; + + const int luxelsS = (extents[0] >> 4) + 1; + const int luxelsT = (extents[1] >> 4) + 1; + if (luxelsS <= 0 || luxelsT <= 0) + return Bad("negative surface extents", Reason::InvalidFormat); + + int styles = 0; + while (styles < LightStyles && Read(face + FaceStylesOffset + styles) != NoLightStyle) + ++styles; + + const auto samples = static_cast(luxelsS) * static_cast(luxelsT) * static_cast(styles); + if (!Range(static_cast(lightOffset), samples, LightSample, lighting)) + return Bad("light samples outside lighting"); + } + + return true; + } + + //The values are unsigned shorts in the file. + bool MarkSurfaces() + { + const auto faces = Count(FaceLump, FaceBytes); + for (size_t i = 0; i < Count(MarkSurfaceLump, MarkSurfaceBytes); ++i) + { + if (ReadUInt16(Record(MarkSurfaceLump, i, MarkSurfaceBytes)) >= faces) + return Bad("mark surface out of range"); + } + + return true; + } + + bool Models() + { + const auto nodes = Count(NodeLump, NodeBytes); + const auto clipNodes = Count(ClipNodeLump, ClipNodeBytes); + const auto leaves = Count(LeafLump, LeafBytes); + const auto faces = Count(FaceLump, FaceBytes); + + for (size_t i = 0; i < Count(ModelLump, ModelBytes); ++i) + { + const auto model = Record(ModelLump, i, ModelBytes); + //Mins, maxs and origin. + if (!Finite(model, Axes * 3)) + return Bad("non-finite model bounds", Reason::InvalidFormat); + + for (int axis = 0; axis < Axes; ++axis) + { + if (ReadFloat(model + axis * sizeof(float)) > ReadFloat(model + ModelMaxsOffset + axis * sizeof(float))) + return Bad("inverted model bounds", Reason::InvalidFormat); + } + + if (!Index(ReadInt32(model + ModelHeadNodeOffset), 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 + //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) + { + const std::int64_t head = ReadInt32(model + ModelHeadNodeOffset + hull * sizeof(std::int32_t)); + if (head >= 0 && static_cast(head) > clipNodes) + return Bad("model clipping head out of range"); + } + + //A model's visible leaves exclude leaf 0. + if (!Index(ReadInt32(model + ModelVisLeafsOffset), leaves)) + return Bad("model visible leaves out of range"); + + if (!Span(ReadInt32(model + ModelFirstFaceOffset), ReadInt32(model + ModelFaceCountOffset), faces)) + return Bad("model faces out of range"); + } + + return true; + } + + //Children are nodes when not negative, otherwise leaf -1 - child. + bool Nodes() + { + const auto planes = Count(PlaneLump, PlaneBytes); + const auto nodes = Count(NodeLump, NodeBytes); + const auto leaves = Count(LeafLump, LeafBytes); + const auto faces = Count(FaceLump, FaceBytes); + + for (size_t i = 0; i < nodes; ++i) + { + const auto node = Record(NodeLump, i, NodeBytes); + if (!Index(ReadInt32(node), planes)) + return Bad("node plane out of range"); + + for (int side = 0; side < 2; ++side) + { + const std::int64_t child = ReadInt16(node + NodeChildrenOffset + side * sizeof(std::int16_t)); + if (child >= 0 ? !Index(child, nodes) : !Index(-1 - child, leaves)) + return Bad("node child out of range"); + } + + if (!Span(ReadUInt16(node + NodeFirstFaceOffset), ReadUInt16(node + NodeFaceCountOffset), faces)) + return Bad("node faces out of range"); + } + + return true; + } + + //CM_DecompressPVS decodes at least once and stops at rowBytes, but does not clip the run of zeros that crosses it, + //which may end anywhere up to runBytes. + bool VisibilityRow(size_t offset, size_t rowBytes, size_t runBytes) + { + const auto* pVisibility = m_pData + m_Offsets[VisibilityLump]; + const auto length = m_Lengths[VisibilityLump]; + size_t written = 0; + + do + { + if (offset >= length) + return Bad("visibility row outside lump"); + + if (pVisibility[offset]) + { + ++written; + ++offset; + } + else + { + if (offset + 1 >= length) + return Bad("visibility row outside lump"); + + written += pVisibility[offset + 1]; + offset += 2; + } + + if (written > runBytes) + return Bad("visibility run past the compiled row"); + } while (written < rowBytes); + + return true; + } + + bool Leaves() + { + const auto marks = Count(MarkSurfaceLump, MarkSurfaceBytes); + //Mod_SetupSubmodel sets the world's leaf count to model 0's visible leaves, which sizes every row the engine + //decodes. Compilers write longer rows: hlvis pads them to 64 leaves, and the stock Opposing Force and + //Counter-Strike maps hold a bit for every leaf, so a run of zeros may end at the longer of the two. Within the + //caller's leaf capacity that stays inside its MAX_MAP_LEAFS / 8 buffers; callers decoding into rows sized by + //the visible leaves must clip the run. + const auto visibleLeaves = static_cast(ReadInt32(Record(ModelLump, 0, ModelBytes) + ModelVisLeafsOffset)); + const auto rowBytes = (visibleLeaves + 7) >> 3; + const auto paddedBytes = ((visibleLeaves + 63) & ~size_t(63)) >> 3; + const auto leafBytes = (Count(LeafLump, LeafBytes) + 7) >> 3; + const auto runBytes = paddedBytes > leafBytes ? paddedBytes : leafBytes; + + for (size_t i = 0; i < Count(LeafLump, LeafBytes); ++i) + { + const auto leaf = Record(LeafLump, i, LeafBytes); + + //Mod_SetParent and Mod_MakeHull0 tell leaves from nodes by negative contents. + if (ReadInt32(leaf) >= 0) + return Bad("leaf contents not negative", Reason::InvalidFormat); + + if (!Span(ReadUInt16(leaf + LeafFirstMarkOffset), ReadUInt16(leaf + LeafMarkCountOffset), marks)) + return Bad("leaf mark surfaces out of range"); + + const auto visibility = ReadInt32(leaf + LeafVisOffset); + if (visibility == AbsentOffset) + continue; + + if (visibility < 0) + return Bad("negative visibility offset"); + + //Without visibility data the row is the null visdata plus the offset, and only 0 is null, which sees + //everything (brush model files such as sphere256.bsp give their leaves 0). + if (!m_Lengths[VisibilityLump]) + { + if (visibility) + return Bad("visibility offset without visibility"); + + continue; + } + + if (!VisibilityRow(static_cast(visibility), rowBytes, runBytes)) + return false; + } + + return true; + } + + //Children are clipnodes when not negative, otherwise contents. + bool ClipNodes() + { + const auto planes = Count(PlaneLump, PlaneBytes); + const auto clipNodes = Count(ClipNodeLump, ClipNodeBytes); + + for (size_t i = 0; i < clipNodes; ++i) + { + const auto node = Record(ClipNodeLump, i, ClipNodeBytes); + if (!Index(ReadInt32(node), planes)) + return Bad("clipnode plane out of range"); + + for (int side = 0; side < 2; ++side) + { + const auto child = ReadInt16(node + ClipChildrenOffset + side * sizeof(std::int16_t)); + if (child >= 0 && !Index(child, clipNodes)) + return Bad("clipnode child out of range"); + } + } + + return true; + } + + //Every node of a graph whose non-negative children are nodes must reach a leaf within MaxTreeDepth levels, which + //also rules out cycles. Shared subtrees are allowed: compilers merge identical clipnodes. + bool Acyclic(size_t lump, size_t recordBytes, size_t childrenOffset) + { + enum : std::uint8_t + { + Unvisited, + Open, + Closed + }; + + struct Frame + { + size_t node; + int side; + }; + + const auto count = Count(lump, recordBytes); + std::vector state(count, Unvisited); + std::vector height(count, 0); + std::vector stack; + + for (size_t root = 0; root < count; ++root) + { + if (state[root] != Unvisited) + continue; + + state[root] = Open; + stack.push_back({root, 0}); + + while (!stack.empty()) + { + const auto node = stack.back().node; + + if (stack.back().side < 2) + { + const auto side = stack.back().side++; + const auto child = ReadInt16(Record(lump, node, recordBytes) + childrenOffset + side * sizeof(std::int16_t)); + if (child < 0) + continue; + + if (state[child] == Open) + return Bad("cyclic node graph", Reason::InvalidFormat); + + if (state[child] == Unvisited) + { + state[child] = Open; + stack.push_back({static_cast(child), 0}); + } + + continue; + } + + int deepest = 0; + for (int side = 0; side < 2; ++side) + { + const auto child = ReadInt16(Record(lump, node, recordBytes) + childrenOffset + side * sizeof(std::int16_t)); + if (child >= 0 && height[child] > deepest) + deepest = height[child]; + } + + height[node] = deepest + 1; + if (height[node] > MaxTreeDepth) + return Bad("node graph too deep", Reason::InvalidFormat); + + state[node] = Closed; + stack.pop_back(); + } + } + + return true; + } + + bool Trees() + { + if (!Acyclic(NodeLump, NodeBytes, NodeChildrenOffset) || + !Acyclic(ClipNodeLump, ClipNodeBytes, ClipChildrenOffset)) + { + return false; + } + + //Mod_SetParent recurses the drawing tree from node 0 into every child: no node may be reached twice. + const auto nodes = Count(NodeLump, NodeBytes); + std::vector visited(nodes); + std::vector pending{0}; + + while (!pending.empty()) + { + const auto node = pending.back(); + pending.pop_back(); + + if (visited[node]) + return Bad("shared node in the drawing tree", Reason::InvalidFormat); + + visited[node] = true; + + for (int side = 0; side < 2; ++side) + { + const auto child = ReadInt16(Record(NodeLump, node, NodeBytes) + NodeChildrenOffset + side * sizeof(std::int16_t)); + if (child >= 0) + pending.push_back(static_cast(child)); + } + } + + return true; + } + + const std::uint8_t* m_pData; + size_t m_Size; + Layout m_Layout; + UtilAssetsIntegrityCheckResult_BSP* m_pResult; + Reason m_Reason{Reason::OK}; + size_t m_Offsets[LumpCount]{}; + size_t m_Lengths[LumpCount]{}; + size_t m_Miptexes{}; +}; diff --git a/src/UtilAssetsIntegrity.cpp b/src/UtilAssetsIntegrity.cpp index acf8121..90cd58a 100644 --- a/src/UtilAssetsIntegrity.cpp +++ b/src/UtilAssetsIntegrity.cpp @@ -1,5 +1,6 @@ #include #include "StudioModelValidation.h" +#include "BSPValidation.h" #include #include @@ -94,6 +95,15 @@ class CUtilAssetsIntegrity : public IUtilAssetsIntegrity { return CStudioModelValidation(group, groupBytes, result).CheckGroup(model, modelBytes, groupIndex); } + + UtilAssetsIntegrityCheckReason CheckBSP( + const void* buf, + size_t bufSize, + UtilAssetsIntegrityBSPLayout layout, + UtilAssetsIntegrityCheckResult_BSP* result) noexcept override + { + return CBSPValidation(buf, bufSize, layout, result).Check(); + } }; EXPOSE_SINGLE_INTERFACE(CUtilAssetsIntegrity, IUtilAssetsIntegrity, UTIL_ASSETS_INTEGRITY_INTERFACE_VERSION); diff --git a/tests/BSPTests.h b/tests/BSPTests.h new file mode 100644 index 0000000..7bd9383 --- /dev/null +++ b/tests/BSPTests.h @@ -0,0 +1,662 @@ +#pragma once + +#include +#include + +//A complete single-face world, kept lump by lump so each case breaks one field. The layout follows the BSP version 30 +//records independently of the validator. +struct BSPFixture +{ + enum Lump + { + Entities, + Planes, + Textures, + Vertexes, + Visibility, + Nodes, + TexInfos, + Faces, + Lighting, + ClipNodes, + Leafs, + MarkSurfaces, + Edges, + SurfEdges, + Models, + LumpCount + }; + + struct Plane + { + float normal[3]; + float dist; + std::int32_t type; + }; + + struct Miptex + { + char name[16]; + std::uint32_t width, height; + std::uint32_t offsets[4]; + }; + + struct Node + { + std::int32_t plane; + std::int16_t children[2]; + std::int16_t mins[3], maxs[3]; + std::uint16_t firstFace, faceCount; + }; + + struct TexInfo + { + float vecs[2][4]; + std::int32_t miptex, flags; + }; + + struct Face + { + std::int16_t plane, side; + std::int32_t firstEdge; + std::int16_t edgeCount, texInfo; + std::uint8_t styles[4]; + std::int32_t lightOffset; + }; + + struct ClipNode + { + std::int32_t plane; + std::int16_t children[2]; + }; + + struct Leaf + { + std::int32_t contents, visOffset; + std::int16_t mins[3], maxs[3]; + std::uint16_t firstMark, markCount; + std::uint8_t ambient[4]; + }; + + struct Edge + { + std::uint16_t v[2]; + }; + + struct Model + { + float mins[3], maxs[3], origin[3]; + std::int32_t headNodes[4]; + std::int32_t visLeaves, firstFace, faceCount; + }; + + static_assert(sizeof(Plane) == 20 && sizeof(Miptex) == 40 && sizeof(Node) == 24 && sizeof(TexInfo) == 40, "BSP records"); + static_assert(sizeof(Face) == 20 && sizeof(ClipNode) == 8 && sizeof(Leaf) == 28 && sizeof(Model) == 64, "BSP records"); + + static constexpr int Version = 30; + static constexpr size_t HeaderBytes = sizeof(std::int32_t) + LumpCount * 2 * sizeof(std::int32_t); + static constexpr int Side = 16; + static constexpr size_t MipBytes = Side * Side * 85 / 64; + static constexpr size_t PaletteColors = 256; + static constexpr size_t PaletteBytes = PaletteColors * 3; + //2x2 luxels of one style: the face spans one 16 texel luxel on each axis. + static constexpr size_t LightBytes = 2 * 2 * 3; + + //Miptex 0 starts after the count and its one offset. + static constexpr size_t MiptexOffset = 2 * sizeof(std::int32_t); + + std::vector lumps[LumpCount]; + + template void Append(Lump lump, const T& value) + { + auto& bytes = lumps[lump]; + bytes.resize(bytes.size() + sizeof(value)); + std::memcpy(bytes.data() + bytes.size() - sizeof(value), &value, sizeof(value)); + } + + template T Get(Lump lump, size_t index = 0, size_t base = 0) const + { + T value{}; + std::memcpy(&value, lumps[lump].data() + base + index * sizeof(T), sizeof(value)); + return value; + } + + template void Set(Lump lump, const T& value, size_t index = 0, size_t base = 0) + { + std::memcpy(lumps[lump].data() + base + index * sizeof(T), &value, sizeof(value)); + } + + BSPFixture() + { + const char entities[] = "{\n\"classname\" \"worldspawn\"\n}\n"; + lumps[Entities].assign(entities, entities + sizeof(entities)); + + Append(Planes, Plane{{0, 0, 1}, 0, 2}); + + Append(Textures, std::int32_t{1}); + Append(Textures, static_cast(MiptexOffset)); + Miptex miptex{"fixture", Side, Side, {sizeof(Miptex), 0, 0, 0}}; + Append(Textures, miptex); + lumps[Textures].resize(lumps[Textures].size() + MipBytes); + Append(Textures, static_cast(PaletteColors)); + lumps[Textures].resize(lumps[Textures].size() + PaletteBytes); + + const float vertices[][3] = {{0, 0, 0}, {Side, 0, 0}, {0, Side, 0}}; + for (const auto& vertex : vertices) + Append(Vertexes, vertex); + + Append(Visibility, std::uint8_t{1}); + + Append(Nodes, Node{0, {-1, -2}, {0, 0, 0}, {Side, Side, 0}, 0, 1}); + Append(TexInfos, TexInfo{{{1, 0, 0, 0}, {0, 1, 0, 0}}, 0, 0}); + Append(Faces, Face{0, 0, 0, 3, 0, {0, 255, 255, 255}, 0}); + lumps[Lighting].resize(LightBytes); + Append(ClipNodes, ClipNode{0, {-1, -2}}); + + //Leaf 0 is the solid leaf; leaf 1 sees itself. + Append(Leafs, Leaf{-2, -1, {}, {}, 0, 0, {}}); + Append(Leafs, Leaf{-1, 0, {0, 0, 0}, {Side, Side, 0}, 0, 1, {}}); + Append(MarkSurfaces, std::uint16_t{0}); + + //Edge 0 is never used: negative surface edges walk an edge backwards. + const Edge edges[] = {{{0, 0}}, {{0, 1}}, {{1, 2}}, {{2, 0}}}; + for (const auto& edge : edges) + Append(Edges, edge); + for (std::int32_t edge = 1; edge <= 3; ++edge) + Append(SurfEdges, edge); + + Append(Models, Model{{0, 0, 0}, {Side, Side, 0}, {0, 0, 0}, {0, 0, 0, 0}, 1, 0, 1}); + } + + std::vector Build(UtilAssetsIntegrityBSPLayout layout = UtilAssetsIntegrityBSPLayout::GoldSrc) const + { + std::vector file(HeaderBytes); + const std::int32_t version = Version; + std::memcpy(file.data(), &version, sizeof(version)); + + for (int lump = 0; lump < LumpCount; ++lump) + { + //Blue Shift's table holds the planes in the entities entry and the entities in the planes entry. + auto slot = lump; + if (layout == UtilAssetsIntegrityBSPLayout::BlueShift && (lump == Entities || lump == Planes)) + slot = lump == Entities ? Planes : Entities; + + file.resize((file.size() + 3) & ~size_t(3)); + const std::int32_t entry[] = {static_cast(file.size()), static_cast(lumps[lump].size())}; + std::memcpy(file.data() + sizeof(version) + slot * sizeof(entry), entry, sizeof(entry)); + file.insert(file.end(), lumps[lump].begin(), lumps[lump].end()); + } + + return file; + } +}; + +void CheckBSPs(IUtilAssetsIntegrity& api) +{ + using Reason = UtilAssetsIntegrityCheckReason; + using Layout = UtilAssetsIntegrityBSPLayout; + using F = BSPFixture; + + UtilAssetsIntegrityCheckResult_BSP result; + result.MaxLeafs = 2; + + const auto check = [&](const std::vector& file, Layout layout = Layout::GoldSrc) { + return api.CheckBSP(file.data(), file.size(), layout, &result); + }; + const auto expect = [&](Reason expected, const F& fixture, const char* scenario) { + Expect(expected, check(fixture.Build()), scenario); + }; + + const F valid; + expect(Reason::OK, valid, "BSP fixture"); + Check(result.ReasonStr[0] == '\0', "BSP fixture diagnostic is empty"); + const auto file = valid.Build(); + Expect(Reason::OK, api.CheckBSP(file.data(), file.size(), Layout::GoldSrc, nullptr), "BSP without result"); + + //Header and lump table. + Expect(Reason::SizeTooSmall, api.CheckBSP(nullptr, 0, Layout::GoldSrc, &result), "empty BSP"); + Check(result.ReasonStr[0] != '\0', "empty BSP diagnostic"); + Expect(Reason::SizeTooSmall, api.CheckBSP(file.data(), F::HeaderBytes - 1, Layout::GoldSrc, &result), "truncated BSP header"); + Expect(Reason::InvalidFormat, check(file, static_cast(2)), "unknown BSP layout"); + { + auto bytes = file; + bytes[0] = 29; + Expect(Reason::VersionMismatch, check(bytes), "BSP version 29"); + } + { + //The models are the last lump: one more byte runs past the file. + auto bytes = file; + const auto entry = sizeof(std::int32_t) + F::Models * 2 * sizeof(std::int32_t); + const std::int32_t longer = static_cast(valid.lumps[F::Models].size() + 1); + std::memcpy(bytes.data() + entry + sizeof(std::int32_t), &longer, sizeof(longer)); + Expect(Reason::OutOfBound, check(bytes), "BSP lump past file end"); + const std::int32_t negative = -1; + std::memcpy(bytes.data() + entry, &negative, sizeof(negative)); + Expect(Reason::OutOfBound, check(bytes), "BSP negative lump offset"); + } + { + F f; + f.lumps[F::Planes].push_back(0); + expect(Reason::InvalidFormat, f, "BSP partial plane record"); + } + { + F f; + f.lumps[F::Models].clear(); + expect(Reason::InvalidFormat, f, "BSP without models"); + } + + //The caller's leaf capacity. + result.MaxLeafs = 1; + expect(Reason::SizeTooLarge, valid, "BSP leaf capacity"); + UtilAssetsIntegrityCheckResult_BSP defaults; + Expect(Reason::SizeTooLarge, api.CheckBSP(file.data(), file.size(), Layout::GoldSrc, &defaults), "BSP zero leaf capacity"); + result.MaxLeafs = 2; + + { + F f; + //Condition Zero: Deleted Scenes ends its entities with a space; callers copy them with a terminator. + f.lumps[F::Entities].back() = ' '; + expect(Reason::OK, f, "BSP unterminated entities"); + } + + //Planes, vertices, edges and surface edges. + { + F f; + auto plane = f.Get(F::Planes); + plane.normal[0] = std::numeric_limits::quiet_NaN(); + f.Set(F::Planes, plane); + expect(Reason::InvalidFormat, f, "BSP non-finite plane"); + } + { + F f; + f.Set(F::Vertexes, std::numeric_limits::infinity(), 4); + expect(Reason::InvalidFormat, f, "BSP non-finite vertex"); + } + { + F f; + f.Set(F::Edges, F::Edge{{0, 3}}, 1); + expect(Reason::OutOfBound, f, "BSP edge vertex out of range"); + } + { + F f; + f.Set(F::SurfEdges, std::int32_t{-4}, 1); + expect(Reason::OutOfBound, f, "BSP backward surface edge out of range"); + f.Set(F::SurfEdges, std::int32_t{INT_MIN}, 1); + expect(Reason::OutOfBound, f, "BSP surface edge INT_MIN"); + } + + //Texture lump. + { + F f; + f.lumps[F::Textures].resize(2); + expect(Reason::OutOfBound, f, "BSP truncated texture lump"); + } + { + F f; + f.Set(F::Textures, std::int32_t{-1}); + expect(Reason::OutOfBound, f, "BSP negative miptex count"); + f.Set(F::Textures, std::int32_t{INT_MAX}); + expect(Reason::OutOfBound, f, "BSP miptex table past lump"); + } + { + F f; + f.Set(F::Textures, static_cast(f.lumps[F::Textures].size() - sizeof(F::Miptex) + 1), 1); + expect(Reason::OutOfBound, f, "BSP miptex past lump"); + f.Set(F::Textures, std::int32_t{-2}, 1); + expect(Reason::OutOfBound, f, "BSP negative miptex offset"); + f.Set(F::Textures, std::int32_t{-1}, 1); + expect(Reason::OK, f, "BSP absent miptex"); + } + { + F f; + auto miptex = f.Get(F::Textures, 0, F::MiptexOffset); + std::memset(miptex.name, 'a', sizeof(miptex.name)); + f.Set(F::Textures, miptex, 0, F::MiptexOffset); + expect(Reason::OutOfBound, f, "BSP unterminated miptex name"); + } + { + F f; + auto miptex = f.Get(F::Textures, 0, F::MiptexOffset); + miptex.width = 0; + f.Set(F::Textures, miptex, 0, F::MiptexOffset); + expect(Reason::InvalidFormat, f, "BSP zero miptex width"); + miptex.width = 32768; + f.Set(F::Textures, miptex, 0, F::MiptexOffset); + expect(Reason::InvalidFormat, f, "BSP oversized miptex"); + miptex.width = 24; + f.Set(F::Textures, miptex, 0, F::MiptexOffset); + expect(Reason::InvalidFormat, f, "BSP unaligned embedded miptex"); + //A WAD texture's pixels are not in the file. + miptex.offsets[0] = 0; + f.Set(F::Textures, miptex, 0, F::MiptexOffset); + expect(Reason::OK, f, "BSP unaligned WAD miptex"); + } + { + F f; + f.lumps[F::Textures].pop_back(); + expect(Reason::OutOfBound, f, "BSP miptex palette past lump"); + } + { + //The pixels and palette count of a 32x32 miptex no longer fit. + F f; + auto miptex = f.Get(F::Textures, 0, F::MiptexOffset); + miptex.width = 2 * F::Side; + miptex.height = 2 * F::Side; + f.Set(F::Textures, miptex, 0, F::MiptexOffset); + expect(Reason::OutOfBound, f, "BSP miptex pixels past lump"); + } + { + const auto count = F::MiptexOffset + sizeof(F::Miptex) + F::MipBytes; + F f; + f.Set(F::Textures, std::uint16_t{16}, 0, count); + expect(Reason::OK, f, "BSP short palette count"); + f.lumps[F::Textures].pop_back(); + expect(Reason::OutOfBound, f, "BSP short palette count still reads 256 colors"); + f = F(); + f.Set(F::Textures, std::uint16_t{257}, 0, count); + expect(Reason::OutOfBound, f, "BSP palette count past lump"); + } + + //Texture infos. + { + F f; + auto texInfo = f.Get(F::TexInfos); + texInfo.miptex = 1; + f.Set(F::TexInfos, texInfo); + expect(Reason::OutOfBound, f, "BSP texinfo miptex out of range"); + texInfo.miptex = -1; + f.Set(F::TexInfos, texInfo); + expect(Reason::OutOfBound, f, "BSP negative texinfo miptex"); + //Without a texture lump every face is drawn with the missing texture. + f.lumps[F::Textures].clear(); + expect(Reason::OK, f, "BSP texinfo without texture lump"); + } + { + F f; + auto texInfo = f.Get(F::TexInfos); + texInfo.vecs[1][3] = std::numeric_limits::infinity(); + f.Set(F::TexInfos, texInfo); + expect(Reason::InvalidFormat, f, "BSP non-finite texture vector"); + texInfo.vecs[1][3] = 1e30f; + f.Set(F::TexInfos, texInfo); + expect(Reason::InvalidFormat, f, "BSP texture coordinate overflow"); + } + + //Faces. + const auto face = [](F& f, void (*change)(F::Face&)) { + auto value = f.Get(F::Faces); + change(value); + f.Set(F::Faces, value); + }; + { + F f; + face(f, [](F::Face& value) { value.plane = 1; }); + expect(Reason::OutOfBound, f, "BSP face plane out of range"); + face(f, [](F::Face& value) { value.plane = -1; }); + expect(Reason::OutOfBound, f, "BSP negative face plane"); + } + { + F f; + face(f, [](F::Face& value) { value.texInfo = 1; }); + expect(Reason::OutOfBound, f, "BSP face texinfo out of range"); + } + { + F f; + face(f, [](F::Face& value) { value.edgeCount = 0; }); + expect(Reason::OutOfBound, f, "BSP face without edges"); + face(f, [](F::Face& value) { value.edgeCount = -1; }); + expect(Reason::OutOfBound, f, "BSP negative face edge count"); + face(f, [](F::Face& value) { value.edgeCount = 3, value.firstEdge = 1; }); + expect(Reason::OutOfBound, f, "BSP face edges past surface edges"); + face(f, [](F::Face& value) { value.firstEdge = INT_MAX; }); + expect(Reason::OutOfBound, f, "BSP face edge range overflow"); + } + { + F f; + face(f, [](F::Face& value) { value.lightOffset = -2; }); + expect(Reason::OutOfBound, f, "BSP negative light offset"); + face(f, [](F::Face& value) { value.lightOffset = -1; }); + expect(Reason::OK, f, "BSP face without light samples"); + } + { + F f; + f.lumps[F::Lighting].pop_back(); + expect(Reason::OutOfBound, f, "BSP light samples past lighting"); + f.lumps[F::Lighting].clear(); + expect(Reason::OK, f, "BSP unlit file with light offset 0"); + face(f, [](F::Face& value) { value.lightOffset = 3; }); + expect(Reason::OutOfBound, f, "BSP light offset without lighting"); + } + { + F f; + face(f, [](F::Face& value) { value.styles[1] = 1; }); + expect(Reason::OutOfBound, f, "BSP second light style past lighting"); + } + { + //Texture coordinates up to 640 make the face 640 texels wide. + F f; + auto texInfo = f.Get(F::TexInfos); + texInfo.vecs[0][0] = 40; + f.Set(F::TexInfos, texInfo); + expect(Reason::InvalidFormat, f, "BSP surface extents over 512"); + texInfo.flags = 1; + f.Set(F::TexInfos, texInfo); + expect(Reason::OK, f, "BSP large special surface has no lightmap"); + } + { + //A missing texture clears a special face's flags, so its light samples can be read. + F f; + auto texInfo = f.Get(F::TexInfos); + texInfo.flags = 1; + f.Set(F::TexInfos, texInfo); + f.lumps[F::Lighting].pop_back(); + expect(Reason::OutOfBound, f, "BSP small special surface light samples"); + } + + //Mark surfaces are unsigned. + { + F f; + f.Set(F::MarkSurfaces, std::uint16_t{1}); + expect(Reason::OutOfBound, f, "BSP mark surface out of range"); + f.Set(F::MarkSurfaces, std::uint16_t{0x8000}); + expect(Reason::OutOfBound, f, "BSP mark surface past signed range"); + } + + //Models. + const auto model = [](F& f, void (*change)(F::Model&)) { + auto value = f.Get(F::Models); + change(value); + f.Set(F::Models, value); + }; + { + F f; + model(f, [](F::Model& value) { value.headNodes[0] = 1; }); + expect(Reason::OutOfBound, f, "BSP model head node out of range"); + model(f, [](F::Model& value) { value.headNodes[0] = -1; }); + expect(Reason::OutOfBound, f, "BSP negative model head node"); + } + { + F f; + //The clipnode count is the head compilers give a model without clipping brushes. + model(f, [](F::Model& value) { value.headNodes[1] = 1; }); + expect(Reason::OK, f, "BSP model clipping head at the clipnode count"); + model(f, [](F::Model& value) { value.headNodes[1] = 2; }); + expect(Reason::OutOfBound, f, "BSP model clipping head out of range"); + model(f, [](F::Model& value) { value.headNodes[1] = -1; }); + expect(Reason::OK, f, "BSP model with an empty hull"); + } + { + F f; + model(f, [](F::Model& value) { value.visLeaves = 2; }); + expect(Reason::OutOfBound, f, "BSP model visible leaves out of range"); + model(f, [](F::Model& value) { value.visLeaves = -1; }); + expect(Reason::OutOfBound, f, "BSP negative model visible leaves"); + } + { + F f; + model(f, [](F::Model& value) { value.firstFace = 1; }); + expect(Reason::OutOfBound, f, "BSP model faces out of range"); + model(f, [](F::Model& value) { value.firstFace = 0, value.faceCount = -1; }); + expect(Reason::OutOfBound, f, "BSP negative model face count"); + } + { + F f; + model(f, [](F::Model& value) { value.origin[2] = std::numeric_limits::quiet_NaN(); }); + expect(Reason::InvalidFormat, f, "BSP non-finite model origin"); + model(f, [](F::Model& value) { value.origin[2] = 0, value.mins[0] = F::Side + 1; }); + expect(Reason::InvalidFormat, f, "BSP inverted model bounds"); + } + + //Nodes. + const auto node = [](F& f, void (*change)(F::Node&), size_t index = 0) { + auto value = f.Get(F::Nodes, index); + change(value); + f.Set(F::Nodes, value, index); + }; + { + F f; + node(f, [](F::Node& value) { value.plane = 1; }); + expect(Reason::OutOfBound, f, "BSP node plane out of range"); + } + { + F f; + node(f, [](F::Node& value) { value.children[0] = 1; }); + expect(Reason::OutOfBound, f, "BSP node child out of range"); + node(f, [](F::Node& value) { value.children[0] = -3; }); + expect(Reason::OutOfBound, f, "BSP node leaf out of range"); + } + { + F f; + node(f, [](F::Node& value) { value.faceCount = 2; }); + expect(Reason::OutOfBound, f, "BSP node faces out of range"); + } + + //Leaves and visibility rows; model 0's one visible leaf makes the rows one byte long. + const auto leaf = [](F& f, void (*change)(F::Leaf&)) { + auto value = f.Get(F::Leafs, 1); + change(value); + f.Set(F::Leafs, value, 1); + }; + { + F f; + leaf(f, [](F::Leaf& value) { value.contents = 0; }); + expect(Reason::InvalidFormat, f, "BSP leaf with node contents"); + } + { + F f; + leaf(f, [](F::Leaf& value) { value.firstMark = 1; }); + expect(Reason::OutOfBound, f, "BSP leaf mark surfaces out of range"); + leaf(f, [](F::Leaf& value) { value.firstMark = 0, value.markCount = 2; }); + expect(Reason::OutOfBound, f, "BSP leaf mark surface count out of range"); + } + { + F f; + leaf(f, [](F::Leaf& value) { value.visOffset = 1; }); + expect(Reason::OutOfBound, f, "BSP visibility offset past lump"); + leaf(f, [](F::Leaf& value) { value.visOffset = -2; }); + expect(Reason::OutOfBound, f, "BSP negative visibility offset"); + leaf(f, [](F::Leaf& value) { value.visOffset = 0; }); + f.lumps[F::Visibility].clear(); + expect(Reason::OK, f, "BSP visibility offset 0 without visibility"); + leaf(f, [](F::Leaf& value) { value.visOffset = 1; }); + expect(Reason::OutOfBound, f, "BSP visibility offset without visibility"); + } + { + F f; + f.lumps[F::Visibility] = {0, 1}; + expect(Reason::OK, f, "BSP visibility run filling its row"); + //Compiled rows are padded to 64 leaves: a run may end 8 bytes in, not 9. + f.lumps[F::Visibility] = {0, 8}; + expect(Reason::OK, f, "BSP visibility run to the padded row"); + f.lumps[F::Visibility] = {0, 9}; + expect(Reason::OutOfBound, f, "BSP visibility run past the padded row"); + f.lumps[F::Visibility] = {0}; + expect(Reason::OutOfBound, f, "BSP truncated visibility run"); + } + { + //A row compiled for all 100 leaves is 13 bytes long. + F f; + for (int i = 2; i < 100; ++i) + f.Append(F::Leafs, F::Leaf{-2, -1, {}, {}, 0, 0, {}}); + result.MaxLeafs = 100; + f.lumps[F::Visibility] = {0, 13}; + expect(Reason::OK, f, "BSP visibility run to the row of every leaf"); + f.lumps[F::Visibility] = {0, 14}; + expect(Reason::OutOfBound, f, "BSP visibility run past the row of every leaf"); + result.MaxLeafs = 2; + } + { + //An empty row still decodes once, within the row of every leaf. + F f; + model(f, [](F::Model& value) { value.visLeaves = 0; }); + f.lumps[F::Visibility] = {0, 0}; + expect(Reason::OK, f, "BSP empty visibility row"); + f.lumps[F::Visibility] = {0, 2}; + expect(Reason::OutOfBound, f, "BSP visibility run past an empty row"); + } + + //Clipnodes. + const auto clipNode = [](F& f, void (*change)(F::ClipNode&)) { + auto value = f.Get(F::ClipNodes); + change(value); + f.Set(F::ClipNodes, value); + }; + { + F f; + clipNode(f, [](F::ClipNode& value) { value.plane = 1; }); + expect(Reason::OutOfBound, f, "BSP clipnode plane out of range"); + clipNode(f, [](F::ClipNode& value) { value.plane = 0, value.children[1] = 1; }); + expect(Reason::OutOfBound, f, "BSP clipnode child out of range"); + } + + //Node graphs. + { + F f; + node(f, [](F::Node& value) { value.children[1] = 0; }); + expect(Reason::InvalidFormat, f, "BSP node cycle"); + } + { + F f; + clipNode(f, [](F::ClipNode& value) { value.children[1] = 0; }); + expect(Reason::InvalidFormat, f, "BSP clipnode cycle"); + } + { + //Node 0 reaches node 1 twice; clipnodes may share subtrees. + F f; + f.Append(F::Nodes, F::Node{0, {-1, -2}, {}, {}, 0, 0}); + node(f, [](F::Node& value) { value.children[0] = 1, value.children[1] = 1; }); + expect(Reason::InvalidFormat, f, "BSP shared drawing node"); + + f = F(); + f.Append(F::ClipNodes, F::ClipNode{0, {-1, -2}}); + clipNode(f, [](F::ClipNode& value) { value.children[0] = 1, value.children[1] = 1; }); + expect(Reason::OK, f, "BSP shared clipnode"); + } + { + //A chain of nodes, each one level deeper. + const auto chain = [](int depth) { + F f; + for (int i = 1; i < depth; ++i) + f.Append(F::Nodes, F::Node{0, {-1, -2}, {}, {}, 0, 0}); + for (int i = 0; i + 1 < depth; ++i) + { + auto value = f.Get(F::Nodes, i); + value.children[0] = static_cast(i + 1); + f.Set(F::Nodes, value, i); + } + return f; + }; + constexpr int maxDepth = 1024; + expect(Reason::OK, chain(maxDepth), "BSP node chain at the depth limit"); + expect(Reason::InvalidFormat, chain(maxDepth + 1), "BSP node chain past the depth limit"); + } + + //Blue Shift swaps the entities and planes entries. + { + Check(valid.lumps[F::Entities].size() % sizeof(F::Plane) != 0, "BSP fixture entities are not whole planes"); + const auto blueShift = valid.Build(Layout::BlueShift); + Expect(Reason::OK, check(blueShift, Layout::BlueShift), "Blue Shift BSP"); + Expect(Reason::InvalidFormat, check(blueShift, Layout::GoldSrc), "Blue Shift BSP read as GoldSrc"); + Expect(Reason::InvalidFormat, check(file, Layout::BlueShift), "GoldSrc BSP read as Blue Shift"); + } +} diff --git a/tests/SmokeTests.cpp b/tests/SmokeTests.cpp index 6551ce2..690e83b 100644 --- a/tests/SmokeTests.cpp +++ b/tests/SmokeTests.cpp @@ -173,6 +173,7 @@ void CheckBmps(IUtilAssetsIntegrity& api) Expect(Reason::BogusHeader, api.Check8bitBMP(image.data(), sizeof(BITMAPFILEHEADER), &result), "truncated BMP"); } #include "StudioModelTests.h" +#include "BSPTests.h" } // namespace int wmain(int argc, wchar_t** argv) @@ -180,7 +181,7 @@ int wmain(int argc, wchar_t** argv) SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX); if (argc != 3 && argc != 4) { - std::cerr << "Usage: UtilAssetsIntegritySmokeTests [model directory]\n"; + std::cerr << "Usage: UtilAssetsIntegritySmokeTests [asset directory]\n"; return 2; } try @@ -208,13 +209,38 @@ int wmain(int argc, wchar_t** argv) CheckStudioRegressions(*api); CheckSequenceGroups(library.handle, *api); CheckBmps(*api); + CheckBSPs(*api); if (failures != 0) return 1; if (argc == 4) { - size_t checked = 0, checkedGroups = 0, rejected = 0; + size_t checked = 0, checkedGroups = 0, checkedMaps = 0, rejected = 0; for (const auto& entry : std::filesystem::recursive_directory_iterator(argv[3])) { + if (entry.is_regular_file() && entry.path().extension() == L".bsp") + { + //Brush files below a bshift directory use Blue Shift's lump table, as the engine picks it by game. + auto layout = UtilAssetsIntegrityBSPLayout::GoldSrc; + for (const auto& part : entry.path()) + { + if (!_wcsicmp(part.c_str(), L"bshift")) + layout = UtilAssetsIntegrityBSPLayout::BlueShift; + } + + std::ifstream input(entry.path(), std::ios::binary); + const std::vector bytes((std::istreambuf_iterator(input)), std::istreambuf_iterator()); + UtilAssetsIntegrityCheckResult_BSP result; + //Capacity is the caller's: a scan checks the format alone. + result.MaxLeafs = SIZE_MAX; + ++checkedMaps; + if (api->CheckBSP(bytes.data(), bytes.size(), layout, &result) != UtilAssetsIntegrityCheckReason::OK) + { + ++rejected; + std::cout << entry.path().string() << ": " << result.ReasonStr << '\n'; + } + continue; + } + if (!entry.is_regular_file() || entry.path().extension() != L".mdl") continue; std::ifstream input(entry.path(), std::ios::binary); const std::vector bytes((std::istreambuf_iterator(input)), std::istreambuf_iterator()); @@ -251,9 +277,9 @@ int wmain(int argc, wchar_t** argv) } } } - std::cout << "Scanned " << checked << " MDL files and " << checkedGroups << " sequence groups against their main models; rejected " << rejected << ".\n"; + std::cout << "Scanned " << checked << " MDL files, " << checkedGroups << " sequence groups against their main models and " << checkedMaps << " BSP files; rejected " << rejected << ".\n"; } - std::cout << "Public interface, StudioModel and BMP smoke tests passed.\n"; + std::cout << "Public interface, StudioModel, BMP and BSP smoke tests passed.\n"; return 0; } catch (const std::exception& error)