Skip to content

Commit 56cf643

Browse files
authored
Merge pull request LandSandBoat#6735 from WinterSolstice8/m_errmsg_junk
[core] Add safety around m_errmsg in states
2 parents 71c557f + fa588ef commit 56cf643

8 files changed

Lines changed: 97 additions & 18 deletions

File tree

src/map/ai/states/ability_state.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,16 @@ CAbilityState::CAbilityState(CBattleEntity* PEntity, uint16 targid, uint16 abili
4545
}
4646
auto* PTarget = m_PEntity->IsValidTarget(m_targid, PAbility->getValidTarget(), m_errorMsg);
4747

48-
if (!PTarget || m_errorMsg)
48+
if (!PTarget || this->HasErrorMsg())
4949
{
50-
throw CStateInitException(m_errorMsg->copy());
50+
if (this->HasErrorMsg())
51+
{
52+
throw CStateInitException(m_errorMsg->copy());
53+
}
54+
else
55+
{
56+
throw CStateInitException(std::make_unique<CBasicPacket>());
57+
}
5158
}
5259
SetTarget(PTarget->targid);
5360
m_PAbility = std::make_unique<CAbility>(*PAbility);

src/map/ai/states/attack_state.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,20 @@ CAttackState::CAttackState(CBattleEntity* PEntity, uint16 targid)
3535
PEntity->SetBattleTargetID(targid);
3636
PEntity->SetBattleStartTime(server_clock::now());
3737
CAttackState::UpdateTarget();
38+
3839
if (!GetTarget() || m_errorMsg)
3940
{
4041
PEntity->SetBattleTargetID(0);
41-
throw CStateInitException(m_errorMsg->copy());
42+
if (this->HasErrorMsg())
43+
{
44+
throw CStateInitException(m_errorMsg->copy());
45+
}
46+
else
47+
{
48+
throw CStateInitException(std::make_unique<CBasicPacket>());
49+
}
4250
}
51+
4352
if (PEntity->PAI->PathFind)
4453
{
4554
PEntity->PAI->PathFind->Clear();

src/map/ai/states/item_state.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,16 @@ CItemState::CItemState(CCharEntity* PEntity, uint16 targid, uint8 loc, uint8 slo
8080
UpdateTarget(PEntity->IsValidTarget(targid, m_PItem->getValidTarget(), m_errorMsg));
8181
auto* PTarget = GetTarget();
8282

83-
if (!PTarget || m_errorMsg)
83+
if (!PTarget || this->HasErrorMsg())
8484
{
85-
throw CStateInitException(m_errorMsg->copy());
85+
if (this->HasErrorMsg())
86+
{
87+
throw CStateInitException(m_errorMsg->copy());
88+
}
89+
else
90+
{
91+
throw CStateInitException(std::make_unique<CBasicPacket>());
92+
}
8693
}
8794

8895
auto [error, param, value] = luautils::OnItemCheck(PTarget, m_PItem, ITEMCHECK::NONE, m_PEntity);
@@ -294,7 +301,14 @@ void CItemState::InterruptItem(action_t& action)
294301
actionTarget.messageID = 0;
295302
actionTarget.knockback = 0;
296303

297-
m_PEntity->pushPacket(m_errorMsg->copy());
304+
if (this->HasErrorMsg())
305+
{
306+
m_PEntity->pushPacket(m_errorMsg->copy());
307+
}
308+
else
309+
{
310+
throw CStateInitException(std::make_unique<CBasicPacket>());
311+
}
298312
}
299313
}
300314

