Skip to content

Commit

Permalink
Update Pak_RegisterGuidRefAtOffset to reflect new repak changes
Browse files Browse the repository at this point in the history
Since internal usage count and resolving is now performed after all assets have been added, we no longer need to check if an asset exists here to increment some inline counter. The new system is a lot more reliable and guarantees that all internal usages are counter for any given asset.
  • Loading branch information
Mauler125 committed Dec 23, 2024
1 parent 6877163 commit 0d487e9
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/assets/animrig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void Assets::AddAnimRigAsset_v4(CPakFileBuilder* const pak, const PakGuid_t asse
const size_t offset = base + (i * sizeof(PakGuid_t));
const PakGuid_t guid = *reinterpret_cast<PakGuid_t*>(&rigChunk.data[offset]);

Pak_RegisterGuidRefAtOffset(pak, guid, offset, rigChunk, asset);
Pak_RegisterGuidRefAtOffset(guid, offset, rigChunk, asset);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/assets/animseq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static void AnimSeq_InternalAddAnimSeq(CPakFileBuilder* const pak, const PakGuid
const mstudioautolayer_t* const autolayer = dataBuf.get<const mstudioautolayer_t>();

const size_t offset = dataBuf.getPosition() + offsetof(mstudioautolayer_t, guid);
Pak_RegisterGuidRefAtOffset(pak, autolayer->guid, offset, dataChunk, asset);
Pak_RegisterGuidRefAtOffset(autolayer->guid, offset, dataChunk, asset);
}

asset.InitAsset(assetPath, assetGuid, hdrChunk.GetPointer(), hdrChunk.size, PagePtr_t::NullPtr(), UINT64_MAX, UINT64_MAX, AssetType::ASEQ);
Expand Down
20 changes: 11 additions & 9 deletions src/assets/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ static void Material_AddTextureRefs(CPakFileBuilder* const pak, PakPageLump_s& d
reinterpret_cast<PakGuid_t*>(dataBuf)[bindPoint] = textureGuid;
const size_t offset = alignedPathSize + (bindPoint * sizeof(PakGuid_t));

if (!Pak_RegisterGuidRefAtOffset(pak, textureGuid, offset, dataChunk, asset))
Pak_RegisterGuidRefAtOffset(textureGuid, offset, dataChunk, asset);

if (!pak->GetAssetByGuid(textureGuid))
Warning("Unable to find texture #%zu within the local assets.\n", bindPoint);
}
}
Expand Down Expand Up @@ -619,12 +621,12 @@ static void Material_InternalAddMaterialV12(CPakFileBuilder* const pak, const Pa

// register referenced assets (depth materials, colpass material, shader sets)

Pak_RegisterGuidRefAtOffset(pak, matlAsset.passMaterials[0], offsetof(MaterialAssetHeader_v12_t, passMaterials[0]), hdrChunk, asset);
Pak_RegisterGuidRefAtOffset(pak, matlAsset.passMaterials[1], offsetof(MaterialAssetHeader_v12_t, passMaterials[1]), hdrChunk, asset);
Pak_RegisterGuidRefAtOffset(pak, matlAsset.passMaterials[2], offsetof(MaterialAssetHeader_v12_t, passMaterials[2]), hdrChunk, asset);
Pak_RegisterGuidRefAtOffset(pak, matlAsset.passMaterials[3], offsetof(MaterialAssetHeader_v12_t, passMaterials[3]), hdrChunk, asset);
Pak_RegisterGuidRefAtOffset(matlAsset.passMaterials[0], offsetof(MaterialAssetHeader_v12_t, passMaterials[0]), hdrChunk, asset);
Pak_RegisterGuidRefAtOffset(matlAsset.passMaterials[1], offsetof(MaterialAssetHeader_v12_t, passMaterials[1]), hdrChunk, asset);
Pak_RegisterGuidRefAtOffset(matlAsset.passMaterials[2], offsetof(MaterialAssetHeader_v12_t, passMaterials[2]), hdrChunk, asset);
Pak_RegisterGuidRefAtOffset(matlAsset.passMaterials[3], offsetof(MaterialAssetHeader_v12_t, passMaterials[3]), hdrChunk, asset);

Pak_RegisterGuidRefAtOffset(pak, matlAsset.shaderSet, offsetof(MaterialAssetHeader_v12_t, shaderSet), hdrChunk, asset);
Pak_RegisterGuidRefAtOffset(matlAsset.shaderSet, offsetof(MaterialAssetHeader_v12_t, shaderSet), hdrChunk, asset);

// write header now that we are done setting it up
matlAsset.WriteToBuffer(hdrChunk.data);
Expand Down Expand Up @@ -738,11 +740,11 @@ static void Material_InternalAddMaterialV15(CPakFileBuilder* const pak, const Pa
const PakGuid_t guid = matlAsset.passMaterials[i];
const size_t offset = offsetof(MaterialAssetHeader_v15_t, passMaterials[i]);

Pak_RegisterGuidRefAtOffset(pak, guid, offset, hdrChunk, asset);
Pak_RegisterGuidRefAtOffset(guid, offset, hdrChunk, asset);
}

Pak_RegisterGuidRefAtOffset(pak, matlAsset.shaderSet, offsetof(MaterialAssetHeader_v15_t, shaderSet), hdrChunk, asset);
Pak_RegisterGuidRefAtOffset(pak, matlAsset.textureAnimation, offsetof(MaterialAssetHeader_v15_t, textureAnimation), hdrChunk, asset);
Pak_RegisterGuidRefAtOffset(matlAsset.shaderSet, offsetof(MaterialAssetHeader_v15_t, shaderSet), hdrChunk, asset);
Pak_RegisterGuidRefAtOffset(matlAsset.textureAnimation, offsetof(MaterialAssetHeader_v15_t, textureAnimation), hdrChunk, asset);

// write header now that we are done setting it up
matlAsset.WriteToBuffer(hdrChunk.data);
Expand Down
9 changes: 5 additions & 4 deletions src/assets/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ static void Model_AllocateIntermediateDataChunk(CPakFileBuilder* const pak, PakP
const size_t offset = base + (i * sizeof(PakGuid_t));
const PakGuid_t guid = *reinterpret_cast<PakGuid_t*>(&intermediateChunk.data[offset]);

Pak_RegisterGuidRefAtOffset(pak, guid, offset, intermediateChunk, asset);
Pak_RegisterGuidRefAtOffset(guid, offset, intermediateChunk, asset);
}
}

