Skip to content

Commit

Permalink
Replace raw Trajectory ptr with unique_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
chaserli committed Oct 4, 2024
1 parent db19446 commit 27530fa
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 97 deletions.
8 changes: 2 additions & 6 deletions src/Ext/Bullet/Body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ void BulletExt::ExtData::Serialize(T& Stm)
.Process(this->LaserTrails)
.Process(this->SnappedToTarget)
.Process(this->DamageNumberOffset)
;

this->Trajectory = PhobosTrajectory::ProcessFromStream(Stm, this->Trajectory);
.Process(this->Trajectory) // Keep this shit at last
;
}

void BulletExt::ExtData::LoadFromStream(PhobosStreamReader& Stm)
Expand Down Expand Up @@ -213,10 +213,6 @@ DEFINE_HOOK(0x4664BA, BulletClass_CTOR, 0x5)
DEFINE_HOOK(0x4665E9, BulletClass_DTOR, 0xA)
{
GET(BulletClass*, pItem, ESI);

if (auto pTraj = BulletExt::ExtMap.Find(pItem)->Trajectory)
delete pTraj;

BulletExt::ExtMap.Remove(pItem);
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Ext/Bullet/Body.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class BulletExt
bool SnappedToTarget; // Used for custom trajectory projectile target snap checks
int DamageNumberOffset;

PhobosTrajectory* Trajectory; // TODO: why not unique_ptr
TrajectoryPointer Trajectory;

ExtData(BulletClass* OwnerObject) : Extension<BulletClass>(OwnerObject)
, TypeExtData { nullptr }
Expand Down
4 changes: 2 additions & 2 deletions src/Ext/Bullet/Trajectories/BombardTrajectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

#include <Ext/BulletType/Body.h>

PhobosTrajectory* BombardTrajectoryType::CreateInstance() const
std::unique_ptr<PhobosTrajectory> BombardTrajectoryType::CreateInstance() const
{
return new BombardTrajectory(this);
return std::make_unique<BombardTrajectory>(this);
}

template<typename T>
Expand Down
2 changes: 1 addition & 1 deletion src/Ext/Bullet/Trajectories/BombardTrajectory.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class BombardTrajectoryType final : public PhobosTrajectoryType

virtual bool Load(PhobosStreamReader& Stm, bool RegisterForChange) override;
virtual bool Save(PhobosStreamWriter& Stm) const override;
virtual PhobosTrajectory* CreateInstance() const override;
virtual std::unique_ptr<PhobosTrajectory> CreateInstance() const override;

virtual void Read(CCINIClass* const pINI, const char* pSection) override;

Expand Down
119 changes: 57 additions & 62 deletions src/Ext/Bullet/Trajectories/PhobosTrajectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,50 @@ bool TrajectoryTypePointer::Save(PhobosStreamWriter& Stm) const
return true;
}

bool TrajectoryPointer::Load(PhobosStreamReader& Stm, bool registerForChange)
{
PhobosTrajectory* PTR = nullptr;
if (!Stm.Load(PTR))
return false;
TrajectoryFlag flag = TrajectoryFlag::Invalid;
if (PTR && Stm.Load(flag))
{
switch (flag)
{
case TrajectoryFlag::Straight:
_ptr = std::make_unique<StraightTrajectory>(noinit_t {});
break;
case TrajectoryFlag::Bombard:
_ptr = std::make_unique<BombardTrajectory>(noinit_t {});
break;
default:
_ptr.reset();
break;
}
if (_ptr.get())
{
// PhobosSwizzle::RegisterChange(PTR, _ptr.get()); // not used elsewhere yet, if anyone does then reenable this shit
_ptr->Flag = flag;
return _ptr->Load(Stm, registerForChange);
}
}
return true;
}

bool TrajectoryPointer::Save(PhobosStreamWriter& Stm) const
{
auto* raw = get();
Stm.Process(raw);
if (raw)
{
Stm.Process(raw->Flag);
return raw->Save(Stm);
}
return true;
}

// ------------------------------------------------------------------------------ //

