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
10 changes: 6 additions & 4 deletions include/Interface/IUtilAssetsIntegrity.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修改了ABI之后是不是应该递增INTERFACE版本号?

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.
Expand All @@ -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
Expand All @@ -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.
Expand Down
31 changes: 20 additions & 11 deletions src/BSPValidation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<const std::uint8_t*>(pData)),
m_Size(size),
m_Layout(layout),
m_pResult(pResult)
m_pResult(pResult),
m_bIsSvEngine(bIsSvEngine)
{
if (m_pResult)
m_pResult->ReasonStr[0] = 0;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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<double>(ExtentsInitialMin),
m_bIsSvEngine ? SvEngineExtentsBound : static_cast<double>(ExtentsInitialMin)};
double maxs[TextureAxes] = {
m_bIsSvEngine ? -SvEngineExtentsBound : static_cast<double>(ExtentsInitialMax),
m_bIsSvEngine ? -SvEngineExtentsBound : static_cast<double>(ExtentsInitialMax)};

const auto firstEdge = ReadInt32(face + FaceFirstEdgeOffset);
const auto edgeCount = ReadInt16(face + FaceEdgeCountOffset);
Expand All @@ -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<float>(value);
const double coordinate = m_bIsSvEngine ? value : static_cast<double>(static_cast<float>(value));
if (coordinate < mins[axis])
mins[axis] = coordinate;
if (coordinate > maxs[axis])
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]{};
Expand Down
26 changes: 20 additions & 6 deletions src/StudioModelValidation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<const byte*>(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;
Expand Down Expand Up @@ -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<size_t>(m_Header.length) != m_Size)
if (m_Header.length < sizeof(studiohdr_t) ||
(m_bIsSvEngine ? static_cast<size_t>(m_Header.length) > m_Size : static_cast<size_t>(m_Header.length) != m_Size))
{
return Fail("invalid file length");
}

if (m_bIsSvEngine && static_cast<size_t>(m_Header.length) < m_Size)
{
m_Size = static_cast<size_t>(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.
Expand Down Expand Up @@ -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<studiohdr_t>(0);
const auto& h = model.m_Header;

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
};
15 changes: 9 additions & 6 deletions src/UtilAssetsIntegrity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion tests/BSPTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
24 changes: 24 additions & 0 deletions tests/StudioModelTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::int16_t>((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<mstudioattachment_t, 8> attachments{};
manyAttachments.header.numattachments = static_cast<int>(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<mstudioattachment_t, 65> overflowAttachments{};
tooManyAttachments.header.numattachments = static_cast<int>(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");
}
}
Loading