src/map/ai/states/magic_state.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,28 @@ CMagicState::CMagicState(CBattleEntity* PEntity, uint16 targid, SpellID spellid,
5050
m_PSpell = PSpell->clone();
5151

5252
auto* PTarget = m_PEntity->IsValidTarget(m_targid, m_PSpell->getValidTarget(), m_errorMsg);
53-
if (!PTarget || m_errorMsg)
53+
if (!PTarget || this->HasErrorMsg())
5454
{
55-
throw CStateInitException(m_errorMsg->copy());
55+
if (this->HasErrorMsg())
56+
{
57+
throw CStateInitException(m_errorMsg->copy());
58+
}
59+
else
60+
{
61+
throw CStateInitException(std::make_unique<CBasicPacket>());
62+
}
5663
}
5764

5865
if (!CanCastSpell(PTarget, false))
5966
{
60-
throw CStateInitException(m_errorMsg->copy());
67+
if (HasErrorMsg())
68+
{
69+
throw CStateInitException(m_errorMsg->copy());
70+
}
71+
else
72+
{
73+
throw CStateInitException(std::make_unique<CBasicPacket>());
74+
}
6175
}
6276

6377
auto errorMsg = luautils::OnMagicCastingCheck(m_PEntity, PTarget, GetSpell());

src/map/ai/states/mobskill_state.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,16 @@ CMobSkillState::CMobSkillState(CBattleEntity* PEntity, uint16 targid, uint16 wsi
4747

4848
auto* PTarget = m_PEntity->IsValidTarget(m_targid, skill->getValidTargets(), m_errorMsg);
4949

50-
if (!PTarget || m_errorMsg)
50+
if (!PTarget | this->HasErrorMsg())
5151
{
52-
throw CStateInitException(m_errorMsg->copy());
52+
if (this->HasErrorMsg())
53+
{
54+
throw CStateInitException(m_errorMsg->copy());
55+
}
56+
else
57+
{
58+
throw CStateInitException(std::make_unique<CBasicPacket>());
59+
}
5360
}
5461

5562
m_PSkill = std::make_unique<CMobSkill>(*skill);

src/map/ai/states/petskill_state.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,16 @@ CPetSkillState::CPetSkillState(CPetEntity* PEntity, uint16 targid, uint16 wsid)
4646

4747
auto* PTarget = m_PEntity->IsValidTarget(m_targid, skill->getValidTargets(), m_errorMsg);
4848

49-
if (!PTarget || m_errorMsg)
49+
if (!PTarget || this->HasErrorMsg())
5050
{
51-
throw CStateInitException(m_errorMsg->copy());
51+
if (this->HasErrorMsg())
52+
{
53+
throw CStateInitException(m_errorMsg->copy());
54+
}
55+
else
56+
{
57+
throw CStateInitException(std::make_unique<CBasicPacket>());
58+
}
5259
}
5360

5461
m_PSkill = std::make_unique<CPetSkill>(*skill);

src/map/ai/states/range_state.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,28 @@ CRangeState::CRangeState(CBattleEntity* PEntity, uint16 targid)
3636
{
3737
auto* PTarget = m_PEntity->IsValidTarget(m_targid, TARGET_ENEMY, m_errorMsg);
3838

39-
if (!PTarget || m_errorMsg)
39+
if (!PTarget || this->HasErrorMsg())
4040
{
41-
throw CStateInitException(m_errorMsg->copy());
41+
if (this->HasErrorMsg())
42+
{
43+
throw CStateInitException(m_errorMsg->copy());
44+
}
45+
else
46+
{
47+
throw CStateInitException(std::make_unique<CBasicPacket>());
48+
}
4249
}
4350

4451
if (!CanUseRangedAttack(PTarget, false))
4552
{
46-
throw CStateInitException(m_errorMsg->copy());
53+
if (this->HasErrorMsg())
54+
{
55+
throw CStateInitException(m_errorMsg->copy());
56+
}
57+
else
58+
{
59+
throw CStateInitException(std::make_unique<CBasicPacket>());
60+
}
4761
}
4862

4963
if (distance(m_PEntity->loc.p, PTarget->loc.p) > 25)

src/map/ai/states/weaponskill_state.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,16 @@ CWeaponSkillState::CWeaponSkillState(CBattleEntity* PEntity, uint16 targid, uint
4242
auto target_flags = battleutils::isValidSelfTargetWeaponskill(wsid) ? TARGET_SELF : TARGET_ENEMY;
4343
auto* PTarget = m_PEntity->IsValidTarget(m_targid, target_flags, m_errorMsg);
4444

45-
if (!PTarget || m_errorMsg)
45+
if (!PTarget || this->HasErrorMsg())
4646
{
47-
throw CStateInitException(m_errorMsg->copy());
47+
if (this->HasErrorMsg())
48+
{
49+
throw CStateInitException(m_errorMsg->copy());
50+
}
51+
else
52+
{
53+
throw CStateInitException(std::make_unique<CBasicPacket>());
54+
}
4855
}
4956

5057
if (!m_PEntity->CanSeeTarget(PTarget, false))

0 commit comments

Comments
 (0)