diff --git a/include/Interface/IUtilAssetsIntegrity.h b/include/Interface/IUtilAssetsIntegrity.h index af028ff..17b7104 100644 --- a/include/Interface/IUtilAssetsIntegrity.h +++ b/include/Interface/IUtilAssetsIntegrity.h @@ -74,8 +74,8 @@ class UtilAssetsIntegrityCheckResult_BSP : public UtilAssetsIntegrityCheckResult class IUtilAssetsIntegrity : public IBaseInterface { public: - virtual UtilAssetsIntegrityCheckReason CheckStudioModel(const void* buf, size_t bufSize, UtilAssetsIntegrityCheckResult_StudioModel* checkResult) = 0; - virtual UtilAssetsIntegrityCheckReason Check8bitBMP(const void* buf, size_t bufSize, UtilAssetsIntegrityCheckResult_BMP* checkResult) = 0; + virtual UtilAssetsIntegrityCheckReason CheckStudioModel(const void* buf, size_t bufSize, UtilAssetsIntegrityCheckResult_StudioModel* checkResult, bool bIsSvEngine = false) = 0; + virtual UtilAssetsIntegrityCheckReason Check8bitBMP(const void* buf, size_t bufSize, UtilAssetsIntegrityCheckResult_BMP* checkResult) = 0; //Checks every sequence belonging to groupIndex using the main model's structural header (raw or engine-cached). //Unlike standalone IDSQ header validation, this checks the bones, blends, all six RLE channels and interpolation reads. @@ -88,7 +88,8 @@ class IUtilAssetsIntegrity : public IBaseInterface int groupIndex, const void* group, size_t groupBytes, - UtilAssetsIntegrityCheckResult_StudioModel* result) = 0; + UtilAssetsIntegrityCheckResult_StudioModel* result, + bool bIsSvEngine = false) = 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 @@ -97,7 +98,8 @@ class IUtilAssetsIntegrity : public IBaseInterface const void* buf, size_t bufSize, UtilAssetsIntegrityBSPLayout layout, - UtilAssetsIntegrityCheckResult_BSP* result) = 0; + UtilAssetsIntegrityCheckResult_BSP* result, + bool bIsSvEngine = false) = 0; //Version 2 IDSP, a complete WAD2/WAD3 file (directory ranges only), and one unpacked //GoldSrc miptex with pixel and palette data, respectively. Diagnostics are optional. diff --git a/src/BSPValidation.h b/src/BSPValidation.h index 9e58426..c2ea493 100644 --- a/src/BSPValidation.h +++ b/src/BSPValidation.h @@ -19,11 +19,12 @@ class CBSPValidation using Reason = UtilAssetsIntegrityCheckReason; using Layout = UtilAssetsIntegrityBSPLayout; - CBSPValidation(const void* pData, size_t size, Layout layout, UtilAssetsIntegrityCheckResult_BSP* pResult) : + CBSPValidation(const void* pData, size_t size, Layout layout, UtilAssetsIntegrityCheckResult_BSP* pResult, bool bIsSvEngine = false) : m_pData(static_cast(pData)), m_Size(size), m_Layout(layout), - m_pResult(pResult) + m_pResult(pResult), + m_bIsSvEngine(bIsSvEngine) { if (m_pResult) m_pResult->ReasonStr[0] = 0; @@ -133,10 +134,12 @@ class CBSPValidation static constexpr int TexSpecial = 1; static constexpr int AbsentOffset = -1; //A face whose extents exceed this either errors in CalcSurfaceExtents or is TEX_SPECIAL, which has no lightmap. - static constexpr int MaxSurfaceExtent = 512; + static constexpr int MaxSurfaceExtent = 512; + static constexpr int SvEngineMaxSurfaceExtent = 1024; //CalcSurfaceExtents' starting extremes, kept as its floats. - static constexpr float ExtentsInitialMin = 999999.0f; - static constexpr float ExtentsInitialMax = -99999.0f; + static constexpr float ExtentsInitialMin = 999999.0f; + static constexpr float ExtentsInitialMax = -99999.0f; + static constexpr double SvEngineExtentsBound = 2147483648.0; //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. @@ -345,8 +348,12 @@ class CBSPValidation //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}; + double mins[TextureAxes] = { + m_bIsSvEngine ? SvEngineExtentsBound : static_cast(ExtentsInitialMin), + m_bIsSvEngine ? SvEngineExtentsBound : static_cast(ExtentsInitialMin)}; + double maxs[TextureAxes] = { + m_bIsSvEngine ? -SvEngineExtentsBound : static_cast(ExtentsInitialMax), + m_bIsSvEngine ? -SvEngineExtentsBound : static_cast(ExtentsInitialMax)}; const auto firstEdge = ReadInt32(face + FaceFirstEdgeOffset); const auto edgeCount = ReadInt16(face + FaceEdgeCountOffset); @@ -369,7 +376,7 @@ class CBSPValidation if (!(std::fabs(value) <= MaxTextureCoordinate)) return Bad("texture coordinate out of range", Reason::InvalidFormat); - const auto coordinate = static_cast(value); + const double coordinate = m_bIsSvEngine ? value : static_cast(static_cast(value)); if (coordinate < mins[axis]) mins[axis] = coordinate; if (coordinate > maxs[axis]) @@ -413,10 +420,11 @@ class CBSPValidation if (!FaceExtents(face, texInfo, extents)) return false; - const bool bSpecial = (ReadInt32(texInfo + TexInfoFlagsOffset) & TexSpecial) != 0; - const bool bLarge = extents[0] > MaxSurfaceExtent || extents[1] > MaxSurfaceExtent; + const int maxExtent = m_bIsSvEngine ? SvEngineMaxSurfaceExtent : MaxSurfaceExtent; + const bool bSpecial = (ReadInt32(texInfo + TexInfoFlagsOffset) & TexSpecial) != 0; + const bool bLarge = extents[0] > maxExtent || extents[1] > maxExtent; if (!bSpecial && bLarge) - return Bad("surface extents exceed 512", Reason::InvalidFormat); + return Bad(m_bIsSvEngine ? "surface extents exceed 1024" : "surface extents exceed 512", Reason::InvalidFormat); const auto lightOffset = ReadInt32(face + FaceLightOffset); if (lightOffset == AbsentOffset) @@ -759,6 +767,7 @@ class CBSPValidation size_t m_Size; Layout m_Layout; UtilAssetsIntegrityCheckResult_BSP* m_pResult; + bool m_bIsSvEngine{false}; Reason m_Reason{Reason::OK}; size_t m_Offsets[LumpCount]{}; size_t m_Lengths[LumpCount]{}; diff --git a/src/StudioModelValidation.h b/src/StudioModelValidation.h index 2dde6ae..e1acde3 100644 --- a/src/StudioModelValidation.h +++ b/src/StudioModelValidation.h @@ -22,12 +22,13 @@ class CStudioModelValidation public: using Reason = UtilAssetsIntegrityCheckReason; - CStudioModelValidation(const void* pData, size_t size, UtilAssetsIntegrityCheckResult_StudioModel* pResult) : + CStudioModelValidation(const void* pData, size_t size, UtilAssetsIntegrityCheckResult_StudioModel* pResult, bool bIsSvEngine = false) : m_pData(static_cast(pData)), m_Size(size), m_Limit(size), m_pResult(pResult), - m_LookaheadLimit(size) + m_LookaheadLimit(size), + m_bIsSvEngine(bIsSvEngine) { if (m_pResult) m_pResult->ReasonStr[0] = 0; @@ -55,8 +56,18 @@ class CStudioModelValidation if (m_Header.version != 10) return Fail("expected studio version 10", Reason::VersionMismatch); - if (m_Header.length < sizeof(studiohdr_t) || static_cast(m_Header.length) != m_Size) + if (m_Header.length < sizeof(studiohdr_t) || + (m_bIsSvEngine ? static_cast(m_Header.length) > m_Size : static_cast(m_Header.length) != m_Size)) + { return Fail("invalid file length"); + } + + if (m_bIsSvEngine && static_cast(m_Header.length) < m_Size) + { + m_Size = static_cast(m_Header.length); + m_Limit = m_Size; + m_LookaheadLimit = m_Size; + } //Header names are the source path studiomdl wrote, which may fill the field unterminated (Counter-Strike's //v_knife.mdl), as the original engine accepts; only names of records must be terminated. @@ -102,7 +113,7 @@ class CStudioModelValidation if (!pModel || modelBytes < sizeof(studiohdr_t) || modelBytes > INT_MAX) return Fail("missing main model for sequence group"); - CStudioModelValidation model(pModel, modelBytes, m_pResult); + CStudioModelValidation model(pModel, modelBytes, m_pResult, m_bIsSvEngine); model.m_Header = model.Read(0); const auto& h = model.m_Header; @@ -526,9 +537,11 @@ class CStudioModelValidation bool HitboxesAndAttachments() { - const auto& h = m_Header; + static constexpr int SvEngineAttachmentsLimit = 64; + const auto& h = m_Header; + const int attachmentsLimit = m_bIsSvEngine ? SvEngineAttachmentsLimit : AttachmentsLimit; if (!Table(h.hitboxindex, h.numhitboxes, sizeof(mstudiobbox_t), HitboxesLimit) || - !Table(h.attachmentindex, h.numattachments, sizeof(mstudioattachment_t), AttachmentsLimit)) + !Table(h.attachmentindex, h.numattachments, sizeof(mstudioattachment_t), attachmentsLimit)) { return false; } @@ -577,4 +590,5 @@ class CStudioModelValidation //Where interpolation reads may end: m_Limit, or past it by the tail callers keep after a sequence group. size_t m_LookaheadLimit; + bool m_bIsSvEngine; }; diff --git a/src/UtilAssetsIntegrity.cpp b/src/UtilAssetsIntegrity.cpp index a572762..fecf606 100644 --- a/src/UtilAssetsIntegrity.cpp +++ b/src/UtilAssetsIntegrity.cpp @@ -14,9 +14,10 @@ class CUtilAssetsIntegrity : public IUtilAssetsIntegrity UtilAssetsIntegrityCheckReason CheckStudioModel( const void* buf, size_t bufSize, - UtilAssetsIntegrityCheckResult_StudioModel* checkResult) noexcept override + UtilAssetsIntegrityCheckResult_StudioModel* checkResult, + bool bIsSvEngine) noexcept override { - return CStudioModelValidation(buf, bufSize, checkResult).Check(); + return CStudioModelValidation(buf, bufSize, checkResult, bIsSvEngine).Check(); } UtilAssetsIntegrityCheckReason Check8bitBMP( @@ -91,18 +92,20 @@ class CUtilAssetsIntegrity : public IUtilAssetsIntegrity int groupIndex, const void* group, size_t groupBytes, - UtilAssetsIntegrityCheckResult_StudioModel* result) noexcept override + UtilAssetsIntegrityCheckResult_StudioModel* result, + bool bIsSvEngine) noexcept override { - return CStudioModelValidation(group, groupBytes, result).CheckGroup(model, modelBytes, groupIndex); + return CStudioModelValidation(group, groupBytes, result, bIsSvEngine).CheckGroup(model, modelBytes, groupIndex); } UtilAssetsIntegrityCheckReason CheckBSP( const void* buf, size_t bufSize, UtilAssetsIntegrityBSPLayout layout, - UtilAssetsIntegrityCheckResult_BSP* result) noexcept override + UtilAssetsIntegrityCheckResult_BSP* result, + bool bIsSvEngine) noexcept override { - return CBSPValidation(buf, bufSize, layout, result).Check(); + return CBSPValidation(buf, bufSize, layout, result, bIsSvEngine).Check(); } UtilAssetsIntegrityCheckReason CheckSprite(const void* buf, size_t bufSize, UtilAssetsIntegrityCheckResult* result) noexcept override diff --git a/tests/BSPTests.h b/tests/BSPTests.h index 19a19fb..47b69cb 100644 --- a/tests/BSPTests.h +++ b/tests/BSPTests.h @@ -439,7 +439,19 @@ void CheckBSPs(IUtilAssetsIntegrity& api) texInfo.vecs[0][0] = 40; f.Set(F::TexInfos, texInfo); expect(Reason::InvalidFormat, f, "BSP surface extents over 512"); - texInfo.flags = 1; + //SvEngine allows surface extents up to 1024 when lightmap samples cover the face. + f.lumps[F::Lighting].assign(41 * 2 * 3, 0); + const auto svFile = f.Build(); + Expect(Reason::InvalidFormat, api.CheckBSP(svFile.data(), svFile.size(), Layout::GoldSrc, &result, false), "BSP surface extents 640 rejected in GoldSrc"); + Expect(Reason::OK, api.CheckBSP(svFile.data(), svFile.size(), Layout::GoldSrc, &result, true), "BSP surface extents 640 accepted in SvEngine"); + texInfo.vecs[0][0] = 65; // 1040 texels wide (> 1024) + f.Set(F::TexInfos, texInfo); + f.lumps[F::Lighting].assign(66 * 2 * 3, 0); + const auto svTooLarge = f.Build(); + Expect(Reason::InvalidFormat, api.CheckBSP(svTooLarge.data(), svTooLarge.size(), Layout::GoldSrc, &result, true), "BSP surface extents over 1024 rejected in SvEngine"); + + texInfo.vecs[0][0] = 40; + texInfo.flags = 1; f.Set(F::TexInfos, texInfo); expect(Reason::OK, f, "BSP large special surface has no lightmap"); } diff --git a/tests/StudioModelTests.h b/tests/StudioModelTests.h index 5d38121..31d31af 100644 --- a/tests/StudioModelTests.h +++ b/tests/StudioModelTests.h @@ -301,4 +301,28 @@ void CheckSequenceGroups(HMODULE library, IUtilAssetsIntegrity& api) Check(api.CheckStudioModel(mainModel.bytes.data(), mainModel.bytes.size(), nullptr) != Reason::OK, "main model lookahead past its kept structure"); mainModel.Write(mainRun, static_cast((3 << 8) | 2)); Expect(Reason::OK, api.CheckStudioModel(mainModel.bytes.data(), mainModel.bytes.size(), nullptr), "main model span longer than its sequence"); + + //SvEngine relaxations: trailing padding and >4 attachments (up to 64). + { + const StudioFixture valid{}; + auto padded = valid; + padded.bytes.resize(padded.bytes.size() + 16, 0); + Check(api.CheckStudioModel(padded.bytes.data(), padded.bytes.size(), nullptr, false) != Reason::OK, "trailing MDL padding rejected in GoldSrc"); + Expect(Reason::OK, api.CheckStudioModel(padded.bytes.data(), padded.bytes.size(), nullptr, true), "trailing MDL padding accepted in SvEngine"); + + auto manyAttachments = valid; + std::array attachments{}; + manyAttachments.header.numattachments = static_cast(attachments.size()); + manyAttachments.header.attachmentindex = manyAttachments.AppendStructure(attachments); + manyAttachments.Finish(); + Check(api.CheckStudioModel(manyAttachments.bytes.data(), manyAttachments.bytes.size(), nullptr, false) != Reason::OK, "8 attachments rejected in GoldSrc"); + Expect(Reason::OK, api.CheckStudioModel(manyAttachments.bytes.data(), manyAttachments.bytes.size(), nullptr, true), "8 attachments accepted in SvEngine"); + + auto tooManyAttachments = valid; + std::array overflowAttachments{}; + tooManyAttachments.header.numattachments = static_cast(overflowAttachments.size()); + tooManyAttachments.header.attachmentindex = tooManyAttachments.AppendStructure(overflowAttachments); + tooManyAttachments.Finish(); + Check(api.CheckStudioModel(tooManyAttachments.bytes.data(), tooManyAttachments.bytes.size(), nullptr, true) != Reason::OK, "65 attachments rejected in SvEngine"); + } }