bool PhobosTrajectoryType::Load(PhobosStreamReader& Stm, bool RegisterForChange)
{
Stm
Expand All @@ -111,71 +155,22 @@ bool PhobosTrajectoryType::Save(PhobosStreamWriter& Stm) const

bool PhobosTrajectory::Load(PhobosStreamReader& Stm, bool RegisterForChange)
{
Stm.Process(this->Flag, false);

Stm
.Process(this->Flag)
.Process(this->Speed)
;
return true;
}

bool PhobosTrajectory::Save(PhobosStreamWriter& Stm) const
{
Stm.Process(this->Flag);
Stm
.Process(this->Flag, false)
.Process(this->Speed)
;
return true;
}

#pragma region RedoAllThesePleaseItsInFactVerySimpleJustFollowTemplateDef_h
PhobosTrajectory* PhobosTrajectory::LoadFromStream(PhobosStreamReader& Stm)
{
PhobosTrajectory* pTraj = nullptr;
TrajectoryFlag flag = TrajectoryFlag::Invalid;
Stm.Process(pTraj, false);

if (pTraj)
{
Stm.Process(flag, false);
auto old = pTraj;
switch (flag)
{
case TrajectoryFlag::Straight:
pTraj = new StraightTrajectory(noinit_t {});
break;
case TrajectoryFlag::Bombard:
pTraj = new BombardTrajectory(noinit_t {});
break;
default:
return nullptr;
}

pTraj->Flag = flag;
pTraj->Load(Stm, false);
PhobosSwizzle::RegisterChange(old, pTraj);
}

return pTraj;
}

void PhobosTrajectory::WriteToStream(PhobosStreamWriter& Stm, PhobosTrajectory* pTraj)
{
Stm.Process(pTraj);
if (pTraj)
{
Stm.Process(pTraj->Flag);
pTraj->Save(Stm);
}
}

PhobosTrajectory* PhobosTrajectory::ProcessFromStream(PhobosStreamReader& Stm, PhobosTrajectory* pTraj)
{
UNREFERENCED_PARAMETER(pTraj);
return LoadFromStream(Stm);
}

PhobosTrajectory* PhobosTrajectory::ProcessFromStream(PhobosStreamWriter& Stm, PhobosTrajectory* pTraj)
{
WriteToStream(Stm, pTraj);
return pTraj;
}
#pragma endregion

DEFINE_HOOK(0x4666F7, BulletClass_AI_Trajectories, 0x6)
{
enum { Detonate = 0x467E53 };
Expand All @@ -185,7 +180,7 @@ DEFINE_HOOK(0x4666F7, BulletClass_AI_Trajectories, 0x6)
auto const pExt = BulletExt::ExtMap.Find(pThis);
bool detonate = false;

if (auto pTraj = pExt->Trajectory)
if (auto pTraj = pExt->Trajectory.get())
detonate = pTraj->OnAI(pThis);

if (detonate && !pThis->SpawnNextAnim)
Expand Down Expand Up @@ -218,7 +213,7 @@ DEFINE_HOOK(0x467E53, BulletClass_AI_PreDetonation_Trajectories, 0x6)

auto const pExt = BulletExt::ExtMap.Find(pThis);

if (auto pTraj = pExt->Trajectory)
if (auto pTraj = pExt->Trajectory.get())
pTraj->OnAIPreDetonate(pThis);

return 0;
Expand All @@ -232,7 +227,7 @@ DEFINE_HOOK(0x46745C, BulletClass_AI_Position_Trajectories, 0x7)

auto const pExt = BulletExt::ExtMap.Find(pThis);

if (auto pTraj = pExt->Trajectory)
if (auto pTraj = pExt->Trajectory.get())
pTraj->OnAIVelocity(pThis, pSpeed, pPosition);

return 0;
Expand All @@ -246,7 +241,7 @@ DEFINE_HOOK(0x4677D3, BulletClass_AI_TargetCoordCheck_Trajectories, 0x5)

auto const pExt = BulletExt::ExtMap.Find(pThis);

if (auto pTraj = pExt->Trajectory)
if (auto pTraj = pExt->Trajectory.get())
{
switch (pTraj->OnAITargetCoordCheck(pThis))
{
Expand Down Expand Up @@ -276,7 +271,7 @@ DEFINE_HOOK(0x467927, BulletClass_AI_TechnoCheck_Trajectories, 0x5)

auto const pExt = BulletExt::ExtMap.Find(pThis);

if (auto pTraj = pExt->Trajectory)
if (auto pTraj = pExt->Trajectory.get())
{
switch (pTraj->OnAITechnoCheck(pThis, pTechno))
{
Expand Down
57 changes: 35 additions & 22 deletions src/Ext/Bullet/Trajectories/PhobosTrajectory.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class PhobosTrajectory;
class PhobosTrajectoryType
{
public:
PhobosTrajectoryType(noinit_t) { }
PhobosTrajectoryType() { }
PhobosTrajectoryType(TrajectoryFlag flag) : Flag { flag }, Trajectory_Speed { 100.0 }
{ }

Expand All @@ -35,27 +35,12 @@ class PhobosTrajectoryType
virtual bool Save(PhobosStreamWriter& Stm) const;

virtual void Read(CCINIClass* const pINI, const char* pSection) = 0;
virtual PhobosTrajectory* CreateInstance() const = 0;
virtual std::unique_ptr<PhobosTrajectory> CreateInstance() const = 0;

TrajectoryFlag Flag;
Valueable<double> Trajectory_Speed;
};

class TrajectoryTypePointer
{
std::unique_ptr<PhobosTrajectoryType> _ptr {};
public:
explicit TrajectoryTypePointer(TrajectoryFlag flag);
explicit TrajectoryTypePointer() { }
void LoadFromINI(CCINIClass* pINI, const char* pSection);
bool Load(PhobosStreamReader& stm, bool registerForChange);
bool Save(PhobosStreamWriter& stm) const;
[[nodiscard]] PhobosTrajectoryType* operator->() const noexcept { return _ptr.get(); }
[[nodiscard]] PhobosTrajectoryType* get() const noexcept { return _ptr.get(); }
explicit operator bool() const noexcept { return _ptr.get() != nullptr; }
};


class PhobosTrajectory
{
public:
Expand All @@ -74,11 +59,6 @@ class PhobosTrajectory
virtual TrajectoryCheckReturnType OnAITargetCoordCheck(BulletClass* pBullet) = 0;
virtual TrajectoryCheckReturnType OnAITechnoCheck(BulletClass* pBullet, TechnoClass* pTechno) = 0;

static PhobosTrajectory* LoadFromStream(PhobosStreamReader& Stm);
static void WriteToStream(PhobosStreamWriter& Stm, PhobosTrajectory* pTraj);
static PhobosTrajectory* ProcessFromStream(PhobosStreamReader& Stm, PhobosTrajectory* pTraj);
static PhobosTrajectory* ProcessFromStream(PhobosStreamWriter& Stm, PhobosTrajectory* pTraj);

TrajectoryFlag Flag;
double Speed;
};
Expand Down Expand Up @@ -153,3 +133,36 @@ class PhobosTrajectory
* ^$^ *##^ *** ^$
*
*/

// I removed most part but kept a few so that you know what you are eating
class TrajectoryTypePointer
{
std::unique_ptr<PhobosTrajectoryType> _ptr {};
public:
explicit TrajectoryTypePointer(TrajectoryFlag flag);
explicit TrajectoryTypePointer() { }
TrajectoryTypePointer(const TrajectoryTypePointer&) = delete;
TrajectoryTypePointer& operator=(const TrajectoryTypePointer&) = delete;
void LoadFromINI(CCINIClass* pINI, const char* pSection);
bool Load(PhobosStreamReader& stm, bool registerForChange);
bool Save(PhobosStreamWriter& stm) const;
[[nodiscard]] PhobosTrajectoryType* operator->() const noexcept { return _ptr.get(); }
[[nodiscard]] PhobosTrajectoryType* get() const noexcept { return _ptr.get(); }
operator bool() const noexcept { return _ptr.get() != nullptr; }
};

class TrajectoryPointer
{
std::unique_ptr<PhobosTrajectory> _ptr;
public:
TrajectoryPointer(std::nullptr_t) : _ptr { nullptr } { }
TrajectoryPointer(const TrajectoryPointer&) = delete;
TrajectoryPointer& operator=(const TrajectoryPointer&) = delete;
TrajectoryPointer& operator=(std::unique_ptr<PhobosTrajectory>&& ptr) noexcept { _ptr = std::move(ptr); return *this; }
bool Load(PhobosStreamReader& stm, bool registerForChange);
bool Save(PhobosStreamWriter& stm) const;
[[nodiscard]] PhobosTrajectory* operator->() const noexcept { return _ptr.get(); }
[[nodiscard]] PhobosTrajectory* get() const noexcept { return _ptr.get(); }
operator bool() const noexcept { return _ptr.get() != nullptr; }
operator std::unique_ptr<PhobosTrajectory>() noexcept { return std::move(_ptr); }
};
4 changes: 2 additions & 2 deletions src/Ext/Bullet/Trajectories/StraightTrajectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#include <Ext/Bullet/Body.h>
#include <Ext/WeaponType/Body.h>

PhobosTrajectory* StraightTrajectoryType::CreateInstance() const
std::unique_ptr<PhobosTrajectory> StraightTrajectoryType::CreateInstance() const
{
return new StraightTrajectory(this);
return std::make_unique<StraightTrajectory>(this);
}

template<typename T>
Expand Down
2 changes: 1 addition & 1 deletion src/Ext/Bullet/Trajectories/StraightTrajectory.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class StraightTrajectoryType final : public PhobosTrajectoryType

virtual bool Load(PhobosStreamReader& Stm, bool RegisterForChange) override;
virtual bool Save(PhobosStreamWriter& Stm) const override;
virtual PhobosTrajectory* CreateInstance() const override;
virtual std::unique_ptr<PhobosTrajectory> CreateInstance() const override;
virtual void Read(CCINIClass* const pINI, const char* pSection) override;

Valueable<Leptons> DetonationDistance;
Expand Down

0 comments on commit 27530fa

Please sign in to comment.