From 52f6f2f15b5c63243e8bf8bdec8db21c671510d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francesco=20Borz=C3=AC?= Date: Sat, 1 May 2021 14:18:29 +0200 Subject: [PATCH] fix(Core/Unit): potential crash + extract duplicated code (#5303) --- src/server/game/Entities/Unit/Unit.cpp | 75 +++++++++++++------------- src/server/game/Entities/Unit/Unit.h | 2 + 2 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/server/game/Entities/Unit/Unit.cpp b/src/server/game/Entities/Unit/Unit.cpp index fa401436c..b28228f01 100644 --- a/src/server/game/Entities/Unit/Unit.cpp +++ b/src/server/game/Entities/Unit/Unit.cpp @@ -11241,25 +11241,7 @@ uint32 Unit::SpellDamageBonusTaken(Unit* caster, SpellInfo const* spellProto, ui if (spellProto->ValidateAttribute6SpellDamageMods(caster, *i, damagetype == DOT)) AddPct(TakenTotalMod, (*i)->GetAmount()); - // .. taken pct: dummy auras - AuraEffectList const& mDummyAuras = GetAuraEffectsByType(SPELL_AURA_DUMMY); - for (AuraEffectList::const_iterator i = mDummyAuras.begin(); i != mDummyAuras.end(); ++i) - { - switch ((*i)->GetSpellInfo()->SpellIconID) - { - // Cheat Death - case 2109: - if ((*i)->GetMiscValue() & SPELL_SCHOOL_MASK_NORMAL) - { - // Patch 2.4.3: The resilience required to reach the 90% damage reduction cap - // is 22.5% critical strike damage reduction, or 444 resilience. - // To calculate for 90%, we multiply the 100% by 4 (22.5% * 4 = 90%) - float mod = -1.0f * GetMeleeCritDamageReduction(400); - AddPct(TakenTotalMod, std::max(mod, float((*i)->GetAmount()))); - } - break; - } - } + TakenTotalMod = processDummyAuras(TakenTotalMod); // From caster spells if (caster) @@ -11354,6 +11336,41 @@ uint32 Unit::SpellDamageBonusTaken(Unit* caster, SpellInfo const* spellProto, ui return uint32(std::max(tmpDamage, 0.0f)); } +float Unit::processDummyAuras(float TakenTotalMod) const +{ + // note: old code coming from TC, just extracted here to remove the code duplication + solve potential crash + // see: https://github.com/TrinityCore/TrinityCore/commit/c85710e148d75450baedf6632b9ca6fd40b4148e + + // .. taken pct: dummy auras + auto const& mDummyAuras = GetAuraEffectsByType(SPELL_AURA_DUMMY); + for (auto i = mDummyAuras.begin(); i != mDummyAuras.end(); ++i) + { + if (!(*i) || !(*i)->GetSpellInfo()) + { + continue; + } + + if (auto spellIconId = (*i)->GetSpellInfo()->SpellIconID) + { + switch (spellIconId) + { + // Cheat Death + case 2109: + if ((*i)->GetMiscValue() & SPELL_SCHOOL_MASK_NORMAL) + { + // Patch 2.4.3: The resilience required to reach the 90% damage reduction cap + // is 22.5% critical strike damage reduction, or 444 resilience. + // To calculate for 90%, we multiply the 100% by 4 (22.5% * 4 = 90%) + float mod = -1.0f * GetMeleeCritDamageReduction(400); + AddPct(TakenTotalMod, std::max(mod, float((*i)->GetAmount()))); + } + break; + } + } + } + return TakenTotalMod; +} + int32 Unit::SpellBaseDamageBonusDone(SpellSchoolMask schoolMask) { int32 DoneAdvertisedBenefit = 0; @@ -12591,25 +12608,7 @@ uint32 Unit::MeleeDamageBonusTaken(Unit* attacker, uint32 pdamage, WeaponAttackT } } - // .. taken pct: dummy auras - AuraEffectList const& mDummyAuras = GetAuraEffectsByType(SPELL_AURA_DUMMY); - for (AuraEffectList::const_iterator i = mDummyAuras.begin(); i != mDummyAuras.end(); ++i) - { - switch ((*i)->GetSpellInfo()->SpellIconID) - { - // Cheat Death - case 2109: - if ((*i)->GetMiscValue() & SPELL_SCHOOL_MASK_NORMAL) - { - // Patch 2.4.3: The resilience required to reach the 90% damage reduction cap - // is 22.5% critical strike damage reduction, or 444 resilience. - // To calculate for 90%, we multiply the 100% by 4 (22.5% * 4 = 90%) - float mod = -1.0f * GetMeleeCritDamageReduction(400); - AddPct(TakenTotalMod, std::max(mod, float((*i)->GetAmount()))); - } - break; - } - } + TakenTotalMod = processDummyAuras(TakenTotalMod); // .. taken pct: class scripts /*AuraEffectList const& mclassScritAuras = GetAuraEffectsByType(SPELL_AURA_OVERRIDE_CLASS_SCRIPTS); diff --git a/src/server/game/Entities/Unit/Unit.h b/src/server/game/Entities/Unit/Unit.h index 602e14141..f6e7414f7 100644 --- a/src/server/game/Entities/Unit/Unit.h +++ b/src/server/game/Entities/Unit/Unit.h @@ -2620,6 +2620,8 @@ private: uint32 _oldFactionId; ///< faction before charm bool m_petCatchUp; + + float processDummyAuras(float TakenTotalMod) const; }; namespace acore