From e2b78b44a419807522e7e14d1bee21bd21e17905 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Wed, 24 Jan 2024 18:32:10 +0100 Subject: [PATCH 01/10] Stuff --- src/Components/Loader.cpp | 2 +- src/Components/Modules/AssetHandler.cpp | 480 +++++++++++++++++++++++- src/Game/Structs.hpp | 6 + src/Steam/Proxy.cpp | 2 + 4 files changed, 488 insertions(+), 2 deletions(-) diff --git a/src/Components/Loader.cpp b/src/Components/Loader.cpp index f50b2689..aa3ce5d6 100644 --- a/src/Components/Loader.cpp +++ b/src/Components/Loader.cpp @@ -173,7 +173,7 @@ namespace Components Register(new Threading()); Register(new Toast()); Register(new UIFeeder()); - Register(new Updater()); + //Register(new Updater()); Register(new VisionFile()); Register(new Voice()); Register(new Vote()); diff --git a/src/Components/Modules/AssetHandler.cpp b/src/Components/Modules/AssetHandler.cpp index b76def14..7ebc7ad4 100644 --- a/src/Components/Modules/AssetHandler.cpp +++ b/src/Components/Modules/AssetHandler.cpp @@ -221,7 +221,7 @@ namespace Components retn } } - +#pragma optimize( "", off ) void AssetHandler::ModifyAsset(Game::XAssetType type, Game::XAssetHeader asset, const std::string& name) { #ifdef DEBUG @@ -236,6 +236,483 @@ namespace Components } #endif + if (name == "body_urban_civ_female_a"s) + //if (name == "mp_body_tf141_lmg"s) + { + class QuatInt16 + { + public: + static uint16_t ToInt16(const float quat) + { + return static_cast(quat * INT16_MAX); + } + + static float ToFloat(const uint16_t quat) + { + return static_cast(quat) / static_cast(INT16_MAX); + } + }; + + struct BoneEnsemble + { + uint16_t name; + Game::DObjAnimMat mat; + Game::XBoneInfo info; + int16_t quat[4]; + float trans[3]; + + BoneEnsemble() {}; + + BoneEnsemble(Game::XModel* model, int index) + { + name = model->boneNames[index]; + mat = model->baseMat[index]; + info = model->boneInfo[index]; + + std::memcpy(quat, &model->quats[(index - 1) * 4], 4 * sizeof(uint16_t)); + std::memcpy(trans, &model->trans[(index - 1) * 3], 3 * sizeof(float)); + } + }; + + const auto equals = [](const BoneEnsemble& a, const BoneEnsemble& b) + { + if (a.name == b.name) + { + if (b.mat.transWeight != a.mat.transWeight) + { + return false; + } + + for (size_t i = 0; i < 4; i++) + { + if (b.mat.quat[i] != a.mat.quat[i]) + { + return false; + } + + if (b.quat[i] != a.quat[i]) + { + return false; + } + } + + for (size_t i = 0; i < 3; i++) + { + if (b.mat.trans[i] != a.mat.trans[i]) + { + return false; + } + + if (b.trans[i] != a.trans[i]) + { + return false; + } + + if (b.info.bounds.halfSize[i] != a.info.bounds.halfSize[i]) + { + return false; + } + + if (b.info.bounds.midPoint[i] != a.info.bounds.midPoint[i]) + { + return false; + } + } + + if (b.info.radiusSquared != a.info.radiusSquared) + { + return false; + } + + return true; + } + + return false; + }; + + const auto model = asset.model; + + static std::vector names{}; + + for (auto i = 0; i < model->numBones; i++) + { + const auto bone = model->boneNames[i]; + const auto name = Game::SL_ConvertToString(bone); + names.push_back(name); + } + + const auto getIndexOfBone = [&](std::string name) + { + for (uint8_t i = 0; i < model->numBones; i++) + { + const auto bone = model->boneNames[i]; + const auto boneName = Game::SL_ConvertToString(bone); + if (name == boneName) + { + return i; + } + } + + return static_cast(UCHAR_MAX); + }; + + const auto getParentIndexOfBone = [&](uint8_t index) + { + const auto parentIndex = index - model->parentList[index - model->numRootBones]; + return parentIndex; + }; + + const auto setParentIndexOfBone = [&](uint8_t boneIndex, uint8_t parentIndex) + { + if (boneIndex == SCHAR_MAX) + { + return; + } + + model->parentList[boneIndex - model->numRootBones] = boneIndex - parentIndex; + }; + + const auto getParentOfBone = [&](int index) + { + const auto parentIndex = getParentIndexOfBone(index); + const auto boneName = Game::SL_ConvertToString(model->boneNames[parentIndex]); + return boneName; + }; + + const auto insertBone = [&](const std::string& boneName, uint8_t parentIndex) + { + uint8_t newBoneCount = model->numBones + 1; + uint8_t newBoneCountMinusRoot = newBoneCount - model->numRootBones; + + int atPosition = model->numBones; + + // Find best position to insert it + for (int index = model->numRootBones; index < model->numBones; index++) + { + int parent = getParentIndexOfBone(index); + if (parent >= parentIndex) + { + atPosition = index; + break; + } + } + + const auto newBoneIndex = atPosition; + const auto newBoneIndexMinusRoot = atPosition - model->numRootBones; + + // Reallocate + const auto newBoneNames = (uint16_t*)Game::Z_Malloc(sizeof(uint16_t) * newBoneCount); + const auto newMats = (Game::DObjAnimMat*)Game::Z_Malloc(sizeof(Game::DObjAnimMat) * newBoneCount); + const auto newBoneInfo = (Game::XBoneInfo*)Game::Z_Malloc(sizeof(Game::XBoneInfo) * newBoneCount); + const auto newQuats = (int16_t*)Game::Z_Malloc(sizeof(uint16_t) * 4 * newBoneCountMinusRoot); + const auto newTrans = (float*)Game::Z_Malloc(sizeof(float) * 3 * newBoneCountMinusRoot); + const auto newParentList = reinterpret_cast(Game::Z_Malloc(sizeof(uint8_t) * newBoneCountMinusRoot)); + + int lengthOfFirstPart = atPosition; + int lengthOfSecondPart = model->numBones - atPosition; + + int lengthOfFirstPartM1 = atPosition - model->numRootBones; + int lengthOfSecondPartM1 = model->numBones - model->numRootBones - (atPosition - model->numRootBones); + + int atPositionM1 = atPosition - model->numRootBones; + + // should be equal to model->numBones + int total = lengthOfFirstPart + lengthOfSecondPart; + assert(total = model->numBones); + + // should be equal to model->numBones - model->numRootBones + int totalM1 = lengthOfFirstPartM1 + lengthOfSecondPartM1; + assert(totalM1 == model->numBones - model->numRootBones); + + // Copy before + if (lengthOfFirstPart > 0) + { + std::memcpy(newBoneNames, model->boneNames, sizeof(uint16_t) * lengthOfFirstPart); + std::memcpy(newMats, model->baseMat, sizeof(Game::DObjAnimMat) * lengthOfFirstPart); + std::memcpy(newBoneInfo, model->boneInfo, sizeof(Game::XBoneInfo) * lengthOfFirstPart); + std::memcpy(newQuats, model->quats, sizeof(uint16_t) * 4 * lengthOfFirstPartM1); + std::memcpy(newTrans, model->trans, sizeof(float) * 3 * lengthOfFirstPartM1); + } + + // Insert new bone + { + unsigned int name = Game::SL_GetString(boneName.data(), 0); + Game::XBoneInfo boneInfo{}; + + Game::DObjAnimMat mat{}; + + mat = model->baseMat[parentIndex]; + boneInfo = model->boneInfo[parentIndex]; + + uint16_t quat[4]{}; + std::memcpy(quat, &model->quats[(parentIndex - model->numRootBones) * sizeof(uint16_t)], ARRAYSIZE(quat) * sizeof(uint16_t)); + + float trans[3]{}; + std::memcpy(trans, &model->trans[(parentIndex - model->numRootBones) * sizeof(float)], ARRAYSIZE(trans) * sizeof(float)); + + //mat.quat[3] = 1.0f; + + //mat.trans[0] = -3.4; + //mat.trans[1] = -6; + //mat.trans[2] = 37; + + //mat.transWeight = 2.0f; + + uint8_t part = 5; // Unused ? + + newMats[newBoneIndex] = mat; + newBoneInfo[newBoneIndex] = boneInfo; + newBoneNames[newBoneIndex] = name; + + std::memcpy(&newQuats[newBoneIndexMinusRoot * 4], quat, ARRAYSIZE(quat) * sizeof(uint16_t)); + std::memcpy(&newTrans[newBoneIndexMinusRoot * 3], trans, ARRAYSIZE(trans) * sizeof(float)); + } + + // Copy after + if (lengthOfSecondPart > 0) + { + std::memcpy(&newBoneNames[atPosition + 1], &model->boneNames[atPosition], sizeof(uint16_t) * lengthOfSecondPart); + std::memcpy(&newMats[atPosition + 1], &model->baseMat[atPosition], sizeof(Game::DObjAnimMat) * lengthOfSecondPart); + std::memcpy(&newBoneInfo[atPosition + 1], &model->boneInfo[atPosition], sizeof(Game::XBoneInfo) * lengthOfSecondPart); + std::memcpy(&newQuats[(atPositionM1 + 1) * 4], &model->quats[atPositionM1 * 4], sizeof(uint16_t) * 4 * lengthOfSecondPartM1); + std::memcpy(&newTrans[(atPositionM1 + 1) * 3], &model->trans[atPositionM1 * 3], sizeof(float) * 3 * lengthOfSecondPartM1); + } + + // Assign reallocated + model->baseMat = newMats; + model->boneInfo = newBoneInfo; + model->boneNames = newBoneNames; + model->quats = newQuats; + model->trans = newTrans; + model->parentList = newParentList; + + model->numBones++; + + // Update vertex weights + for (int i = 0; i < model->numLods; i++) + { + const auto lod = &model->lodInfo[i]; + size_t weightOffset = 0u; + + for (int surfIndex = 0; surfIndex < lod->modelSurfs->numsurfs; surfIndex++) + { + auto vertsBlendOffset = 0u; + + const auto surface = &lod->modelSurfs->surfs[surfIndex]; + + { + const auto fixVertexBlendIndex = [&](unsigned int offset) { + int index = static_cast(surface->vertInfo.vertsBlend[offset] / sizeof(Game::DObjSkelMat)); + if (index >= atPosition) + { + if (index < 0 || index >= model->numBones - 1) + { + assert(false); + } + + index++; + + surface->vertInfo.vertsBlend[offset] = index * sizeof(Game::DObjSkelMat); + } + }; + + // Fix bone offsets + if (surface->vertList) + { + for (auto vertListIndex = 0u; vertListIndex < surface->vertListCount; vertListIndex++) + { + const auto vertList = &surface->vertList[vertListIndex]; + auto index = vertList->boneOffset / sizeof(Game::DObjSkelMat); + if (index < 0 || index >= model->numBones - 1) + { + assert(false); + } + + if (index >= atPosition) + { + index++; + vertList->boneOffset = index * sizeof(Game::DObjSkelMat); + } + } + } + + // 1 bone weight + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[0]; vertIndex++) + { + fixVertexBlendIndex(vertsBlendOffset + 0); + + vertsBlendOffset += 1; + } + + // 2 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[1]; vertIndex++) + { + fixVertexBlendIndex(vertsBlendOffset + 0); + fixVertexBlendIndex(vertsBlendOffset + 1); + + vertsBlendOffset += 3; + } + + // 3 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[2]; vertIndex++) + { + fixVertexBlendIndex(vertsBlendOffset + 0); + fixVertexBlendIndex(vertsBlendOffset + 1); + fixVertexBlendIndex(vertsBlendOffset + 3); + + + vertsBlendOffset += 5; + } + + // 4 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[3]; vertIndex++) + { + fixVertexBlendIndex(vertsBlendOffset + 0); + fixVertexBlendIndex(vertsBlendOffset + 1); + fixVertexBlendIndex(vertsBlendOffset + 3); + fixVertexBlendIndex(vertsBlendOffset + 5); + + vertsBlendOffset += 7; + } + } + } + } + + // TODO free memory of original lists + printf(""); + + return atPosition; // Bone index of added bone + }; + + auto indexOfSpine = getIndexOfBone("j_spinelower"); + if (indexOfSpine < UCHAR_MAX) + { + const auto nameOfParent = getParentOfBone(indexOfSpine); + const auto stabilizer = getIndexOfBone("torso_stabilizer"); + const auto otherIndex = stabilizer - model->numRootBones; + + { + const auto root = getIndexOfBone("j_mainroot"); + if (root < UCHAR_MAX) { + + // + std::map parentsBefore{}; + std::map bonesBefore{}; + for (int i = model->numRootBones; i < model->numBones; i++) + { + parentsBefore[Game::SL_ConvertToString(model->boneNames[i])] = getParentOfBone(i); + bonesBefore[Game::SL_ConvertToString(model->boneNames[i])] = BoneEnsemble(model, i); + } + // + +#if 0 + // Add pelvis + auto indexOfPelvis = getIndexOfBone("pelvis"); + if (indexOfPelvis == UCHAR_MAX) + { + indexOfPelvis = insertBone("pelvis", root); + + parentsBefore["pelvis"] = "j_mainroot"; + setParentIndexOfBone(indexOfPelvis, getIndexOfBone("j_mainroot")); + + assert(root == getIndexOfBone("j_mainroot")); + + parentsBefore["j_hip_le"] = "pelvis"; + setParentIndexOfBone(getIndexOfBone("j_hip_le"), getIndexOfBone("pelvis")); + + parentsBefore["j_hip_ri"] = "pelvis"; + setParentIndexOfBone(getIndexOfBone("j_hip_ri"), getIndexOfBone("pelvis")); + + parentsBefore["tag_stowed_hip_rear"] = "pelvis"; + setParentIndexOfBone(getIndexOfBone("tag_stowed_hip_rear"), getIndexOfBone("pelvis")); + + } + + uint8_t torsoStabilizer = insertBone("torso_stabilizer", indexOfPelvis); + assert(indexOfPelvis == getIndexOfBone("pelvis")); + +#else + const auto parentIndex = 30; + uint8_t torsoStabilizer = insertBone("torso_stabilizer", parentIndex); + //setParentIndexOfBone(torsoStabilizer, parentIndex); +#endif + +#if DEBUG + const auto newRoot = getIndexOfBone("j_mainroot"); + assert(root == newRoot); +#endif + //parentsBefore["j_spinelower"] = "torso_stabilizer"; + //setParentIndexOfBone(getIndexOfBone("j_spinelower"), torsoStabilizer); + + //parentsBefore["torso_stabilizer"] = "pelvis"; + //setParentIndexOfBone(torsoStabilizer, indexOfPelvis); + + // + std::map parentsAfter{}; + std::map bonesAfter{}; + for (int i = model->numRootBones; i < model->numBones; i++) + { + parentsAfter[Game::SL_ConvertToString(model->boneNames[i])] = getParentOfBone(i); + bonesAfter[Game::SL_ConvertToString(model->boneNames[i])] = BoneEnsemble(model, i); + } + // + + for (const auto& kv : parentsBefore) + { + // Fix parents + const auto key = kv.first; + const auto beforeVal = kv.second; + const auto afterVal = parentsAfter[kv.first]; + if (beforeVal != afterVal) + { + const auto parentIndex = getIndexOfBone(beforeVal); + const auto index = getIndexOfBone(key); + setParentIndexOfBone(index, parentIndex); + parentsAfter[Game::SL_ConvertToString(model->boneNames[index])] = getParentOfBone(index); + } + } + + + // check + for (const auto& kv : parentsBefore) + { + const auto key = kv.first; + const auto beforeVal = kv.second; + + const auto afterVal = parentsAfter[key]; + + if (beforeVal != afterVal) + { + printf(""); + } + } + // + + // + for (const auto& kv : bonesBefore) + { + const auto key = kv.first; + const auto beforeVal = kv.second; + const auto afterVal = bonesAfter[kv.first]; + if (equals(beforeVal, afterVal)) + { + // good + } + else + { + printf(""); + } + } + // + printf(""); + } + } + printf(""); + } + + printf(""); + } + if (type == Game::ASSET_TYPE_MATERIAL && (name == "gfx_distortion_knife_trail" || name == "gfx_distortion_heat_far" || name == "gfx_distortion_ring_light" || name == "gfx_distortion_heat") && asset.material->info.sortKey >= 43) { if (Zones::Version() >= VERSION_ALPHA2) @@ -291,6 +768,7 @@ namespace Components asset.gfxWorld->sortKeyDistortion = 43; } } +#pragma optimize( "", on ) bool AssetHandler::IsAssetEligible(Game::XAssetType type, Game::XAssetHeader *asset) { diff --git a/src/Game/Structs.hpp b/src/Game/Structs.hpp index 21842c63..8e2ae66f 100644 --- a/src/Game/Structs.hpp +++ b/src/Game/Structs.hpp @@ -1133,6 +1133,12 @@ namespace Game unsigned __int16 triCount; XSurfaceCollisionTree* collisionTree; }; + + struct DObjSkelMat + { + float axis[3][4]; + float origin[4]; + }; struct XSurface { diff --git a/src/Steam/Proxy.cpp b/src/Steam/Proxy.cpp index c4710b4a..248723ff 100644 --- a/src/Steam/Proxy.cpp +++ b/src/Steam/Proxy.cpp @@ -276,6 +276,8 @@ namespace Steam void Proxy::RunFrame() { + return; + std::lock_guard _(Proxy::CallMutex); if (Proxy::SteamUtils) From 1bd6f0c4adc45976bbd6ebe4fb58a14bb3045f27 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Thu, 25 Jan 2024 01:52:56 +0100 Subject: [PATCH 02/10] uuuh --- src/Components/Modules/AssetHandler.cpp | 638 ++++++++++++++---------- src/Components/Modules/QuickPatch.cpp | 19 + src/Components/Modules/Renderer.cpp | 27 + src/Game/Structs.hpp | 16 +- 4 files changed, 449 insertions(+), 251 deletions(-) diff --git a/src/Components/Modules/AssetHandler.cpp b/src/Components/Modules/AssetHandler.cpp index 7ebc7ad4..37bf8ea5 100644 --- a/src/Components/Modules/AssetHandler.cpp +++ b/src/Components/Modules/AssetHandler.cpp @@ -118,7 +118,7 @@ namespace Components } return header; - } + } int AssetHandler::HasThreadBypass() { @@ -160,7 +160,7 @@ namespace Components // Check if custom handler should be bypassed call AssetHandler::HasThreadBypass - mov [esp + 20h], eax + mov[esp + 20h], eax popad pop eax @@ -180,45 +180,45 @@ namespace Components add esp, 8h - mov [esp + 20h], eax + mov[esp + 20h], eax popad pop eax test eax, eax jnz finishFound - checkTempAssets: + checkTempAssets : mov al, AssetHandler::ShouldSearchTempAssets // check to see if enabled - test eax, eax - jz finishOriginal + test eax, eax + jz finishOriginal - mov ecx, [esp + 18h] // Asset type - mov ebx, [esp + 1Ch] // Filename + mov ecx, [esp + 18h] // Asset type + mov ebx, [esp + 1Ch] // Filename - push ebx - push ecx + push ebx + push ecx - call AssetHandler::FindTemporaryAsset + call AssetHandler::FindTemporaryAsset - add esp, 8h + add esp, 8h - test eax, eax - jnz finishFound + test eax, eax + jnz finishFound - finishOriginal: + finishOriginal : // Asset not found using custom handlers or in temp assets or bypasses were enabled // redirect to DB_FindXAssetHeader - mov ebx, ds:6D7190h // InterlockedDecrement - mov eax, 40793Bh - jmp eax + mov ebx, ds : 6D7190h // InterlockedDecrement + mov eax, 40793Bh + jmp eax - finishFound: + finishFound : pop edi - pop esi - pop ebp - pop ebx - pop ecx - retn + pop esi + pop ebp + pop ebx + pop ecx + retn } } #pragma optimize( "", off ) @@ -236,9 +236,38 @@ namespace Components } #endif - if (name == "body_urban_civ_female_a"s) - //if (name == "mp_body_tf141_lmg"s) + if (type == Game::XAssetType::ASSET_TYPE_XMODEL) + //if (name == "body_urban_civ_female_a"s || name == "mp_body_opforce_arab_assault_a"s) { + const auto model = asset.model; + if (!model->quats || !model->trans) + { + return; + } + + // Update vertex weights + //for (int i = 0; i < model->numLods; i++) + //{ + // const auto lod = &model->lodInfo[i]; + + // for (int surfIndex = 0; surfIndex < lod->modelSurfs->numsurfs; surfIndex++) + // { + // auto vertsBlendOffset = 0u; + + // const auto surface = &lod->modelSurfs->surfs[surfIndex]; + // surface->partBits[0] = 0x00000000; + // surface->partBits[1] = 0x00000000; + // surface->partBits[2] = 0x00000000; + // surface->partBits[3] = 0x00000000; + // surface->partBits[4] = 0x00000000; + // surface->partBits[5] = 0x00000000; + // } + //} + + //return; + + + class QuatInt16 { public: @@ -253,94 +282,31 @@ namespace Components } }; - struct BoneEnsemble - { - uint16_t name; - Game::DObjAnimMat mat; - Game::XBoneInfo info; - int16_t quat[4]; - float trans[3]; - - BoneEnsemble() {}; - - BoneEnsemble(Game::XModel* model, int index) - { - name = model->boneNames[index]; - mat = model->baseMat[index]; - info = model->boneInfo[index]; - - std::memcpy(quat, &model->quats[(index - 1) * 4], 4 * sizeof(uint16_t)); - std::memcpy(trans, &model->trans[(index - 1) * 3], 3 * sizeof(float)); - } - }; - - const auto equals = [](const BoneEnsemble& a, const BoneEnsemble& b) - { - if (a.name == b.name) - { - if (b.mat.transWeight != a.mat.transWeight) - { - return false; - } - - for (size_t i = 0; i < 4; i++) - { - if (b.mat.quat[i] != a.mat.quat[i]) - { - return false; - } - - if (b.quat[i] != a.quat[i]) - { - return false; - } - } - - for (size_t i = 0; i < 3; i++) - { - if (b.mat.trans[i] != a.mat.trans[i]) - { - return false; - } - - if (b.trans[i] != a.trans[i]) - { - return false; - } - - if (b.info.bounds.halfSize[i] != a.info.bounds.halfSize[i]) - { - return false; - } - - if (b.info.bounds.midPoint[i] != a.info.bounds.midPoint[i]) - { - return false; - } - } - - if (b.info.radiusSquared != a.info.radiusSquared) - { - return false; - } - - return true; - } - - return false; - }; - - const auto model = asset.model; - + // static std::vector names{}; + names.clear(); for (auto i = 0; i < model->numBones; i++) { const auto bone = model->boneNames[i]; const auto name = Game::SL_ConvertToString(bone); - names.push_back(name); + names.push_back( + std::format("{} => q({} {} {} {}) t({} {} {})", + name, + model->baseMat[i-1].quat[0], + model->baseMat[i-1].quat[1], + model->baseMat[i-1].quat[2], + model->baseMat[i-1].quat[3], + model->baseMat[i-1].trans[0], + model->baseMat[i-1].trans[1], + model->baseMat[i-1].trans[2] + ) + ); } + printf(""); + // + const auto getIndexOfBone = [&](std::string name) { for (uint8_t i = 0; i < model->numBones; i++) @@ -379,23 +345,73 @@ namespace Components return boneName; }; - const auto insertBone = [&](const std::string& boneName, uint8_t parentIndex) + const auto insertBoneInPartbits = [&](int8_t atPosition, int* partBits, bool hasAnyWeight = false) { + constexpr auto LENGTH = 6; + // Bit masks and bit shifting are a pain honestly + std::vector flags{}; + + int check[LENGTH]{}; + std::memcpy(check, partBits, LENGTH * sizeof(int)); + + for (auto i = 0; i < LENGTH; i++) + { + for (signed int bitPosition = 32 - 1; bitPosition >= 0; bitPosition--) + { + flags.emplace_back((partBits[i] >> bitPosition) & 0x1); + } + } + + // copy parent flag + const auto partBitIndex = atPosition / 32; + const auto bitPosition = atPosition % 32; // The vector is already reversed so this should be correct + + flags.insert(flags.begin() + (partBitIndex * 32 + bitPosition), hasAnyWeight); + + flags.pop_back(); + + // clear it + for (auto i = 0; i < LENGTH; i++) + { + partBits[i] = 0; + } + + // Write it + for (auto i = 0; i < LENGTH; i++) + { + partBits[i] = 0; + for (int bitPosition = 0; bitPosition < 32; bitPosition++) + { + const auto value = flags[i * 32 + 31 - bitPosition]; + partBits[i] |= (value << bitPosition); + } + } + + flags.clear(); + }; + + + const auto insertBone = [&](const std::string& boneName, const std::string& parentName) + { + assert(getIndexOfBone(boneName) == UCHAR_MAX); + + // Start with backing up parent links that we will have to restore + // We'll restore them at the end + std::map parentsToRestore{}; + for (int i = model->numRootBones; i < model->numBones; i++) + { + parentsToRestore[Game::SL_ConvertToString(model->boneNames[i])] = getParentOfBone(i); + } + + uint8_t newBoneCount = model->numBones + 1; uint8_t newBoneCountMinusRoot = newBoneCount - model->numRootBones; - int atPosition = model->numBones; + const auto parentIndex = getIndexOfBone(parentName); - // Find best position to insert it - for (int index = model->numRootBones; index < model->numBones; index++) - { - int parent = getParentIndexOfBone(index); - if (parent >= parentIndex) - { - atPosition = index; - break; - } - } + assert(parentIndex != UCHAR_MAX); + + int atPosition = parentIndex + 1; const auto newBoneIndex = atPosition; const auto newBoneIndexMinusRoot = atPosition - model->numRootBones; @@ -441,24 +457,18 @@ namespace Components Game::DObjAnimMat mat{}; + // It's ABSOLUTE! mat = model->baseMat[parentIndex]; + boneInfo = model->boneInfo[parentIndex]; + // It's RELATIVE ! uint16_t quat[4]{}; - std::memcpy(quat, &model->quats[(parentIndex - model->numRootBones) * sizeof(uint16_t)], ARRAYSIZE(quat) * sizeof(uint16_t)); + quat[3] = 32767; // 0 0 0 32767 float trans[3]{}; - std::memcpy(trans, &model->trans[(parentIndex - model->numRootBones) * sizeof(float)], ARRAYSIZE(trans) * sizeof(float)); - //mat.quat[3] = 1.0f; - - //mat.trans[0] = -3.4; - //mat.trans[1] = -6; - //mat.trans[2] = 37; - - //mat.transWeight = 2.0f; - - uint8_t part = 5; // Unused ? + mat.transWeight = 1.9999f; // Should be 1.9999 like everybody? newMats[newBoneIndex] = mat; newBoneInfo[newBoneIndex] = boneInfo; @@ -492,7 +502,8 @@ namespace Components for (int i = 0; i < model->numLods; i++) { const auto lod = &model->lodInfo[i]; - size_t weightOffset = 0u; + insertBoneInPartbits(atPosition, lod->partBits, false); + insertBoneInPartbits(atPosition, lod->modelSurfs->partBits, false); for (int surfIndex = 0; surfIndex < lod->modelSurfs->numsurfs; surfIndex++) { @@ -500,6 +511,15 @@ namespace Components const auto surface = &lod->modelSurfs->surfs[surfIndex]; + //surface->partBits[0] = 0b00000000000000000000000000000000; + //surface->partBits[1] = 0b01010101010101010101010101010101; + //surface->partBits[2] = 0b00110011001100110011001100110011; + //surface->partBits[3] = 0b00011100011100011100011100011100; + //surface->partBits[4] = 0b00001111000011110000111100001111; + //surface->partBits[5] = 0b00000111110000011111000001111100; + + insertBoneInPartbits(atPosition, surface->partBits, false); + { const auto fixVertexBlendIndex = [&](unsigned int offset) { int index = static_cast(surface->vertInfo.vertsBlend[offset] / sizeof(Game::DObjSkelMat)); @@ -507,14 +527,14 @@ namespace Components { if (index < 0 || index >= model->numBones - 1) { - assert(false); + //assert(false); } index++; surface->vertInfo.vertsBlend[offset] = index * sizeof(Game::DObjSkelMat); } - }; + }; // Fix bone offsets if (surface->vertList) @@ -525,7 +545,7 @@ namespace Components auto index = vertList->boneOffset / sizeof(Game::DObjSkelMat); if (index < 0 || index >= model->numBones - 1) { - assert(false); + //assert(false); } if (index >= atPosition) @@ -560,7 +580,6 @@ namespace Components fixVertexBlendIndex(vertsBlendOffset + 1); fixVertexBlendIndex(vertsBlendOffset + 3); - vertsBlendOffset += 5; } @@ -581,136 +600,255 @@ namespace Components // TODO free memory of original lists printf(""); + setParentIndexOfBone(atPosition, parentIndex); + + // Restore parents + for (const auto& kv : parentsToRestore) + { + // Fix parents + const auto key = kv.first; + const auto beforeVal = kv.second; + + const auto parentIndex = getIndexOfBone(beforeVal); + const auto index = getIndexOfBone(key); + setParentIndexOfBone(index, parentIndex); + } + + + // check + for (const auto& kv : parentsToRestore) + { + const auto key = kv.first; + const auto beforeVal = kv.second; + + const auto index = getIndexOfBone(key); + const auto afterVal = getParentOfBone(index); + + if (beforeVal != afterVal) + { + printf(""); + } + } + // + return atPosition; // Bone index of added bone }; + + const auto transferWeights = [&](const uint8_t origin, const uint8_t destination) + { + return; + + const auto originalWeights = model->baseMat[origin].transWeight; + model->baseMat[origin].transWeight = model->baseMat[destination].transWeight; + model->baseMat[destination].transWeight = originalWeights; + + for (int i = 0; i < model->numLods; i++) + { + const auto lod = &model->lodInfo[i]; + + for (int surfIndex = 0; surfIndex < lod->modelSurfs->numsurfs; surfIndex++) + { + auto vertsBlendOffset = 0u; + + const auto surface = &lod->modelSurfs->surfs[surfIndex]; + { + const auto transferVertexBlendIndex = [&](unsigned int offset) { + int index = static_cast(surface->vertInfo.vertsBlend[offset] / sizeof(Game::DObjSkelMat)); + if (index == origin) + { + if (index < 0 || index >= model->numBones - 1) + { + assert(false); + } + + index = destination; + + surface->vertInfo.vertsBlend[offset] = index * sizeof(Game::DObjSkelMat); + } + }; + + // Fix bone offsets + if (surface->vertList) + { + for (auto vertListIndex = 0u; vertListIndex < surface->vertListCount; vertListIndex++) + { + const auto vertList = &surface->vertList[vertListIndex]; + auto index = vertList->boneOffset / sizeof(Game::DObjSkelMat); + if (index < 0 || index >= model->numBones - 1) + { + assert(false); + } + + if (index == origin) + { + index = destination; + vertList->boneOffset = index * sizeof(Game::DObjSkelMat); + } + } + } + + // 1 bone weight + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[0]; vertIndex++) + { + transferVertexBlendIndex(vertsBlendOffset + 0); + + vertsBlendOffset += 1; + } + + // 2 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[1]; vertIndex++) + { + transferVertexBlendIndex(vertsBlendOffset + 0); + transferVertexBlendIndex(vertsBlendOffset + 1); + + vertsBlendOffset += 3; + } + + // 3 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[2]; vertIndex++) + { + transferVertexBlendIndex(vertsBlendOffset + 0); + transferVertexBlendIndex(vertsBlendOffset + 1); + transferVertexBlendIndex(vertsBlendOffset + 3); + + + vertsBlendOffset += 5; + } + + // 4 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[3]; vertIndex++) + { + transferVertexBlendIndex(vertsBlendOffset + 0); + transferVertexBlendIndex(vertsBlendOffset + 1); + transferVertexBlendIndex(vertsBlendOffset + 3); + transferVertexBlendIndex(vertsBlendOffset + 5); + + vertsBlendOffset += 7; + } + } + } + } + }; + auto indexOfSpine = getIndexOfBone("j_spinelower"); if (indexOfSpine < UCHAR_MAX) { const auto nameOfParent = getParentOfBone(indexOfSpine); - const auto stabilizer = getIndexOfBone("torso_stabilizer"); - const auto otherIndex = stabilizer - model->numRootBones; + if (getIndexOfBone("torso_stabilizer") == UCHAR_MAX) { + // No stabilizer - let's do surgery + // We're trying to get there: + // tag_origin + // j_main_root + // pelvis + // j_hip_le + // j_hip_ri + // tag_stowed_hip_rear + // torso_stabilizer + // j_spinelower + // back_low + // j_spineupper + // back_mid + // j_spine4 + + const auto root = getIndexOfBone("j_mainroot"); if (root < UCHAR_MAX) { - // - std::map parentsBefore{}; - std::map bonesBefore{}; - for (int i = model->numRootBones; i < model->numBones; i++) - { - parentsBefore[Game::SL_ConvertToString(model->boneNames[i])] = getParentOfBone(i); - bonesBefore[Game::SL_ConvertToString(model->boneNames[i])] = BoneEnsemble(model, i); - } - // - -#if 0 +#if true // Add pelvis - auto indexOfPelvis = getIndexOfBone("pelvis"); - if (indexOfPelvis == UCHAR_MAX) - { - indexOfPelvis = insertBone("pelvis", root); + const uint8_t indexOfPelvis = insertBone("pelvis", "j_mainroot"); + transferWeights(root, indexOfPelvis); - parentsBefore["pelvis"] = "j_mainroot"; - setParentIndexOfBone(indexOfPelvis, getIndexOfBone("j_mainroot")); + setParentIndexOfBone(getIndexOfBone("j_hip_le"), indexOfPelvis); + setParentIndexOfBone(getIndexOfBone("j_hip_ri"), indexOfPelvis); + setParentIndexOfBone(getIndexOfBone("tag_stowed_hip_rear"), indexOfPelvis); - assert(root == getIndexOfBone("j_mainroot")); + const uint8_t torsoStabilizer = insertBone("torso_stabilizer", "pelvis"); + setParentIndexOfBone(getIndexOfBone("j_spinelower"), torsoStabilizer); - parentsBefore["j_hip_le"] = "pelvis"; - setParentIndexOfBone(getIndexOfBone("j_hip_le"), getIndexOfBone("pelvis")); + const uint8_t backLow = insertBone("back_low", "j_spinelower"); + transferWeights(getIndexOfBone("j_spinelower"), backLow); + setParentIndexOfBone(getIndexOfBone("j_spineupper"), backLow); - parentsBefore["j_hip_ri"] = "pelvis"; - setParentIndexOfBone(getIndexOfBone("j_hip_ri"), getIndexOfBone("pelvis")); + const uint8_t backMid = insertBone("back_mid", "j_spineupper"); + transferWeights(getIndexOfBone("j_spineupper"), backMid); + setParentIndexOfBone(getIndexOfBone("j_spine4"), backMid); - parentsBefore["tag_stowed_hip_rear"] = "pelvis"; - setParentIndexOfBone(getIndexOfBone("tag_stowed_hip_rear"), getIndexOfBone("pelvis")); - } - - uint8_t torsoStabilizer = insertBone("torso_stabilizer", indexOfPelvis); + assert(root == getIndexOfBone("j_mainroot")); assert(indexOfPelvis == getIndexOfBone("pelvis")); + assert(backLow == getIndexOfBone("back_low")); + assert(backMid == getIndexOfBone("back_mid")); + + // Fix up torso stabilizer + model->baseMat[torsoStabilizer].quat[0] = 0.F; + model->baseMat[torsoStabilizer].quat[1] = 0.F; + model->baseMat[torsoStabilizer].quat[2] = 0.F; + model->baseMat[torsoStabilizer].quat[3] = 1.F; + + const auto spineLowerM1 = getIndexOfBone("j_spinelower")-1; + + //model->trans[(torsoStabilizer-model->numRootBones) * 3 + 0] = 2.0f; + //model->trans[(torsoStabilizer-model->numRootBones) * 3 + 1] = -5.0f; + //model->trans[(torsoStabilizer-model->numRootBones) * 3 + 2] = 2.0F; + + model->trans[spineLowerM1 * 3 + 0] = 0.069828756; + model->trans[spineLowerM1 * 3 + 1] = -0.0f; + model->trans[spineLowerM1 * 3 + 2] = 5.2035017F; + + //// Euler -180.000 88.572 -90.000 + model->quats[(torsoStabilizer-model->numRootBones) * 4 + 0] = 16179; // 0.4952 + model->quats[(torsoStabilizer-model->numRootBones) * 4 + 1] = 16586; // 0.5077 + model->quats[(torsoStabilizer-model->numRootBones) * 4 + 2] = 16586; // 0.5077 + model->quats[(torsoStabilizer-model->numRootBones) * 4 + 3] = 16178; // 0.4952 #else - const auto parentIndex = 30; - uint8_t torsoStabilizer = insertBone("torso_stabilizer", parentIndex); - //setParentIndexOfBone(torsoStabilizer, parentIndex); + const uint8_t torsoStabilizer = insertBone("torso_stabilizer", "j_mainroot"); + transferWeights(getIndexOfBone("j_mainroot"), getIndexOfBone("torso_stabilizer")); + setParentIndexOfBone(getIndexOfBone("j_spinelower"), torsoStabilizer); #endif #if DEBUG const auto newRoot = getIndexOfBone("j_mainroot"); assert(root == newRoot); #endif - //parentsBefore["j_spinelower"] = "torso_stabilizer"; - //setParentIndexOfBone(getIndexOfBone("j_spinelower"), torsoStabilizer); - //parentsBefore["torso_stabilizer"] = "pelvis"; - //setParentIndexOfBone(torsoStabilizer, indexOfPelvis); - - // - std::map parentsAfter{}; - std::map bonesAfter{}; - for (int i = model->numRootBones; i < model->numBones; i++) - { - parentsAfter[Game::SL_ConvertToString(model->boneNames[i])] = getParentOfBone(i); - bonesAfter[Game::SL_ConvertToString(model->boneNames[i])] = BoneEnsemble(model, i); - } - // - - for (const auto& kv : parentsBefore) - { - // Fix parents - const auto key = kv.first; - const auto beforeVal = kv.second; - const auto afterVal = parentsAfter[kv.first]; - if (beforeVal != afterVal) - { - const auto parentIndex = getIndexOfBone(beforeVal); - const auto index = getIndexOfBone(key); - setParentIndexOfBone(index, parentIndex); - parentsAfter[Game::SL_ConvertToString(model->boneNames[index])] = getParentOfBone(index); - } - } - - - // check - for (const auto& kv : parentsBefore) - { - const auto key = kv.first; - const auto beforeVal = kv.second; - - const auto afterVal = parentsAfter[key]; - - if (beforeVal != afterVal) - { - printf(""); - } - } - // - - // - for (const auto& kv : bonesBefore) - { - const auto key = kv.first; - const auto beforeVal = kv.second; - const auto afterVal = bonesAfter[kv.first]; - if (equals(beforeVal, afterVal)) - { - // good - } - else - { - printf(""); - } - } - // - printf(""); + printf(""); } } printf(""); } printf(""); + + // + names.clear(); + for (auto i = 1; i < model->numBones; i++) + { + const auto bone = model->boneNames[i]; + const auto name = Game::SL_ConvertToString(bone); + const auto m1 = i-1; + const auto fmt = std::format("{} => q({} {} {} {}) t({} {} {})", + name, + model->quats[m1 * 4 + 0], + model->quats[m1* 4 + 1], + model->quats[m1 * 4 + 2], + model->quats[m1 * 4 + 3], + model->trans[m1 * 3 + 0], + model->trans[m1* 3 +1], + model->trans[m1 * 3 +2] + ); + names.push_back( + fmt + ); + printf(""); + } + + printf(""); + // } if (type == Game::ASSET_TYPE_MATERIAL && (name == "gfx_distortion_knife_trail" || name == "gfx_distortion_heat_far" || name == "gfx_distortion_ring_light" || name == "gfx_distortion_heat") && asset.material->info.sortKey >= 43) @@ -770,7 +908,7 @@ namespace Components } #pragma optimize( "", on ) - bool AssetHandler::IsAssetEligible(Game::XAssetType type, Game::XAssetHeader *asset) + bool AssetHandler::IsAssetEligible(Game::XAssetType type, Game::XAssetHeader* asset) { const char* name = Game::DB_GetXAssetNameHandlers[type](asset); if (!name) return false; @@ -811,12 +949,12 @@ namespace Components push eax pushad - push [esp + 2Ch] - push [esp + 2Ch] + push[esp + 2Ch] + push[esp + 2Ch] call AssetHandler::IsAssetEligible add esp, 8h - mov [esp + 20h], eax + mov[esp + 20h], eax popad pop eax @@ -828,9 +966,9 @@ namespace Components mov ecx, 5BB657h jmp ecx - doNotLoad: + doNotLoad : mov eax, [esp + 8h] - retn + retn } } @@ -843,9 +981,9 @@ namespace Components { AssetHandler::RestrictSignal.connect(callback); - return [callback](){ + return [callback]() { AssetHandler::RestrictSignal.disconnect(callback); - }; + }; } void AssetHandler::ClearRelocations() @@ -1051,25 +1189,25 @@ namespace Components // Log missing empty assets Scheduler::Loop([] - { - if (FastFiles::Ready() && !AssetHandler::EmptyAssets.empty()) { - for (auto& asset : AssetHandler::EmptyAssets) + if (FastFiles::Ready() && !AssetHandler::EmptyAssets.empty()) { - Logger::Warning(Game::CON_CHANNEL_FILES, "Could not load {} \"{}\".\n", Game::DB_GetXAssetTypeName(asset.first), asset.second); - } + for (auto& asset : AssetHandler::EmptyAssets) + { + Logger::Warning(Game::CON_CHANNEL_FILES, "Could not load {} \"{}\".\n", Game::DB_GetXAssetTypeName(asset.first), asset.second); + } - AssetHandler::EmptyAssets.clear(); - } - }, Scheduler::Pipeline::MAIN); + AssetHandler::EmptyAssets.clear(); + } + }, Scheduler::Pipeline::MAIN); AssetHandler::OnLoad([](Game::XAssetType type, Game::XAssetHeader asset, std::string name, bool*) - { - if (Dvar::Var("r_noVoid").get() && type == Game::ASSET_TYPE_XMODEL && name == "void") { - asset.model->numLods = 0; - } - }); + if (Dvar::Var("r_noVoid").get() && type == Game::ASSET_TYPE_XMODEL && name == "void") + { + asset.model->numLods = 0; + } + }); Game::ReallocateAssetPool(Game::ASSET_TYPE_GAMEWORLD_SP, 1); Game::ReallocateAssetPool(Game::ASSET_TYPE_IMAGE, ZoneBuilder::IsEnabled() ? 14336 * 2 : 7168); diff --git a/src/Components/Modules/QuickPatch.cpp b/src/Components/Modules/QuickPatch.cpp index e8a7ded9..8eeb44e7 100644 --- a/src/Components/Modules/QuickPatch.cpp +++ b/src/Components/Modules/QuickPatch.cpp @@ -298,8 +298,27 @@ namespace Components return Game::Dvar_RegisterBool(dvarName, value_, flags, description); } + void DObjCalcAnim(Game::DObj *a1, int *partBits, Game::XAnimCalcAnimInfo *a3) + { + printf(""); + + if (a1->models[0]->name == "body_urban_civ_female_a"s) + { + printf(""); + } + + Utils::Hook::Call(0x49E230)(a1, partBits, a3); + + printf(""); + } + QuickPatch::QuickPatch() { + // Do not call DObjCalcAnim + Utils::Hook(0x6508C4, DObjCalcAnim, HOOK_CALL).install()->quick(); + Utils::Hook(0x06508F6, DObjCalcAnim, HOOK_CALL).install()->quick(); + + // Filtering any mapents that is intended for Spec:Ops gamemode (CODO) and prevent them from spawning Utils::Hook(0x5FBD6E, QuickPatch::IsDynClassname_Stub, HOOK_CALL).install()->quick(); diff --git a/src/Components/Modules/Renderer.cpp b/src/Components/Modules/Renderer.cpp index 191b82f5..8df1b33f 100644 --- a/src/Components/Modules/Renderer.cpp +++ b/src/Components/Modules/Renderer.cpp @@ -713,6 +713,32 @@ namespace Components return result; } + void DebugTest() + { + //auto clientNum = Game::CG_GetClientNum(); + //auto* clientEntity = &Game::g_entities[clientNum]; + + //// Ingame only & player only + //if (!Game::CL_IsCgameInitialized() || clientEntity->client == nullptr) + //{ + // return; + //} + + + //static std::string str = ""; + + //str += std::format("\n{} => {} {} {} {} {} {}", "s.partBits", clientEntity->s.partBits[0], clientEntity->s.partBits[1], clientEntity->s.partBits[2], clientEntity->s.partBits[3], clientEntity->s.partBits[4], clientEntity->s.partBits[5]); + // + + //const auto clientNumber = clientEntity->r.ownerNum.number; + //Game::scene->sceneDObj[clientNumber].obj->hidePartBits; + + //str += std::format("\n{} => {} {} {} {} {} {}", "DOBJ hidePartBits", clientEntity->s.partBits[0], clientEntity->s.partBits[1], clientEntity->s.partBits[2], clientEntity->s.partBits[3], clientEntity->s.partBits[4], clientEntity->s.partBits[5]); + + //Game::R_AddCmdDrawText(); + + } + Renderer::Renderer() { if (Dedicated::IsEnabled()) return; @@ -731,6 +757,7 @@ namespace Components ListSamplers(); DrawPrimaryLights(); DebugDrawClipmap(); + DebugTest(); } }, Scheduler::Pipeline::RENDERER); diff --git a/src/Game/Structs.hpp b/src/Game/Structs.hpp index 8e2ae66f..3fd346d3 100644 --- a/src/Game/Structs.hpp +++ b/src/Game/Structs.hpp @@ -8368,8 +8368,22 @@ namespace Game { DSkelPartBits partBits; int timeStamp; - /*DObjAnimMat*/void* mat; + DObjAnimMat* mat; }; + + struct bitarray + { + int array[6]; + }; + + /* 1923 */ + struct XAnimCalcAnimInfo + { + DObjAnimMat rotTransArray[1152]; + bitarray animPartBits; + bitarray ignorePartBits; + }; + struct DObj { From 1edf6ce398e80376f8173b901e115fadf1b45db5 Mon Sep 17 00:00:00 2001 From: Roxanne Date: Fri, 26 Jan 2024 14:10:13 +0100 Subject: [PATCH 03/10] clean it up! --- .../Modules/AssetInterfaces/IXModel.cpp | 685 +++++++++++++++++- .../Modules/AssetInterfaces/IXModel.hpp | 12 + src/Components/Modules/ZoneBuilder.cpp | 472 ++---------- src/Components/Modules/ZoneBuilder.hpp | 6 +- src/Game/Structs.hpp | 2 +- 5 files changed, 756 insertions(+), 421 deletions(-) diff --git a/src/Components/Modules/AssetInterfaces/IXModel.cpp b/src/Components/Modules/AssetInterfaces/IXModel.cpp index d4444712..ad4f76b0 100644 --- a/src/Components/Modules/AssetInterfaces/IXModel.cpp +++ b/src/Components/Modules/AssetInterfaces/IXModel.cpp @@ -1,13 +1,17 @@ #include + +#include + #include "IXModel.hpp" namespace Assets { + void IXModel::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder) { header->model = builder->getIW4OfApi()->read(Game::XAssetType::ASSET_TYPE_XMODEL, name); - if (header->model) + if (header->model) { // ??? if (header->model->physCollmap) @@ -257,4 +261,683 @@ namespace Assets buffer->popBlock(); } + + + uint8_t IXModel::GetIndexOfBone(const Game::XModel* model, std::string name) + { + for (uint8_t i = 0; i < model->numBones; i++) + { + const auto bone = model->boneNames[i]; + const auto boneName = Game::SL_ConvertToString(bone); + if (name == boneName) + { + return i; + } + } + + return static_cast(UCHAR_MAX); + }; + + uint8_t IXModel::GetParentIndexOfBone(const Game::XModel* model, uint8_t index) + { + const auto parentIndex = index - model->parentList[index - model->numRootBones]; + return static_cast(parentIndex); + }; + + void IXModel::SetParentIndexOfBone(Game::XModel* model, uint8_t boneIndex, uint8_t parentIndex) + { + if (boneIndex == SCHAR_MAX) + { + return; + } + + model->parentList[boneIndex - model->numRootBones] = boneIndex - parentIndex; + }; + + std::string IXModel::GetParentOfBone(Game::XModel* model, uint8_t index) + { + const auto parentIndex = GetParentIndexOfBone(model, index); + const auto boneName = Game::SL_ConvertToString(model->boneNames[parentIndex]); + return boneName; + }; + + uint8_t IXModel::GetHighestAffectingBoneIndex(const Game::XModelLodInfo* lod) + { + uint8_t highestBoneIndex = 0; + constexpr auto LENGTH = 6; + + { + for (auto surfIndex = 0; surfIndex < lod->numsurfs; surfIndex++) + { + const auto surface = &lod->surfs[surfIndex]; + + auto vertsBlendOffset = 0; + + int rebuiltPartBits[6]{}; + std::unordered_set affectingBones{}; + + const auto registerBoneAffectingSurface = [&](unsigned int offset) { + uint8_t index = static_cast(surface->vertInfo.vertsBlend[offset] / sizeof(Game::DObjSkelMat)); + highestBoneIndex = std::max(highestBoneIndex, index); + }; + + + // 1 bone weight + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[0]; vertIndex++) + { + registerBoneAffectingSurface(vertsBlendOffset + 0); + + vertsBlendOffset += 1; + } + + // 2 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[1]; vertIndex++) + { + registerBoneAffectingSurface(vertsBlendOffset + 0); + registerBoneAffectingSurface(vertsBlendOffset + 1); + + vertsBlendOffset += 3; + } + + // 3 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[2]; vertIndex++) + { + registerBoneAffectingSurface(vertsBlendOffset + 0); + registerBoneAffectingSurface(vertsBlendOffset + 1); + registerBoneAffectingSurface(vertsBlendOffset + 3); + + vertsBlendOffset += 5; + } + + // 4 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[3]; vertIndex++) + { + registerBoneAffectingSurface(vertsBlendOffset + 0); + registerBoneAffectingSurface(vertsBlendOffset + 1); + registerBoneAffectingSurface(vertsBlendOffset + 3); + registerBoneAffectingSurface(vertsBlendOffset + 5); + + vertsBlendOffset += 7; + } + + for (auto vertListIndex = 0; vertListIndex < surface->vertListCount; vertListIndex++) + { + highestBoneIndex = std::max(highestBoneIndex, static_cast(surface->vertList[vertListIndex].boneOffset / sizeof(Game::DObjSkelMat))); + } + } + } + + return highestBoneIndex; + }; + + void IXModel::RebuildPartBits(Game::XModel* model) + { + constexpr auto LENGTH = 6; + + for (auto i = 0; i < model->numLods; i++) + { + const auto lod = &model->lodInfo[i]; + int lodPartBits[6]{}; + + for (auto surfIndex = 0; surfIndex < lod->numsurfs; surfIndex++) + { + const auto surface = &lod->surfs[surfIndex]; + + auto vertsBlendOffset = 0; + + int rebuiltPartBits[6]{}; + std::unordered_set affectingBones{}; + + const auto registerBoneAffectingSurface = [&](unsigned int offset) { + uint8_t index = static_cast(surface->vertInfo.vertsBlend[offset] / sizeof(Game::DObjSkelMat)); + + assert(index >= 0); + assert(index < model->numBones); + + affectingBones.emplace(index); + }; + + + // 1 bone weight + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[0]; vertIndex++) + { + registerBoneAffectingSurface(vertsBlendOffset + 0); + + vertsBlendOffset += 1; + } + + // 2 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[1]; vertIndex++) + { + registerBoneAffectingSurface(vertsBlendOffset + 0); + registerBoneAffectingSurface(vertsBlendOffset + 1); + + vertsBlendOffset += 3; + } + + // 3 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[2]; vertIndex++) + { + registerBoneAffectingSurface(vertsBlendOffset + 0); + registerBoneAffectingSurface(vertsBlendOffset + 1); + registerBoneAffectingSurface(vertsBlendOffset + 3); + + vertsBlendOffset += 5; + } + + // 4 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[3]; vertIndex++) + { + registerBoneAffectingSurface(vertsBlendOffset + 0); + registerBoneAffectingSurface(vertsBlendOffset + 1); + registerBoneAffectingSurface(vertsBlendOffset + 3); + registerBoneAffectingSurface(vertsBlendOffset + 5); + + vertsBlendOffset += 7; + } + + for (auto vertListIndex = 0; vertListIndex < surface->vertListCount; vertListIndex++) + { + affectingBones.emplace(surface->vertList[vertListIndex].boneOffset / sizeof(Game::DObjSkelMat)); + } + + // Actually rebuilding + for (const auto& boneIndex : affectingBones) + { + const auto bitPosition = 31 - boneIndex % 32; + const auto groupIndex = boneIndex / 32; + + assert(groupIndex < 6); + assert(groupIndex >= 0); + + rebuiltPartBits[groupIndex] |= 1 << bitPosition; + lodPartBits[groupIndex] |= 1 << bitPosition; + } + + std::memcpy(surface->partBits, rebuiltPartBits, 6 * sizeof(int32_t)); + } + + std::memcpy(lod->partBits, lodPartBits, 6 * sizeof(int32_t)); + std::memcpy(lod->modelSurfs->partBits, lodPartBits, 6 * sizeof(int32_t)); + + // here's a little lesson in trickery: + // We set the 192nd part bit to TRUE because it has no consequences + // but allows us to find out whether that surf was already converted in the past or not + lod->partBits[LENGTH - 1] |= 0x1; + lod->modelSurfs->partBits[LENGTH - 1] |= 0x1; + } + }; + + + uint8_t IXModel::InsertBone(Game::XModel* model, const std::string& boneName, const std::string& parentName, Utils::Memory::Allocator& allocator) + { + assert(GetIndexOfBone(model, boneName) == UCHAR_MAX); + + constexpr auto MAX_BONES = 192; + + assert(model->numBones < MAX_BONES); + + // Start with backing up parent links that we will have to restore + // We'll restore them at the end + std::map parentsToRestore{}; + for (int i = model->numRootBones; i < model->numBones; i++) + { + parentsToRestore[Game::SL_ConvertToString(model->boneNames[i])] = GetParentOfBone(model, i); + } + + const uint8_t newBoneCount = model->numBones + 1; + const uint8_t newBoneCountMinusRoot = newBoneCount - model->numRootBones; + + const auto parentIndex = GetIndexOfBone(model, parentName); + + assert(parentIndex != UCHAR_MAX); + + const uint8_t atPosition = parentIndex + 1; + + const uint8_t newBoneIndex = atPosition; + const uint8_t newBoneIndexMinusRoot = atPosition - model->numRootBones; + + Components::Logger::Print("Inserting bone {} at position {} (between {} and {})\n", boneName, atPosition, Game::SL_ConvertToString(model->boneNames[atPosition-1]), Game::SL_ConvertToString(model->boneNames[atPosition+1])); + + // Reallocate + const auto newBoneNames = allocator.allocateArray(newBoneCount); + const auto newMats = allocator.allocateArray(newBoneCount); + const auto newBoneInfo = allocator.allocateArray(newBoneCount); + const auto newPartsClassification = allocator.allocateArray(newBoneCount); + const auto newQuats = allocator.allocateArray(4 * newBoneCountMinusRoot); + const auto newTrans = allocator.allocateArray(3 * newBoneCountMinusRoot); + const auto newParentList = allocator.allocateArray(newBoneCountMinusRoot); + + const uint8_t lengthOfFirstPart = atPosition; + const uint8_t lengthOfSecondPart = model->numBones - atPosition; + + const uint8_t lengthOfFirstPartM1 = atPosition - model->numRootBones; + const uint8_t lengthOfSecondPartM1 = model->numBones - model->numRootBones - (atPosition - model->numRootBones); + + const uint8_t atPositionM1 = atPosition - model->numRootBones; + + // should be equal to model->numBones + int total = lengthOfFirstPart + lengthOfSecondPart; + assert(total = model->numBones); + + // should be equal to model->numBones - model->numRootBones + int totalM1 = lengthOfFirstPartM1 + lengthOfSecondPartM1; + assert(totalM1 == model->numBones - model->numRootBones); + + // Copy before + if (lengthOfFirstPart > 0) + { + std::memcpy(newBoneNames, model->boneNames, sizeof(uint16_t) * lengthOfFirstPart); + std::memcpy(newMats, model->baseMat, sizeof(Game::DObjAnimMat) * lengthOfFirstPart); + std::memcpy(newPartsClassification, model->partClassification, lengthOfFirstPart); + std::memcpy(newBoneInfo, model->boneInfo, sizeof(Game::XBoneInfo) * lengthOfFirstPart); + std::memcpy(newQuats, model->quats, sizeof(uint16_t) * 4 * lengthOfFirstPartM1); + std::memcpy(newTrans, model->trans, sizeof(float) * 3 * lengthOfFirstPartM1); + } + + // Insert new bone + { + unsigned int name = Game::SL_GetString(boneName.data(), 0); + Game::XBoneInfo boneInfo{}; + + Game::DObjAnimMat mat{}; + + // It's ABSOLUTE! + mat = model->baseMat[parentIndex]; + + boneInfo = model->boneInfo[parentIndex]; + + // It's RELATIVE ! + uint16_t quat[4]{}; + quat[3] = 32767; // 0 0 0 32767 + + float trans[3]{}; + + mat.transWeight = 1.9999f; // Should be 1.9999 like everybody? + + newMats[newBoneIndex] = mat; + newBoneInfo[newBoneIndex] = boneInfo; + newBoneNames[newBoneIndex] = static_cast(name); + + // TODO parts Classification + + std::memcpy(&newQuats[newBoneIndexMinusRoot * 4], quat, ARRAYSIZE(quat) * sizeof(uint16_t)); + std::memcpy(&newTrans[newBoneIndexMinusRoot * 3], trans, ARRAYSIZE(trans) * sizeof(float)); + } + + // Copy after + if (lengthOfSecondPart > 0) + { + std::memcpy(&newBoneNames[atPosition + 1], &model->boneNames[atPosition], sizeof(uint16_t) * lengthOfSecondPart); + std::memcpy(&newMats[atPosition + 1], &model->baseMat[atPosition], sizeof(Game::DObjAnimMat) * lengthOfSecondPart); + std::memcpy(&newPartsClassification[atPosition+1], &model->partClassification[atPosition], lengthOfSecondPart); + std::memcpy(&newBoneInfo[atPosition + 1], &model->boneInfo[atPosition], sizeof(Game::XBoneInfo) * lengthOfSecondPart); + std::memcpy(&newQuats[(atPositionM1 + 1) * 4], &model->quats[atPositionM1 * 4], sizeof(uint16_t) * 4 * lengthOfSecondPartM1); + std::memcpy(&newTrans[(atPositionM1 + 1) * 3], &model->trans[atPositionM1 * 3], sizeof(float) * 3 * lengthOfSecondPartM1); + } + + //Game::Z_VirtualFree(model->baseMat); + //Game::Z_VirtualFree(model->boneInfo); + //Game::Z_VirtualFree(model->boneNames); + //Game::Z_VirtualFree(model->quats); + //Game::Z_VirtualFree(model->trans); + //Game::Z_VirtualFree(model->parentList); + + // Assign reallocated + model->baseMat = newMats; + model->boneInfo = newBoneInfo; + model->boneNames = newBoneNames; + model->quats = newQuats; + model->trans = newTrans; + model->parentList = newParentList; + + model->numBones = newBoneCount; + + // Update vertex weight + for (uint8_t lodIndex = 0; lodIndex < model->numLods; lodIndex++) + { + const auto lod = &model->lodInfo[lodIndex]; + + if ((lod->partBits[5] & 0x1) == 0x1) + { + // surface lod already converted (more efficient) + continue; + } + + if (GetHighestAffectingBoneIndex(lod) >= model->numBones) + { + // surface lod already converted (more accurate) + continue; + } + + for (int surfIndex = 0; surfIndex < lod->modelSurfs->numsurfs; surfIndex++) + { + auto vertsBlendOffset = 0u; + + const auto surface = &lod->modelSurfs->surfs[surfIndex]; + + static_assert(sizeof(Game::DObjSkelMat) == 64); + + { + const auto fixVertexBlendIndex = [&](unsigned int offset) { + + int index = static_cast(surface->vertInfo.vertsBlend[offset] / sizeof(Game::DObjSkelMat)); + + if (index >= atPosition) + { + index++; + + if (index < 0 || index >= model->numBones) + { + Components::Logger::Print("Unexpected 'bone index' {} out of {} bones while working vertex blend of model {} lod {} surf {}\n", index, model->numBones, model->name, lodIndex, surfIndex); + assert(false); + } + + surface->vertInfo.vertsBlend[offset] = static_cast(index * sizeof(Game::DObjSkelMat)); + } + }; + + // Fix bone offsets + if (surface->vertList) + { + for (auto vertListIndex = 0u; vertListIndex < surface->vertListCount; vertListIndex++) + { + const auto vertList = &surface->vertList[vertListIndex]; + + auto index = vertList->boneOffset / sizeof(Game::DObjSkelMat); + if (index >= atPosition) + { + index++; + + if (index < 0 || index >= model->numBones) + { + Components::Logger::Print("Unexpected 'bone index' {} out of {} bones while working list blend of model {} lod {} surf {}\n", index, model->numBones, model->name, lodIndex, surfIndex); + assert(false); + } + + vertList->boneOffset = static_cast(index * sizeof(Game::DObjSkelMat)); + } + } + } + + // 1 bone weight + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[0]; vertIndex++) + { + fixVertexBlendIndex(vertsBlendOffset + 0); + + vertsBlendOffset += 1; + } + + // 2 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[1]; vertIndex++) + { + fixVertexBlendIndex(vertsBlendOffset + 0); + fixVertexBlendIndex(vertsBlendOffset + 1); + + vertsBlendOffset += 3; + } + + // 3 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[2]; vertIndex++) + { + fixVertexBlendIndex(vertsBlendOffset + 0); + fixVertexBlendIndex(vertsBlendOffset + 1); + fixVertexBlendIndex(vertsBlendOffset + 3); + + vertsBlendOffset += 5; + } + + // 4 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[3]; vertIndex++) + { + fixVertexBlendIndex(vertsBlendOffset + 0); + fixVertexBlendIndex(vertsBlendOffset + 1); + fixVertexBlendIndex(vertsBlendOffset + 3); + fixVertexBlendIndex(vertsBlendOffset + 5); + + vertsBlendOffset += 7; + } + } + } + } + + SetParentIndexOfBone(model, atPosition, parentIndex); + + // Restore parents + for (const auto& kv : parentsToRestore) + { + // Fix parents + const auto key = kv.first; + const auto beforeVal = kv.second; + + const auto parentIndex = GetIndexOfBone(model, beforeVal); + const auto index = GetIndexOfBone(model, key); + SetParentIndexOfBone(model, index, parentIndex); + } + +#if DEBUG + // check + for (const auto& kv : parentsToRestore) + { + const auto key = kv.first; + const auto beforeVal = kv.second; + + const auto index = GetIndexOfBone(model, key); + const auto afterVal = GetParentOfBone(model, index); + + if (beforeVal != afterVal) + { + printf(""); + } + } + // +#endif + + return atPosition; // Bone index of added bone + }; + + + void IXModel::TransferWeights(Game::XModel* model, const uint8_t origin, const uint8_t destination) + { + // Does not work + return; + + const auto originalWeights = model->baseMat[origin].transWeight; + model->baseMat[origin].transWeight = model->baseMat[destination].transWeight; + model->baseMat[destination].transWeight = originalWeights; + + for (int i = 0; i < model->numLods; i++) + { + const auto lod = &model->lodInfo[i]; + + for (int surfIndex = 0; surfIndex < lod->modelSurfs->numsurfs; surfIndex++) + { + auto vertsBlendOffset = 0u; + + const auto surface = &lod->modelSurfs->surfs[surfIndex]; + { + const auto transferVertexBlendIndex = [&](unsigned int offset) { + int index = static_cast(surface->vertInfo.vertsBlend[offset] / sizeof(Game::DObjSkelMat)); + if (index == origin) + { + if (index < 0 || index >= model->numBones - 1) + { + assert(false); + } + + index = destination; + + surface->vertInfo.vertsBlend[offset] = static_cast(index * sizeof(Game::DObjSkelMat)); + } + }; + + // Fix bone offsets + if (surface->vertList) + { + for (auto vertListIndex = 0u; vertListIndex < surface->vertListCount; vertListIndex++) + { + const auto vertList = &surface->vertList[vertListIndex]; + auto index = vertList->boneOffset / sizeof(Game::DObjSkelMat); + if (index < 0 || index >= model->numBones - 1) + { + assert(false); + } + + if (index == origin) + { + index = destination; + vertList->boneOffset = static_cast(index * sizeof(Game::DObjSkelMat)); + } + } + } + + // 1 bone weight + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[0]; vertIndex++) + { + transferVertexBlendIndex(vertsBlendOffset + 0); + + vertsBlendOffset += 1; + } + + // 2 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[1]; vertIndex++) + { + transferVertexBlendIndex(vertsBlendOffset + 0); + transferVertexBlendIndex(vertsBlendOffset + 1); + + vertsBlendOffset += 3; + } + + // 3 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[2]; vertIndex++) + { + transferVertexBlendIndex(vertsBlendOffset + 0); + transferVertexBlendIndex(vertsBlendOffset + 1); + transferVertexBlendIndex(vertsBlendOffset + 3); + + + vertsBlendOffset += 5; + } + + // 4 bone weights + for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[3]; vertIndex++) + { + transferVertexBlendIndex(vertsBlendOffset + 0); + transferVertexBlendIndex(vertsBlendOffset + 1); + transferVertexBlendIndex(vertsBlendOffset + 3); + transferVertexBlendIndex(vertsBlendOffset + 5); + + vertsBlendOffset += 7; + } + } + } + } + }; + + + void IXModel::ConvertPlayerModelFromSingleplayerToMultiplayer(Game::XModel* model, Utils::Memory::Allocator& allocator) + { + auto indexOfSpine = GetIndexOfBone(model, "j_spinelower"); + if (indexOfSpine < UCHAR_MAX) // It has a spine so it must be some sort of humanoid + { + const auto nameOfParent = GetParentOfBone(model, indexOfSpine); + + if (GetIndexOfBone(model, "torso_stabilizer") == UCHAR_MAX) // Singleplayer model is likely + { + + Components::Logger::Print("Converting {}\n", model->name); + + // No stabilizer - let's do surgery + // We're trying to get there: + // tag_origin + // j_main_root + // pelvis + // j_hip_le + // j_hip_ri + // tag_stowed_hip_rear + // torso_stabilizer + // j_spinelower + // back_low + // j_spineupper + // back_mid + // j_spine4 + + + const auto root = GetIndexOfBone(model, "j_mainroot"); + if (root < UCHAR_MAX) { + +#if true + //// Works + //InsertBone(model, "offsetron_the_great_offsetter_of_bones", "j_mainroot", allocator); + + //// Breaks the model + //InsertBone(model, "offsetron2_the_greater_offsetter_of_bones", "j_mainroot", allocator); + + //for (auto lodIndex = 0; lodIndex < model->numLods; lodIndex++) + //{ + // convertedSurfs.emplace(model->lodInfo[lodIndex].modelSurfs); + //} + + //RebuildPartBits(model); + + //return; + + // Add pelvis + const uint8_t indexOfPelvis = InsertBone(model, "pelvis", "j_mainroot", allocator); + + TransferWeights(model, root, indexOfPelvis); + + SetParentIndexOfBone(model, GetIndexOfBone(model, "j_hip_le"), indexOfPelvis); + SetParentIndexOfBone(model, GetIndexOfBone(model, "j_hip_ri"), indexOfPelvis); + SetParentIndexOfBone(model, GetIndexOfBone(model, "tag_stowed_hip_rear"), indexOfPelvis); + + const uint8_t torsoStabilizer = InsertBone(model, "torso_stabilizer", "pelvis", allocator); + SetParentIndexOfBone(model, GetIndexOfBone(model, "j_spinelower"), torsoStabilizer); + + const uint8_t backLow = InsertBone(model, "back_low", "j_spinelower", allocator); + TransferWeights(model, GetIndexOfBone(model, "j_spinelower"), backLow); + SetParentIndexOfBone(model, GetIndexOfBone(model, "j_spineupper"), backLow); + + const uint8_t backMid = InsertBone(model, "back_mid", "j_spineupper", allocator); + TransferWeights(model, GetIndexOfBone(model, "j_spineupper"), backMid); + SetParentIndexOfBone(model, GetIndexOfBone(model, "j_spine4"), backMid); + + + assert(root == GetIndexOfBone(model, "j_mainroot")); + assert(indexOfPelvis == GetIndexOfBone(model, "pelvis")); + assert(backLow == GetIndexOfBone(model, "back_low")); + assert(backMid == GetIndexOfBone(model, "back_mid")); + + // Fix up torso stabilizer + model->baseMat[torsoStabilizer].quat[0] = 0.F; + model->baseMat[torsoStabilizer].quat[1] = 0.F; + model->baseMat[torsoStabilizer].quat[2] = 0.F; + model->baseMat[torsoStabilizer].quat[3] = 1.F; + + const auto spineLowerM1 = GetIndexOfBone(model, "j_spinelower") - model->numRootBones; + + // This doesn't feel like it should be necessary + model->trans[spineLowerM1 * 3 + 0] = 0.069828756; + model->trans[spineLowerM1 * 3 + 1] = -0.0f; + model->trans[spineLowerM1 * 3 + 2] = 5.2035017F; + + //// Euler -180.000 88.572 -90.000 + model->quats[(torsoStabilizer - model->numRootBones) * 4 + 0] = 16179; // 0.4952 + model->quats[(torsoStabilizer - model->numRootBones) * 4 + 1] = 16586; // 0.5077 + model->quats[(torsoStabilizer - model->numRootBones) * 4 + 2] = 16586; // 0.5077 + model->quats[(torsoStabilizer - model->numRootBones) * 4 + 3] = 16178; // 0.4952 + +#else + const uint8_t torsoStabilizer = insertBone("torso_stabilizer", "j_mainroot"); + transferWeights(getIndexOfBone("j_mainroot"), getIndexOfBone("torso_stabilizer")); + setParentIndexOfBone(getIndexOfBone("j_spinelower"), torsoStabilizer); +#endif + + RebuildPartBits(model); + } + } + printf(""); + } + } + } diff --git a/src/Components/Modules/AssetInterfaces/IXModel.hpp b/src/Components/Modules/AssetInterfaces/IXModel.hpp index 2c68489a..13a58b32 100644 --- a/src/Components/Modules/AssetInterfaces/IXModel.hpp +++ b/src/Components/Modules/AssetInterfaces/IXModel.hpp @@ -10,5 +10,17 @@ namespace Assets void save(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder) override; void mark(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder) override; void load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder) override; + + static void ConvertPlayerModelFromSingleplayerToMultiplayer(Game::XModel* model, Utils::Memory::Allocator& allocator); + + private: + static uint8_t GetIndexOfBone(const Game::XModel* model, std::string name); + static uint8_t GetParentIndexOfBone(const Game::XModel* model, uint8_t index); + static void SetParentIndexOfBone(Game::XModel* model, uint8_t boneIndex, uint8_t parentIndex); + static std::string GetParentOfBone(Game::XModel* model, uint8_t index); + static uint8_t GetHighestAffectingBoneIndex(const Game::XModelLodInfo* lod); + static void RebuildPartBits(Game::XModel* model); + static uint8_t InsertBone(Game::XModel* model, const std::string& boneName, const std::string& parentName, Utils::Memory::Allocator& allocator); + static void TransferWeights(Game::XModel* model, const uint8_t origin, const uint8_t destination); }; } diff --git a/src/Components/Modules/ZoneBuilder.cpp b/src/Components/Modules/ZoneBuilder.cpp index d045a34a..c2e39cab 100644 --- a/src/Components/Modules/ZoneBuilder.cpp +++ b/src/Components/Modules/ZoneBuilder.cpp @@ -789,7 +789,7 @@ namespace Components params.request_mark_asset = [this](int type, void* data) -> void { - Game::XAsset asset {static_cast(type), {data}}; + Game::XAsset asset{ static_cast(type), {data} }; AssetHandler::ZoneMark(asset, this); this->addRawAsset(static_cast(type), data); @@ -1150,6 +1150,35 @@ namespace Components return params; } + std::function ZoneBuilder::LoadZoneWithTrace(const std::string& zone, OUT std::vector>& assets) + { + ZoneBuilder::BeginAssetTrace(zone); + + Game::XZoneInfo info{}; + info.name = zone.data(); + info.allocFlags = Game::DB_ZONE_MOD; + info.freeFlags = 0; + + Logger::Print("Loading zone '{}'...\n", zone); + + Game::DB_LoadXAssets(&info, 1, true); + AssetHandler::FindOriginalAsset(Game::XAssetType::ASSET_TYPE_RAWFILE, zone.data()); // Lock until zone is loaded + + assets = ZoneBuilder::EndAssetTrace(); + + return [zone]() { + Logger::Print("Unloading zone '{}'...\n", zone); + + Game::XZoneInfo info{}; + info.freeFlags = Game::DB_ZONE_MOD; + info.allocFlags = 0; + info.name = nullptr; + + Game::DB_LoadXAssets(&info, 1, true); + AssetHandler::FindOriginalAsset(Game::XAssetType::ASSET_TYPE_RAWFILE, "default"); // Lock until zone is unloaded + }; + } + ZoneBuilder::ZoneBuilder() { // ReSharper disable CppStaticAssertFailure @@ -1259,83 +1288,47 @@ namespace Components Utils::Hook::Nop(0x5BC791, 5); AssetHandler::OnLoad([](Game::XAssetType type, Game::XAssetHeader /* asset*/, const std::string& name, bool* /*restrict*/) + { + if (!ZoneBuilder::TraceZone.empty() && ZoneBuilder::TraceZone == FastFiles::Current()) { - if (!ZoneBuilder::TraceZone.empty() && ZoneBuilder::TraceZone == FastFiles::Current()) - { - ZoneBuilder::TraceAssets.emplace_back(std::make_pair(type, name)); -#ifdef _DEBUG - OutputDebugStringA(Utils::String::Format("%s\n", name)); -#endif - } - }); + ZoneBuilder::TraceAssets.emplace_back(std::make_pair(type, name)); + } + }); Command::Add("dumpzone", [](const Command::Params* params) { if (params->size() < 2) return; - + std::string zone = params->get(1); + ZoneBuilder::DumpingZone = zone; ZoneBuilder::RefreshExporterWorkDirectory(); - Game::XZoneInfo info; - info.name = zone.data(); - info.allocFlags = Game::DB_ZONE_MOD; - info.freeFlags = 0; + std::vector> assets{}; + const auto unload = ZoneBuilder::LoadZoneWithTrace(zone, assets); - Logger::Print("Loading zone '{}'...\n", zone); + Logger::Print("Dumping zone '{}'...\n", zone); + for (const auto& asset : assets) { - struct asset_t + const auto type = asset.first; + const auto name = asset.second; + if (ExporterAPI.is_type_supported(type) && name[0] != ',') { - Game::XAssetType type; - char name[128]; - }; - - std::vector assets{}; - const auto handle = AssetHandler::OnLoad([&](Game::XAssetType type, Game::XAssetHeader asset, const std::string& name, bool* /*restrict*/) - { - if (ExporterAPI.is_type_supported(type) && name[0] != ',') - { - Game::XAsset xasset = { type, asset }; - asset_t assetIdentifier{}; - - // Fetch name - const auto assetName = Game::DB_GetXAssetName(&xasset); - std::memcpy(assetIdentifier.name, assetName, strnlen(assetName, ARRAYSIZE(assetIdentifier.name) - 1)); - assetIdentifier.name[ARRAYSIZE(assetIdentifier.name) - 1] = '\x00'; - - assetIdentifier.type = type; - - assets.push_back(assetIdentifier); - } - }); - - Game::DB_LoadXAssets(&info, 1, true); - AssetHandler::FindOriginalAsset(Game::ASSET_TYPE_RAWFILE, zone.data()); // Lock until zone is loaded - - Logger::Print("Dumping zone '{}'...\n", zone); - handle(); // Release - for (const auto& asset : assets) - { - const auto assetHeader = Game::DB_FindXAssetHeader(asset.type, asset.name); + const auto assetHeader = Game::DB_FindXAssetHeader(type, name.data()); if (assetHeader.data) { - ExporterAPI.write(asset.type, assetHeader.data); + ExporterAPI.write(type, assetHeader.data); } else { - Logger::Warning(Game::conChannel_t::CON_CHANNEL_ERROR, "Asset {} has disappeared while dumping!", asset.name); + Logger::Warning(Game::conChannel_t::CON_CHANNEL_ERROR, "Asset {} has disappeared while dumping!\n", name); } } } - Logger::Print("Unloading zone '{}'...\n", zone); - info.freeFlags = Game::DB_ZONE_MOD; - info.allocFlags = 0; - info.name = nullptr; + unload(); - Game::DB_LoadXAssets(&info, 1, true); - AssetHandler::FindOriginalAsset(Game::ASSET_TYPE_RAWFILE, "default"); // Lock until zone is unloaded Logger::Print("Zone '{}' dumped", ZoneBuilder::DumpingZone); ZoneBuilder::DumpingZone = std::string(); }); @@ -1345,30 +1338,8 @@ namespace Components if (params->size() < 2) return; std::string zone = params->get(1); - - ZoneBuilder::BeginAssetTrace(zone); - - Game::XZoneInfo info; - info.name = zone.data(); - info.allocFlags = Game::DB_ZONE_MOD; - info.freeFlags = 0; - - Logger::Print("Loading zone '{}'...\n", zone); - - Game::DB_LoadXAssets(&info, 1, true); - AssetHandler::FindOriginalAsset(Game::XAssetType::ASSET_TYPE_RAWFILE, zone.data()); // Lock until zone is loaded - - auto assets = ZoneBuilder::EndAssetTrace(); - - Logger::Print("Unloading zone '{}'...\n", zone); - info.freeFlags = Game::DB_ZONE_MOD; - info.allocFlags = 0; - info.name = nullptr; - - Game::DB_LoadXAssets(&info, 1, true); - AssetHandler::FindOriginalAsset(Game::XAssetType::ASSET_TYPE_RAWFILE, "default"); // Lock until zone is unloaded - - Logger::Print("Zone '{}' loaded with {} assets:\n", zone, assets.size()); + std::vector> assets{}; + const auto unload = ZoneBuilder::LoadZoneWithTrace(zone, assets); int count = 0; for (auto i = assets.begin(); i != assets.end(); ++i, ++count) @@ -1377,6 +1348,7 @@ namespace Components } Logger::Print("\n"); + unload(); }); Command::Add("buildzone", [](const Command::Params* params) @@ -1405,23 +1377,6 @@ namespace Components } }); - static std::set curTechsets_list; - static std::set techsets_list; - - AssetHandler::OnLoad([](Game::XAssetType type, Game::XAssetHeader, const std::string& name, bool*) - { - if (type == Game::ASSET_TYPE_TECHNIQUE_SET) - { - if (name[0] == ',') return; // skip techsets from common_mp - if (techsets_list.find(name) == techsets_list.end()) - { - curTechsets_list.emplace(name); - techsets_list.emplace(name); - } - } - }); - - AssetHandler::OnLoad([](Game::XAssetType type, Game::XAssetHeader asset, [[maybe_unused]] const std::string& name, [[maybe_unused]] bool* restrict) { if (type != Game::ASSET_TYPE_SOUND) @@ -1441,6 +1396,11 @@ namespace Components { // ouch // This should never happen and will cause a memory leak +#if DEBUG + // TODO: Crash the game proper when this happen, and do it in ModifyAsset maybe? + __debugbreak(); + assert(false); +#else // Let's change it to a streamed sound instead thisSound.soundFile->type = Game::SAT_STREAMED; @@ -1451,264 +1411,12 @@ namespace Components auto dir = virtualPath.remove_filename().string(); dir = dir.substr(0, dir.size() - 1); // remove / thisSound.soundFile->u.streamSnd.filename.info.raw.dir = Utils::Memory::DuplicateString(dir); +#endif } } } }); - Command::Add("buildtechsets", [](const Command::Params*) - { - Utils::IO::CreateDir("zone_source/techsets"); - Utils::IO::CreateDir("zone/techsets"); - - std::string csvStr; - - const auto dir = std::format("zone/{}", Game::Win_GetLanguage()); - auto fileList = Utils::IO::ListFiles(dir, false); - for (const auto& entry : fileList) - { - auto zone = entry.path().string(); - Utils::String::Replace(zone, Utils::String::VA("zone/%s/", Game::Win_GetLanguage()), ""); - Utils::String::Replace(zone, ".ff", ""); - - if (Utils::IO::FileExists("zone/techsets/" + zone + "_techsets.ff")) - { - Logger::Print("Skipping previously generated zone {}\n", zone); - continue; - } - - if (zone.find("_load") != std::string::npos) - { - Logger::Print("Skipping loadscreen zone {}\n", zone); - continue; - } - - if (Game::DB_IsZoneLoaded(zone.c_str()) || !FastFiles::Exists(zone)) - { - continue; - } - - if (zone[0] == '.') continue; // fucking mac dotfiles - - curTechsets_list.clear(); // clear from last run - - // load the zone - Game::XZoneInfo info; - info.name = zone.c_str(); - info.allocFlags = Game::DB_ZONE_MOD; - info.freeFlags = 0x0; - Game::DB_LoadXAssets(&info, 1, 0); - - while (!Game::Sys_IsDatabaseReady()) std::this_thread::sleep_for(100ms); // wait till its fully loaded - - if (curTechsets_list.empty()) - { - Logger::Print("Skipping empty zone {}\n", zone); - // unload zone - info.name = nullptr; - info.allocFlags = 0x0; - info.freeFlags = Game::DB_ZONE_MOD; - Game::DB_LoadXAssets(&info, 1, true); - continue; - } - - // ok so we're just gonna use the materials because they will use the techsets - csvStr.clear(); - for (auto tech : curTechsets_list) - { - std::string mat = ZoneBuilder::FindMaterialByTechnique(tech); - if (mat.length() == 0) - { - csvStr.append("techset," + tech + "\n"); - } - else - { - csvStr.append("material," + mat + "\n"); - } - } - - // save csv - Utils::IO::WriteFile("zone_source/techsets/" + zone + "_techsets.csv", csvStr); - - // build the techset zone - std::string zoneName = "techsets/" + zone + "_techsets"; - Logger::Print("Building zone '{}'...\n", zoneName); - Zone(zoneName).build(); - - // unload original zone - info.name = nullptr; - info.allocFlags = 0x0; - info.freeFlags = Game::DB_ZONE_MOD; - Game::DB_LoadXAssets(&info, 1, true); - - while (!Game::Sys_IsDatabaseReady()) std::this_thread::sleep_for(10ms); // wait till its fully loaded - } - - curTechsets_list.clear(); - techsets_list.clear(); - - Game::DB_EnumXAssets(Game::ASSET_TYPE_TECHNIQUE_SET, [](Game::XAssetHeader header, void*) - { - curTechsets_list.emplace(header.techniqueSet->name); - techsets_list.emplace(header.techniqueSet->name); - }, nullptr, false); - - // HACK: set language to 'techsets' to load from that dir - const char* language = Utils::Hook::Get(0x649E740); - Utils::Hook::Set(0x649E740, "techsets"); - - // load generated techset fastfiles - auto list = Utils::IO::ListFiles("zone/techsets", false); - int i = 0; - int subCount = 0; - for (const auto& entry : list) - { - auto it = entry.path().string(); - - Utils::String::Replace(it, "zone/techsets/", ""); - Utils::String::Replace(it, ".ff", ""); - - if (it.find("_techsets") == std::string::npos) continue; // skip files we didn't generate for this - - if (!Game::DB_IsZoneLoaded(it.data())) - { - Game::XZoneInfo info; - info.name = it.data(); - info.allocFlags = Game::DB_ZONE_MOD; - info.freeFlags = 0; - - Game::DB_LoadXAssets(&info, 1, 0); - while (!Game::Sys_IsDatabaseReady()) std::this_thread::sleep_for(10ms); // wait till its fully loaded - } - else - { - Logger::Print("Zone '{}' already loaded\n", it); - } - - if (i == 20) // cap at 20 just to be safe - { - // create csv with the techsets in it - csvStr.clear(); - for (auto tech : curTechsets_list) - { - std::string mat = ZoneBuilder::FindMaterialByTechnique(tech); - if (mat.length() == 0) - { - csvStr.append("techset," + tech + "\n"); - } - else - { - csvStr.append("material," + mat + "\n"); - } - } - - std::string tempZoneFile = Utils::String::VA("zone_source/techsets/techsets%d.csv", subCount); - std::string tempZone = Utils::String::VA("techsets/techsets%d", subCount); - - Utils::IO::WriteFile(tempZoneFile, csvStr); - - Logger::Print("Building zone '{}'...\n", tempZone); - Zone(tempZone).build(); - - // unload all zones - Game::XZoneInfo info; - info.name = nullptr; - info.allocFlags = 0x0; - info.freeFlags = Game::DB_ZONE_MOD; - Game::DB_LoadXAssets(&info, 1, true); - - Utils::Hook::Set(0x649E740, "techsets"); - - i = 0; - subCount++; - curTechsets_list.clear(); - techsets_list.clear(); - } - - i++; - } - - // last iteration - if (i != 0) - { - // create csv with the techsets in it - csvStr.clear(); - for (auto tech : curTechsets_list) - { - std::string mat = ZoneBuilder::FindMaterialByTechnique(tech); - if (mat.length() == 0) - { - Logger::Print("Couldn't find a material for techset {}. Sort Keys will be incorrect.\n", tech); - csvStr.append("techset," + tech + "\n"); - } - else - { - csvStr.append("material," + mat + "\n"); - } - } - - std::string tempZoneFile = Utils::String::VA("zone_source/techsets/techsets%d.csv", subCount); - std::string tempZone = Utils::String::VA("techsets/techsets%d", subCount); - - Utils::IO::WriteFile(tempZoneFile, csvStr); - - Logger::Print("Building zone '{}'...\n", tempZone); - Zone(tempZone).build(); - - // unload all zones - Game::XZoneInfo info; - info.name = nullptr; - info.allocFlags = 0x0; - info.freeFlags = Game::DB_ZONE_MOD; - Game::DB_LoadXAssets(&info, 1, true); - - subCount++; - } - - // build final techsets fastfile - if (subCount > 24) - { - Logger::Error(Game::ERR_DROP, "How did you have 576 fastfiles?\n"); - } - - curTechsets_list.clear(); - techsets_list.clear(); - - for (int j = 0; j < subCount; ++j) - { - Game::XZoneInfo info; - info.name = Utils::String::VA("techsets%d", j); - info.allocFlags = Game::DB_ZONE_MOD; - info.freeFlags = 0; - - Game::DB_LoadXAssets(&info, 1, 0); - while (!Game::Sys_IsDatabaseReady()) std::this_thread::sleep_for(10ms); // wait till its fully loaded - } - - // create csv with the techsets in it - csvStr.clear(); - for (const auto& tech : curTechsets_list) - { - auto mat = ZoneBuilder::FindMaterialByTechnique(tech); - if (mat.length() == 0) - { - csvStr.append("techset," + tech + "\n"); - } - else - { - csvStr.append("material," + mat + "\n"); - } - } - - Utils::IO::WriteFile("zone_source/techsets/techsets.csv", csvStr); - - // set language back - Utils::Hook::Set(0x649E740, language); - - Logger::Print("Building zone 'techsets/techsets'...\n"); - Zone("techsets/techsets").build(); - }); - Command::Add("listassets", [](const Command::Params* params) { if (params->size() < 2) return; @@ -1723,39 +1431,6 @@ namespace Components }, &type, false); } }); - - Command::Add("loadtempzone", [](const Command::Params* params) - { - if (params->size() < 2) return; - - if (FastFiles::Exists(params->get(1))) - { - Game::XZoneInfo info; - info.name = params->get(1); - info.allocFlags = 0x80; - info.freeFlags = 0x0; - Game::DB_LoadXAssets(&info, 1, 0); - } - }); - - Command::Add("unloadtempzones", [](const Command::Params*) - { - Game::XZoneInfo info; - info.name = nullptr; - info.allocFlags = 0x0; - info.freeFlags = 0x80; - Game::DB_LoadXAssets(&info, 1, true); - AssetHandler::FindOriginalAsset(Game::XAssetType::ASSET_TYPE_RAWFILE, "default"); // Lock until zone is unloaded - }); - - Command::Add("materialInfoDump", [](const Command::Params*) - { - Game::DB_EnumXAssets(Game::ASSET_TYPE_MATERIAL, [](Game::XAssetHeader header, void*) - { - Logger::Print("{}: {:#X} {:#X} {:#X}\n", - header.material->info.name, header.material->info.sortKey & 0xFF, header.material->info.gameFlags & 0xFF, header.material->stateFlags & 0xFF); - }, nullptr, false); - }); } } @@ -1767,37 +1442,4 @@ namespace Components ZoneBuilder::CommandThread.join(); } } - -#if defined(DEBUG) || defined(FORCE_UNIT_TESTS) - bool ZoneBuilder::unitTest() - { - printf("Testing circular bit shifting (left)..."); - - unsigned int integer = 0x80000000; - Utils::RotLeft(integer, 1); - - if (integer != 1) - { - printf("Error\n"); - printf("Bit shifting failed: %X\n", integer); - return false; - } - - printf("Success\n"); - printf("Testing circular bit shifting (right)..."); - - unsigned char byte = 0b00000011; - Utils::RotRight(byte, 2); - - if (byte != 0b11000000) - { - printf("Error\n"); - printf("Bit shifting failed %X\n", byte & 0xFF); - return false; - } - - printf("Success\n"); - return true; - } -#endif } diff --git a/src/Components/Modules/ZoneBuilder.hpp b/src/Components/Modules/ZoneBuilder.hpp index 1450faef..4f4a4427 100644 --- a/src/Components/Modules/ZoneBuilder.hpp +++ b/src/Components/Modules/ZoneBuilder.hpp @@ -124,10 +124,6 @@ namespace Components ZoneBuilder(); ~ZoneBuilder(); -#if defined(DEBUG) || defined(FORCE_UNIT_TESTS) - bool unitTest() override; -#endif - static bool IsEnabled(); static bool IsDumpingZone() { return DumpingZone.length() > 0; }; @@ -161,6 +157,8 @@ namespace Components static iw4of::params_t GetExporterAPIParams(); + static std::function LoadZoneWithTrace(const std::string& zone, OUT std::vector> &assets); + static void Com_Quitf_t(); static void CommandThreadCallback(); diff --git a/src/Game/Structs.hpp b/src/Game/Structs.hpp index 3fd346d3..4b2b0c2a 100644 --- a/src/Game/Structs.hpp +++ b/src/Game/Structs.hpp @@ -7118,7 +7118,7 @@ namespace Game IMG_FORMAT_COUNT = 0x17, }; - enum $25EF9448C800B18F0C83DB367159AFD6 + enum XAnimPartType { PART_TYPE_NO_QUAT = 0x0, PART_TYPE_HALF_QUAT = 0x1, From f1ec16983a3a9636a8c7b729d423b471b8b43789 Mon Sep 17 00:00:00 2001 From: Roxanne Date: Fri, 26 Jan 2024 14:10:33 +0100 Subject: [PATCH 04/10] more cleanup & some fucking around with xanimparts --- src/Components/Modules/AssetHandler.cpp | 682 ++++-------------------- 1 file changed, 108 insertions(+), 574 deletions(-) diff --git a/src/Components/Modules/AssetHandler.cpp b/src/Components/Modules/AssetHandler.cpp index 37bf8ea5..5c17fc1a 100644 --- a/src/Components/Modules/AssetHandler.cpp +++ b/src/Components/Modules/AssetHandler.cpp @@ -235,615 +235,148 @@ namespace Components } } #endif + if (type == Game::XAssetType::ASSET_TYPE_XANIMPARTS) + { + auto anim = asset.parts; + if (anim->name == "unarmed_cowerstand_idle"s || anim->name == "civilian_walk_cool"s || anim->name == "civilian_run_upright"s) + { + auto str = Game::SL_FindString("j_spinelower"); + int interestingPart = 0; + for (size_t i = 0; i < anim->boneCount[Game::XAnimPartType::PART_TYPE_ALL]; i++) + { + if (anim->names[i] == str) + { + interestingPart = i; + //anim->names[i] = Game::SL_FindString("torso_stabilizer"); + } + } + + // Something interesting to do there + //for (size_t frameIndex = 0; frameIndex < anim->numframes; frameIndex++) + //{ + // const auto delta = &anim->deltaPart[frameIndex]; + + // const auto frameCount = anim->numframes; + + // if (delta->trans->size) + // { + // const auto test = delta->trans->u.frames; + + // if (frameCount > 0xFF) + // { + // delta->trans->u.frames.indices._1 + // buffer->saveArray(delta->trans->u.frames.indices._2, delta->trans->size + 1); + // } + // else + // { + // buffer->saveArray(delta->trans->u.frames.indices._1, delta->trans->size + 1); + // } + + // if (delta->trans->u.frames.frames._1) + // { + // if (delta->trans->smallTrans) + // { + // buffer->save(delta->trans->u.frames.frames._1, 3, delta->trans->size + 1); + // } + // else + // { + // buffer->align(Utils::Stream::ALIGN_4); + // buffer->save(delta->trans->u.frames.frames._1, 6, delta->trans->size + 1); + // } + // } + // } + // else + // { + // buffer->save(delta->trans->u.frame0, 12); + // } + //} + } + } + if (type == Game::XAssetType::ASSET_TYPE_XMODEL) - //if (name == "body_urban_civ_female_a"s || name == "mp_body_opforce_arab_assault_a"s) + //if (name == "body_urban_civ_female_a"s || name == "mp_body_opforce_arab_assault_a"s) { - const auto model = asset.model; - if (!model->quats || !model->trans) + + if (name == "body_urban_civ_male_aa"s) { - return; + printf(""); } - // Update vertex weights - //for (int i = 0; i < model->numLods; i++) - //{ - // const auto lod = &model->lodInfo[i]; + const auto model = asset.model; - // for (int surfIndex = 0; surfIndex < lod->modelSurfs->numsurfs; surfIndex++) - // { - // auto vertsBlendOffset = 0u; + int total = + model->noScalePartBits[0] | + model->noScalePartBits[1] | + model->noScalePartBits[2] | + model->noScalePartBits[3] | + model->noScalePartBits[4] | + model->noScalePartBits[5]; - // const auto surface = &lod->modelSurfs->surfs[surfIndex]; - // surface->partBits[0] = 0x00000000; - // surface->partBits[1] = 0x00000000; - // surface->partBits[2] = 0x00000000; - // surface->partBits[3] = 0x00000000; - // surface->partBits[4] = 0x00000000; - // surface->partBits[5] = 0x00000000; - // } - //} - - //return; - - - - class QuatInt16 + if (total != 0) { - public: - static uint16_t ToInt16(const float quat) - { - return static_cast(quat * INT16_MAX); - } + printf(""); + } - static float ToFloat(const uint16_t quat) - { - return static_cast(quat) / static_cast(INT16_MAX); - } - }; + if (!model->quats || !model->trans || model->numBones < 10) + { + // Unlikely candidate + return; + } // static std::vector names{}; names.clear(); + static Utils::Memory::Allocator allocator{}; for (auto i = 0; i < model->numBones; i++) { const auto bone = model->boneNames[i]; - const auto name = Game::SL_ConvertToString(bone); + const auto boneName = Game::SL_ConvertToString(bone); + const auto str = std::format("{} => q({} {} {} {}) t({} {} {})", + boneName, + model->baseMat[i - 1].quat[0], + model->baseMat[i - 1].quat[1], + model->baseMat[i - 1].quat[2], + model->baseMat[i - 1].quat[3], + model->baseMat[i - 1].trans[0], + model->baseMat[i - 1].trans[1], + model->baseMat[i - 1].trans[2] + ); + + const auto duplicated = allocator.duplicateString(str); names.push_back( - std::format("{} => q({} {} {} {}) t({} {} {})", - name, - model->baseMat[i-1].quat[0], - model->baseMat[i-1].quat[1], - model->baseMat[i-1].quat[2], - model->baseMat[i-1].quat[3], - model->baseMat[i-1].trans[0], - model->baseMat[i-1].trans[1], - model->baseMat[i-1].trans[2] - ) + duplicated ); } - printf(""); // - const auto getIndexOfBone = [&](std::string name) - { - for (uint8_t i = 0; i < model->numBones; i++) - { - const auto bone = model->boneNames[i]; - const auto boneName = Game::SL_ConvertToString(bone); - if (name == boneName) - { - return i; - } - } - - return static_cast(UCHAR_MAX); - }; - - const auto getParentIndexOfBone = [&](uint8_t index) - { - const auto parentIndex = index - model->parentList[index - model->numRootBones]; - return parentIndex; - }; - - const auto setParentIndexOfBone = [&](uint8_t boneIndex, uint8_t parentIndex) - { - if (boneIndex == SCHAR_MAX) - { - return; - } - - model->parentList[boneIndex - model->numRootBones] = boneIndex - parentIndex; - }; - - const auto getParentOfBone = [&](int index) - { - const auto parentIndex = getParentIndexOfBone(index); - const auto boneName = Game::SL_ConvertToString(model->boneNames[parentIndex]); - return boneName; - }; - - const auto insertBoneInPartbits = [&](int8_t atPosition, int* partBits, bool hasAnyWeight = false) - { - constexpr auto LENGTH = 6; - // Bit masks and bit shifting are a pain honestly - std::vector flags{}; - - int check[LENGTH]{}; - std::memcpy(check, partBits, LENGTH * sizeof(int)); - - for (auto i = 0; i < LENGTH; i++) - { - for (signed int bitPosition = 32 - 1; bitPosition >= 0; bitPosition--) - { - flags.emplace_back((partBits[i] >> bitPosition) & 0x1); - } - } - - // copy parent flag - const auto partBitIndex = atPosition / 32; - const auto bitPosition = atPosition % 32; // The vector is already reversed so this should be correct - - flags.insert(flags.begin() + (partBitIndex * 32 + bitPosition), hasAnyWeight); - - flags.pop_back(); - - // clear it - for (auto i = 0; i < LENGTH; i++) - { - partBits[i] = 0; - } - - // Write it - for (auto i = 0; i < LENGTH; i++) - { - partBits[i] = 0; - for (int bitPosition = 0; bitPosition < 32; bitPosition++) - { - const auto value = flags[i * 32 + 31 - bitPosition]; - partBits[i] |= (value << bitPosition); - } - } - - flags.clear(); - }; - - - const auto insertBone = [&](const std::string& boneName, const std::string& parentName) - { - assert(getIndexOfBone(boneName) == UCHAR_MAX); - - // Start with backing up parent links that we will have to restore - // We'll restore them at the end - std::map parentsToRestore{}; - for (int i = model->numRootBones; i < model->numBones; i++) - { - parentsToRestore[Game::SL_ConvertToString(model->boneNames[i])] = getParentOfBone(i); - } - - - uint8_t newBoneCount = model->numBones + 1; - uint8_t newBoneCountMinusRoot = newBoneCount - model->numRootBones; - - const auto parentIndex = getIndexOfBone(parentName); - - assert(parentIndex != UCHAR_MAX); - - int atPosition = parentIndex + 1; - - const auto newBoneIndex = atPosition; - const auto newBoneIndexMinusRoot = atPosition - model->numRootBones; - - // Reallocate - const auto newBoneNames = (uint16_t*)Game::Z_Malloc(sizeof(uint16_t) * newBoneCount); - const auto newMats = (Game::DObjAnimMat*)Game::Z_Malloc(sizeof(Game::DObjAnimMat) * newBoneCount); - const auto newBoneInfo = (Game::XBoneInfo*)Game::Z_Malloc(sizeof(Game::XBoneInfo) * newBoneCount); - const auto newQuats = (int16_t*)Game::Z_Malloc(sizeof(uint16_t) * 4 * newBoneCountMinusRoot); - const auto newTrans = (float*)Game::Z_Malloc(sizeof(float) * 3 * newBoneCountMinusRoot); - const auto newParentList = reinterpret_cast(Game::Z_Malloc(sizeof(uint8_t) * newBoneCountMinusRoot)); - - int lengthOfFirstPart = atPosition; - int lengthOfSecondPart = model->numBones - atPosition; - - int lengthOfFirstPartM1 = atPosition - model->numRootBones; - int lengthOfSecondPartM1 = model->numBones - model->numRootBones - (atPosition - model->numRootBones); - - int atPositionM1 = atPosition - model->numRootBones; - - // should be equal to model->numBones - int total = lengthOfFirstPart + lengthOfSecondPart; - assert(total = model->numBones); - - // should be equal to model->numBones - model->numRootBones - int totalM1 = lengthOfFirstPartM1 + lengthOfSecondPartM1; - assert(totalM1 == model->numBones - model->numRootBones); - - // Copy before - if (lengthOfFirstPart > 0) - { - std::memcpy(newBoneNames, model->boneNames, sizeof(uint16_t) * lengthOfFirstPart); - std::memcpy(newMats, model->baseMat, sizeof(Game::DObjAnimMat) * lengthOfFirstPart); - std::memcpy(newBoneInfo, model->boneInfo, sizeof(Game::XBoneInfo) * lengthOfFirstPart); - std::memcpy(newQuats, model->quats, sizeof(uint16_t) * 4 * lengthOfFirstPartM1); - std::memcpy(newTrans, model->trans, sizeof(float) * 3 * lengthOfFirstPartM1); - } - - // Insert new bone - { - unsigned int name = Game::SL_GetString(boneName.data(), 0); - Game::XBoneInfo boneInfo{}; - - Game::DObjAnimMat mat{}; - - // It's ABSOLUTE! - mat = model->baseMat[parentIndex]; - - boneInfo = model->boneInfo[parentIndex]; - - // It's RELATIVE ! - uint16_t quat[4]{}; - quat[3] = 32767; // 0 0 0 32767 - - float trans[3]{}; - - mat.transWeight = 1.9999f; // Should be 1.9999 like everybody? - - newMats[newBoneIndex] = mat; - newBoneInfo[newBoneIndex] = boneInfo; - newBoneNames[newBoneIndex] = name; - - std::memcpy(&newQuats[newBoneIndexMinusRoot * 4], quat, ARRAYSIZE(quat) * sizeof(uint16_t)); - std::memcpy(&newTrans[newBoneIndexMinusRoot * 3], trans, ARRAYSIZE(trans) * sizeof(float)); - } - - // Copy after - if (lengthOfSecondPart > 0) - { - std::memcpy(&newBoneNames[atPosition + 1], &model->boneNames[atPosition], sizeof(uint16_t) * lengthOfSecondPart); - std::memcpy(&newMats[atPosition + 1], &model->baseMat[atPosition], sizeof(Game::DObjAnimMat) * lengthOfSecondPart); - std::memcpy(&newBoneInfo[atPosition + 1], &model->boneInfo[atPosition], sizeof(Game::XBoneInfo) * lengthOfSecondPart); - std::memcpy(&newQuats[(atPositionM1 + 1) * 4], &model->quats[atPositionM1 * 4], sizeof(uint16_t) * 4 * lengthOfSecondPartM1); - std::memcpy(&newTrans[(atPositionM1 + 1) * 3], &model->trans[atPositionM1 * 3], sizeof(float) * 3 * lengthOfSecondPartM1); - } - - // Assign reallocated - model->baseMat = newMats; - model->boneInfo = newBoneInfo; - model->boneNames = newBoneNames; - model->quats = newQuats; - model->trans = newTrans; - model->parentList = newParentList; - - model->numBones++; - - // Update vertex weights - for (int i = 0; i < model->numLods; i++) - { - const auto lod = &model->lodInfo[i]; - insertBoneInPartbits(atPosition, lod->partBits, false); - insertBoneInPartbits(atPosition, lod->modelSurfs->partBits, false); - - for (int surfIndex = 0; surfIndex < lod->modelSurfs->numsurfs; surfIndex++) - { - auto vertsBlendOffset = 0u; - - const auto surface = &lod->modelSurfs->surfs[surfIndex]; - - //surface->partBits[0] = 0b00000000000000000000000000000000; - //surface->partBits[1] = 0b01010101010101010101010101010101; - //surface->partBits[2] = 0b00110011001100110011001100110011; - //surface->partBits[3] = 0b00011100011100011100011100011100; - //surface->partBits[4] = 0b00001111000011110000111100001111; - //surface->partBits[5] = 0b00000111110000011111000001111100; - - insertBoneInPartbits(atPosition, surface->partBits, false); - - { - const auto fixVertexBlendIndex = [&](unsigned int offset) { - int index = static_cast(surface->vertInfo.vertsBlend[offset] / sizeof(Game::DObjSkelMat)); - if (index >= atPosition) - { - if (index < 0 || index >= model->numBones - 1) - { - //assert(false); - } - - index++; - - surface->vertInfo.vertsBlend[offset] = index * sizeof(Game::DObjSkelMat); - } - }; - - // Fix bone offsets - if (surface->vertList) - { - for (auto vertListIndex = 0u; vertListIndex < surface->vertListCount; vertListIndex++) - { - const auto vertList = &surface->vertList[vertListIndex]; - auto index = vertList->boneOffset / sizeof(Game::DObjSkelMat); - if (index < 0 || index >= model->numBones - 1) - { - //assert(false); - } - - if (index >= atPosition) - { - index++; - vertList->boneOffset = index * sizeof(Game::DObjSkelMat); - } - } - } - - // 1 bone weight - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[0]; vertIndex++) - { - fixVertexBlendIndex(vertsBlendOffset + 0); - - vertsBlendOffset += 1; - } - - // 2 bone weights - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[1]; vertIndex++) - { - fixVertexBlendIndex(vertsBlendOffset + 0); - fixVertexBlendIndex(vertsBlendOffset + 1); - - vertsBlendOffset += 3; - } - - // 3 bone weights - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[2]; vertIndex++) - { - fixVertexBlendIndex(vertsBlendOffset + 0); - fixVertexBlendIndex(vertsBlendOffset + 1); - fixVertexBlendIndex(vertsBlendOffset + 3); - - vertsBlendOffset += 5; - } - - // 4 bone weights - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[3]; vertIndex++) - { - fixVertexBlendIndex(vertsBlendOffset + 0); - fixVertexBlendIndex(vertsBlendOffset + 1); - fixVertexBlendIndex(vertsBlendOffset + 3); - fixVertexBlendIndex(vertsBlendOffset + 5); - - vertsBlendOffset += 7; - } - } - } - } - - // TODO free memory of original lists - printf(""); - - setParentIndexOfBone(atPosition, parentIndex); - - // Restore parents - for (const auto& kv : parentsToRestore) - { - // Fix parents - const auto key = kv.first; - const auto beforeVal = kv.second; - - const auto parentIndex = getIndexOfBone(beforeVal); - const auto index = getIndexOfBone(key); - setParentIndexOfBone(index, parentIndex); - } - - - // check - for (const auto& kv : parentsToRestore) - { - const auto key = kv.first; - const auto beforeVal = kv.second; - - const auto index = getIndexOfBone(key); - const auto afterVal = getParentOfBone(index); - - if (beforeVal != afterVal) - { - printf(""); - } - } - // - - return atPosition; // Bone index of added bone - }; - - - const auto transferWeights = [&](const uint8_t origin, const uint8_t destination) - { - return; - - const auto originalWeights = model->baseMat[origin].transWeight; - model->baseMat[origin].transWeight = model->baseMat[destination].transWeight; - model->baseMat[destination].transWeight = originalWeights; - - for (int i = 0; i < model->numLods; i++) - { - const auto lod = &model->lodInfo[i]; - - for (int surfIndex = 0; surfIndex < lod->modelSurfs->numsurfs; surfIndex++) - { - auto vertsBlendOffset = 0u; - - const auto surface = &lod->modelSurfs->surfs[surfIndex]; - { - const auto transferVertexBlendIndex = [&](unsigned int offset) { - int index = static_cast(surface->vertInfo.vertsBlend[offset] / sizeof(Game::DObjSkelMat)); - if (index == origin) - { - if (index < 0 || index >= model->numBones - 1) - { - assert(false); - } - - index = destination; - - surface->vertInfo.vertsBlend[offset] = index * sizeof(Game::DObjSkelMat); - } - }; - - // Fix bone offsets - if (surface->vertList) - { - for (auto vertListIndex = 0u; vertListIndex < surface->vertListCount; vertListIndex++) - { - const auto vertList = &surface->vertList[vertListIndex]; - auto index = vertList->boneOffset / sizeof(Game::DObjSkelMat); - if (index < 0 || index >= model->numBones - 1) - { - assert(false); - } - - if (index == origin) - { - index = destination; - vertList->boneOffset = index * sizeof(Game::DObjSkelMat); - } - } - } - - // 1 bone weight - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[0]; vertIndex++) - { - transferVertexBlendIndex(vertsBlendOffset + 0); - - vertsBlendOffset += 1; - } - - // 2 bone weights - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[1]; vertIndex++) - { - transferVertexBlendIndex(vertsBlendOffset + 0); - transferVertexBlendIndex(vertsBlendOffset + 1); - - vertsBlendOffset += 3; - } - - // 3 bone weights - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[2]; vertIndex++) - { - transferVertexBlendIndex(vertsBlendOffset + 0); - transferVertexBlendIndex(vertsBlendOffset + 1); - transferVertexBlendIndex(vertsBlendOffset + 3); - - - vertsBlendOffset += 5; - } - - // 4 bone weights - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[3]; vertIndex++) - { - transferVertexBlendIndex(vertsBlendOffset + 0); - transferVertexBlendIndex(vertsBlendOffset + 1); - transferVertexBlendIndex(vertsBlendOffset + 3); - transferVertexBlendIndex(vertsBlendOffset + 5); - - vertsBlendOffset += 7; - } - } - } - } - }; - - auto indexOfSpine = getIndexOfBone("j_spinelower"); - if (indexOfSpine < UCHAR_MAX) - { - const auto nameOfParent = getParentOfBone(indexOfSpine); - - if (getIndexOfBone("torso_stabilizer") == UCHAR_MAX) - { - // No stabilizer - let's do surgery - // We're trying to get there: - // tag_origin - // j_main_root - // pelvis - // j_hip_le - // j_hip_ri - // tag_stowed_hip_rear - // torso_stabilizer - // j_spinelower - // back_low - // j_spineupper - // back_mid - // j_spine4 - - - const auto root = getIndexOfBone("j_mainroot"); - if (root < UCHAR_MAX) { - -#if true - // Add pelvis - const uint8_t indexOfPelvis = insertBone("pelvis", "j_mainroot"); - transferWeights(root, indexOfPelvis); - - setParentIndexOfBone(getIndexOfBone("j_hip_le"), indexOfPelvis); - setParentIndexOfBone(getIndexOfBone("j_hip_ri"), indexOfPelvis); - setParentIndexOfBone(getIndexOfBone("tag_stowed_hip_rear"), indexOfPelvis); - - const uint8_t torsoStabilizer = insertBone("torso_stabilizer", "pelvis"); - setParentIndexOfBone(getIndexOfBone("j_spinelower"), torsoStabilizer); - - const uint8_t backLow = insertBone("back_low", "j_spinelower"); - transferWeights(getIndexOfBone("j_spinelower"), backLow); - setParentIndexOfBone(getIndexOfBone("j_spineupper"), backLow); - - const uint8_t backMid = insertBone("back_mid", "j_spineupper"); - transferWeights(getIndexOfBone("j_spineupper"), backMid); - setParentIndexOfBone(getIndexOfBone("j_spine4"), backMid); - - - assert(root == getIndexOfBone("j_mainroot")); - assert(indexOfPelvis == getIndexOfBone("pelvis")); - assert(backLow == getIndexOfBone("back_low")); - assert(backMid == getIndexOfBone("back_mid")); - - // Fix up torso stabilizer - model->baseMat[torsoStabilizer].quat[0] = 0.F; - model->baseMat[torsoStabilizer].quat[1] = 0.F; - model->baseMat[torsoStabilizer].quat[2] = 0.F; - model->baseMat[torsoStabilizer].quat[3] = 1.F; - - const auto spineLowerM1 = getIndexOfBone("j_spinelower")-1; - - //model->trans[(torsoStabilizer-model->numRootBones) * 3 + 0] = 2.0f; - //model->trans[(torsoStabilizer-model->numRootBones) * 3 + 1] = -5.0f; - //model->trans[(torsoStabilizer-model->numRootBones) * 3 + 2] = 2.0F; - - model->trans[spineLowerM1 * 3 + 0] = 0.069828756; - model->trans[spineLowerM1 * 3 + 1] = -0.0f; - model->trans[spineLowerM1 * 3 + 2] = 5.2035017F; - - //// Euler -180.000 88.572 -90.000 - model->quats[(torsoStabilizer-model->numRootBones) * 4 + 0] = 16179; // 0.4952 - model->quats[(torsoStabilizer-model->numRootBones) * 4 + 1] = 16586; // 0.5077 - model->quats[(torsoStabilizer-model->numRootBones) * 4 + 2] = 16586; // 0.5077 - model->quats[(torsoStabilizer-model->numRootBones) * 4 + 3] = 16178; // 0.4952 - -#else - const uint8_t torsoStabilizer = insertBone("torso_stabilizer", "j_mainroot"); - transferWeights(getIndexOfBone("j_mainroot"), getIndexOfBone("torso_stabilizer")); - setParentIndexOfBone(getIndexOfBone("j_spinelower"), torsoStabilizer); -#endif - -#if DEBUG - const auto newRoot = getIndexOfBone("j_mainroot"); - assert(root == newRoot); -#endif - - printf(""); - } - } - printf(""); - } - + Assets::IXModel::ConvertPlayerModelFromSingleplayerToMultiplayer(model, allocator); printf(""); + // names.clear(); for (auto i = 1; i < model->numBones; i++) { const auto bone = model->boneNames[i]; - const auto name = Game::SL_ConvertToString(bone); - const auto m1 = i-1; + const auto boneName = Game::SL_ConvertToString(bone); + const auto m1 = i - 1; const auto fmt = std::format("{} => q({} {} {} {}) t({} {} {})", - name, - model->quats[m1 * 4 + 0], - model->quats[m1* 4 + 1], - model->quats[m1 * 4 + 2], - model->quats[m1 * 4 + 3], - model->trans[m1 * 3 + 0], - model->trans[m1* 3 +1], - model->trans[m1 * 3 +2] - ); + boneName, + model->quats[m1 * 4 + 0], + model->quats[m1 * 4 + 1], + model->quats[m1 * 4 + 2], + model->quats[m1 * 4 + 3], + model->trans[m1 * 3 + 0], + model->trans[m1 * 3 + 1], + model->trans[m1 * 3 + 2] + ); names.push_back( fmt ); + printf(""); } @@ -911,6 +444,7 @@ namespace Components bool AssetHandler::IsAssetEligible(Game::XAssetType type, Game::XAssetHeader* asset) { const char* name = Game::DB_GetXAssetNameHandlers[type](asset); + if (!name) return false; for (auto i = AssetHandler::EmptyAssets.begin(); i != AssetHandler::EmptyAssets.end();) From 8fdde66bdc5ab5fad996a62fddf28b5513f0feed Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sun, 28 Jan 2024 02:23:52 +0100 Subject: [PATCH 05/10] Zonebuilder improvements --- src/Components/Modules/AssetHandler.cpp | 95 +-------- .../Modules/AssetInterfaces/IXModel.cpp | 171 ++++++++++------ .../Modules/AssetInterfaces/IXModel.hpp | 3 + src/Components/Modules/QuickPatch.cpp | 3 - src/Components/Modules/ZoneBuilder.cpp | 193 +++++++++++++++--- src/Components/Modules/ZoneBuilder.hpp | 5 +- src/Game/Structs.hpp | 2 +- 7 files changed, 284 insertions(+), 188 deletions(-) diff --git a/src/Components/Modules/AssetHandler.cpp b/src/Components/Modules/AssetHandler.cpp index 5c17fc1a..3424c04a 100644 --- a/src/Components/Modules/AssetHandler.cpp +++ b/src/Components/Modules/AssetHandler.cpp @@ -242,54 +242,14 @@ namespace Components { auto str = Game::SL_FindString("j_spinelower"); int interestingPart = 0; - for (size_t i = 0; i < anim->boneCount[Game::XAnimPartType::PART_TYPE_ALL]; i++) { if (anim->names[i] == str) { interestingPart = i; - //anim->names[i] = Game::SL_FindString("torso_stabilizer"); } } // Something interesting to do there - //for (size_t frameIndex = 0; frameIndex < anim->numframes; frameIndex++) - //{ - // const auto delta = &anim->deltaPart[frameIndex]; - - // const auto frameCount = anim->numframes; - - // if (delta->trans->size) - // { - // const auto test = delta->trans->u.frames; - - // if (frameCount > 0xFF) - // { - // delta->trans->u.frames.indices._1 - // buffer->saveArray(delta->trans->u.frames.indices._2, delta->trans->size + 1); - // } - // else - // { - // buffer->saveArray(delta->trans->u.frames.indices._1, delta->trans->size + 1); - // } - - // if (delta->trans->u.frames.frames._1) - // { - // if (delta->trans->smallTrans) - // { - // buffer->save(delta->trans->u.frames.frames._1, 3, delta->trans->size + 1); - // } - // else - // { - // buffer->align(Utils::Stream::ALIGN_4); - // buffer->save(delta->trans->u.frames.frames._1, 6, delta->trans->size + 1); - // } - // } - // } - // else - // { - // buffer->save(delta->trans->u.frame0, 12); - // } - //} } } @@ -323,65 +283,14 @@ namespace Components // Unlikely candidate return; } - - // - static std::vector names{}; - names.clear(); - static Utils::Memory::Allocator allocator{}; - for (auto i = 0; i < model->numBones; i++) - { - const auto bone = model->boneNames[i]; - const auto boneName = Game::SL_ConvertToString(bone); - const auto str = std::format("{} => q({} {} {} {}) t({} {} {})", - boneName, - model->baseMat[i - 1].quat[0], - model->baseMat[i - 1].quat[1], - model->baseMat[i - 1].quat[2], - model->baseMat[i - 1].quat[3], - model->baseMat[i - 1].trans[0], - model->baseMat[i - 1].trans[1], - model->baseMat[i - 1].trans[2] - ); - - const auto duplicated = allocator.duplicateString(str); - names.push_back( - duplicated - ); - } // - - Assets::IXModel::ConvertPlayerModelFromSingleplayerToMultiplayer(model, allocator); - printf(""); - - - // - names.clear(); - for (auto i = 1; i < model->numBones; i++) + //if (name == "body_urban_civ_female_a"s) { - const auto bone = model->boneNames[i]; - const auto boneName = Game::SL_ConvertToString(bone); - const auto m1 = i - 1; - const auto fmt = std::format("{} => q({} {} {} {}) t({} {} {})", - boneName, - model->quats[m1 * 4 + 0], - model->quats[m1 * 4 + 1], - model->quats[m1 * 4 + 2], - model->quats[m1 * 4 + 3], - model->trans[m1 * 3 + 0], - model->trans[m1 * 3 + 1], - model->trans[m1 * 3 + 2] - ); - names.push_back( - fmt - ); - + Assets::IXModel::ConvertPlayerModelFromSingleplayerToMultiplayer(model, allocator); printf(""); } - - printf(""); - // } if (type == Game::ASSET_TYPE_MATERIAL && (name == "gfx_distortion_knife_trail" || name == "gfx_distortion_heat_far" || name == "gfx_distortion_ring_light" || name == "gfx_distortion_heat") && asset.material->info.sortKey >= 43) diff --git a/src/Components/Modules/AssetInterfaces/IXModel.cpp b/src/Components/Modules/AssetInterfaces/IXModel.cpp index ad4f76b0..74561206 100644 --- a/src/Components/Modules/AssetInterfaces/IXModel.cpp +++ b/src/Components/Modules/AssetInterfaces/IXModel.cpp @@ -296,6 +296,7 @@ namespace Assets std::string IXModel::GetParentOfBone(Game::XModel* model, uint8_t index) { + assert(index > 0); const auto parentIndex = GetParentIndexOfBone(model, index); const auto boneName = Game::SL_ConvertToString(model->boneNames[parentIndex]); return boneName; @@ -313,17 +314,17 @@ namespace Assets auto vertsBlendOffset = 0; - int rebuiltPartBits[6]{}; + int rebuiltPartBits[LENGTH]{}; std::unordered_set affectingBones{}; const auto registerBoneAffectingSurface = [&](unsigned int offset) { uint8_t index = static_cast(surface->vertInfo.vertsBlend[offset] / sizeof(Game::DObjSkelMat)); highestBoneIndex = std::max(highestBoneIndex, index); - }; + }; // 1 bone weight - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[0]; vertIndex++) + for (unsigned int vertIndex = 0; vertIndex < surface->vertInfo.vertCount[0]; vertIndex++) { registerBoneAffectingSurface(vertsBlendOffset + 0); @@ -331,7 +332,7 @@ namespace Assets } // 2 bone weights - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[1]; vertIndex++) + for (unsigned int vertIndex = 0; vertIndex < surface->vertInfo.vertCount[1]; vertIndex++) { registerBoneAffectingSurface(vertsBlendOffset + 0); registerBoneAffectingSurface(vertsBlendOffset + 1); @@ -340,7 +341,7 @@ namespace Assets } // 3 bone weights - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[2]; vertIndex++) + for (unsigned int vertIndex = 0; vertIndex < surface->vertInfo.vertCount[2]; vertIndex++) { registerBoneAffectingSurface(vertsBlendOffset + 0); registerBoneAffectingSurface(vertsBlendOffset + 1); @@ -350,7 +351,7 @@ namespace Assets } // 4 bone weights - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[3]; vertIndex++) + for (unsigned int vertIndex = 0; vertIndex < surface->vertInfo.vertCount[3]; vertIndex++) { registerBoneAffectingSurface(vertsBlendOffset + 0); registerBoneAffectingSurface(vertsBlendOffset + 1); @@ -360,7 +361,7 @@ namespace Assets vertsBlendOffset += 7; } - for (auto vertListIndex = 0; vertListIndex < surface->vertListCount; vertListIndex++) + for (unsigned int vertListIndex = 0; vertListIndex < surface->vertListCount; vertListIndex++) { highestBoneIndex = std::max(highestBoneIndex, static_cast(surface->vertList[vertListIndex].boneOffset / sizeof(Game::DObjSkelMat))); } @@ -395,7 +396,7 @@ namespace Assets assert(index < model->numBones); affectingBones.emplace(index); - }; + }; // 1 bone weight @@ -438,7 +439,7 @@ namespace Assets for (auto vertListIndex = 0; vertListIndex < surface->vertListCount; vertListIndex++) { - affectingBones.emplace(surface->vertList[vertListIndex].boneOffset / sizeof(Game::DObjSkelMat)); + affectingBones.emplace(static_cast(surface->vertList[vertListIndex].boneOffset / sizeof(Game::DObjSkelMat))); } // Actually rebuilding @@ -497,7 +498,7 @@ namespace Assets const uint8_t newBoneIndex = atPosition; const uint8_t newBoneIndexMinusRoot = atPosition - model->numRootBones; - Components::Logger::Print("Inserting bone {} at position {} (between {} and {})\n", boneName, atPosition, Game::SL_ConvertToString(model->boneNames[atPosition-1]), Game::SL_ConvertToString(model->boneNames[atPosition+1])); + Components::Logger::Print("Inserting bone {} at position {} (between {} and {})\n", boneName, atPosition, Game::SL_ConvertToString(model->boneNames[atPosition - 1]), Game::SL_ConvertToString(model->boneNames[atPosition + 1])); // Reallocate const auto newBoneNames = allocator.allocateArray(newBoneCount); @@ -549,7 +550,7 @@ namespace Assets // It's RELATIVE ! uint16_t quat[4]{}; - quat[3] = 32767; // 0 0 0 32767 + quat[3] = SHRT_MAX; // 0 0 0 1 float trans[3]{}; @@ -570,7 +571,7 @@ namespace Assets { std::memcpy(&newBoneNames[atPosition + 1], &model->boneNames[atPosition], sizeof(uint16_t) * lengthOfSecondPart); std::memcpy(&newMats[atPosition + 1], &model->baseMat[atPosition], sizeof(Game::DObjAnimMat) * lengthOfSecondPart); - std::memcpy(&newPartsClassification[atPosition+1], &model->partClassification[atPosition], lengthOfSecondPart); + std::memcpy(&newPartsClassification[atPosition + 1], &model->partClassification[atPosition], lengthOfSecondPart); std::memcpy(&newBoneInfo[atPosition + 1], &model->boneInfo[atPosition], sizeof(Game::XBoneInfo) * lengthOfSecondPart); std::memcpy(&newQuats[(atPositionM1 + 1) * 4], &model->quats[atPositionM1 * 4], sizeof(uint16_t) * 4 * lengthOfSecondPartM1); std::memcpy(&newTrans[(atPositionM1 + 1) * 3], &model->trans[atPositionM1 * 3], sizeof(float) * 3 * lengthOfSecondPartM1); @@ -635,7 +636,7 @@ namespace Assets surface->vertInfo.vertsBlend[offset] = static_cast(index * sizeof(Game::DObjSkelMat)); } - }; + }; // Fix bone offsets if (surface->vertList) @@ -739,8 +740,9 @@ namespace Assets void IXModel::TransferWeights(Game::XModel* model, const uint8_t origin, const uint8_t destination) { - // Does not work - return; + const auto from = Game::SL_ConvertToString(model->boneNames[origin]); + const auto to = Game::SL_ConvertToString(model->boneNames[destination]); + Components::Logger::Print("Transferring bone weights from {} to {}\n", from, to); const auto originalWeights = model->baseMat[origin].transWeight; model->baseMat[origin].transWeight = model->baseMat[destination].transWeight; @@ -750,6 +752,12 @@ namespace Assets { const auto lod = &model->lodInfo[i]; + if ((lod->partBits[5] & 0x1) == 0x1) + { + // surface lod already converted (more efficient) + continue; + } + for (int surfIndex = 0; surfIndex < lod->modelSurfs->numsurfs; surfIndex++) { auto vertsBlendOffset = 0u; @@ -760,12 +768,13 @@ namespace Assets int index = static_cast(surface->vertInfo.vertsBlend[offset] / sizeof(Game::DObjSkelMat)); if (index == origin) { - if (index < 0 || index >= model->numBones - 1) + index = destination; + + if (index < 0 || index >= model->numBones) { assert(false); } - index = destination; surface->vertInfo.vertsBlend[offset] = static_cast(index * sizeof(Game::DObjSkelMat)); } @@ -778,13 +787,14 @@ namespace Assets { const auto vertList = &surface->vertList[vertListIndex]; auto index = vertList->boneOffset / sizeof(Game::DObjSkelMat); - if (index < 0 || index >= model->numBones - 1) - { - assert(false); - } if (index == origin) { + if (index < 0 || index >= model->numBones) + { + assert(false); + } + index = destination; vertList->boneOffset = static_cast(index * sizeof(Game::DObjSkelMat)); } @@ -834,6 +844,46 @@ namespace Assets } }; + void IXModel::SetBoneTrans(Game::XModel* model, uint8_t boneIndex, bool baseMat, float x, float y, float z) + { + if (baseMat) + { + model->baseMat[boneIndex].trans[0] = x; + model->baseMat[boneIndex].trans[1] = y; + model->baseMat[boneIndex].trans[2] = z; + } + else + { + const auto index = boneIndex - model->numRootBones; + assert(index >= 0); + + model->trans[index * 3 + 0] = x; + model->trans[index * 3 + 1] = y; + model->trans[index * 3 + 2] = z; + } + } + + void IXModel::SetBoneQuaternion(Game::XModel* model, uint8_t boneIndex, bool baseMat, float x, float y, float z, float w) + { + if (baseMat) + { + model->baseMat[boneIndex].quat[0] = x; + model->baseMat[boneIndex].quat[1] = y; + model->baseMat[boneIndex].quat[2] = z; + model->baseMat[boneIndex].quat[3] = w; + } + else + { + const auto index = boneIndex - model->numRootBones; + assert(index >= 0); + + model->quats[index * 4 + 0] = static_cast(x * SHRT_MAX); + model->quats[index * 4 + 1] = static_cast(y * SHRT_MAX); + model->quats[index * 4 + 2] = static_cast(z * SHRT_MAX); + model->quats[index * 4 + 3] = static_cast(w * SHRT_MAX); + } + } + void IXModel::ConvertPlayerModelFromSingleplayerToMultiplayer(Game::XModel* model, Utils::Memory::Allocator& allocator) { @@ -866,24 +916,14 @@ namespace Assets const auto root = GetIndexOfBone(model, "j_mainroot"); if (root < UCHAR_MAX) { -#if true - //// Works - //InsertBone(model, "offsetron_the_great_offsetter_of_bones", "j_mainroot", allocator); - - //// Breaks the model - //InsertBone(model, "offsetron2_the_greater_offsetter_of_bones", "j_mainroot", allocator); - - //for (auto lodIndex = 0; lodIndex < model->numLods; lodIndex++) - //{ - // convertedSurfs.emplace(model->lodInfo[lodIndex].modelSurfs); - //} - - //RebuildPartBits(model); - - //return; - // Add pelvis +#if false + const uint8_t backLow = InsertBone(model, "back_low", "j_spinelower", allocator); + TransferWeights(model, GetIndexOfBone(model, "j_spinelower"), backLow); + SetParentIndexOfBone(model, GetIndexOfBone(model, "j_spineupper"), backLow); +#else const uint8_t indexOfPelvis = InsertBone(model, "pelvis", "j_mainroot", allocator); + SetBoneQuaternion(model, indexOfPelvis, true, -0.494f, -0.506f, -0.506f, 0.494); TransferWeights(model, root, indexOfPelvis); @@ -891,11 +931,23 @@ namespace Assets SetParentIndexOfBone(model, GetIndexOfBone(model, "j_hip_ri"), indexOfPelvis); SetParentIndexOfBone(model, GetIndexOfBone(model, "tag_stowed_hip_rear"), indexOfPelvis); + // These two are optional + if (GetIndexOfBone(model, "j_coatfront_le") == UCHAR_MAX) + { + InsertBone(model, "j_coatfront_le", "pelvis", allocator); + } + + if (GetIndexOfBone(model, "j_coatfront_ri") == UCHAR_MAX) + { + InsertBone(model, "j_coatfront_ri", "pelvis", allocator); + } + const uint8_t torsoStabilizer = InsertBone(model, "torso_stabilizer", "pelvis", allocator); - SetParentIndexOfBone(model, GetIndexOfBone(model, "j_spinelower"), torsoStabilizer); + const uint8_t lowerSpine = GetIndexOfBone(model, "j_spinelower"); + SetParentIndexOfBone(model, lowerSpine, torsoStabilizer); const uint8_t backLow = InsertBone(model, "back_low", "j_spinelower", allocator); - TransferWeights(model, GetIndexOfBone(model, "j_spinelower"), backLow); + TransferWeights(model, lowerSpine, backLow); SetParentIndexOfBone(model, GetIndexOfBone(model, "j_spineupper"), backLow); const uint8_t backMid = InsertBone(model, "back_mid", "j_spineupper", allocator); @@ -908,29 +960,34 @@ namespace Assets assert(backLow == GetIndexOfBone(model, "back_low")); assert(backMid == GetIndexOfBone(model, "back_mid")); - // Fix up torso stabilizer - model->baseMat[torsoStabilizer].quat[0] = 0.F; - model->baseMat[torsoStabilizer].quat[1] = 0.F; - model->baseMat[torsoStabilizer].quat[2] = 0.F; - model->baseMat[torsoStabilizer].quat[3] = 1.F; + // Twister bone + SetBoneQuaternion(model, lowerSpine, false, -0.492f, -0.507f, -0.507f, 0.492f); + SetBoneQuaternion(model, torsoStabilizer, false, 0.494f, 0.506f, 0.506f, 0.494f); - const auto spineLowerM1 = GetIndexOfBone(model, "j_spinelower") - model->numRootBones; // This doesn't feel like it should be necessary - model->trans[spineLowerM1 * 3 + 0] = 0.069828756; - model->trans[spineLowerM1 * 3 + 1] = -0.0f; - model->trans[spineLowerM1 * 3 + 2] = 5.2035017F; + // It is, on singleplayer models unfortunately. Could we add an extra bone to compensate this? + // Or compensate it another way? + SetBoneTrans(model, GetIndexOfBone(model, "j_spinelower"), false, 0.07, 0.0f, 5.2f); - //// Euler -180.000 88.572 -90.000 - model->quats[(torsoStabilizer - model->numRootBones) * 4 + 0] = 16179; // 0.4952 - model->quats[(torsoStabilizer - model->numRootBones) * 4 + 1] = 16586; // 0.5077 - model->quats[(torsoStabilizer - model->numRootBones) * 4 + 2] = 16586; // 0.5077 - model->quats[(torsoStabilizer - model->numRootBones) * 4 + 3] = 16178; // 0.4952 + // These are often messed up on civilian models, but there is no obvious way to tell from code + const auto stowedBack = GetIndexOfBone(model, "tag_stowed_back"); + if (stowedBack != UCHAR_MAX) + { + SetBoneTrans(model, stowedBack, false, -0.32f, -6.27f, -2.65F); + SetBoneQuaternion(model, stowedBack, false, -0.044, 0.088, -0.995, 0.025); + SetBoneTrans(model, stowedBack, true, -9.571f, -2.654f, 51.738f); + SetBoneQuaternion(model, stowedBack, true, -0.071f, 0.0f, -0.997f, 0.0f); + } -#else - const uint8_t torsoStabilizer = insertBone("torso_stabilizer", "j_mainroot"); - transferWeights(getIndexOfBone("j_mainroot"), getIndexOfBone("torso_stabilizer")); - setParentIndexOfBone(getIndexOfBone("j_spinelower"), torsoStabilizer); + if (stowedBack != UCHAR_MAX) + { + const auto stowedRear = GetIndexOfBone(model, "tag_stowed_hip_rear"); + SetBoneTrans(model, stowedRear, false, -0.75f, -6.45f, -4.99f); + SetBoneQuaternion(model, stowedRear, false, -0.553f, -0.062f, -0.049f, 0.830f); + SetBoneTrans(model, stowedBack, true, -9.866f, -4.989f, 36.315f); + SetBoneQuaternion(model, stowedRear, true, -0.054, -0.025f, -0.975f, 0.214f); + } #endif RebuildPartBits(model); diff --git a/src/Components/Modules/AssetInterfaces/IXModel.hpp b/src/Components/Modules/AssetInterfaces/IXModel.hpp index 13a58b32..df4c54e4 100644 --- a/src/Components/Modules/AssetInterfaces/IXModel.hpp +++ b/src/Components/Modules/AssetInterfaces/IXModel.hpp @@ -22,5 +22,8 @@ namespace Assets static void RebuildPartBits(Game::XModel* model); static uint8_t InsertBone(Game::XModel* model, const std::string& boneName, const std::string& parentName, Utils::Memory::Allocator& allocator); static void TransferWeights(Game::XModel* model, const uint8_t origin, const uint8_t destination); + + static void SetBoneTrans(Game::XModel* model, uint8_t boneIndex, bool baseMat, float x, float y, float z); + static void SetBoneQuaternion(Game::XModel* model, uint8_t boneIndex, bool baseMat, float x, float y, float z, float w); }; } diff --git a/src/Components/Modules/QuickPatch.cpp b/src/Components/Modules/QuickPatch.cpp index 8eeb44e7..eafc0b47 100644 --- a/src/Components/Modules/QuickPatch.cpp +++ b/src/Components/Modules/QuickPatch.cpp @@ -314,9 +314,6 @@ namespace Components QuickPatch::QuickPatch() { - // Do not call DObjCalcAnim - Utils::Hook(0x6508C4, DObjCalcAnim, HOOK_CALL).install()->quick(); - Utils::Hook(0x06508F6, DObjCalcAnim, HOOK_CALL).install()->quick(); // Filtering any mapents that is intended for Spec:Ops gamemode (CODO) and prevent them from spawning diff --git a/src/Components/Modules/ZoneBuilder.cpp b/src/Components/Modules/ZoneBuilder.cpp index c2e39cab..56314b4e 100644 --- a/src/Components/Modules/ZoneBuilder.cpp +++ b/src/Components/Modules/ZoneBuilder.cpp @@ -7,6 +7,7 @@ #include #include "AssetInterfaces/ILocalizeEntry.hpp" +#include "Branding.hpp" namespace Components { @@ -21,7 +22,8 @@ namespace Components iw4of::api ZoneBuilder::ExporterAPI(GetExporterAPIParams()); std::string ZoneBuilder::DumpingZone{}; - ZoneBuilder::Zone::Zone(const std::string& name) : indexStart(0), externalSize(0), + ZoneBuilder::Zone::Zone(const std::string& name, const std::string& sourceName, const std::string& destination) : + indexStart(0), externalSize(0), // Reserve 100MB by default. // That's totally fine, as the dedi doesn't load images and therefore doesn't need much memory. // That way we can be sure it won't need to reallocate memory. @@ -29,11 +31,17 @@ namespace Components // Well, decompressed maps can get way larger than 100MB, so let's increase that. buffer(0xC800000), zoneName(name), - dataMap("zone_source/" + name + ".csv"), + destination(destination), + dataMap("zone_source/" + sourceName + ".csv"), branding{ nullptr }, assetDepth(0), iw4ofApi(getIW4OfApiParams()) { + + } + + ZoneBuilder::Zone::Zone(const std::string& name) : ZoneBuilder::Zone::Zone(name, name, std::format("zone/english/{}.ff", name)) + { } ZoneBuilder::Zone::~Zone() @@ -148,7 +156,7 @@ namespace Components { Game::XZoneInfo info; info.name = fastfile.data(); - info.allocFlags = 0x20; + info.allocFlags = Game::DB_ZONE_LOAD; info.freeFlags = 0; Game::DB_LoadXAssets(&info, 1, true); @@ -449,11 +457,11 @@ namespace Components zoneBuffer = Utils::Compression::ZLib::Compress(zoneBuffer); outBuffer.append(zoneBuffer); - std::string outFile = "zone/" + this->zoneName + ".ff"; - Utils::IO::WriteFile(outFile, outBuffer); - Logger::Print("done.\n"); - Logger::Print("Zone '{}' written with {} assets and {} script strings\n", outFile, (this->aliasList.size() + this->loadedAssets.size()), this->scriptStrings.size()); + Utils::IO::WriteFile(destination, outBuffer); + + Logger::Print("done writing {}\n", destination); + Logger::Print("Zone '{}' written with {} assets and {} script strings\n", destination, (this->aliasList.size() + this->loadedAssets.size()), this->scriptStrings.size()); } void ZoneBuilder::Zone::saveData() @@ -759,18 +767,23 @@ namespace Components return header; } - void ZoneBuilder::RefreshExporterWorkDirectory() + std::string ZoneBuilder::GetDumpingZonePath() { if (ZoneBuilder::DumpingZone.empty()) { - ExporterAPI.set_work_path(std::format("userraw/dump/stray")); + return std::format("userraw/dump/stray"); } else { - ExporterAPI.set_work_path(std::format("userraw/dump/{}", ZoneBuilder::DumpingZone)); + return std::format("userraw/dump/{}", ZoneBuilder::DumpingZone); } } + void ZoneBuilder::RefreshExporterWorkDirectory() + { + ExporterAPI.set_work_path(GetDumpingZonePath()); + } + iw4of::api* ZoneBuilder::GetExporter() { return &ExporterAPI; @@ -988,10 +1001,10 @@ namespace Components Logger::Print(" --------------------------------------------------------------------------------\n"); Logger::Print(" IW4x ZoneBuilder - {}\n", REVISION_STR); Logger::Print(" Commands:\n"); - Logger::Print("\t-buildzone [zone]: builds a zone from a csv located in zone_source\n"); - Logger::Print("\t-buildall: builds all zones in zone_source\n"); + Logger::Print("\t-buildmod [mod name]: Build a mod.ff from the source located in zone_source/mod_name.csv\n"); + Logger::Print("\t-buildzone [zone]: Builds a zone from a csv located in zone_source\n"); + Logger::Print("\t-dumpzone [zone]: Loads and dump the specified zone in userraw/dump\n"); Logger::Print("\t-verifyzone [zone]: loads and verifies the specified zone\n"); - Logger::Print("\t-dumpzone [zone]: loads and dump the specified zone\n"); Logger::Print("\t-listassets [assettype]: lists all loaded assets of the specified type\n"); Logger::Print("\t-quit: quits the program\n"); Logger::Print(" --------------------------------------------------------------------------------\n"); @@ -1176,7 +1189,7 @@ namespace Components Game::DB_LoadXAssets(&info, 1, true); AssetHandler::FindOriginalAsset(Game::XAssetType::ASSET_TYPE_RAWFILE, "default"); // Lock until zone is unloaded - }; + }; } ZoneBuilder::ZoneBuilder() @@ -1288,43 +1301,139 @@ namespace Components Utils::Hook::Nop(0x5BC791, 5); AssetHandler::OnLoad([](Game::XAssetType type, Game::XAssetHeader /* asset*/, const std::string& name, bool* /*restrict*/) - { - if (!ZoneBuilder::TraceZone.empty() && ZoneBuilder::TraceZone == FastFiles::Current()) { - ZoneBuilder::TraceAssets.emplace_back(std::make_pair(type, name)); - } - }); + if (!ZoneBuilder::TraceZone.empty() && ZoneBuilder::TraceZone == FastFiles::Current()) + { + ZoneBuilder::TraceAssets.emplace_back(std::make_pair(type, name)); + } + }); Command::Add("dumpzone", [](const Command::Params* params) { if (params->size() < 2) return; - + std::string zone = params->get(1); ZoneBuilder::DumpingZone = zone; ZoneBuilder::RefreshExporterWorkDirectory(); std::vector> assets{}; - const auto unload = ZoneBuilder::LoadZoneWithTrace(zone, assets); + const auto unload = ZoneBuilder::LoadZoneWithTrace(zone, assets); Logger::Print("Dumping zone '{}'...\n", zone); - for (const auto& asset : assets) { - const auto type = asset.first; - const auto name = asset.second; - if (ExporterAPI.is_type_supported(type) && name[0] != ',') + Utils::IO::CreateDir(GetDumpingZonePath()); + std::ofstream csv(std::filesystem::path(GetDumpingZonePath()) / std::format("{}.csv", zone)); + csv + << std::format("### Zone '{}' dumped with Zonebuilder {}", zone, Components::Branding::GetVersionString()) + << "\n\n"; + + constexpr Game::XAssetType typeOrder[] = { + Game::XAssetType::ASSET_TYPE_GAMEWORLD_MP, + Game::XAssetType::ASSET_TYPE_GAMEWORLD_SP, + Game::XAssetType::ASSET_TYPE_GFXWORLD, + Game::XAssetType::ASSET_TYPE_COMWORLD, + Game::XAssetType::ASSET_TYPE_FXWORLD, + Game::XAssetType::ASSET_TYPE_CLIPMAP_MP, + Game::XAssetType::ASSET_TYPE_CLIPMAP_SP, + Game::XAssetType::ASSET_TYPE_RAWFILE, + Game::XAssetType::ASSET_TYPE_VEHICLE, + Game::XAssetType::ASSET_TYPE_WEAPON, + Game::XAssetType::ASSET_TYPE_FX, + Game::XAssetType::ASSET_TYPE_TRACER, + Game::XAssetType::ASSET_TYPE_XMODEL, + Game::XAssetType::ASSET_TYPE_MATERIAL, + Game::XAssetType::ASSET_TYPE_TECHNIQUE_SET, + Game::XAssetType::ASSET_TYPE_PIXELSHADER, + Game::XAssetType::ASSET_TYPE_VERTEXSHADER, + Game::XAssetType::ASSET_TYPE_VERTEXDECL, + Game::XAssetType::ASSET_TYPE_IMAGE, + Game::XAssetType::ASSET_TYPE_SOUND, + Game::XAssetType::ASSET_TYPE_LOADED_SOUND, + Game::XAssetType::ASSET_TYPE_SOUND_CURVE, + Game::XAssetType::ASSET_TYPE_PHYSPRESET, + }; + + std::unordered_map typePriority{}; + for (auto i = 0; i < ARRAYSIZE(typeOrder); i++) { - const auto assetHeader = Game::DB_FindXAssetHeader(type, name.data()); - if (assetHeader.data) + const auto type = typeOrder[i]; + typePriority.emplace(type, 1 + ARRAYSIZE(typeOrder) - i); + } + + std::map> invalidAssets{}; + + std::sort(assets.begin(), assets.end(), [&]( + const std::pair& a, + const std::pair& b + ) { + if (a.first == b.first) + { + + return a.second.compare(b.second) < 0; + } + else + { + const auto priorityA = typePriority[a.first]; + const auto priorityB = typePriority[b.first]; + + if (priorityA == priorityB) + { + return a.second.compare(b.second) < 0; + } + else + { + return priorityB < priorityA; + } + } + }); + + // Used to format the CSV + Game::XAssetType lastTypeEncountered{}; + + for (const auto& asset : assets) + { + const auto type = asset.first; + const auto name = asset.second; + if (ExporterAPI.is_type_supported(type) && name[0] != ',') { - ExporterAPI.write(type, assetHeader.data); + const auto assetHeader = Game::DB_FindXAssetHeader(type, name.data()); + if (assetHeader.data) + { + ExporterAPI.write(type, assetHeader.data); + const auto typeName = Game::DB_GetXAssetTypeName(type) ; + + if (type != lastTypeEncountered) + { + csv << "\n### " << typeName << "\n"; + lastTypeEncountered = type; + } + + csv << typeName << "," << name << "\n"; + } + else + { + Logger::Warning(Game::conChannel_t::CON_CHANNEL_ERROR, "Asset {} has disappeared while dumping!\n", name); + invalidAssets["The following assets disappeared while dumping"].push_back(std::format("{},{}", Game::DB_GetXAssetTypeName(type), name)); + } } else { - Logger::Warning(Game::conChannel_t::CON_CHANNEL_ERROR, "Asset {} has disappeared while dumping!\n", name); + invalidAssets["The following assets are unsupported or not dumped as individual assets, but still present in the zone"].push_back(std::format("{},{}", Game::DB_GetXAssetTypeName(type), name)); } } + + for (const auto& kv : invalidAssets) + { + csv << "\n### " << kv.first << "\n"; + for (const auto& line : kv.second) + { + csv << "#" << line << "\n"; + } + } + + csv << std::format("\n### {} assets", assets.size()) << "\n"; } unload(); @@ -1339,7 +1448,7 @@ namespace Components std::string zone = params->get(1); std::vector> assets{}; - const auto unload = ZoneBuilder::LoadZoneWithTrace(zone, assets); + const auto unload = ZoneBuilder::LoadZoneWithTrace(zone, assets); int count = 0; for (auto i = assets.begin(); i != assets.end(); ++i, ++count) @@ -1361,6 +1470,24 @@ namespace Components Zone(zoneName).build(); }); + Command::Add("buildmod", [](const Command::Params* params) + { + if (params->size() < 2) return; + + std::string modName = params->get(1); + Logger::Print("Building zone for mod '{}'...\n", modName); + + const std::string previousFsGame = Dvar::Var("fs_game").get(); + const std::string dir = "mods/" + modName; + Utils::IO::CreateDir(dir); + + Dvar::Var("fs_game").set(dir); + + Zone("mod", modName, dir + "/mod.ff").build(); + + Dvar::Var("fs_game").set(previousFsGame); + }); + Command::Add("buildall", []() { auto path = std::format("{}\\zone_source", (*Game::fs_basepath)->current.string); @@ -1414,8 +1541,8 @@ namespace Components #endif } } - } - }); + } + }); Command::Add("listassets", [](const Command::Params* params) { @@ -1431,7 +1558,7 @@ namespace Components }, &type, false); } }); - } +} } ZoneBuilder::~ZoneBuilder() diff --git a/src/Components/Modules/ZoneBuilder.hpp b/src/Components/Modules/ZoneBuilder.hpp index 4f4a4427..bc3affb5 100644 --- a/src/Components/Modules/ZoneBuilder.hpp +++ b/src/Components/Modules/ZoneBuilder.hpp @@ -32,7 +32,8 @@ namespace Components private: Zone* builder; }; - + + Zone(const std::string& zoneName, const std::string& sourceName, const std::string& destination); Zone(const std::string& zoneName); ~Zone(); @@ -100,6 +101,7 @@ namespace Components iw4of::api iw4ofApi; std::string zoneName; + std::string destination; Utils::CSV dataMap; Utils::Memory::Allocator memAllocator; @@ -134,6 +136,7 @@ namespace Components static std::vector> EndAssetTrace(); static Game::XAssetHeader GetEmptyAssetIfCommon(Game::XAssetType type, const std::string& name, Zone* builder); + static std::string GetDumpingZonePath(); static void RefreshExporterWorkDirectory(); static iw4of::api* GetExporter(); diff --git a/src/Game/Structs.hpp b/src/Game/Structs.hpp index 4b2b0c2a..67aea351 100644 --- a/src/Game/Structs.hpp +++ b/src/Game/Structs.hpp @@ -960,7 +960,7 @@ namespace Game union XAnimDynamicFrames { - char(*_1)[3]; + uint8_t(*_1)[3]; unsigned __int16(*_2)[3]; }; From adddbd5ecc2522c6aebad35b608ebc2654a80323 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sun, 28 Jan 2024 14:05:06 +0100 Subject: [PATCH 06/10] Bump IW4OF --- deps/iw4-open-formats | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/iw4-open-formats b/deps/iw4-open-formats index f174f1c4..4f7041b9 160000 --- a/deps/iw4-open-formats +++ b/deps/iw4-open-formats @@ -1 +1 @@ -Subproject commit f174f1c4c4c465c337166b92e03b355695c5bc93 +Subproject commit 4f7041b9f259d09bc6ca06c3bc182a66c961f2e4 From 4a61617e88b0148c321f6ba5b262eee380a8b759 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sun, 28 Jan 2024 16:26:24 +0100 Subject: [PATCH 07/10] Cleanup & store extra part bit in msurf instead of lodinfo --- src/Components/Loader.cpp | 2 +- src/Components/Modules/AssetHandler.cpp | 60 +--- .../Modules/AssetInterfaces/IXModel.cpp | 293 +++++++++--------- src/Components/Modules/QuickPatch.cpp | 2 - src/Components/Modules/ZoneBuilder.cpp | 18 +- src/Components/Modules/ZoneBuilder.hpp | 2 + src/Game/Structs.hpp | 2 +- 7 files changed, 164 insertions(+), 215 deletions(-) diff --git a/src/Components/Loader.cpp b/src/Components/Loader.cpp index aa3ce5d6..f50b2689 100644 --- a/src/Components/Loader.cpp +++ b/src/Components/Loader.cpp @@ -173,7 +173,7 @@ namespace Components Register(new Threading()); Register(new Toast()); Register(new UIFeeder()); - //Register(new Updater()); + Register(new Updater()); Register(new VisionFile()); Register(new Voice()); Register(new Vote()); diff --git a/src/Components/Modules/AssetHandler.cpp b/src/Components/Modules/AssetHandler.cpp index 3424c04a..6d9e0a6c 100644 --- a/src/Components/Modules/AssetHandler.cpp +++ b/src/Components/Modules/AssetHandler.cpp @@ -221,7 +221,7 @@ namespace Components retn } } -#pragma optimize( "", off ) + void AssetHandler::ModifyAsset(Game::XAssetType type, Game::XAssetHeader asset, const std::string& name) { #ifdef DEBUG @@ -235,63 +235,6 @@ namespace Components } } #endif - if (type == Game::XAssetType::ASSET_TYPE_XANIMPARTS) - { - auto anim = asset.parts; - if (anim->name == "unarmed_cowerstand_idle"s || anim->name == "civilian_walk_cool"s || anim->name == "civilian_run_upright"s) - { - auto str = Game::SL_FindString("j_spinelower"); - int interestingPart = 0; - { - if (anim->names[i] == str) - { - interestingPart = i; - } - } - - // Something interesting to do there - } - } - - - if (type == Game::XAssetType::ASSET_TYPE_XMODEL) - //if (name == "body_urban_civ_female_a"s || name == "mp_body_opforce_arab_assault_a"s) - { - - if (name == "body_urban_civ_male_aa"s) - { - printf(""); - } - - const auto model = asset.model; - - int total = - model->noScalePartBits[0] | - model->noScalePartBits[1] | - model->noScalePartBits[2] | - model->noScalePartBits[3] | - model->noScalePartBits[4] | - model->noScalePartBits[5]; - - if (total != 0) - { - printf(""); - } - - if (!model->quats || !model->trans || model->numBones < 10) - { - // Unlikely candidate - return; - } - static Utils::Memory::Allocator allocator{}; - - // - //if (name == "body_urban_civ_female_a"s) - { - Assets::IXModel::ConvertPlayerModelFromSingleplayerToMultiplayer(model, allocator); - printf(""); - } - } if (type == Game::ASSET_TYPE_MATERIAL && (name == "gfx_distortion_knife_trail" || name == "gfx_distortion_heat_far" || name == "gfx_distortion_ring_light" || name == "gfx_distortion_heat") && asset.material->info.sortKey >= 43) { @@ -348,7 +291,6 @@ namespace Components asset.gfxWorld->sortKeyDistortion = 43; } } -#pragma optimize( "", on ) bool AssetHandler::IsAssetEligible(Game::XAssetType type, Game::XAssetHeader* asset) { diff --git a/src/Components/Modules/AssetInterfaces/IXModel.cpp b/src/Components/Modules/AssetInterfaces/IXModel.cpp index 74561206..d5667578 100644 --- a/src/Components/Modules/AssetInterfaces/IXModel.cpp +++ b/src/Components/Modules/AssetInterfaces/IXModel.cpp @@ -11,8 +11,19 @@ namespace Assets { header->model = builder->getIW4OfApi()->read(Game::XAssetType::ASSET_TYPE_XMODEL, name); + if (!header->model) + { + // In that case if we want to convert it later potentially we have to grab it now: + header->model = Game::DB_FindXAssetHeader(Game::XAssetType::ASSET_TYPE_XMODEL, name.data()).model; + } + if (header->model) { + if (Components::ZoneBuilder::zb_sp_to_mp.get()) + { + Assets::IXModel::ConvertPlayerModelFromSingleplayerToMultiplayer(header->model, *builder->getAllocator()); + } + // ??? if (header->model->physCollmap) { @@ -305,16 +316,13 @@ namespace Assets uint8_t IXModel::GetHighestAffectingBoneIndex(const Game::XModelLodInfo* lod) { uint8_t highestBoneIndex = 0; - constexpr auto LENGTH = 6; { for (auto surfIndex = 0; surfIndex < lod->numsurfs; surfIndex++) { const auto surface = &lod->surfs[surfIndex]; - auto vertsBlendOffset = 0; - int rebuiltPartBits[LENGTH]{}; std::unordered_set affectingBones{}; const auto registerBoneAffectingSurface = [&](unsigned int offset) { @@ -380,7 +388,7 @@ namespace Assets const auto lod = &model->lodInfo[i]; int lodPartBits[6]{}; - for (auto surfIndex = 0; surfIndex < lod->numsurfs; surfIndex++) + for (unsigned short surfIndex = 0; surfIndex < lod->numsurfs; surfIndex++) { const auto surface = &lod->surfs[surfIndex]; @@ -400,7 +408,7 @@ namespace Assets // 1 bone weight - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[0]; vertIndex++) + for (unsigned int vertIndex = 0; vertIndex < surface->vertInfo.vertCount[0]; vertIndex++) { registerBoneAffectingSurface(vertsBlendOffset + 0); @@ -408,7 +416,7 @@ namespace Assets } // 2 bone weights - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[1]; vertIndex++) + for (unsigned int vertIndex = 0; vertIndex < surface->vertInfo.vertCount[1]; vertIndex++) { registerBoneAffectingSurface(vertsBlendOffset + 0); registerBoneAffectingSurface(vertsBlendOffset + 1); @@ -417,7 +425,7 @@ namespace Assets } // 3 bone weights - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[2]; vertIndex++) + for (unsigned int vertIndex = 0; vertIndex < surface->vertInfo.vertCount[2]; vertIndex++) { registerBoneAffectingSurface(vertsBlendOffset + 0); registerBoneAffectingSurface(vertsBlendOffset + 1); @@ -427,7 +435,7 @@ namespace Assets } // 4 bone weights - for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[3]; vertIndex++) + for (unsigned int vertIndex = 0; vertIndex < surface->vertInfo.vertCount[3]; vertIndex++) { registerBoneAffectingSurface(vertsBlendOffset + 0); registerBoneAffectingSurface(vertsBlendOffset + 1); @@ -437,7 +445,7 @@ namespace Assets vertsBlendOffset += 7; } - for (auto vertListIndex = 0; vertListIndex < surface->vertListCount; vertListIndex++) + for (unsigned int vertListIndex = 0; vertListIndex < surface->vertListCount; vertListIndex++) { affectingBones.emplace(static_cast(surface->vertList[vertListIndex].boneOffset / sizeof(Game::DObjSkelMat))); } @@ -481,7 +489,7 @@ namespace Assets // Start with backing up parent links that we will have to restore // We'll restore them at the end std::map parentsToRestore{}; - for (int i = model->numRootBones; i < model->numBones; i++) + for (uint8_t i = model->numRootBones; i < model->numBones; i++) { parentsToRestore[Game::SL_ConvertToString(model->boneNames[i])] = GetParentOfBone(model, i); } @@ -498,8 +506,6 @@ namespace Assets const uint8_t newBoneIndex = atPosition; const uint8_t newBoneIndexMinusRoot = atPosition - model->numRootBones; - Components::Logger::Print("Inserting bone {} at position {} (between {} and {})\n", boneName, atPosition, Game::SL_ConvertToString(model->boneNames[atPosition - 1]), Game::SL_ConvertToString(model->boneNames[atPosition + 1])); - // Reallocate const auto newBoneNames = allocator.allocateArray(newBoneCount); const auto newMats = allocator.allocateArray(newBoneCount); @@ -518,8 +524,8 @@ namespace Assets const uint8_t atPositionM1 = atPosition - model->numRootBones; // should be equal to model->numBones - int total = lengthOfFirstPart + lengthOfSecondPart; - assert(total = model->numBones); + unsigned int total = lengthOfFirstPart + lengthOfSecondPart; + assert(total == model->numBones); // should be equal to model->numBones - model->numRootBones int totalM1 = lengthOfFirstPartM1 + lengthOfSecondPartM1; @@ -599,9 +605,10 @@ namespace Assets { const auto lod = &model->lodInfo[lodIndex]; - if ((lod->partBits[5] & 0x1) == 0x1) + if ((lod->modelSurfs->partBits[5] & 0x1) == 0x1) { // surface lod already converted (more efficient) + std::memcpy(lod->partBits, lod->modelSurfs->partBits, 6 * sizeof(uint32_t)); continue; } @@ -630,7 +637,7 @@ namespace Assets if (index < 0 || index >= model->numBones) { - Components::Logger::Print("Unexpected 'bone index' {} out of {} bones while working vertex blend of model {} lod {} surf {}\n", index, model->numBones, model->name, lodIndex, surfIndex); + Components::Logger::Print("Unexpected 'bone index' {} out of {} bones while working vertex blend of: xmodel {} lod {} xmodelsurf {} surf #{}", index, model->numBones, model->name, lodIndex, lod->modelSurfs->name, lodIndex, surfIndex); assert(false); } @@ -652,7 +659,7 @@ namespace Assets if (index < 0 || index >= model->numBones) { - Components::Logger::Print("Unexpected 'bone index' {} out of {} bones while working list blend of model {} lod {} surf {}\n", index, model->numBones, model->name, lodIndex, surfIndex); + Components::Logger::Print("Unexpected 'bone index' {} out of {} bones while working vertex list of: xmodel {} lod {} xmodelsurf {} surf #{}\n", index, model->numBones, model->name, lodIndex, lod->modelSurfs->name, surfIndex); assert(false); } @@ -711,39 +718,17 @@ namespace Assets const auto key = kv.first; const auto beforeVal = kv.second; - const auto parentIndex = GetIndexOfBone(model, beforeVal); + const auto p = GetIndexOfBone(model, beforeVal); const auto index = GetIndexOfBone(model, key); - SetParentIndexOfBone(model, index, parentIndex); + SetParentIndexOfBone(model, index, p); } -#if DEBUG - // check - for (const auto& kv : parentsToRestore) - { - const auto key = kv.first; - const auto beforeVal = kv.second; - - const auto index = GetIndexOfBone(model, key); - const auto afterVal = GetParentOfBone(model, index); - - if (beforeVal != afterVal) - { - printf(""); - } - } - // -#endif - return atPosition; // Bone index of added bone }; void IXModel::TransferWeights(Game::XModel* model, const uint8_t origin, const uint8_t destination) { - const auto from = Game::SL_ConvertToString(model->boneNames[origin]); - const auto to = Game::SL_ConvertToString(model->boneNames[destination]); - Components::Logger::Print("Transferring bone weights from {} to {}\n", from, to); - const auto originalWeights = model->baseMat[origin].transWeight; model->baseMat[origin].transWeight = model->baseMat[destination].transWeight; model->baseMat[destination].transWeight = originalWeights; @@ -887,114 +872,130 @@ namespace Assets void IXModel::ConvertPlayerModelFromSingleplayerToMultiplayer(Game::XModel* model, Utils::Memory::Allocator& allocator) { - auto indexOfSpine = GetIndexOfBone(model, "j_spinelower"); - if (indexOfSpine < UCHAR_MAX) // It has a spine so it must be some sort of humanoid + if (model->name == "body_airport_com_a"s) { - const auto nameOfParent = GetParentOfBone(model, indexOfSpine); - - if (GetIndexOfBone(model, "torso_stabilizer") == UCHAR_MAX) // Singleplayer model is likely - { - - Components::Logger::Print("Converting {}\n", model->name); - - // No stabilizer - let's do surgery - // We're trying to get there: - // tag_origin - // j_main_root - // pelvis - // j_hip_le - // j_hip_ri - // tag_stowed_hip_rear - // torso_stabilizer - // j_spinelower - // back_low - // j_spineupper - // back_mid - // j_spine4 - - - const auto root = GetIndexOfBone(model, "j_mainroot"); - if (root < UCHAR_MAX) { - - // Add pelvis -#if false - const uint8_t backLow = InsertBone(model, "back_low", "j_spinelower", allocator); - TransferWeights(model, GetIndexOfBone(model, "j_spinelower"), backLow); - SetParentIndexOfBone(model, GetIndexOfBone(model, "j_spineupper"), backLow); -#else - const uint8_t indexOfPelvis = InsertBone(model, "pelvis", "j_mainroot", allocator); - SetBoneQuaternion(model, indexOfPelvis, true, -0.494f, -0.506f, -0.506f, 0.494); - - TransferWeights(model, root, indexOfPelvis); - - SetParentIndexOfBone(model, GetIndexOfBone(model, "j_hip_le"), indexOfPelvis); - SetParentIndexOfBone(model, GetIndexOfBone(model, "j_hip_ri"), indexOfPelvis); - SetParentIndexOfBone(model, GetIndexOfBone(model, "tag_stowed_hip_rear"), indexOfPelvis); - - // These two are optional - if (GetIndexOfBone(model, "j_coatfront_le") == UCHAR_MAX) - { - InsertBone(model, "j_coatfront_le", "pelvis", allocator); - } - - if (GetIndexOfBone(model, "j_coatfront_ri") == UCHAR_MAX) - { - InsertBone(model, "j_coatfront_ri", "pelvis", allocator); - } - - const uint8_t torsoStabilizer = InsertBone(model, "torso_stabilizer", "pelvis", allocator); - const uint8_t lowerSpine = GetIndexOfBone(model, "j_spinelower"); - SetParentIndexOfBone(model, lowerSpine, torsoStabilizer); - - const uint8_t backLow = InsertBone(model, "back_low", "j_spinelower", allocator); - TransferWeights(model, lowerSpine, backLow); - SetParentIndexOfBone(model, GetIndexOfBone(model, "j_spineupper"), backLow); - - const uint8_t backMid = InsertBone(model, "back_mid", "j_spineupper", allocator); - TransferWeights(model, GetIndexOfBone(model, "j_spineupper"), backMid); - SetParentIndexOfBone(model, GetIndexOfBone(model, "j_spine4"), backMid); - - - assert(root == GetIndexOfBone(model, "j_mainroot")); - assert(indexOfPelvis == GetIndexOfBone(model, "pelvis")); - assert(backLow == GetIndexOfBone(model, "back_low")); - assert(backMid == GetIndexOfBone(model, "back_mid")); - - // Twister bone - SetBoneQuaternion(model, lowerSpine, false, -0.492f, -0.507f, -0.507f, 0.492f); - SetBoneQuaternion(model, torsoStabilizer, false, 0.494f, 0.506f, 0.506f, 0.494f); - - - // This doesn't feel like it should be necessary - // It is, on singleplayer models unfortunately. Could we add an extra bone to compensate this? - // Or compensate it another way? - SetBoneTrans(model, GetIndexOfBone(model, "j_spinelower"), false, 0.07, 0.0f, 5.2f); - - // These are often messed up on civilian models, but there is no obvious way to tell from code - const auto stowedBack = GetIndexOfBone(model, "tag_stowed_back"); - if (stowedBack != UCHAR_MAX) - { - SetBoneTrans(model, stowedBack, false, -0.32f, -6.27f, -2.65F); - SetBoneQuaternion(model, stowedBack, false, -0.044, 0.088, -0.995, 0.025); - SetBoneTrans(model, stowedBack, true, -9.571f, -2.654f, 51.738f); - SetBoneQuaternion(model, stowedBack, true, -0.071f, 0.0f, -0.997f, 0.0f); - } - - if (stowedBack != UCHAR_MAX) - { - const auto stowedRear = GetIndexOfBone(model, "tag_stowed_hip_rear"); - SetBoneTrans(model, stowedRear, false, -0.75f, -6.45f, -4.99f); - SetBoneQuaternion(model, stowedRear, false, -0.553f, -0.062f, -0.049f, 0.830f); - SetBoneTrans(model, stowedBack, true, -9.866f, -4.989f, 36.315f); - SetBoneQuaternion(model, stowedRear, true, -0.054, -0.025f, -0.975f, 0.214f); - } -#endif - - RebuildPartBits(model); - } - } printf(""); } + + std::string requiredBonesForHumanoid[] = { + "j_spinelower", + "j_spineupper", + "j_spine4", + "j_mainroot" + }; + + for (const auto& required : requiredBonesForHumanoid) + { + if (GetIndexOfBone(model, required) == UCHAR_MAX) + { + // Not humanoid - nothing to do + return; + } + } + + auto indexOfSpine = GetIndexOfBone(model, "j_spinelower"); + const auto nameOfParent = GetParentOfBone(model, indexOfSpine); + + if (GetIndexOfBone(model, "torso_stabilizer") == UCHAR_MAX) // Singleplayer model is likely + { + + Components::Logger::Print("Converting {} skeleton from SP to MP...\n", model->name); + + // No stabilizer - let's do surgery + // We're trying to get there: + // tag_origin + // j_main_root + // pelvis + // j_hip_le + // j_hip_ri + // tag_stowed_hip_rear + // torso_stabilizer + // j_spinelower + // back_low + // j_spineupper + // back_mid + // j_spine4 + + + const auto root = GetIndexOfBone(model, "j_mainroot"); + if (root < UCHAR_MAX) { + + // Add pelvis + const uint8_t indexOfPelvis = InsertBone(model, "pelvis", "j_mainroot", allocator); + SetBoneQuaternion(model, indexOfPelvis, true, -0.494f, -0.506f, -0.506f, 0.494f); + + TransferWeights(model, root, indexOfPelvis); + + SetParentIndexOfBone(model, GetIndexOfBone(model, "j_hip_le"), indexOfPelvis); + SetParentIndexOfBone(model, GetIndexOfBone(model, "j_hip_ri"), indexOfPelvis); + SetParentIndexOfBone(model, GetIndexOfBone(model, "tag_stowed_hip_rear"), indexOfPelvis); + + // These two are optional + if (GetIndexOfBone(model, "j_coatfront_le") == UCHAR_MAX) + { + InsertBone(model, "j_coatfront_le", "pelvis", allocator); + } + + if (GetIndexOfBone(model, "j_coatfront_ri") == UCHAR_MAX) + { + InsertBone(model, "j_coatfront_ri", "pelvis", allocator); + } + + const uint8_t torsoStabilizer = InsertBone(model, "torso_stabilizer", "pelvis", allocator); + const uint8_t lowerSpine = GetIndexOfBone(model, "j_spinelower"); + SetParentIndexOfBone(model, lowerSpine, torsoStabilizer); + + const uint8_t backLow = InsertBone(model, "back_low", "j_spinelower", allocator); + TransferWeights(model, lowerSpine, backLow); + SetParentIndexOfBone(model, GetIndexOfBone(model, "j_spineupper"), backLow); + + const uint8_t backMid = InsertBone(model, "back_mid", "j_spineupper", allocator); + TransferWeights(model, GetIndexOfBone(model, "j_spineupper"), backMid); + SetParentIndexOfBone(model, GetIndexOfBone(model, "j_spine4"), backMid); + + + assert(root == GetIndexOfBone(model, "j_mainroot")); + assert(indexOfPelvis == GetIndexOfBone(model, "pelvis")); + assert(backLow == GetIndexOfBone(model, "back_low")); + assert(backMid == GetIndexOfBone(model, "back_mid")); + + // Twister bone + SetBoneQuaternion(model, lowerSpine, false, -0.492f, -0.507f, -0.507f, 0.492f); + SetBoneQuaternion(model, torsoStabilizer, false, 0.494f, 0.506f, 0.506f, 0.494f); + + + // This doesn't feel like it should be necessary + // It is, on singleplayer models unfortunately. Could we add an extra bone to compensate this? + // Or compensate it another way? + SetBoneTrans(model, GetIndexOfBone(model, "j_spinelower"), false, 0.07f, 0.0f, 5.2f); + + // These are often messed up on civilian models, but there is no obvious way to tell from code + auto stowedBack = GetIndexOfBone(model, "tag_stowed_back"); + if (stowedBack == UCHAR_MAX) + { + stowedBack = InsertBone(model, "tag_stowed_back", "j_spine4", allocator); + } + + SetBoneTrans(model, stowedBack, false, -0.32f, -6.27f, -2.65F); + SetBoneQuaternion(model, stowedBack, false, -0.044f, 0.088f, -0.995f, 0.025f); + SetBoneTrans(model, stowedBack, true, -9.571f, -2.654f, 51.738f); + SetBoneQuaternion(model, stowedBack, true, -0.071f, 0.0f, -0.997f, 0.0f); + + auto stowedRear = GetIndexOfBone(model, "tag_stowed_hip_rear"); + if (stowedRear == UCHAR_MAX) + { + stowedBack = InsertBone(model, "tag_stowed_hip_rear", "pelvis", allocator); + } + + SetBoneTrans(model, stowedRear, false, -0.75f, -6.45f, -4.99f); + SetBoneQuaternion(model, stowedRear, false, -0.553f, -0.062f, -0.049f, 0.830f); + SetBoneTrans(model, stowedBack, true, -9.866f, -4.989f, 36.315f); + SetBoneQuaternion(model, stowedRear, true, -0.054f, -0.025f, -0.975f, 0.214f); + + + RebuildPartBits(model); + } + } } } diff --git a/src/Components/Modules/QuickPatch.cpp b/src/Components/Modules/QuickPatch.cpp index eafc0b47..cde7379a 100644 --- a/src/Components/Modules/QuickPatch.cpp +++ b/src/Components/Modules/QuickPatch.cpp @@ -314,8 +314,6 @@ namespace Components QuickPatch::QuickPatch() { - - // Filtering any mapents that is intended for Spec:Ops gamemode (CODO) and prevent them from spawning Utils::Hook(0x5FBD6E, QuickPatch::IsDynClassname_Stub, HOOK_CALL).install()->quick(); diff --git a/src/Components/Modules/ZoneBuilder.cpp b/src/Components/Modules/ZoneBuilder.cpp index 56314b4e..b0606ef4 100644 --- a/src/Components/Modules/ZoneBuilder.cpp +++ b/src/Components/Modules/ZoneBuilder.cpp @@ -7,6 +7,8 @@ #include #include "AssetInterfaces/ILocalizeEntry.hpp" + +#include "Events.hpp" #include "Branding.hpp" namespace Components @@ -22,6 +24,8 @@ namespace Components iw4of::api ZoneBuilder::ExporterAPI(GetExporterAPIParams()); std::string ZoneBuilder::DumpingZone{}; + Dvar::Var ZoneBuilder::zb_sp_to_mp{}; + ZoneBuilder::Zone::Zone(const std::string& name, const std::string& sourceName, const std::string& destination) : indexStart(0), externalSize(0), // Reserve 100MB by default. @@ -115,7 +119,7 @@ namespace Components return &iw4ofApi; } - void ZoneBuilder::Zone::Zone::build() + void ZoneBuilder::Zone::build() { if (!this->dataMap.isValid()) { @@ -1189,7 +1193,7 @@ namespace Components Game::DB_LoadXAssets(&info, 1, true); AssetHandler::FindOriginalAsset(Game::XAssetType::ASSET_TYPE_RAWFILE, "default"); // Lock until zone is unloaded - }; + }; } ZoneBuilder::ZoneBuilder() @@ -1300,6 +1304,8 @@ namespace Components // don't remap techsets Utils::Hook::Nop(0x5BC791, 5); + ZoneBuilder::zb_sp_to_mp = Game::Dvar_RegisterBool("zb_sp_to_mp", false, Game::DVAR_ARCHIVE, "Attempt to convert singleplayer assets to multiplayer format whenever possible"); + AssetHandler::OnLoad([](Game::XAssetType type, Game::XAssetHeader /* asset*/, const std::string& name, bool* /*restrict*/) { if (!ZoneBuilder::TraceZone.empty() && ZoneBuilder::TraceZone == FastFiles::Current()) @@ -1402,7 +1408,7 @@ namespace Components if (assetHeader.data) { ExporterAPI.write(type, assetHeader.data); - const auto typeName = Game::DB_GetXAssetTypeName(type) ; + const auto typeName = Game::DB_GetXAssetTypeName(type); if (type != lastTypeEncountered) { @@ -1541,8 +1547,8 @@ namespace Components #endif } } - } - }); + } + }); Command::Add("listassets", [](const Command::Params* params) { @@ -1558,7 +1564,7 @@ namespace Components }, &type, false); } }); -} + } } ZoneBuilder::~ZoneBuilder() diff --git a/src/Components/Modules/ZoneBuilder.hpp b/src/Components/Modules/ZoneBuilder.hpp index bc3affb5..7fb5a05d 100644 --- a/src/Components/Modules/ZoneBuilder.hpp +++ b/src/Components/Modules/ZoneBuilder.hpp @@ -135,6 +135,8 @@ namespace Components static void BeginAssetTrace(const std::string& zone); static std::vector> EndAssetTrace(); + static Dvar::Var zb_sp_to_mp; + static Game::XAssetHeader GetEmptyAssetIfCommon(Game::XAssetType type, const std::string& name, Zone* builder); static std::string GetDumpingZonePath(); static void RefreshExporterWorkDirectory(); diff --git a/src/Game/Structs.hpp b/src/Game/Structs.hpp index 67aea351..d1233fb5 100644 --- a/src/Game/Structs.hpp +++ b/src/Game/Structs.hpp @@ -1066,7 +1066,7 @@ namespace Game struct XSurfaceVertexInfo { - __int16 vertCount[4]; + unsigned __int16 vertCount[4]; unsigned __int16* vertsBlend; }; From a603dbea523f6b8179970e32b35491dec5e3fa58 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sun, 28 Jan 2024 17:54:19 +0100 Subject: [PATCH 08/10] Some more cleanup! --- src/Components/Modules/ClientCommand.cpp | 2 +- src/Components/Modules/QuickPatch.cpp | 14 ---- src/Components/Modules/Renderer.cpp | 27 -------- src/Components/Modules/Weapon.cpp | 83 ++++++++++++++---------- src/Components/Modules/Weapon.hpp | 10 +-- src/Steam/Proxy.cpp | 2 - 6 files changed, 54 insertions(+), 84 deletions(-) diff --git a/src/Components/Modules/ClientCommand.cpp b/src/Components/Modules/ClientCommand.cpp index f7c8f5ca..bd284e8c 100644 --- a/src/Components/Modules/ClientCommand.cpp +++ b/src/Components/Modules/ClientCommand.cpp @@ -386,7 +386,7 @@ namespace Components { if (Components::Weapon::GModelIndexHasBeenReallocated) { - model = Components::Weapon::G_ModelIndexReallocated[ent->model]; + model = Components::Weapon::cached_models_reallocated[ent->model]; } else { diff --git a/src/Components/Modules/QuickPatch.cpp b/src/Components/Modules/QuickPatch.cpp index cde7379a..e8a7ded9 100644 --- a/src/Components/Modules/QuickPatch.cpp +++ b/src/Components/Modules/QuickPatch.cpp @@ -298,20 +298,6 @@ namespace Components return Game::Dvar_RegisterBool(dvarName, value_, flags, description); } - void DObjCalcAnim(Game::DObj *a1, int *partBits, Game::XAnimCalcAnimInfo *a3) - { - printf(""); - - if (a1->models[0]->name == "body_urban_civ_female_a"s) - { - printf(""); - } - - Utils::Hook::Call(0x49E230)(a1, partBits, a3); - - printf(""); - } - QuickPatch::QuickPatch() { // Filtering any mapents that is intended for Spec:Ops gamemode (CODO) and prevent them from spawning diff --git a/src/Components/Modules/Renderer.cpp b/src/Components/Modules/Renderer.cpp index 8df1b33f..191b82f5 100644 --- a/src/Components/Modules/Renderer.cpp +++ b/src/Components/Modules/Renderer.cpp @@ -713,32 +713,6 @@ namespace Components return result; } - void DebugTest() - { - //auto clientNum = Game::CG_GetClientNum(); - //auto* clientEntity = &Game::g_entities[clientNum]; - - //// Ingame only & player only - //if (!Game::CL_IsCgameInitialized() || clientEntity->client == nullptr) - //{ - // return; - //} - - - //static std::string str = ""; - - //str += std::format("\n{} => {} {} {} {} {} {}", "s.partBits", clientEntity->s.partBits[0], clientEntity->s.partBits[1], clientEntity->s.partBits[2], clientEntity->s.partBits[3], clientEntity->s.partBits[4], clientEntity->s.partBits[5]); - // - - //const auto clientNumber = clientEntity->r.ownerNum.number; - //Game::scene->sceneDObj[clientNumber].obj->hidePartBits; - - //str += std::format("\n{} => {} {} {} {} {} {}", "DOBJ hidePartBits", clientEntity->s.partBits[0], clientEntity->s.partBits[1], clientEntity->s.partBits[2], clientEntity->s.partBits[3], clientEntity->s.partBits[4], clientEntity->s.partBits[5]); - - //Game::R_AddCmdDrawText(); - - } - Renderer::Renderer() { if (Dedicated::IsEnabled()) return; @@ -757,7 +731,6 @@ namespace Components ListSamplers(); DrawPrimaryLights(); DebugDrawClipmap(); - DebugTest(); } }, Scheduler::Pipeline::RENDERER); diff --git a/src/Components/Modules/Weapon.cpp b/src/Components/Modules/Weapon.cpp index 5f3fd9f0..0413fd9d 100644 --- a/src/Components/Modules/Weapon.cpp +++ b/src/Components/Modules/Weapon.cpp @@ -6,9 +6,18 @@ namespace Components { const Game::dvar_t* Weapon::BGWeaponOffHandFix; - Game::XModel* Weapon::G_ModelIndexReallocated[G_MODELINDEX_LIMIT]; + Game::XModel* Weapon::cached_models_reallocated[G_MODELINDEX_LIMIT]; bool Weapon::GModelIndexHasBeenReallocated; + // Config strings mapping + // 0-1067 unknown + // 1125-1580 cached models (512 long range) + // 1581-1637 also reserved for models? + // 1637-4082 unknown + // 4082-above = bad index? + // 4137 : timescale + // 4138-above = reserved for weapons? + Game::WeaponCompleteDef* Weapon::LoadWeaponCompleteDef(const char* name) { if (auto* rawWeaponFile = Game::BG_LoadWeaponCompleteDefInternal("mp", name)) @@ -22,7 +31,7 @@ namespace Components const char* Weapon::GetWeaponConfigString(int index) { - if (index >= (1200 + 2804)) index += (2939 - 2804); + if (index >= (BASEGAME_WEAPON_LIMIT + 2804)) index += (2939 - 2804); return Game::CL_GetConfigString(index); } @@ -34,7 +43,7 @@ namespace Components { for (unsigned int i = 1; i < Game::BG_GetNumWeapons(); ++i) { - Game::SV_SetConfigstring(i + (i >= 1200 ? 2939 : 2804), Game::BG_GetWeaponName(i)); + Game::SV_SetConfigstring(i + (i >= BASEGAME_WEAPON_LIMIT ? 2939 : 2804), Game::BG_GetWeaponName(i)); } } } @@ -57,12 +66,14 @@ namespace Components return 0; } - if (index >= 4139) + if (index >= BASEGAME_MAX_CONFIGSTRINGS) { + // if above 4139, remap to 1200<>? index -= 2939; } - else if (index > 2804 && index <= 2804 + 1200) + else if (index > 2804 && index <= 2804 + BASEGAME_WEAPON_LIMIT) { + // from 2804 to 4004, remap to 0<>1200 index -= 2804; } else @@ -283,8 +294,8 @@ namespace Components // And http://reverseengineering.stackexchange.com/questions/1397/how-can-i-reverse-optimized-integer-division-modulo-by-constant-operations // The game's magic number is computed using this formula: (1 / 1200) * (2 ^ (32 + 7) // I'm too lazy to generate the new magic number, so we can make use of the fact that using powers of 2 as scales allows to change the compensating shift - static_assert(((WEAPON_LIMIT / 1200) * 1200) == WEAPON_LIMIT && (WEAPON_LIMIT / 1200) != 0 && !((WEAPON_LIMIT / 1200) & ((WEAPON_LIMIT / 1200) - 1)), "WEAPON_LIMIT / 1200 is not a power of 2!"); - const unsigned char compensation = 7 + static_cast(log2(WEAPON_LIMIT / 1200)); // 7 is the compensation the game uses + static_assert(((WEAPON_LIMIT / BASEGAME_WEAPON_LIMIT) * BASEGAME_WEAPON_LIMIT) == WEAPON_LIMIT && (WEAPON_LIMIT / BASEGAME_WEAPON_LIMIT) != 0 && !((WEAPON_LIMIT / BASEGAME_WEAPON_LIMIT) & ((WEAPON_LIMIT / BASEGAME_WEAPON_LIMIT) - 1)), "WEAPON_LIMIT / 1200 is not a power of 2!"); + const unsigned char compensation = 7 + static_cast(log2(WEAPON_LIMIT / BASEGAME_WEAPON_LIMIT)); // 7 is the compensation the game uses Utils::Hook::Set(0x49263D, compensation); Utils::Hook::Set(0x5E250C, compensation); Utils::Hook::Set(0x5E2B43, compensation); @@ -407,46 +418,46 @@ namespace Components // Patch bg_weaponDefs on the stack Utils::Hook::Set(0x40C31D, sizeof(bg_weaponDefs)); Utils::Hook::Set(0x40C32F, sizeof(bg_weaponDefs)); - Utils::Hook::Set(0x40C311, 0x258C + ((sizeof(bg_weaponDefs) * 2) - (1200 * 4 * 2))); - Utils::Hook::Set(0x40C45F, 0x258C + ((sizeof(bg_weaponDefs) * 2) - (1200 * 4 * 2))); - Utils::Hook::Set(0x40C478, 0x258C + ((sizeof(bg_weaponDefs) * 2) - (1200 * 4 * 2))); - Utils::Hook::Set(0x40C434, 0x258C + ((sizeof(bg_weaponDefs) * 2) - (1200 * 4 * 2))); - Utils::Hook::Set(0x40C434, 0x258C + ((sizeof(bg_weaponDefs) * 2) - (1200 * 4 * 2))); + Utils::Hook::Set(0x40C311, 0x258C + ((sizeof(bg_weaponDefs) * 2) - (BASEGAME_WEAPON_LIMIT * 4 * 2))); + Utils::Hook::Set(0x40C45F, 0x258C + ((sizeof(bg_weaponDefs) * 2) - (BASEGAME_WEAPON_LIMIT * 4 * 2))); + Utils::Hook::Set(0x40C478, 0x258C + ((sizeof(bg_weaponDefs) * 2) - (BASEGAME_WEAPON_LIMIT * 4 * 2))); + Utils::Hook::Set(0x40C434, 0x258C + ((sizeof(bg_weaponDefs) * 2) - (BASEGAME_WEAPON_LIMIT * 4 * 2))); + Utils::Hook::Set(0x40C434, 0x258C + ((sizeof(bg_weaponDefs) * 2) - (BASEGAME_WEAPON_LIMIT * 4 * 2))); // Move second buffer pointers - Utils::Hook::Set(0x40C336, 0x12E4 + ((sizeof(bg_weaponDefs)) - (1200 * 4))); - Utils::Hook::Set(0x40C3C6, 0x12DC + ((sizeof(bg_weaponDefs)) - (1200 * 4))); - Utils::Hook::Set(0x40C3CE, 0x12DC + ((sizeof(bg_weaponDefs)) - (1200 * 4))); + Utils::Hook::Set(0x40C336, 0x12E4 + ((sizeof(bg_weaponDefs)) - (BASEGAME_WEAPON_LIMIT * 4))); + Utils::Hook::Set(0x40C3C6, 0x12DC + ((sizeof(bg_weaponDefs)) - (BASEGAME_WEAPON_LIMIT * 4))); + Utils::Hook::Set(0x40C3CE, 0x12DC + ((sizeof(bg_weaponDefs)) - (BASEGAME_WEAPON_LIMIT * 4))); // Move arg0 pointers - Utils::Hook::Set(0x40C365, 0x259C + ((sizeof(bg_weaponDefs) * 2) - (1200 * 4 * 2))); - Utils::Hook::Set(0x40C44E, 0x259C + ((sizeof(bg_weaponDefs) * 2) - (1200 * 4 * 2))); - Utils::Hook::Set(0x40C467, 0x259C + ((sizeof(bg_weaponDefs) * 2) - (1200 * 4 * 2))); + Utils::Hook::Set(0x40C365, 0x259C + ((sizeof(bg_weaponDefs) * 2) - (BASEGAME_WEAPON_LIMIT * 4 * 2))); + Utils::Hook::Set(0x40C44E, 0x259C + ((sizeof(bg_weaponDefs) * 2) - (BASEGAME_WEAPON_LIMIT * 4 * 2))); + Utils::Hook::Set(0x40C467, 0x259C + ((sizeof(bg_weaponDefs) * 2) - (BASEGAME_WEAPON_LIMIT * 4 * 2))); // Move arg4 pointers - Utils::Hook::Set(0x40C344, 0x25B4 + ((sizeof(bg_weaponDefs) * 2) - (1200 * 4 * 2))); + Utils::Hook::Set(0x40C344, 0x25B4 + ((sizeof(bg_weaponDefs) * 2) - (BASEGAME_WEAPON_LIMIT * 4 * 2))); // Patch bg_sharedAmmoCaps on the stack Utils::Hook::Set(0x4F76E6, sizeof(bg_sharedAmmoCaps)); - Utils::Hook::Set(0x4F7621, 0x12C8 + (sizeof(bg_sharedAmmoCaps) - (1200 * 4))); - Utils::Hook::Set(0x4F76AF, 0x12C8 + (sizeof(bg_sharedAmmoCaps) - (1200 * 4))); - Utils::Hook::Set(0x4F76DA, 0x12C8 + (sizeof(bg_sharedAmmoCaps) - (1200 * 4))); - Utils::Hook::Set(0x4F77C5, 0x12C8 + (sizeof(bg_sharedAmmoCaps) - (1200 * 4))); + Utils::Hook::Set(0x4F7621, 0x12C8 + (sizeof(bg_sharedAmmoCaps) - (BASEGAME_WEAPON_LIMIT * 4))); + Utils::Hook::Set(0x4F76AF, 0x12C8 + (sizeof(bg_sharedAmmoCaps) - (BASEGAME_WEAPON_LIMIT * 4))); + Utils::Hook::Set(0x4F76DA, 0x12C8 + (sizeof(bg_sharedAmmoCaps) - (BASEGAME_WEAPON_LIMIT * 4))); + Utils::Hook::Set(0x4F77C5, 0x12C8 + (sizeof(bg_sharedAmmoCaps) - (BASEGAME_WEAPON_LIMIT * 4))); // Move arg0 pointers - Utils::Hook::Set(0x4F766D, 0x12DC + (sizeof(bg_sharedAmmoCaps) - (1200 * 4))); - Utils::Hook::Set(0x4F76B7, 0x12DC + (sizeof(bg_sharedAmmoCaps) - (1200 * 4))); - Utils::Hook::Set(0x4F76FB, 0x12EC + (sizeof(bg_sharedAmmoCaps) - (1200 * 4))); + Utils::Hook::Set(0x4F766D, 0x12DC + (sizeof(bg_sharedAmmoCaps) - (BASEGAME_WEAPON_LIMIT * 4))); + Utils::Hook::Set(0x4F76B7, 0x12DC + (sizeof(bg_sharedAmmoCaps) - (BASEGAME_WEAPON_LIMIT * 4))); + Utils::Hook::Set(0x4F76FB, 0x12EC + (sizeof(bg_sharedAmmoCaps) - (BASEGAME_WEAPON_LIMIT * 4))); // Move arg4 pointers - Utils::Hook::Set(0x4F7630, 0x12DC + (sizeof(bg_sharedAmmoCaps) - (1200 * 4))); + Utils::Hook::Set(0x4F7630, 0x12DC + (sizeof(bg_sharedAmmoCaps) - (BASEGAME_WEAPON_LIMIT * 4))); // Reallocate G_ModelIndex - Utils::Hook::Set(0x420654 + 3, G_ModelIndexReallocated); - Utils::Hook::Set(0x43BCE4 + 3, G_ModelIndexReallocated); - Utils::Hook::Set(0x44F27B + 3, G_ModelIndexReallocated); - Utils::Hook::Set(0x479087 + 1, G_ModelIndexReallocated); - Utils::Hook::Set(0x48069D + 3, G_ModelIndexReallocated); - Utils::Hook::Set(0x48F088 + 3, G_ModelIndexReallocated); - Utils::Hook::Set(0x4F457C + 3, G_ModelIndexReallocated); - Utils::Hook::Set(0x5FC762 + 3, G_ModelIndexReallocated); - Utils::Hook::Set(0x5FC7BE + 3, G_ModelIndexReallocated); + Utils::Hook::Set(0x420654 + 3, cached_models_reallocated); + Utils::Hook::Set(0x43BCE4 + 3, cached_models_reallocated); + Utils::Hook::Set(0x44F27B + 3, cached_models_reallocated); + Utils::Hook::Set(0x479087 + 1, cached_models_reallocated); + Utils::Hook::Set(0x48069D + 3, cached_models_reallocated); + Utils::Hook::Set(0x48F088 + 3, cached_models_reallocated); + Utils::Hook::Set(0x4F457C + 3, cached_models_reallocated); + Utils::Hook::Set(0x5FC762 + 3, cached_models_reallocated); + Utils::Hook::Set(0x5FC7BE + 3, cached_models_reallocated); Utils::Hook::Set(0x44F256 + 2, G_MODELINDEX_LIMIT); GModelIndexHasBeenReallocated = true; diff --git a/src/Components/Modules/Weapon.hpp b/src/Components/Modules/Weapon.hpp index 60c17c60..3065a1e0 100644 --- a/src/Components/Modules/Weapon.hpp +++ b/src/Components/Modules/Weapon.hpp @@ -1,14 +1,16 @@ #pragma once +#define BASEGAME_WEAPON_LIMIT 1200 +#define BASEGAME_MAX_CONFIGSTRINGS 4139 + // Increase the weapon limit -// Was 1200 before #define WEAPON_LIMIT 2400 -#define MAX_CONFIGSTRINGS (4139 - 1200 + WEAPON_LIMIT) +#define MAX_CONFIGSTRINGS (BASEGAME_MAX_CONFIGSTRINGS - BASEGAME_WEAPON_LIMIT + WEAPON_LIMIT) // Double the limit to allow loading of some heavy-duty MW3 maps #define ADDITIONAL_GMODELS 512 -#define G_MODELINDEX_LIMIT (512 + WEAPON_LIMIT - 1200 + ADDITIONAL_GMODELS) +#define G_MODELINDEX_LIMIT (512 + WEAPON_LIMIT - BASEGAME_WEAPON_LIMIT + ADDITIONAL_GMODELS) namespace Components { @@ -16,7 +18,7 @@ namespace Components { public: Weapon(); - static Game::XModel* G_ModelIndexReallocated[G_MODELINDEX_LIMIT]; + static Game::XModel* cached_models_reallocated[G_MODELINDEX_LIMIT]; static bool GModelIndexHasBeenReallocated; diff --git a/src/Steam/Proxy.cpp b/src/Steam/Proxy.cpp index 248723ff..c4710b4a 100644 --- a/src/Steam/Proxy.cpp +++ b/src/Steam/Proxy.cpp @@ -276,8 +276,6 @@ namespace Steam void Proxy::RunFrame() { - return; - std::lock_guard _(Proxy::CallMutex); if (Proxy::SteamUtils) From a8606cb53316348f72d93f0ff1cb4172493d7f58 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sun, 28 Jan 2024 18:05:16 +0100 Subject: [PATCH 09/10] #if DEBUG --- src/Components/Modules/AssetInterfaces/IXModel.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Components/Modules/AssetInterfaces/IXModel.cpp b/src/Components/Modules/AssetInterfaces/IXModel.cpp index d5667578..e508f570 100644 --- a/src/Components/Modules/AssetInterfaces/IXModel.cpp +++ b/src/Components/Modules/AssetInterfaces/IXModel.cpp @@ -482,9 +482,10 @@ namespace Assets { assert(GetIndexOfBone(model, boneName) == UCHAR_MAX); +#if DEBUG constexpr auto MAX_BONES = 192; - assert(model->numBones < MAX_BONES); +#endif // Start with backing up parent links that we will have to restore // We'll restore them at the end @@ -523,6 +524,7 @@ namespace Assets const uint8_t atPositionM1 = atPosition - model->numRootBones; +#if DEBUG // should be equal to model->numBones unsigned int total = lengthOfFirstPart + lengthOfSecondPart; assert(total == model->numBones); @@ -530,6 +532,7 @@ namespace Assets // should be equal to model->numBones - model->numRootBones int totalM1 = lengthOfFirstPartM1 + lengthOfSecondPartM1; assert(totalM1 == model->numBones - model->numRootBones); +#endif // Copy before if (lengthOfFirstPart > 0) From c11dfd8422f9ba30f52c7b7bdd3cd4e0e93db807 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sun, 28 Jan 2024 18:38:46 +0100 Subject: [PATCH 10/10] Address review --- .../Modules/AssetInterfaces/IXModel.cpp | 2 - src/Components/Modules/ZoneBuilder.cpp | 270 +++++++++--------- src/Components/Modules/ZoneBuilder.hpp | 2 + 3 files changed, 142 insertions(+), 132 deletions(-) diff --git a/src/Components/Modules/AssetInterfaces/IXModel.cpp b/src/Components/Modules/AssetInterfaces/IXModel.cpp index e508f570..ac22bc5e 100644 --- a/src/Components/Modules/AssetInterfaces/IXModel.cpp +++ b/src/Components/Modules/AssetInterfaces/IXModel.cpp @@ -1,7 +1,5 @@ #include -#include - #include "IXModel.hpp" namespace Assets diff --git a/src/Components/Modules/ZoneBuilder.cpp b/src/Components/Modules/ZoneBuilder.cpp index b0606ef4..455b0136 100644 --- a/src/Components/Modules/ZoneBuilder.cpp +++ b/src/Components/Modules/ZoneBuilder.cpp @@ -1167,6 +1167,139 @@ namespace Components return params; } + void ZoneBuilder::DumpZone(const std::string& zone) + { + ZoneBuilder::DumpingZone = zone; + ZoneBuilder::RefreshExporterWorkDirectory(); + + std::vector> assets{}; + const auto unload = ZoneBuilder::LoadZoneWithTrace(zone, assets); + + Logger::Print("Dumping zone '{}'...\n", zone); + + { + Utils::IO::CreateDir(GetDumpingZonePath()); + std::ofstream csv(std::filesystem::path(GetDumpingZonePath()) / std::format("{}.csv", zone)); + csv + << std::format("### Zone '{}' dumped with Zonebuilder {}", zone, Components::Branding::GetVersionString()) + << "\n\n"; + + // Order the CSV around + // TODO: Trim asset list using IW4OF dependencies + constexpr Game::XAssetType typeOrder[] = { + Game::XAssetType::ASSET_TYPE_GAMEWORLD_MP, + Game::XAssetType::ASSET_TYPE_GAMEWORLD_SP, + Game::XAssetType::ASSET_TYPE_GFXWORLD, + Game::XAssetType::ASSET_TYPE_COMWORLD, + Game::XAssetType::ASSET_TYPE_FXWORLD, + Game::XAssetType::ASSET_TYPE_CLIPMAP_MP, + Game::XAssetType::ASSET_TYPE_CLIPMAP_SP, + Game::XAssetType::ASSET_TYPE_RAWFILE, + Game::XAssetType::ASSET_TYPE_VEHICLE, + Game::XAssetType::ASSET_TYPE_WEAPON, + Game::XAssetType::ASSET_TYPE_FX, + Game::XAssetType::ASSET_TYPE_TRACER, + Game::XAssetType::ASSET_TYPE_XMODEL, + Game::XAssetType::ASSET_TYPE_MATERIAL, + Game::XAssetType::ASSET_TYPE_TECHNIQUE_SET, + Game::XAssetType::ASSET_TYPE_PIXELSHADER, + Game::XAssetType::ASSET_TYPE_VERTEXSHADER, + Game::XAssetType::ASSET_TYPE_VERTEXDECL, + Game::XAssetType::ASSET_TYPE_IMAGE, + Game::XAssetType::ASSET_TYPE_SOUND, + Game::XAssetType::ASSET_TYPE_LOADED_SOUND, + Game::XAssetType::ASSET_TYPE_SOUND_CURVE, + Game::XAssetType::ASSET_TYPE_PHYSPRESET, + }; + + std::unordered_map typePriority{}; + for (auto i = 0; i < ARRAYSIZE(typeOrder); i++) + { + const auto type = typeOrder[i]; + typePriority.emplace(type, 1 + ARRAYSIZE(typeOrder) - i); + } + + std::map> invalidAssets{}; + + std::sort(assets.begin(), assets.end(), [&]( + const std::pair& a, + const std::pair& b + ) { + if (a.first == b.first) + { + + return a.second.compare(b.second) < 0; + } + else + { + const auto priorityA = typePriority[a.first]; + const auto priorityB = typePriority[b.first]; + + if (priorityA == priorityB) + { + return a.second.compare(b.second) < 0; + } + else + { + return priorityB < priorityA; + } + } + }); + + // Used to format the CSV + Game::XAssetType lastTypeEncountered{}; + + for (const auto& asset : assets) + { + const auto type = asset.first; + const auto name = asset.second; + if (ExporterAPI.is_type_supported(type) && name[0] != ',') + { + const auto assetHeader = Game::DB_FindXAssetHeader(type, name.data()); + if (assetHeader.data) + { + ExporterAPI.write(type, assetHeader.data); + const auto typeName = Game::DB_GetXAssetTypeName(type); + + if (type != lastTypeEncountered) + { + csv << "\n### " << typeName << "\n"; + lastTypeEncountered = type; + } + + csv << typeName << "," << name << "\n"; + } + else + { + Logger::Warning(Game::conChannel_t::CON_CHANNEL_ERROR, "Asset {} has disappeared while dumping!\n", name); + invalidAssets["The following assets disappeared while dumping"].push_back(std::format("{},{}", Game::DB_GetXAssetTypeName(type), name)); + } + } + else + { + invalidAssets["The following assets are unsupported or not dumped as individual assets, but still present in the zone"].push_back(std::format("{},{}", Game::DB_GetXAssetTypeName(type), name)); + } + } + + for (const auto& kv : invalidAssets) + { + csv << "\n### " << kv.first << "\n"; + for (const auto& line : kv.second) + { + csv << "#" << line << "\n"; + } + } + + csv << std::format("\n### {} assets", assets.size()) << "\n"; + } + + unload(); + + Logger::Print("Zone '{}' dumped", ZoneBuilder::DumpingZone); + ZoneBuilder::DumpingZone = std::string(); + } + + std::function ZoneBuilder::LoadZoneWithTrace(const std::string& zone, OUT std::vector>& assets) { ZoneBuilder::BeginAssetTrace(zone); @@ -1193,7 +1326,7 @@ namespace Components Game::DB_LoadXAssets(&info, 1, true); AssetHandler::FindOriginalAsset(Game::XAssetType::ASSET_TYPE_RAWFILE, "default"); // Lock until zone is unloaded - }; + }; } ZoneBuilder::ZoneBuilder() @@ -1320,132 +1453,7 @@ namespace Components std::string zone = params->get(1); - ZoneBuilder::DumpingZone = zone; - ZoneBuilder::RefreshExporterWorkDirectory(); - - std::vector> assets{}; - const auto unload = ZoneBuilder::LoadZoneWithTrace(zone, assets); - - Logger::Print("Dumping zone '{}'...\n", zone); - - { - Utils::IO::CreateDir(GetDumpingZonePath()); - std::ofstream csv(std::filesystem::path(GetDumpingZonePath()) / std::format("{}.csv", zone)); - csv - << std::format("### Zone '{}' dumped with Zonebuilder {}", zone, Components::Branding::GetVersionString()) - << "\n\n"; - - constexpr Game::XAssetType typeOrder[] = { - Game::XAssetType::ASSET_TYPE_GAMEWORLD_MP, - Game::XAssetType::ASSET_TYPE_GAMEWORLD_SP, - Game::XAssetType::ASSET_TYPE_GFXWORLD, - Game::XAssetType::ASSET_TYPE_COMWORLD, - Game::XAssetType::ASSET_TYPE_FXWORLD, - Game::XAssetType::ASSET_TYPE_CLIPMAP_MP, - Game::XAssetType::ASSET_TYPE_CLIPMAP_SP, - Game::XAssetType::ASSET_TYPE_RAWFILE, - Game::XAssetType::ASSET_TYPE_VEHICLE, - Game::XAssetType::ASSET_TYPE_WEAPON, - Game::XAssetType::ASSET_TYPE_FX, - Game::XAssetType::ASSET_TYPE_TRACER, - Game::XAssetType::ASSET_TYPE_XMODEL, - Game::XAssetType::ASSET_TYPE_MATERIAL, - Game::XAssetType::ASSET_TYPE_TECHNIQUE_SET, - Game::XAssetType::ASSET_TYPE_PIXELSHADER, - Game::XAssetType::ASSET_TYPE_VERTEXSHADER, - Game::XAssetType::ASSET_TYPE_VERTEXDECL, - Game::XAssetType::ASSET_TYPE_IMAGE, - Game::XAssetType::ASSET_TYPE_SOUND, - Game::XAssetType::ASSET_TYPE_LOADED_SOUND, - Game::XAssetType::ASSET_TYPE_SOUND_CURVE, - Game::XAssetType::ASSET_TYPE_PHYSPRESET, - }; - - std::unordered_map typePriority{}; - for (auto i = 0; i < ARRAYSIZE(typeOrder); i++) - { - const auto type = typeOrder[i]; - typePriority.emplace(type, 1 + ARRAYSIZE(typeOrder) - i); - } - - std::map> invalidAssets{}; - - std::sort(assets.begin(), assets.end(), [&]( - const std::pair& a, - const std::pair& b - ) { - if (a.first == b.first) - { - - return a.second.compare(b.second) < 0; - } - else - { - const auto priorityA = typePriority[a.first]; - const auto priorityB = typePriority[b.first]; - - if (priorityA == priorityB) - { - return a.second.compare(b.second) < 0; - } - else - { - return priorityB < priorityA; - } - } - }); - - // Used to format the CSV - Game::XAssetType lastTypeEncountered{}; - - for (const auto& asset : assets) - { - const auto type = asset.first; - const auto name = asset.second; - if (ExporterAPI.is_type_supported(type) && name[0] != ',') - { - const auto assetHeader = Game::DB_FindXAssetHeader(type, name.data()); - if (assetHeader.data) - { - ExporterAPI.write(type, assetHeader.data); - const auto typeName = Game::DB_GetXAssetTypeName(type); - - if (type != lastTypeEncountered) - { - csv << "\n### " << typeName << "\n"; - lastTypeEncountered = type; - } - - csv << typeName << "," << name << "\n"; - } - else - { - Logger::Warning(Game::conChannel_t::CON_CHANNEL_ERROR, "Asset {} has disappeared while dumping!\n", name); - invalidAssets["The following assets disappeared while dumping"].push_back(std::format("{},{}", Game::DB_GetXAssetTypeName(type), name)); - } - } - else - { - invalidAssets["The following assets are unsupported or not dumped as individual assets, but still present in the zone"].push_back(std::format("{},{}", Game::DB_GetXAssetTypeName(type), name)); - } - } - - for (const auto& kv : invalidAssets) - { - csv << "\n### " << kv.first << "\n"; - for (const auto& line : kv.second) - { - csv << "#" << line << "\n"; - } - } - - csv << std::format("\n### {} assets", assets.size()) << "\n"; - } - - unload(); - - Logger::Print("Zone '{}' dumped", ZoneBuilder::DumpingZone); - ZoneBuilder::DumpingZone = std::string(); + ZoneBuilder::DumpZone(zone); }); Command::Add("verifyzone", [](const Command::Params* params) @@ -1480,18 +1488,20 @@ namespace Components { if (params->size() < 2) return; + Dvar::Var fs_game("fs_game"); + std::string modName = params->get(1); Logger::Print("Building zone for mod '{}'...\n", modName); - const std::string previousFsGame = Dvar::Var("fs_game").get(); + const std::string previousFsGame = fs_game.get(); const std::string dir = "mods/" + modName; Utils::IO::CreateDir(dir); - Dvar::Var("fs_game").set(dir); + fs_game.set(dir); Zone("mod", modName, dir + "/mod.ff").build(); - Dvar::Var("fs_game").set(previousFsGame); + fs_game.set(previousFsGame); }); Command::Add("buildall", []() diff --git a/src/Components/Modules/ZoneBuilder.hpp b/src/Components/Modules/ZoneBuilder.hpp index 7fb5a05d..ad4a049b 100644 --- a/src/Components/Modules/ZoneBuilder.hpp +++ b/src/Components/Modules/ZoneBuilder.hpp @@ -162,6 +162,8 @@ namespace Components static iw4of::params_t GetExporterAPIParams(); + static void DumpZone(const std::string& zone); + static std::function LoadZoneWithTrace(const std::string& zone, OUT std::vector> &assets); static void Com_Quitf_t();