Skip to content

Commit

Permalink
Improve performance of Anim techno pointer invalidation
Browse files Browse the repository at this point in the history
  • Loading branch information
Starkku committed Feb 24, 2025
1 parent 7667db7 commit 67477b7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
25 changes: 21 additions & 4 deletions src/Ext/Anim/Body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,32 @@
AnimExt::ExtContainer AnimExt::ExtMap;
std::vector<AnimClass*> AnimExt::AnimsWithAttachedParticles;

AnimExt::ExtData::~ExtData()
{
this->DeleteAttachedSystem();

if (this->Invoker)
TechnoExt::ExtMap.Find(this->Invoker)->AnimRefCount--;

if (this->ParentBuilding)
TechnoExt::ExtMap.Find(this->ParentBuilding)->AnimRefCount--;
}

void AnimExt::ExtData::SetInvoker(TechnoClass* pInvoker)
{
this->Invoker = pInvoker;
this->InvokerHouse = pInvoker ? pInvoker->Owner : nullptr;
this->SetInvoker(pInvoker, pInvoker ? pInvoker->Owner : nullptr);
}

void AnimExt::ExtData::SetInvoker(TechnoClass* pInvoker, HouseClass* pInvokerHouse)
{
if (pInvoker && this->Invoker != pInvoker)
{
if (this->Invoker)
TechnoExt::ExtMap.Find(this->Invoker)->AnimRefCount--;

TechnoExt::ExtMap.Find(pInvoker)->AnimRefCount++;
}

this->Invoker = pInvoker;
this->InvokerHouse = pInvokerHouse;
}
Expand Down Expand Up @@ -269,8 +287,7 @@ void AnimExt::SpawnFireAnims(AnimClass* pThis)
auto const pAnim = GameCreate<AnimClass>(pType, newCoords, 0, loopCount, 0x600u, 0, false);
pAnim->Owner = pThis->Owner;
auto const pExtNew = AnimExt::ExtMap.Find(pAnim);
pExtNew->Invoker = pExt->Invoker;
pExtNew->InvokerHouse = pExt->InvokerHouse;
pExtNew->SetInvoker(pExt->Invoker, pExt->InvokerHouse);

if (attach && pThis->OwnerObject)
pAnim->SetOwnerObject(pThis->OwnerObject);
Expand Down
5 changes: 1 addition & 4 deletions src/Ext/Anim/Body.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ class AnimExt
void CreateAttachedSystem();
void DeleteAttachedSystem();

virtual ~ExtData()
{
this->DeleteAttachedSystem();
}
virtual ~ExtData() override;

virtual void InvalidatePointer(void* ptr, bool bRemoved) override { }

Expand Down
1 change: 1 addition & 0 deletions src/Ext/Building/Hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ DEFINE_HOOK(0x4519A2, BuildingClass_UpdateAnim_SetParentBuilding, 0x6)

auto const pAnimExt = AnimExt::ExtMap.Find(pAnim);
pAnimExt->ParentBuilding = pThis;
TechnoExt::ExtMap.Find(pThis)->AnimRefCount++;

return 0;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Ext/Techno/Body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ TechnoExt::ExtData::~ExtData()
vec.erase(std::remove(vec.begin(), vec.end(), this), vec.end());
}

AnimExt::InvalidateTechnoPointers(pThis);
if (this->AnimRefCount > 0)
AnimExt::InvalidateTechnoPointers(pThis);
}

bool TechnoExt::IsActiveIgnoreEMP(TechnoClass* pThis)
Expand Down Expand Up @@ -485,6 +486,7 @@ void TechnoExt::ExtData::Serialize(T& Stm)
.Process(this->LaserTrails)
.Process(this->AttachedEffects)
.Process(this->AE)
.Process(this->AnimRefCount)
.Process(this->ReceiveDamage)
.Process(this->PassengerDeletionTimer)
.Process(this->CurrentShieldType)
Expand Down
2 changes: 2 additions & 0 deletions src/Ext/Techno/Body.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class TechnoExt
std::vector<LaserTrailClass> LaserTrails;
std::vector<std::unique_ptr<AttachEffectClass>> AttachedEffects;
AttachEffectTechnoProperties AE;
int AnimRefCount; // Used to keep track of how many times this techno is referenced in anims f.ex Invoker, ParentBuilding etc., for pointer invalidation.
bool ReceiveDamage;
bool LastKillWasTeamTarget;
CDTimerClass PassengerDeletionTimer;
Expand Down Expand Up @@ -66,6 +67,7 @@ class TechnoExt
, LaserTrails {}
, AttachedEffects {}
, AE {}
, AnimRefCount { 0 }
, ReceiveDamage { false }
, LastKillWasTeamTarget { false }
, PassengerDeletionTimer {}
Expand Down

0 comments on commit 67477b7

Please sign in to comment.