Expand All @@ -155,7 +155,7 @@ static void Model_AllocateIntermediateDataChunk(CPakFileBuilder* const pak, PakP
const size_t offset = base + (i * sizeof(PakGuid_t));
const PakGuid_t guid = *reinterpret_cast<PakGuid_t*>(&intermediateChunk.data[offset]);

Pak_RegisterGuidRefAtOffset(pak, guid, offset, intermediateChunk, asset);
Pak_RegisterGuidRefAtOffset(guid, offset, intermediateChunk, asset);
}
}
}
Expand Down Expand Up @@ -302,15 +302,16 @@ void Assets::AddModelAsset_v9(CPakFileBuilder* const pak, const PakGuid_t assetG
const size_t pos = (char*)tex - dataChunk.data;
const size_t offset = pos + offsetof(mstudiotexture_t, guid);

PakAsset_t* const internalAsset = Pak_RegisterGuidRefAtOffset(pak, tex->guid, offset, dataChunk, asset);
Pak_RegisterGuidRefAtOffset(tex->guid, offset, dataChunk, asset);
const PakAsset_t* const internalAsset = pak->GetAssetByGuid(tex->guid);

if (internalAsset)
{
// make sure referenced asset is a material for sanity
internalAsset->EnsureType(TYPE_MATL);

// model assets don't exist on r2 so we can be sure that this is a v8 pak (and therefore has v15 materials)
MaterialAssetHeader_v15_t* matlHdr = reinterpret_cast<MaterialAssetHeader_v15_t*>(internalAsset->header);
MaterialAssetHeader_v15_t* const matlHdr = reinterpret_cast<MaterialAssetHeader_v15_t*>(internalAsset->header);

if (matlHdr->materialType != studiohdr->materialType(i))
{
Expand Down
10 changes: 8 additions & 2 deletions src/assets/shaderset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,16 @@ void ShaderSet_InternalCreateSet(CPakFileBuilder* const pak, const char* const a

// todo: can shader refs be null???
if (hdr->vertexShader != 0)
vertexShader = Pak_RegisterGuidRefAtOffset(pak, hdr->vertexShader, offsetof(ShaderSetAssetHeader_t, vertexShader), hdrChunk, asset);
{
Pak_RegisterGuidRefAtOffset(hdr->vertexShader, offsetof(ShaderSetAssetHeader_t, vertexShader), hdrChunk, asset);
vertexShader = pak->GetAssetByGuid(hdr->vertexShader);
}

if (hdr->pixelShader != 0)
pixelShader = Pak_RegisterGuidRefAtOffset(pak, hdr->pixelShader, offsetof(ShaderSetAssetHeader_t, pixelShader), hdrChunk, asset);
{
Pak_RegisterGuidRefAtOffset(hdr->pixelShader, offsetof(ShaderSetAssetHeader_t, pixelShader), hdrChunk, asset);
pixelShader = pak->GetAssetByGuid(hdr->pixelShader);
}

ShaderSet_SetInputSlots(hdr, vertexShader, true, shaderSet, assetVersion);
ShaderSet_SetInputSlots(hdr, pixelShader, false, shaderSet, assetVersion);
Expand Down
2 changes: 1 addition & 1 deletion src/assets/uiatlas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void Assets::AddUIImageAsset_v10(CPakFileBuilder* const pak, const PakGuid_t ass
pHdr->unkCount = unkCount;
pHdr->atlasGUID = atlasGuid;

Pak_RegisterGuidRefAtOffset(pak, atlasGuid, offsetof(UIImageAtlasHeader_t, atlasGUID), hdrLump, asset, atlasAsset);
Pak_RegisterGuidRefAtOffset(atlasGuid, offsetof(UIImageAtlasHeader_t, atlasGUID), hdrLump, asset);

const size_t textureOffsetsDataSize = sizeof(UIImageOffset) * textureCount;

Expand Down
17 changes: 4 additions & 13 deletions src/logic/pakfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,25 +158,16 @@ class CPakFileBuilder
};

// if the asset already existed, the function will return true.
inline PakAsset_t* Pak_RegisterGuidRefAtOffset(CPakFileBuilder* const pak, const PakGuid_t guid, const size_t offset,
PakPageLump_s& chunk, PakAsset_t& asset, PakAsset_t* targetAsset = nullptr)
inline bool Pak_RegisterGuidRefAtOffset(const PakGuid_t guid, const size_t offset,
PakPageLump_s& chunk, PakAsset_t& asset)
{
// NULL guids should never be added. we check it here because otherwise we
// have to do a check at call site, and if we miss one we will end up with
// a hard to track bug. so always call this function, even if your guid
// might be NULL.
if (guid == 0)
return nullptr;
return false;

asset.AddGuid(chunk.GetPointer(offset), guid);

if (!targetAsset)
{
targetAsset = pak->GetAssetByGuid(guid, nullptr, true);

if (!targetAsset)
return nullptr;
}

return targetAsset;
return true;
}

0 comments on commit 0d487e9

Please sign in to comment.