From 3262bc3a86b25ffd09c2f1436149c100c2b3b28e Mon Sep 17 00:00:00 2001 From: David Markowitz <39972741+EmosewaMC@users.noreply.github.com> Date: Sun, 24 Mar 2024 19:43:01 -0700 Subject: [PATCH] chore: Remove `new`s in Behavior members (#1504) * Remove news in behavior members Tested that GrowingFlowers still have their SkillEvent fired with the correct parameters, gftikitorch works, sharks eating stinky fish still work * explicitly default move assignment and copy operators/constructors --------- Co-authored-by: jadebenn --- dGame/dBehaviors/Behavior.cpp | 40 +++++++++---------------- dGame/dBehaviors/Behavior.h | 14 ++++++--- dGame/dBehaviors/SkillEventBehavior.cpp | 11 ++++--- 3 files changed, 29 insertions(+), 36 deletions(-) diff --git a/dGame/dBehaviors/Behavior.cpp b/dGame/dBehaviors/Behavior.cpp index 36607a66..ce1ee053 100644 --- a/dGame/dBehaviors/Behavior.cpp +++ b/dGame/dBehaviors/Behavior.cpp @@ -335,26 +335,22 @@ void Behavior::PlayFx(std::u16string type, const LWOOBJID target, const LWOOBJID const auto typeString = GeneralUtils::UTF16ToWTF8(type); - if (m_effectNames == nullptr) { - m_effectNames = new std::unordered_map(); - } else { - const auto pair = m_effectNames->find(typeString); + const auto itr = m_effectNames.find(typeString); - if (type.empty()) { - type = GeneralUtils::ASCIIToUTF16(*m_effectType); - } + if (type.empty()) { + type = GeneralUtils::ASCIIToUTF16(m_effectType); + } - if (pair != m_effectNames->end()) { - if (renderComponent == nullptr) { - GameMessages::SendPlayFXEffect(targetEntity, effectId, type, pair->second, secondary, 1, 1, true); - - return; - } - - renderComponent->PlayEffect(effectId, type, pair->second, secondary); + if (itr != m_effectNames.end()) { + if (renderComponent == nullptr) { + GameMessages::SendPlayFXEffect(targetEntity, effectId, type, itr->second, secondary, 1, 1, true); return; } + + renderComponent->PlayEffect(effectId, type, itr->second, secondary); + + return; } // The SQlite result object becomes invalid if the query object leaves scope. @@ -388,12 +384,12 @@ void Behavior::PlayFx(std::u16string type, const LWOOBJID target, const LWOOBJID type = GeneralUtils::ASCIIToUTF16(typeResult); - m_effectType = new std::string(typeResult); + m_effectType = typeResult; } result.finalize(); - m_effectNames->insert_or_assign(typeString, name); + m_effectNames.insert_or_assign(typeString, name); if (renderComponent == nullptr) { GameMessages::SendPlayFXEffect(targetEntity, effectId, type, name, secondary, 1, 1, true); @@ -423,7 +419,6 @@ Behavior::Behavior(const uint32_t behaviorId) { if (behaviorId == 0) { this->m_effectId = 0; - this->m_effectHandle = nullptr; this->m_templateId = BehaviorTemplates::BEHAVIOR_EMPTY; } @@ -432,7 +427,6 @@ Behavior::Behavior(const uint32_t behaviorId) { LOG("Failed to load behavior with id (%i)!", behaviorId); this->m_effectId = 0; - this->m_effectHandle = nullptr; this->m_templateId = BehaviorTemplates::BEHAVIOR_EMPTY; return; @@ -442,7 +436,7 @@ Behavior::Behavior(const uint32_t behaviorId) { this->m_effectId = templateInDatabase.effectID; - this->m_effectHandle = *templateInDatabase.effectHandle != "" ? new std::string(*templateInDatabase.effectHandle) : nullptr; + this->m_effectHandle = *templateInDatabase.effectHandle; } @@ -507,9 +501,3 @@ void Behavior::Calculate(BehaviorContext* context, RakNet::BitStream& bitStream, void Behavior::SyncCalculation(BehaviorContext* context, RakNet::BitStream& bitStream, BehaviorBranchContext branch) { } - -Behavior::~Behavior() { - delete m_effectNames; - delete m_effectType; - delete m_effectHandle; -} diff --git a/dGame/dBehaviors/Behavior.h b/dGame/dBehaviors/Behavior.h index af7fd206..e395701a 100644 --- a/dGame/dBehaviors/Behavior.h +++ b/dGame/dBehaviors/Behavior.h @@ -41,9 +41,9 @@ public: uint32_t m_behaviorId; BehaviorTemplates m_templateId; uint32_t m_effectId; - std::string* m_effectHandle = nullptr; - std::unordered_map* m_effectNames = nullptr; - std::string* m_effectType = nullptr; + std::string m_effectHandle; + std::unordered_map m_effectNames; + std::string m_effectType; /* * Behavior parameters @@ -88,5 +88,11 @@ public: */ explicit Behavior(uint32_t behaviorId); - virtual ~Behavior(); + virtual ~Behavior() = default; + + Behavior(const Behavior& other) = default; + Behavior(Behavior&& other) = default; + + Behavior& operator=(const Behavior& other) = default; + Behavior& operator=(Behavior&& other) = default; }; diff --git a/dGame/dBehaviors/SkillEventBehavior.cpp b/dGame/dBehaviors/SkillEventBehavior.cpp index 2de801a2..9161f5d3 100644 --- a/dGame/dBehaviors/SkillEventBehavior.cpp +++ b/dGame/dBehaviors/SkillEventBehavior.cpp @@ -9,17 +9,16 @@ void SkillEventBehavior::Handle(BehaviorContext* context, RakNet::BitStream& bit auto* target = Game::entityManager->GetEntity(branch.target); auto* caster = Game::entityManager->GetEntity(context->originator); - if (caster != nullptr && target != nullptr && this->m_effectHandle != nullptr && !this->m_effectHandle->empty()) { - target->GetScript()->OnSkillEventFired(target, caster, *this->m_effectHandle); + if (caster != nullptr && target != nullptr && !this->m_effectHandle.empty()) { + target->GetScript()->OnSkillEventFired(target, caster, this->m_effectHandle); } } -void -SkillEventBehavior::Calculate(BehaviorContext* context, RakNet::BitStream& bitStream, BehaviorBranchContext branch) { +void SkillEventBehavior::Calculate(BehaviorContext* context, RakNet::BitStream& bitStream, BehaviorBranchContext branch) { auto* target = Game::entityManager->GetEntity(branch.target); auto* caster = Game::entityManager->GetEntity(context->originator); - if (caster != nullptr && target != nullptr && this->m_effectHandle != nullptr && !this->m_effectHandle->empty()) { - target->GetScript()->OnSkillEventFired(target, caster, *this->m_effectHandle); + if (caster != nullptr && target != nullptr && !this->m_effectHandle.empty()) { + target->GetScript()->OnSkillEventFired(target, caster, this->m_effectHandle); } }