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 diff --git a/src/Components/Modules/AssetHandler.cpp b/src/Components/Modules/AssetHandler.cpp index b76def14..6d9e0a6c 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 } } @@ -292,9 +292,10 @@ namespace Components } } - 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; for (auto i = AssetHandler::EmptyAssets.begin(); i != AssetHandler::EmptyAssets.end();) @@ -333,12 +334,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 @@ -350,9 +351,9 @@ namespace Components mov ecx, 5BB657h jmp ecx - doNotLoad: + doNotLoad : mov eax, [esp + 8h] - retn + retn } } @@ -365,9 +366,9 @@ namespace Components { AssetHandler::RestrictSignal.connect(callback); - return [callback](){ + return [callback]() { AssetHandler::RestrictSignal.disconnect(callback); - }; + }; } void AssetHandler::ClearRelocations() @@ -573,25 +574,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/AssetInterfaces/IXModel.cpp b/src/Components/Modules/AssetInterfaces/IXModel.cpp index d4444712..ac22bc5e 100644 --- a/src/Components/Modules/AssetInterfaces/IXModel.cpp +++ b/src/Components/Modules/AssetInterfaces/IXModel.cpp @@ -1,14 +1,27 @@ #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) { + // 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) { @@ -257,4 +270,733 @@ 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) + { + assert(index > 0); + 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; + + { + for (auto surfIndex = 0; surfIndex < lod->numsurfs; surfIndex++) + { + const auto surface = &lod->surfs[surfIndex]; + auto vertsBlendOffset = 0; + + 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 (unsigned int vertIndex = 0; vertIndex < surface->vertInfo.vertCount[0]; vertIndex++) + { + registerBoneAffectingSurface(vertsBlendOffset + 0); + + vertsBlendOffset += 1; + } + + // 2 bone weights + for (unsigned int vertIndex = 0; vertIndex < surface->vertInfo.vertCount[1]; vertIndex++) + { + registerBoneAffectingSurface(vertsBlendOffset + 0); + registerBoneAffectingSurface(vertsBlendOffset + 1); + + vertsBlendOffset += 3; + } + + // 3 bone weights + for (unsigned int vertIndex = 0; vertIndex < surface->vertInfo.vertCount[2]; vertIndex++) + { + registerBoneAffectingSurface(vertsBlendOffset + 0); + registerBoneAffectingSurface(vertsBlendOffset + 1); + registerBoneAffectingSurface(vertsBlendOffset + 3); + + vertsBlendOffset += 5; + } + + // 4 bone weights + for (unsigned int vertIndex = 0; vertIndex < surface->vertInfo.vertCount[3]; vertIndex++) + { + registerBoneAffectingSurface(vertsBlendOffset + 0); + registerBoneAffectingSurface(vertsBlendOffset + 1); + registerBoneAffectingSurface(vertsBlendOffset + 3); + registerBoneAffectingSurface(vertsBlendOffset + 5); + + vertsBlendOffset += 7; + } + + for (unsigned int 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 (unsigned short 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 (unsigned int vertIndex = 0; vertIndex < surface->vertInfo.vertCount[0]; vertIndex++) + { + registerBoneAffectingSurface(vertsBlendOffset + 0); + + vertsBlendOffset += 1; + } + + // 2 bone weights + for (unsigned int vertIndex = 0; vertIndex < surface->vertInfo.vertCount[1]; vertIndex++) + { + registerBoneAffectingSurface(vertsBlendOffset + 0); + registerBoneAffectingSurface(vertsBlendOffset + 1); + + vertsBlendOffset += 3; + } + + // 3 bone weights + for (unsigned int vertIndex = 0; vertIndex < surface->vertInfo.vertCount[2]; vertIndex++) + { + registerBoneAffectingSurface(vertsBlendOffset + 0); + registerBoneAffectingSurface(vertsBlendOffset + 1); + registerBoneAffectingSurface(vertsBlendOffset + 3); + + vertsBlendOffset += 5; + } + + // 4 bone weights + for (unsigned int vertIndex = 0; vertIndex < surface->vertInfo.vertCount[3]; vertIndex++) + { + registerBoneAffectingSurface(vertsBlendOffset + 0); + registerBoneAffectingSurface(vertsBlendOffset + 1); + registerBoneAffectingSurface(vertsBlendOffset + 3); + registerBoneAffectingSurface(vertsBlendOffset + 5); + + vertsBlendOffset += 7; + } + + for (unsigned int vertListIndex = 0; vertListIndex < surface->vertListCount; vertListIndex++) + { + affectingBones.emplace(static_cast(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); + +#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 + std::map parentsToRestore{}; + for (uint8_t 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; + + // 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; + +#if DEBUG + // should be equal to model->numBones + unsigned 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); +#endif + + // 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] = SHRT_MAX; // 0 0 0 1 + + 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->modelSurfs->partBits[5] & 0x1) == 0x1) + { + // surface lod already converted (more efficient) + std::memcpy(lod->partBits, lod->modelSurfs->partBits, 6 * sizeof(uint32_t)); + 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: xmodel {} lod {} xmodelsurf {} surf #{}", index, model->numBones, model->name, lodIndex, lod->modelSurfs->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 vertex list of: xmodel {} lod {} xmodelsurf {} surf #{}\n", index, model->numBones, model->name, lodIndex, lod->modelSurfs->name, 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 p = GetIndexOfBone(model, beforeVal); + const auto index = GetIndexOfBone(model, key); + SetParentIndexOfBone(model, index, p); + } + + return atPosition; // Bone index of added bone + }; + + + void IXModel::TransferWeights(Game::XModel* model, const uint8_t origin, const uint8_t destination) + { + 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]; + + 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; + + 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) + { + index = destination; + + if (index < 0 || index >= model->numBones) + { + 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 == origin) + { + if (index < 0 || index >= model->numBones) + { + assert(false); + } + + 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::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) + { + if (model->name == "body_airport_com_a"s) + { + 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/AssetInterfaces/IXModel.hpp b/src/Components/Modules/AssetInterfaces/IXModel.hpp index 2c68489a..df4c54e4 100644 --- a/src/Components/Modules/AssetInterfaces/IXModel.hpp +++ b/src/Components/Modules/AssetInterfaces/IXModel.hpp @@ -10,5 +10,20 @@ 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); + + 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/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/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/Components/Modules/ZoneBuilder.cpp b/src/Components/Modules/ZoneBuilder.cpp index d045a34a..455b0136 100644 --- a/src/Components/Modules/ZoneBuilder.cpp +++ b/src/Components/Modules/ZoneBuilder.cpp @@ -8,6 +8,9 @@ #include "AssetInterfaces/ILocalizeEntry.hpp" +#include "Events.hpp" +#include "Branding.hpp" + namespace Components { std::string ZoneBuilder::TraceZone; @@ -21,7 +24,10 @@ namespace Components iw4of::api ZoneBuilder::ExporterAPI(GetExporterAPIParams()); std::string ZoneBuilder::DumpingZone{}; - ZoneBuilder::Zone::Zone(const std::string& name) : indexStart(0), externalSize(0), + 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. // 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 +35,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() @@ -107,7 +119,7 @@ namespace Components return &iw4ofApi; } - void ZoneBuilder::Zone::Zone::build() + void ZoneBuilder::Zone::build() { if (!this->dataMap.isValid()) { @@ -148,7 +160,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 +461,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 +771,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; @@ -789,7 +806,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); @@ -988,10 +1005,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"); @@ -1150,6 +1167,168 @@ 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); + + 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 @@ -1258,14 +1437,13 @@ 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()) { ZoneBuilder::TraceAssets.emplace_back(std::make_pair(type, name)); -#ifdef _DEBUG - OutputDebugStringA(Utils::String::Format("%s\n", name)); -#endif } }); @@ -1274,70 +1452,8 @@ namespace Components 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; - - Logger::Print("Loading zone '{}'...\n", zone); - - { - struct asset_t - { - 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); - if (assetHeader.data) - { - ExporterAPI.write(asset.type, assetHeader.data); - } - else - { - Logger::Warning(Game::conChannel_t::CON_CHANNEL_ERROR, "Asset {} has disappeared while dumping!", asset.name); - } - } - } - - 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::ASSET_TYPE_RAWFILE, "default"); // Lock until zone is unloaded - Logger::Print("Zone '{}' dumped", ZoneBuilder::DumpingZone); - ZoneBuilder::DumpingZone = std::string(); + ZoneBuilder::DumpZone(zone); }); Command::Add("verifyzone", [](const Command::Params* params) @@ -1345,30 +1461,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 +1471,7 @@ namespace Components } Logger::Print("\n"); + unload(); }); Command::Add("buildzone", [](const Command::Params* params) @@ -1389,6 +1484,26 @@ namespace Components Zone(zoneName).build(); }); + Command::Add("buildmod", [](const Command::Params* params) + { + 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 = fs_game.get(); + const std::string dir = "mods/" + modName; + Utils::IO::CreateDir(dir); + + fs_game.set(dir); + + Zone("mod", modName, dir + "/mod.ff").build(); + + fs_game.set(previousFsGame); + }); + Command::Add("buildall", []() { auto path = std::format("{}\\zone_source", (*Game::fs_basepath)->current.string); @@ -1405,23 +1520,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 +1539,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 +1554,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 +1574,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 +1585,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..ad4a049b 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; @@ -124,10 +126,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; }; @@ -137,7 +135,10 @@ 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(); static iw4of::api* GetExporter(); @@ -161,6 +162,10 @@ 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(); static void CommandThreadCallback(); diff --git a/src/Game/Structs.hpp b/src/Game/Structs.hpp index 21842c63..d1233fb5 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]; }; @@ -1066,7 +1066,7 @@ namespace Game struct XSurfaceVertexInfo { - __int16 vertCount[4]; + unsigned __int16 vertCount[4]; unsigned __int16* vertsBlend; }; @@ -1133,6 +1133,12 @@ namespace Game unsigned __int16 triCount; XSurfaceCollisionTree* collisionTree; }; + + struct DObjSkelMat + { + float axis[3][4]; + float origin[4]; + }; struct XSurface { @@ -7112,7 +7118,7 @@ namespace Game IMG_FORMAT_COUNT = 0x17, }; - enum $25EF9448C800B18F0C83DB367159AFD6 + enum XAnimPartType { PART_TYPE_NO_QUAT = 0x0, PART_TYPE_HALF_QUAT = 0x1, @@ -8362,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 {