clean it up!

This commit is contained in:
Roxanne 2024-01-26 14:10:13 +01:00
parent 1bd6f0c4ad
commit 1edf6ce398
5 changed files with 756 additions and 421 deletions

View File

@ -1,8 +1,12 @@
#include <STDInclude.hpp> #include <STDInclude.hpp>
#include <stdint.h>
#include "IXModel.hpp" #include "IXModel.hpp"
namespace Assets namespace Assets
{ {
void IXModel::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder) void IXModel::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder)
{ {
header->model = builder->getIW4OfApi()->read<Game::XModel>(Game::XAssetType::ASSET_TYPE_XMODEL, name); header->model = builder->getIW4OfApi()->read<Game::XModel>(Game::XAssetType::ASSET_TYPE_XMODEL, name);
@ -257,4 +261,683 @@ namespace Assets
buffer->popBlock(); 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<uint8_t>(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<uint8_t>(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<uint8_t> affectingBones{};
const auto registerBoneAffectingSurface = [&](unsigned int offset) {
uint8_t index = static_cast<uint8_t>(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<uint8_t>(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<uint8_t> affectingBones{};
const auto registerBoneAffectingSurface = [&](unsigned int offset) {
uint8_t index = static_cast<uint8_t>(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<std::string, std::string> 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<uint16_t>(newBoneCount);
const auto newMats = allocator.allocateArray<Game::DObjAnimMat>(newBoneCount);
const auto newBoneInfo = allocator.allocateArray<Game::XBoneInfo>(newBoneCount);
const auto newPartsClassification = allocator.allocateArray<uint8_t>(newBoneCount);
const auto newQuats = allocator.allocateArray<int16_t>(4 * newBoneCountMinusRoot);
const auto newTrans = allocator.allocateArray<float>(3 * newBoneCountMinusRoot);
const auto newParentList = allocator.allocateArray<uint8_t>(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<uint16_t>(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<int>(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<unsigned short>(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<unsigned short>(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<int>(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<unsigned short>(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<unsigned short>(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("");
}
}
} }

View File

@ -10,5 +10,17 @@ namespace Assets
void save(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder) override; void save(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder) override;
void mark(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; 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);
}; };
} }

View File

@ -1150,6 +1150,35 @@ namespace Components
return params; return params;
} }
std::function<void()> ZoneBuilder::LoadZoneWithTrace(const std::string& zone, OUT std::vector<std::pair<Game::XAssetType, std::string>>& 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() ZoneBuilder::ZoneBuilder()
{ {
// ReSharper disable CppStaticAssertFailure // ReSharper disable CppStaticAssertFailure
@ -1263,9 +1292,6 @@ namespace Components
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)); ZoneBuilder::TraceAssets.emplace_back(std::make_pair(type, name));
#ifdef _DEBUG
OutputDebugStringA(Utils::String::Format("%s\n", name));
#endif
} }
}); });
@ -1274,68 +1300,35 @@ namespace Components
if (params->size() < 2) return; if (params->size() < 2) return;
std::string zone = params->get(1); std::string zone = params->get(1);
ZoneBuilder::DumpingZone = zone; ZoneBuilder::DumpingZone = zone;
ZoneBuilder::RefreshExporterWorkDirectory(); ZoneBuilder::RefreshExporterWorkDirectory();
Game::XZoneInfo info; std::vector<std::pair<Game::XAssetType, std::string>> assets{};
info.name = zone.data(); const auto unload = ZoneBuilder::LoadZoneWithTrace(zone, assets);
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<asset_t> 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); Logger::Print("Dumping zone '{}'...\n", zone);
handle(); // Release
for (const auto& asset : assets) for (const auto& asset : assets)
{ {
const auto assetHeader = Game::DB_FindXAssetHeader(asset.type, asset.name); 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) if (assetHeader.data)
{ {
ExporterAPI.write(asset.type, assetHeader.data); ExporterAPI.write(type, assetHeader.data);
} }
else 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); unload();
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); Logger::Print("Zone '{}' dumped", ZoneBuilder::DumpingZone);
ZoneBuilder::DumpingZone = std::string(); ZoneBuilder::DumpingZone = std::string();
}); });
@ -1345,30 +1338,8 @@ namespace Components
if (params->size() < 2) return; if (params->size() < 2) return;
std::string zone = params->get(1); std::string zone = params->get(1);
std::vector<std::pair<Game::XAssetType, std::string>> assets{};
ZoneBuilder::BeginAssetTrace(zone); const auto unload = ZoneBuilder::LoadZoneWithTrace(zone, assets);
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());
int count = 0; int count = 0;
for (auto i = assets.begin(); i != assets.end(); ++i, ++count) for (auto i = assets.begin(); i != assets.end(); ++i, ++count)
@ -1377,6 +1348,7 @@ namespace Components
} }
Logger::Print("\n"); Logger::Print("\n");
unload();
}); });
Command::Add("buildzone", [](const Command::Params* params) Command::Add("buildzone", [](const Command::Params* params)
@ -1405,23 +1377,6 @@ namespace Components
} }
}); });
static std::set<std::string> curTechsets_list;
static std::set<std::string> 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) AssetHandler::OnLoad([](Game::XAssetType type, Game::XAssetHeader asset, [[maybe_unused]] const std::string& name, [[maybe_unused]] bool* restrict)
{ {
if (type != Game::ASSET_TYPE_SOUND) if (type != Game::ASSET_TYPE_SOUND)
@ -1441,6 +1396,11 @@ namespace Components
{ {
// ouch // ouch
// This should never happen and will cause a memory leak // 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 // Let's change it to a streamed sound instead
thisSound.soundFile->type = Game::SAT_STREAMED; thisSound.soundFile->type = Game::SAT_STREAMED;
@ -1451,264 +1411,12 @@ namespace Components
auto dir = virtualPath.remove_filename().string(); auto dir = virtualPath.remove_filename().string();
dir = dir.substr(0, dir.size() - 1); // remove / dir = dir.substr(0, dir.size() - 1); // remove /
thisSound.soundFile->u.streamSnd.filename.info.raw.dir = Utils::Memory::DuplicateString(dir); 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<const char*>(0x649E740);
Utils::Hook::Set<const char*>(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<const char*>(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<const char*>(0x649E740, language);
Logger::Print("Building zone 'techsets/techsets'...\n");
Zone("techsets/techsets").build();
});
Command::Add("listassets", [](const Command::Params* params) Command::Add("listassets", [](const Command::Params* params)
{ {
if (params->size() < 2) return; if (params->size() < 2) return;
@ -1723,39 +1431,6 @@ namespace Components
}, &type, false); }, &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(); 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
} }

View File

@ -124,10 +124,6 @@ namespace Components
ZoneBuilder(); ZoneBuilder();
~ZoneBuilder(); ~ZoneBuilder();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
bool unitTest() override;
#endif
static bool IsEnabled(); static bool IsEnabled();
static bool IsDumpingZone() { return DumpingZone.length() > 0; }; static bool IsDumpingZone() { return DumpingZone.length() > 0; };
@ -161,6 +157,8 @@ namespace Components
static iw4of::params_t GetExporterAPIParams(); static iw4of::params_t GetExporterAPIParams();
static std::function<void()> LoadZoneWithTrace(const std::string& zone, OUT std::vector<std::pair<Game::XAssetType, std::string>> &assets);
static void Com_Quitf_t(); static void Com_Quitf_t();
static void CommandThreadCallback(); static void CommandThreadCallback();

View File

@ -7118,7 +7118,7 @@ namespace Game
IMG_FORMAT_COUNT = 0x17, IMG_FORMAT_COUNT = 0x17,
}; };
enum $25EF9448C800B18F0C83DB367159AFD6 enum XAnimPartType
{ {
PART_TYPE_NO_QUAT = 0x0, PART_TYPE_NO_QUAT = 0x0,
PART_TYPE_HALF_QUAT = 0x1, PART_TYPE_HALF_QUAT = 0x1,