Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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/<Configuration>`
and `install/x86/<Configuration>`. 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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion README.zh-CN.md
Original file line number Diff line number Diff line change
@@ -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)。
Expand Down Expand Up @@ -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` 指解码后的像素数量,三项限制默认为零,检查非空图像前需要设置;
Expand Down
107 changes: 107 additions & 0 deletions docs/bsp-validation.md
Original file line number Diff line number Diff line change
@@ -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.
31 changes: 31 additions & 0 deletions include/Interface/IUtilAssetsIntegrity.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
23 changes: 23 additions & 0 deletions memory/project_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading
Loading