From 27862c9a04fcc5698e968804fb828cb3fe6fdbd8 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Tue, 24 Dec 2024 00:29:58 +0100 Subject: [PATCH] Simplify pointer assignment and registration API Make an API that registers and sets the pointer to the struct in a single call. The function only expects the objects now and deals with the calls internally. This significantly improves the readability of the code, and also significantly reduces the chances for human error where the previous GetPointer call for example as called on a different struct member than AddPointer. Now it is only defined once on call site. --- src/assets/animrig.cpp | 10 ++-- src/assets/animseq.cpp | 26 +++++----- src/assets/datatable.cpp | 100 +++++++++++++++++++++------------------ src/assets/material.cpp | 37 +++++---------- src/assets/model.cpp | 26 ++++------ src/assets/patch.cpp | 11 ++--- src/assets/shader.cpp | 29 ++++-------- src/assets/shaderset.cpp | 3 +- src/assets/texture.cpp | 3 +- src/assets/uiatlas.cpp | 9 ++-- src/logic/pakfile.cpp | 17 ++++--- src/logic/pakfile.h | 5 +- src/public/datatable.h | 35 -------------- src/public/material.h | 21 -------- 14 files changed, 119 insertions(+), 213 deletions(-) diff --git a/src/assets/animrig.cpp b/src/assets/animrig.cpp index c09e018..f677b89 100644 --- a/src/assets/animrig.cpp +++ b/src/assets/animrig.cpp @@ -39,14 +39,12 @@ void Assets::AddAnimRigAsset_v4(CPakFileBuilder* const pak, const PakGuid_t asse char* const nameBuf = rigChunk.data; memcpy(nameBuf, assetPath, assetNameBufLen); - pHdr->name = rigChunk.GetPointer(); - pak->AddPointer(hdrChunk.GetPointer(offsetof(AnimRigAssetHeader_t, name))); + pak->AddPointer(hdrChunk, offsetof(AnimRigAssetHeader_t, name), rigChunk, 0); studiohdr_t* const studioBuf = reinterpret_cast(&rigChunk.data[assetNameBufLen]); memcpy(studioBuf, animRigFileBuffer, studiohdr->length); - pHdr->data = rigChunk.GetPointer(assetNameBufLen); - pak->AddPointer(hdrChunk.GetPointer(offsetof(AnimRigAssetHeader_t, data))); + pak->AddPointer(hdrChunk, offsetof(AnimRigAssetHeader_t, data), rigChunk, assetNameBufLen); delete[] animRigFileBuffer; @@ -59,9 +57,7 @@ void Assets::AddAnimRigAsset_v4(CPakFileBuilder* const pak, const PakGuid_t asse delete[] sequenceRefs; pHdr->sequenceCount = sequenceCount; - pHdr->pSequences = rigChunk.GetPointer(base); - - pak->AddPointer(hdrChunk.GetPointer(offsetof(AnimRigAssetHeader_t, pSequences))); + pak->AddPointer(hdrChunk, offsetof(AnimRigAssetHeader_t, pSequences), rigChunk, base); for (uint32_t i = 0; i < sequenceCount; ++i) { diff --git a/src/assets/animseq.cpp b/src/assets/animseq.cpp index 73abf47..e52aa03 100644 --- a/src/assets/animseq.cpp +++ b/src/assets/animseq.cpp @@ -19,33 +19,29 @@ static void AnimSeq_InternalAddAnimSeq(CPakFileBuilder* const pak, const PakGuid return; } - PakPageLump_s hdrChunk = pak->CreatePageLump(sizeof(AnimSeqAssetHeader_t), SF_HEAD, 8); + PakPageLump_s hdrLump = pak->CreatePageLump(sizeof(AnimSeqAssetHeader_t), SF_HEAD, 8); const size_t rseqNameBufLen = strlen(assetPath) + 1; const size_t rseqFileSize = rseqInput.GetSize(); - PakPageLump_s dataChunk = pak->CreatePageLump(rseqNameBufLen + rseqFileSize, SF_CPU, 1); + PakPageLump_s dataLump = pak->CreatePageLump(rseqNameBufLen + rseqFileSize, SF_CPU, 1); // write the rseq file path into the data buffer - memcpy(dataChunk.data, assetPath, rseqNameBufLen); + memcpy(dataLump.data, assetPath, rseqNameBufLen); // write the rseq data into the data buffer - rseqInput.Read(dataChunk.data + rseqNameBufLen, rseqFileSize); + rseqInput.Read(dataLump.data + rseqNameBufLen, rseqFileSize); rseqInput.Close(); - const mstudioseqdesc_t& seqdesc = *reinterpret_cast(dataChunk.data + rseqNameBufLen); - AnimSeqAssetHeader_t* const aseqHeader = reinterpret_cast(hdrChunk.data); + const mstudioseqdesc_t& seqdesc = *reinterpret_cast(dataLump.data + rseqNameBufLen); - aseqHeader->szname = dataChunk.GetPointer(); - pak->AddPointer(hdrChunk.GetPointer(offsetof(AnimSeqAssetHeader_t, szname))); - - aseqHeader->data = dataChunk.GetPointer(rseqNameBufLen); - pak->AddPointer(hdrChunk.GetPointer(offsetof(AnimSeqAssetHeader_t, data))); + pak->AddPointer(hdrLump, offsetof(AnimSeqAssetHeader_t, szname), dataLump, 0); + pak->AddPointer(hdrLump, offsetof(AnimSeqAssetHeader_t, data), dataLump, rseqNameBufLen); if (seqdesc.numautolayers > 0) asset.ExpandGuidBuf(seqdesc.numautolayers); - rmem dataBuf(dataChunk.data); + rmem dataBuf(dataLump.data); dataBuf.seek(rseqNameBufLen + seqdesc.autolayerindex, rseekdir::beg); // Iterate over each of the sequence's autolayers to register each of the autolayer GUIDs @@ -56,11 +52,11 @@ static void AnimSeq_InternalAddAnimSeq(CPakFileBuilder* const pak, const PakGuid const mstudioautolayer_t* const autolayer = dataBuf.get(); const size_t offset = dataBuf.getPosition() + offsetof(mstudioautolayer_t, guid); - Pak_RegisterGuidRefAtOffset(autolayer->guid, offset, dataChunk, asset); + Pak_RegisterGuidRefAtOffset(autolayer->guid, offset, dataLump, asset); } - asset.InitAsset(assetPath, assetGuid, hdrChunk.GetPointer(), hdrChunk.size, PagePtr_t::NullPtr(), UINT64_MAX, UINT64_MAX, AssetType::ASEQ); - asset.SetHeaderPointer(hdrChunk.data); + asset.InitAsset(assetPath, assetGuid, hdrLump.GetPointer(), hdrLump.size, PagePtr_t::NullPtr(), UINT64_MAX, UINT64_MAX, AssetType::ASEQ); + asset.SetHeaderPointer(hdrLump.data); asset.version = 7; asset.pageEnd = pak->GetNumPages(); diff --git a/src/assets/datatable.cpp b/src/assets/datatable.cpp index c635cd8..5c340cb 100644 --- a/src/assets/datatable.cpp +++ b/src/assets/datatable.cpp @@ -20,46 +20,48 @@ static void DataTable_ReportInvalidDataTypeError(const char* const type, const u Error("Invalid data type \"%s\" at cell (%u, %u).\n", type, colIdx, rowIdx); } -static void DataTable_SetupRows(const rapidcsv::Document& doc, datatable_asset_t* const pHdrTemp, std::vector& outTypeRow) +template +static void DataTable_SetupRows(const rapidcsv::Document& doc, datatable_t* const dtblHdr, datatable_asset_t& tmp, std::vector& outTypeRow) { // cache it so we don't have to make another deep copy. - outTypeRow = doc.GetRow(pHdrTemp->numRows); + outTypeRow = doc.GetRow(dtblHdr->numRows); const uint32_t numTypeNames = static_cast(outTypeRow.size()); // typically happens when there's an empty line in the csv file. - if (numTypeNames != pHdrTemp->numColumns) - Error("Expected %u columns for type name row, found %u.\n", pHdrTemp->numRows, numTypeNames); + if (numTypeNames != dtblHdr->numColumns) + Error("Expected %u columns for type name row, found %u.\n", dtblHdr->numRows, numTypeNames); - for (uint32_t i = 0; i < pHdrTemp->numColumns; ++i) + for (uint32_t i = 0; i < dtblHdr->numColumns; ++i) { const std::string& typeString = outTypeRow[i]; const dtblcoltype_t type = DataTable_GetTypeFromString(typeString); if (type == dtblcoltype_t::INVALID) - DataTable_ReportInvalidDataTypeError(typeString.c_str(), i, pHdrTemp->numRows); + DataTable_ReportInvalidDataTypeError(typeString.c_str(), i, dtblHdr->numRows); if (DataTable_IsStringType(type)) { - for (uint32_t j = 0; j < pHdrTemp->numRows; ++j) + for (uint32_t j = 0; j < dtblHdr->numRows; ++j) { // this can be std::string since we only deal with the string types here std::vector row = doc.GetRow(j); - pHdrTemp->rowStringValueBufSize += row[i].length() + 1; + tmp.rowStringValueBufSize += row[i].length() + 1; } } - pHdrTemp->rowPodValueBufSize += static_cast(DataTable_GetValueSize(type)) * pHdrTemp->numRows; // size of type * row count (excluding the type row) + tmp.rowPodValueBufSize += static_cast(DataTable_GetValueSize(type)) * dtblHdr->numRows; // size of type * row count (excluding the type row) } } // fills a PakPageDataChunk_s with column data from a provided csv -static void DataTable_SetupColumns(CPakFileBuilder* const pak, PakPageLump_s& dataChunk, const size_t columnNameBase, datatable_asset_t* const pHdrTemp, - const rapidcsv::Document& doc, const std::vector& typeRow) +template +static void DataTable_SetupColumns(CPakFileBuilder* const pak, PakPageLump_s& dataChunk, const size_t columnNameBase, datatable_t* const dtblHdr, + datatable_asset_t& tmp, const rapidcsv::Document& doc, const std::vector& typeRow) { char* const colNameBufBase = &dataChunk.data[columnNameBase]; char* colNameBuf = colNameBufBase; - for (uint32_t i = 0; i < pHdrTemp->numColumns; ++i) + for (uint32_t i = 0; i < dtblHdr->numColumns; ++i) { const std::string name = doc.GetColumnName(i); const size_t nameBufLen = name.length() + 1; @@ -67,23 +69,22 @@ static void DataTable_SetupColumns(CPakFileBuilder* const pak, PakPageLump_s& da // copy the column name into the namebuf memcpy(colNameBuf, name.c_str(), nameBufLen); - datacolumn_t& col = pHdrTemp->pDataColums[i]; - col.pName = dataChunk.GetPointer(columnNameBase + (colNameBuf - colNameBufBase)); + datacolumn_t& col = tmp.pDataColums[i]; // register name pointer - pak->AddPointer(dataChunk.GetPointer(((sizeof(datacolumn_t) * i) + offsetof(datacolumn_t, pName)))); + pak->AddPointer(dataChunk, ((sizeof(datacolumn_t) * i) + offsetof(datacolumn_t, pName)), dataChunk, columnNameBase + (colNameBuf - colNameBufBase)); colNameBuf += nameBufLen; const std::string& typeString = typeRow[i]; const dtblcoltype_t type = DataTable_GetTypeFromString(typeString); if (type == dtblcoltype_t::INVALID) - DataTable_ReportInvalidDataTypeError(typeString.c_str(), i, pHdrTemp->numRows); + DataTable_ReportInvalidDataTypeError(typeString.c_str(), i, dtblHdr->numRows); - col.rowOffset = pHdrTemp->rowStride; + col.rowOffset = dtblHdr->rowStride; col.type = type; - pHdrTemp->rowStride += DataTable_GetValueSize(type); + dtblHdr->rowStride += DataTable_GetValueSize(type); } } @@ -93,17 +94,19 @@ static void DataTable_ReportInvalidValueError(const dtblcoltype_t type, const ui } // fills a PakPageDataChunk_s with row data from a provided csv -static void DataTable_SetupValues(CPakFileBuilder* const pak, PakPageLump_s& dataChunk, const size_t podValueBase, const size_t stringValueBase, datatable_asset_t* const pHdrTemp, rapidcsv::Document& doc) +template +static void DataTable_SetupValues(CPakFileBuilder* const pak, PakPageLump_s& dataChunk, const size_t podValueBase, const size_t stringValueBase, + datatable_t* const dtblHdr, datatable_asset_t& tmp, rapidcsv::Document& doc) { char* const pStringBufBase = &dataChunk.data[stringValueBase]; char* pStringBuf = pStringBufBase; - for (uint32_t rowIdx = 0; rowIdx < pHdrTemp->numRows; ++rowIdx) + for (uint32_t rowIdx = 0; rowIdx < dtblHdr->numRows; ++rowIdx) { - for (uint32_t colIdx = 0; colIdx < pHdrTemp->numColumns; ++colIdx) + for (uint32_t colIdx = 0; colIdx < dtblHdr->numColumns; ++colIdx) { - const datacolumn_t& col = pHdrTemp->pDataColums[colIdx]; - const size_t valueOffset = (pHdrTemp->rowStride * rowIdx) + col.rowOffset; + const datacolumn_t& col = tmp.pDataColums[colIdx]; + const size_t valueOffset = (dtblHdr->rowStride * rowIdx) + col.rowOffset; void* const valueBufBase = &dataChunk.data[podValueBase + valueOffset]; @@ -172,7 +175,8 @@ static void DataTable_SetupValues(CPakFileBuilder* const pak, PakPageLump_s& dat memcpy(pStringBuf, val.c_str(), valBufLen); valbuf.write(dataChunk.GetPointer(stringValueBase + (pStringBuf - pStringBufBase))); - pak->AddPointer(dataChunk.GetPointer(podValueBase + valueOffset)); + + pak->AddPointer(dataChunk, podValueBase + valueOffset); pStringBuf += valBufLen; break; @@ -185,7 +189,8 @@ static void DataTable_SetupValues(CPakFileBuilder* const pak, PakPageLump_s& dat // page chunk structure and order: // - header HEAD (align=8) // - data CPU (align=8) data columns, column names, pod row values then string row values. only data columns is aligned to 8, the rest is 1. -void Assets::AddDataTableAsset(CPakFileBuilder* const pak, const PakGuid_t assetGuid, const char* const assetPath, const rapidjson::Value& mapEntry) +template +static void DataTable_AddDataTable(CPakFileBuilder* const pak, const PakGuid_t assetGuid, const char* const assetPath, const rapidjson::Value& mapEntry) { UNUSED(mapEntry); PakAsset_t asset; @@ -219,50 +224,47 @@ void Assets::AddDataTableAsset(CPakFileBuilder* const pak, const PakGuid_t asset else hdrChunk = pak->CreatePageLump(sizeof(datatable_v1_t), SF_HEAD, 8); - datatable_asset_t dtblHdr{}; // temp header that we store values in, this is for sharing funcs across versions + datatable_t* const dtblHdr = reinterpret_cast(hdrChunk.data); + datatable_asset_t dtblAsset{}; // temp header that we store values in. - dtblHdr.numColumns = static_cast(doc.GetColumnCount()); - dtblHdr.numRows = static_cast(doc.GetRowCount()-1); // -1 because last row isn't added (used for type info) - dtblHdr.assetPath = assetPath; + dtblHdr->numColumns = static_cast(doc.GetColumnCount()); + dtblHdr->numRows = static_cast(doc.GetRowCount() - 1); // -1 because last row isn't added (used for type info) std::vector typeRow; - DataTable_SetupRows(doc, &dtblHdr, typeRow); + DataTable_SetupRows(doc, dtblHdr, dtblAsset, typeRow); - const size_t dataColumnsBufSize = dtblHdr.numColumns * sizeof(datacolumn_t); + const size_t dataColumnsBufSize = dtblHdr->numColumns * sizeof(datacolumn_t); const size_t columnNamesBufSize = DataTable_CalcColumnNameBufSize(doc); - const size_t totalChunkSize = dataColumnsBufSize + columnNamesBufSize + dtblHdr.rowPodValueBufSize + dtblHdr.rowStringValueBufSize; + const size_t totalChunkSize = dataColumnsBufSize + columnNamesBufSize + dtblAsset.rowPodValueBufSize + dtblAsset.rowStringValueBufSize; // create whole datatable chunk PakPageLump_s dataChunk = pak->CreatePageLump(totalChunkSize, SF_CPU, 8); - + // colums from data chunk - dtblHdr.pColumns = dataChunk.GetPointer(); - dtblHdr.pDataColums = reinterpret_cast(dataChunk.data); + dtblAsset.pDataColums = reinterpret_cast(dataChunk.data); - pak->AddPointer(hdrChunk.GetPointer(offsetof(datatable_asset_t, pColumns))); + // datatable v0 and v1 use the same struct offset for pColumns. + pak->AddPointer(hdrChunk, offsetof(datatable_v1_t, pColumns), dataChunk, 0); // setup data in column data chunk - DataTable_SetupColumns(pak, dataChunk, dataColumnsBufSize, &dtblHdr, doc, typeRow); + DataTable_SetupColumns(pak, dataChunk, dataColumnsBufSize, dtblHdr, dtblAsset, doc, typeRow); // Plain-old-data and string values use different buffers! const size_t rowPodValuesBase = dataColumnsBufSize + columnNamesBufSize; - const size_t rowStringValuesBase = rowPodValuesBase + dtblHdr.rowPodValueBufSize; + const size_t rowStringValuesBase = rowPodValuesBase + dtblAsset.rowPodValueBufSize; // setup row data chunks - DataTable_SetupValues(pak, dataChunk, rowPodValuesBase, rowStringValuesBase, &dtblHdr, doc); + DataTable_SetupValues(pak, dataChunk, rowPodValuesBase, rowStringValuesBase, dtblHdr, dtblAsset, doc); - // setup row page ptr - dtblHdr.pRows = dataChunk.GetPointer(rowPodValuesBase); - pak->AddPointer(hdrChunk.GetPointer(offsetof(datatable_asset_t, pRows))); - - dtblHdr.WriteToBuffer(hdrChunk.data, pak->GetVersion()); + // datatable v0 and v1 use the same struct offset for pRows. + pak->AddPointer(hdrChunk, offsetof(datatable_v0_t, pRows), dataChunk, rowPodValuesBase); asset.InitAsset( assetPath, assetGuid, hdrChunk.GetPointer(), hdrChunk.size, - dtblHdr.pRows, + dataChunk.GetPointer(rowPodValuesBase), // points to datatable_asset_t::pRow UINT64_MAX, UINT64_MAX, AssetType::DTBL); asset.SetHeaderPointer(hdrChunk.data); @@ -273,4 +275,12 @@ void Assets::AddDataTableAsset(CPakFileBuilder* const pak, const PakGuid_t asset asset.pageEnd = pak->GetNumPages(); pak->PushAsset(asset); +} + +void Assets::AddDataTableAsset(CPakFileBuilder* const pak, const PakGuid_t assetGuid, const char* const assetPath, const rapidjson::Value& mapEntry) +{ + if (pak->GetVersion() <= 7) + DataTable_AddDataTable(pak, assetGuid, assetPath, mapEntry); + else + DataTable_AddDataTable(pak, assetGuid, assetPath, mapEntry); } \ No newline at end of file diff --git a/src/assets/material.cpp b/src/assets/material.cpp index f8191a4..1e57244 100644 --- a/src/assets/material.cpp +++ b/src/assets/material.cpp @@ -522,9 +522,7 @@ static void Material_InternalAddMaterialV12(CPakFileBuilder* const pak, const Pa PakPageLump_s nameChunk = pak->CreatePageLump(nameBufLen, SF_CPU | SF_DEV, 1); memcpy(nameChunk.data, matlAsset.name.c_str(), nameBufLen); - matlAsset.materialName = nameChunk.GetPointer(); - - pak->AddPointer(hdrChunk.GetPointer(offsetof(MaterialAssetHeader_v12_t, materialName))); + pak->AddPointer(hdrChunk, offsetof(MaterialAssetHeader_v12_t, materialName), nameChunk, 0); } if (matlAsset.materialType != _TYPE_LEGACY) @@ -597,25 +595,21 @@ static void Material_InternalAddMaterialV12(CPakFileBuilder* const pak, const Pa size_t currentDataBufOffset = 0; - matlAsset.textureHandles = dataChunk.GetPointer(currentDataBufOffset); - pak->AddPointer(hdrChunk.GetPointer(offsetof(MaterialAssetHeader_v12_t, textureHandles))); + pak->AddPointer(hdrChunk, offsetof(MaterialAssetHeader_v12_t, textureHandles), dataChunk, currentDataBufOffset); currentDataBufOffset += textureRefSize; - matlAsset.streamingTextureHandles = dataChunk.GetPointer(currentDataBufOffset); - pak->AddPointer(hdrChunk.GetPointer(offsetof(MaterialAssetHeader_v12_t, streamingTextureHandles))); + pak->AddPointer(hdrChunk, offsetof(MaterialAssetHeader_v12_t, streamingTextureHandles), dataChunk, currentDataBufOffset); currentDataBufOffset += textureRefSize; if (surfaceProp1Size) { - matlAsset.surfaceProp = dataChunk.GetPointer(currentDataBufOffset); - pak->AddPointer(hdrChunk.GetPointer(offsetof(MaterialAssetHeader_v12_t, surfaceProp))); + pak->AddPointer(hdrChunk, offsetof(MaterialAssetHeader_v12_t, surfaceProp), dataChunk, currentDataBufOffset); currentDataBufOffset += surfaceProp1Size; } if (surfaceProp2Size) { - matlAsset.surfaceProp2 = dataChunk.GetPointer(currentDataBufOffset); - pak->AddPointer(hdrChunk.GetPointer(offsetof(MaterialAssetHeader_v12_t, surfaceProp2))); + pak->AddPointer(hdrChunk, offsetof(MaterialAssetHeader_v12_t, surfaceProp2), dataChunk, currentDataBufOffset); currentDataBufOffset += surfaceProp2Size; } @@ -639,12 +633,11 @@ static void Material_InternalAddMaterialV12(CPakFileBuilder* const pak, const Pa Material_AddCpuData(pak, &matlAsset, matEntry, uberBufChunk, dxStaticBufSize); MaterialCPUHeader* cpuhdr = reinterpret_cast(uberBufChunk.data); - cpuhdr->dataPtr = uberBufChunk.GetPointer(sizeof(MaterialCPUHeader)); cpuhdr->dataSize = (uint32_t)dxStaticBufSize; cpuhdr->version = 3; // unsure what this value actually is but some cpu headers have // different values. the engine doesn't seem to use it however. - pak->AddPointer(uberBufChunk.GetPointer(offsetof(MaterialCPUHeader, dataPtr))); + pak->AddPointer(uberBufChunk, offsetof(MaterialCPUHeader, dataPtr), uberBufChunk, sizeof(MaterialCPUHeader)); ////////////////////////////////////////// @@ -708,29 +701,24 @@ static void Material_InternalAddMaterialV15(CPakFileBuilder* const pak, const Pa size_t currentDataBufOffset = 0; - matlAsset.materialName = dataChunk.GetPointer(); - pak->AddPointer(hdrChunk.GetPointer(offsetof(MaterialAssetHeader_v15_t, materialName))); + pak->AddPointer(hdrChunk, offsetof(MaterialAssetHeader_v15_t, materialName), dataChunk, currentDataBufOffset); currentDataBufOffset += alignedPathSize; - matlAsset.textureHandles = dataChunk.GetPointer(currentDataBufOffset); - pak->AddPointer(hdrChunk.GetPointer(offsetof(MaterialAssetHeader_v15_t, textureHandles))); + pak->AddPointer(hdrChunk, offsetof(MaterialAssetHeader_v15_t, textureHandles), dataChunk, currentDataBufOffset); currentDataBufOffset += textureRefSize; - matlAsset.streamingTextureHandles = dataChunk.GetPointer(currentDataBufOffset); - pak->AddPointer(hdrChunk.GetPointer(offsetof(MaterialAssetHeader_v15_t, streamingTextureHandles))); + pak->AddPointer(hdrChunk, offsetof(MaterialAssetHeader_v15_t, streamingTextureHandles), dataChunk, currentDataBufOffset); currentDataBufOffset += textureRefSize; if (surfaceProp1Size) { - matlAsset.surfaceProp = dataChunk.GetPointer(currentDataBufOffset); - pak->AddPointer(hdrChunk.GetPointer(offsetof(MaterialAssetHeader_v15_t, surfaceProp))); + pak->AddPointer(hdrChunk, offsetof(MaterialAssetHeader_v15_t, surfaceProp), dataChunk, currentDataBufOffset); currentDataBufOffset += surfaceProp1Size; } if (surfaceProp2Size) { - matlAsset.surfaceProp2 = dataChunk.GetPointer(currentDataBufOffset); - pak->AddPointer(hdrChunk.GetPointer(offsetof(MaterialAssetHeader_v15_t, surfaceProp2))); + pak->AddPointer(hdrChunk, offsetof(MaterialAssetHeader_v15_t, surfaceProp2), dataChunk, currentDataBufOffset); currentDataBufOffset += surfaceProp2Size; } @@ -757,12 +745,11 @@ static void Material_InternalAddMaterialV15(CPakFileBuilder* const pak, const Pa Material_AddCpuData(pak, &matlAsset, matEntry, uberBufChunk, dxStaticBufSize); MaterialCPUHeader* cpuhdr = reinterpret_cast(uberBufChunk.data); - cpuhdr->dataPtr = uberBufChunk.GetPointer(sizeof(MaterialCPUHeader)); cpuhdr->dataSize = (uint32_t)dxStaticBufSize; cpuhdr->version = 3; // unsure what this value actually is but some cpu headers have // different values. the engine doesn't seem to use it however. - pak->AddPointer(uberBufChunk.GetPointer(offsetof(MaterialCPUHeader, dataPtr))); + pak->AddPointer(uberBufChunk, offsetof(MaterialCPUHeader, dataPtr), uberBufChunk, sizeof(MaterialCPUHeader)); ////////////////////////////////////////// diff --git a/src/assets/model.cpp b/src/assets/model.cpp index 39f86d7..92c4580 100644 --- a/src/assets/model.cpp +++ b/src/assets/model.cpp @@ -60,7 +60,7 @@ static char* Model_ReadVGFile(const std::string& path, size_t* const pFileSize) return buf; } -static PakGuid_t* Model_AddAnimRigRefs(uint32_t* const sequenceCount, const rapidjson::Value& mapEntry) +static PakGuid_t* Model_AddAnimRigRefs(uint32_t* const animrigCount, const rapidjson::Value& mapEntry) { rapidjson::Value::ConstMemberIterator it; @@ -87,7 +87,7 @@ static PakGuid_t* Model_AddAnimRigRefs(uint32_t* const sequenceCount, const rapi guidBuf[i] = guid; } - (*sequenceCount) = static_cast(animrigs.Size()); + (*animrigCount) = static_cast(animrigs.Size()); return guidBuf; } @@ -109,8 +109,7 @@ static void Model_AllocateIntermediateDataChunk(CPakFileBuilder* const pak, PakP PakPageLump_s intermediateChunk = pak->CreatePageLump(alignedNameBufLen + animRigRefsBufLen + sequenceRefsBufLen, SF_CPU, hasGuidRefs ? 8 : 1); memcpy(intermediateChunk.data, assetPath, modelNameBufLen); // Write the null-terminated asset path to the chunk buffer. - pHdr->pName = intermediateChunk.GetPointer(); - pak->AddPointer(hdrChunk.GetPointer(offsetof(ModelAssetHeader_t, pName))); + pak->AddPointer(hdrChunk, offsetof(ModelAssetHeader_t, pName), intermediateChunk, 0); if (hasGuidRefs) { @@ -125,9 +124,7 @@ static void Model_AllocateIntermediateDataChunk(CPakFileBuilder* const pak, PakP delete[] animrigRefs; pHdr->animRigCount = animrigCount; - pHdr->pAnimRigs = intermediateChunk.GetPointer(base); - - pak->AddPointer(hdrChunk.GetPointer(offsetof(ModelAssetHeader_t, pAnimRigs))); + pak->AddPointer(hdrChunk, offsetof(ModelAssetHeader_t, pAnimRigs), intermediateChunk, base); for (uint32_t i = 0; i < animrigCount; ++i) { @@ -146,9 +143,7 @@ static void Model_AllocateIntermediateDataChunk(CPakFileBuilder* const pak, PakP delete[] sequenceRefs; pHdr->sequenceCount = sequenceCount; - pHdr->pSequences = intermediateChunk.GetPointer(base); - - pak->AddPointer(hdrChunk.GetPointer(offsetof(ModelAssetHeader_t, pSequences))); + pak->AddPointer(hdrChunk, offsetof(ModelAssetHeader_t, pSequences), intermediateChunk, base); for (uint32_t i = 0; i < sequenceCount; ++i) { @@ -184,9 +179,7 @@ static uint64_t Model_InternalAddVertexGroupData(CPakFileBuilder* const pak, Pak if (studiohdr->IsStaticProp()) { PakPageLump_s vgLump = pak->CreatePageLump(vgFileSize, SF_CPU | SF_TEMP | SF_CLIENT, 1, vgBuf); - - modelHdr->pStaticPropVtxCache = vgLump.GetPointer(); - pak->AddPointer(hdrChunk->GetPointer(offsetof(ModelAssetHeader_t, pStaticPropVtxCache))); + pak->AddPointer(*hdrChunk, offsetof(ModelAssetHeader_t, pStaticPropVtxCache), vgLump, 0); } else delete[] vgBuf; @@ -247,8 +240,7 @@ void Assets::AddModelAsset_v9(CPakFileBuilder* const pak, const PakGuid_t assetG PakPageLump_s phyChunk = pak->CreatePageLump(phyFileSize, SF_CPU | SF_TEMP, 1); phyInput.Read(phyChunk.data, phyFileSize); - pHdr->pPhyData = phyChunk.GetPointer(); - pak->AddPointer(hdrChunk.GetPointer(offsetof(ModelAssetHeader_t, pPhyData))); + pak->AddPointer(hdrChunk, offsetof(ModelAssetHeader_t, pPhyData), phyChunk, 0); } // @@ -263,9 +255,7 @@ void Assets::AddModelAsset_v9(CPakFileBuilder* const pak, const PakGuid_t assetG // the last chunk is the actual data chunk that contains the rmdl PakPageLump_s dataChunk = pak->CreatePageLump(studiohdr->length, SF_CPU, 64, rmdlBuf); - - pHdr->pData = dataChunk.GetPointer(); - pak->AddPointer(hdrChunk.GetPointer(offsetof(ModelAssetHeader_t, pData))); + pak->AddPointer(hdrChunk, offsetof(ModelAssetHeader_t, pData), dataChunk, 0); // Material Overrides Handling rapidjson::Value::ConstMemberIterator materialsIt; diff --git a/src/assets/patch.cpp b/src/assets/patch.cpp index a8e5f74..42821a8 100644 --- a/src/assets/patch.cpp +++ b/src/assets/patch.cpp @@ -38,11 +38,8 @@ void Assets::AddPatchAsset(CPakFileBuilder* const pak, const PakGuid_t assetGuid const int patchNumbersOffset = sizeof(PagePtr_t) * pHdr->patchedPakCount; - pHdr->pPakNames = dataChunk.GetPointer(); - pHdr->pPakPatchNums = dataChunk.GetPointer(patchNumbersOffset); - - pak->AddPointer(hdrChunk.GetPointer(offsetof(PatchAssetHeader_t, pPakNames))); - pak->AddPointer(hdrChunk.GetPointer(offsetof(PatchAssetHeader_t, pPakPatchNums))); + pak->AddPointer(hdrChunk, offsetof(PatchAssetHeader_t, pPakNames), dataChunk, 0); + pak->AddPointer(hdrChunk, offsetof(PatchAssetHeader_t, pPakPatchNums), dataChunk, patchNumbersOffset); rmem dataBuf(dataChunk.data); @@ -56,9 +53,9 @@ void Assets::AddPatchAsset(CPakFileBuilder* const pak, const PakGuid_t assetGuid // write the patch number for this entry into the buffer dataBuf.write(it.highestPatchNum, pHdr->pPakPatchNums.offset + i); - snprintf(dataChunk.data + fileNameOffset, it.pakFileName.length() + 1, "%s", it.pakFileName.c_str()); + memcpy(&dataChunk.data[fileNameOffset], it.pakFileName.c_str(), it.pakFileName.length() + 1); - pak->AddPointer(dataChunk.GetPointer(sizeof(PagePtr_t) * i)); + pak->AddPointer(dataChunk, (sizeof(PagePtr_t) * i)); i++; } diff --git a/src/assets/shader.cpp b/src/assets/shader.cpp index 2fd3760..44bcf87 100644 --- a/src/assets/shader.cpp +++ b/src/assets/shader.cpp @@ -12,7 +12,7 @@ static void Shader_LoadFromMSW(CPakFileBuilder* const pak, const char* const ass template static void Shader_CreateFromMSW(CPakFileBuilder* const pak, PakPageLump_s& cpuDataChunk, ParsedDXShaderData_t* const firstShaderData, - const CMultiShaderWrapperIO::Shader_t* shader, ShaderAssetHeader_t* const hdr) + const CMultiShaderWrapperIO::Shader_t* shader, PakPageLump_s& hdrChunk, ShaderAssetHeader_t* const hdr) { const size_t numShaderBuffers = shader->entries.size(); size_t totalShaderDataSize = 0; @@ -58,22 +58,17 @@ static void Shader_CreateFromMSW(CPakFileBuilder* const pak, PakPageLump_s& cpuD { assert(entry.size > 0); - bc->data = cpuDataChunk.GetPointer(nextBytecodeBufferOffset); + // Register the data pointer at the byte code. + pak->AddPointer(cpuDataChunk, (i * entrySize) + offsetof(ShaderByteCode_t, data), cpuDataChunk, nextBytecodeBufferOffset); bc->dataSize = entry.size; if (hdr->type == eShaderType::Vertex) { + pak->AddPointer(cpuDataChunk, (i * entrySize) + offsetof(ShaderByteCode_t, inputSignatureBlob), cpuDataChunk, nextBytecodeBufferOffset); bc->inputSignatureBlobSize = bc->dataSize; - bc->inputSignatureBlob = bc->data; - - pak->AddPointer(cpuDataChunk.GetPointer((i * entrySize) + offsetof(ShaderByteCode_t, inputSignatureBlob))); } - // Register the data pointer at the - pak->AddPointer(cpuDataChunk.GetPointer((i * entrySize) + offsetof(ShaderByteCode_t, data))); - memcpy_s(cpuDataChunk.data + nextBytecodeBufferOffset, entry.size, entry.buffer, entry.size); - nextBytecodeBufferOffset += IALIGN(entry.size, 8); } else @@ -101,10 +96,8 @@ static void Shader_CreateFromMSW(CPakFileBuilder* const pak, PakPageLump_s& cpuD const size_t reservedDataSize = numShaderBuffers * (16); PakPageLump_s shaderInfoChunk = pak->CreatePageLump(reservedDataSize + inputFlagsDataSize, SF_CPU, 1); - // Get a pointer to the beginning of the reserved data section - hdr->unk_10 = shaderInfoChunk.GetPointer(); - // Get a pointer after the end of the reserved data section - hdr->shaderInputFlags = shaderInfoChunk.GetPointer(reservedDataSize); + pak->AddPointer(hdrChunk, offsetof(ShaderAssetHeader_t, unk_10), shaderInfoChunk, 0); + pak->AddPointer(hdrChunk, offsetof(ShaderAssetHeader_t, shaderInputFlags), shaderInfoChunk, reservedDataSize); uint64_t* const inputFlags = reinterpret_cast(shaderInfoChunk.data + reservedDataSize); size_t i = 0; @@ -152,20 +145,14 @@ static void Shader_InternalAddShader(CPakFileBuilder* const pak, const char* con PakPageLump_s nameChunk = pak->CreatePageLump(stemLen + 1, SF_CPU | SF_DEV, 1); memcpy(nameChunk.data, pathStem, stemLen + 1); - hdr->name = nameChunk.GetPointer(); - pak->AddPointer(hdrChunk.GetPointer(offsetof(ShaderAssetHeader_t, name))); + pak->AddPointer(hdrChunk, offsetof(ShaderAssetHeader_t, name), nameChunk, 0); } } ParsedDXShaderData_t* const shaderData = new ParsedDXShaderData_t; PakPageLump_s dataChunk; - Shader_CreateFromMSW(pak, dataChunk, shaderData, shader, hdr); - - // Register the pointers we have just written. - pak->AddPointer(hdrChunk.GetPointer(offsetof(ShaderAssetHeader_t, unk_10))); - pak->AddPointer(hdrChunk.GetPointer(offsetof(ShaderAssetHeader_t, shaderInputFlags))); - + Shader_CreateFromMSW(pak, dataChunk, shaderData, shader, hdrChunk, hdr); // ======================================= PakAsset_t asset; diff --git a/src/assets/shaderset.cpp b/src/assets/shaderset.cpp index bc9be21..c2d2f8a 100644 --- a/src/assets/shaderset.cpp +++ b/src/assets/shaderset.cpp @@ -77,8 +77,7 @@ void ShaderSet_InternalCreateSet(CPakFileBuilder* const pak, const char* const a PakPageLump_s nameChunk = pak->CreatePageLump(stemLen + 1, SF_CPU | SF_DEV, 1); memcpy(nameChunk.data, pathStem, stemLen + 1); - hdr->name = nameChunk.GetPointer(); - pak->AddPointer(hdrChunk.GetPointer(offsetof(ShaderSetAssetHeader_t, name))); + pak->AddPointer(hdrChunk, offsetof(ShaderSetAssetHeader_t, name), nameChunk, 0); } } diff --git a/src/assets/texture.cpp b/src/assets/texture.cpp index 40a0d44..4c4aa5d 100644 --- a/src/assets/texture.cpp +++ b/src/assets/texture.cpp @@ -175,8 +175,7 @@ static void Texture_InternalAddTexture(CPakFileBuilder* const pak, const PakGuid PakPageLump_s nameChunk = pak->CreatePageLump(stemLen + 1, SF_CPU | SF_DEV, 1); memcpy(nameChunk.data, pathStem, stemLen + 1); - hdr->pName = nameChunk.GetPointer(); - pak->AddPointer(hdrChunk.GetPointer(offsetof(TextureAssetHeader_t, pName))); + pak->AddPointer(hdrChunk, offsetof(TextureAssetHeader_t, pName), nameChunk, 0); } } diff --git a/src/assets/uiatlas.cpp b/src/assets/uiatlas.cpp index 9440221..aea6499 100644 --- a/src/assets/uiatlas.cpp +++ b/src/assets/uiatlas.cpp @@ -75,8 +75,7 @@ void Assets::AddUIImageAsset_v10(CPakFileBuilder* const pak, const PakGuid_t ass rmem ofBuf(offsetLump.data); // set image offset page index and offset - pHdr->pImageOffsets = offsetLump.GetPointer(); - pak->AddPointer(hdrLump.GetPointer(offsetof(UIImageAtlasHeader_t, pImageOffsets))); + pak->AddPointer(hdrLump, offsetof(UIImageAtlasHeader_t, pImageOffsets), offsetLump, 0); //////////////////// // IMAGE OFFSETS @@ -111,8 +110,7 @@ void Assets::AddUIImageAsset_v10(CPakFileBuilder* const pak, const PakGuid_t ass /////////////////////// // IMAGE DIMENSIONS // set image dimensions page index and offset - pHdr->pImageDimensions = infoLump.GetPointer(); - pak->AddPointer(hdrLump.GetPointer(offsetof(UIImageAtlasHeader_t, pImageDimensions))); + pak->AddPointer(hdrLump, offsetof(UIImageAtlasHeader_t, pImageDimensions), infoLump, 0); for (const rapidjson::Value& it : imageArray) { @@ -124,8 +122,7 @@ void Assets::AddUIImageAsset_v10(CPakFileBuilder* const pak, const PakGuid_t ass } // set image hashes page index and offset - pHdr->pImageHashes = infoLump.GetPointer(imageDimensionsDataSize); - pak->AddPointer(hdrLump.GetPointer(offsetof(UIImageAtlasHeader_t, pImageHashes))); + pak->AddPointer(hdrLump, offsetof(UIImageAtlasHeader_t, pImageHashes), infoLump, imageDimensionsDataSize); // TODO: is this used? //uint32_t nextStringTableOffset = 0; diff --git a/src/logic/pakfile.cpp b/src/logic/pakfile.cpp index 6a824bf..85ffe35 100644 --- a/src/logic/pakfile.cpp +++ b/src/logic/pakfile.cpp @@ -99,18 +99,21 @@ void CPakFileBuilder::AddAsset(const rapidjson::Value& file) } //----------------------------------------------------------------------------- -// purpose: adds page pointer to descriptor +// purpose: adds page pointer to the pak file //----------------------------------------------------------------------------- -void CPakFileBuilder::AddPointer(int pageIdx, int pageOffset) +void CPakFileBuilder::AddPointer(PakPageLump_s& pointerLump, const size_t pointerOffset, + const PakPageLump_s& dataLump, const size_t dataOffset) { - PagePtr_t& refHdr = m_pagePointers.emplace_back(); - refHdr.index = pageIdx; - refHdr.offset = pageOffset; + m_pagePointers.push_back(pointerLump.GetPointer(pointerOffset)); + + // Set the pointer field in the struct to the page index and page offset. + char* const pointerField = &pointerLump.data[pointerOffset]; + *reinterpret_cast(pointerField) = dataLump.GetPointer(dataOffset); } -void CPakFileBuilder::AddPointer(PagePtr_t ptr) +void CPakFileBuilder::AddPointer(PakPageLump_s& pointerLump, const size_t pointerOffset) { - m_pagePointers.push_back(ptr); + m_pagePointers.push_back(pointerLump.GetPointer(pointerOffset)); } //----------------------------------------------------------------------------- diff --git a/src/logic/pakfile.h b/src/logic/pakfile.h index f0df7dd..2413e85 100644 --- a/src/logic/pakfile.h +++ b/src/logic/pakfile.h @@ -19,8 +19,9 @@ class CPakFileBuilder bool AddJSONAsset(const char* const targetType, const char* const assetType, const char* const assetPath, const rapidjson::Value& file, AssetTypeFunc_t func_r2 = nullptr, AssetTypeFunc_t func_r5 = nullptr); void AddAsset(const rapidjson::Value& file); - void AddPointer(PagePtr_t ptr); - void AddPointer(int pageIdx, int pageOffset); + + void AddPointer(PakPageLump_s& pointerLump, const size_t pointerOffset, const PakPageLump_s& dataLump, const size_t dataOffset); + void AddPointer(PakPageLump_s& pointerLump, const size_t pointerOffset); void AddStarpakReference(const std::string& path); void AddOptStarpakReference(const std::string& path); diff --git a/src/public/datatable.h b/src/public/datatable.h index ccc21ac..15715ae 100644 --- a/src/public/datatable.h +++ b/src/public/datatable.h @@ -124,46 +124,11 @@ struct datatable_v1_t struct datatable_asset_t { - uint32_t numColumns; - uint32_t numRows; - - // !!!! DO NOT CHANGE THE POSITION OF THESE !!!! - // !!!! IT WILL CAUSE ISSSUES !!!! - // we are lucky and can cheat!! - PagePtr_t pColumns; - PagePtr_t pRows; - - uint32_t rowStride; // Number of bytes per row - - const char* assetPath; // assets path on disk - // previously func vars size_t rowPodValueBufSize; size_t rowStringValueBufSize; datacolumn_t* pDataColums; // pointer to column data from data chunk - - void WriteToBuffer(char* const buf, const int pakVersion) - { - if (pakVersion <= 7) - { - datatable_v0_t* const dtbl = reinterpret_cast(buf); - dtbl->numColumns = numColumns; - dtbl->numRows = numRows; - dtbl->pColumns = pColumns; - dtbl->pRows = pRows; - dtbl->rowStride = rowStride; - } - else - { - datatable_v1_t* const dtbl = reinterpret_cast(buf); - dtbl->numColumns = numColumns; - dtbl->numRows = numRows; - dtbl->pColumns = pColumns; - dtbl->pRows = pRows; - dtbl->rowStride = rowStride; - } - } }; static_assert(sizeof(datacolumn_t) == 16); diff --git a/src/public/material.h b/src/public/material.h index f6d6be7..262b7bc 100644 --- a/src/public/material.h +++ b/src/public/material.h @@ -538,10 +538,6 @@ struct MaterialAsset_t PakGuid_t guid; // guid of this material asset - PagePtr_t materialName; // pointer to partial asset path - PagePtr_t surfaceProp; // pointer to surfaceprop (as defined in surfaceproperties.rson) - PagePtr_t surfaceProp2; // pointer to surfaceprop2 - PakGuid_t passMaterials[RENDER_PASS_MAT_COUNT]; PakGuid_t shaderSet = 0; // guid of the shaderset asset that this material uses @@ -549,9 +545,6 @@ struct MaterialAsset_t uint16_t numAnimationFrames; PakGuid_t textureAnimation; - PagePtr_t textureHandles; // ptr to array of texture guids - PagePtr_t streamingTextureHandles; // ptr to array of streamable texture guids (empty at build time) - short width; short height; short depth; @@ -589,10 +582,6 @@ struct MaterialAsset_t matl->guid = this->guid; - matl->materialName = this->materialName; - matl->surfaceProp = this->surfaceProp; - matl->surfaceProp2 = this->surfaceProp2; - matl->passMaterials[DEPTH_SHADOW] = this->passMaterials[DEPTH_SHADOW]; matl->passMaterials[DEPTH_PREPASS] = this->passMaterials[DEPTH_PREPASS]; matl->passMaterials[DEPTH_VSM] = this->passMaterials[DEPTH_VSM]; @@ -603,9 +592,6 @@ struct MaterialAsset_t matl->shaderSet = this->shaderSet; - matl->textureHandles = this->textureHandles; - matl->streamingTextureHandles = this->streamingTextureHandles; - matl->width = this->width; matl->height = this->height; matl->depth = this->depth; @@ -635,10 +621,6 @@ struct MaterialAsset_t matl->guid = this->guid; - matl->materialName = this->materialName; - matl->surfaceProp = this->surfaceProp; - matl->surfaceProp2 = this->surfaceProp2; - matl->passMaterials[DEPTH_SHADOW] = this->passMaterials[DEPTH_SHADOW]; matl->passMaterials[DEPTH_PREPASS] = this->passMaterials[DEPTH_PREPASS]; matl->passMaterials[DEPTH_VSM] = this->passMaterials[DEPTH_VSM]; @@ -647,9 +629,6 @@ struct MaterialAsset_t matl->shaderSet = this->shaderSet; - matl->textureHandles = this->textureHandles; - matl->streamingTextureHandles = this->streamingTextureHandles; - matl->width = this->width; matl->height = this->height; matl->depth = this->depth;