diff options
author | jackpoz <giacomopoz@gmail.com> | 2018-02-04 21:47:45 +0100 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2021-06-19 23:33:21 +0200 |
commit | b9159407e0d224423e2084dc15bcd28c3b402e24 (patch) | |
tree | 7bfb61c9f4bddef59d5aa2bdafc28dc056cced7e | |
parent | d7542dc3e895839ec681bd7402446ffc74ce5122 (diff) |
Core/AI: Fix assertion triggered with PetAI derived scripts (#21254)
* Core/AI: Fix assertion triggered with PetAI derived scripts
Fix an assertion triggered when spawning through ".npc add" or from SQL db table a Creature with AI set to a custom AI inheriting from PetAI.
(cherry picked from commit 95a76a83fcbad0e52c6013697544e3c5691e7e24)
-rw-r--r-- | src/server/game/AI/AIException.cpp | 23 | ||||
-rw-r--r-- | src/server/game/AI/AIException.h | 40 | ||||
-rw-r--r-- | src/server/game/AI/CoreAI/PetAI.cpp | 5 | ||||
-rw-r--r-- | src/server/game/AI/CreatureAISelector.cpp | 13 |
4 files changed, 78 insertions, 3 deletions
diff --git a/src/server/game/AI/AIException.cpp b/src/server/game/AI/AIException.cpp new file mode 100644 index 00000000000..3af2deda12c --- /dev/null +++ b/src/server/game/AI/AIException.cpp @@ -0,0 +1,23 @@ +/* + * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "AIException.h" + +InvalidAIException::InvalidAIException(char const* _message) +{ + message().assign(_message); +} diff --git a/src/server/game/AI/AIException.h b/src/server/game/AI/AIException.h new file mode 100644 index 00000000000..b3bc318a112 --- /dev/null +++ b/src/server/game/AI/AIException.h @@ -0,0 +1,40 @@ +/* + * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef TRINITY_AIEXCEPTION_H +#define TRINITY_AIEXCEPTION_H + +#include "Define.h" +#include <exception> +#include <string> + +class TC_GAME_API InvalidAIException : public std::exception +{ +public: + InvalidAIException(char const* _message); + ~InvalidAIException() throw() { } + + char const* what() const noexcept override { return msg_.c_str(); } + +protected: + std::string & message() throw() { return msg_; } + +private: + std::string msg_; +}; + +#endif diff --git a/src/server/game/AI/CoreAI/PetAI.cpp b/src/server/game/AI/CoreAI/PetAI.cpp index 50cad20bae1..7c16b7bfa86 100644 --- a/src/server/game/AI/CoreAI/PetAI.cpp +++ b/src/server/game/AI/CoreAI/PetAI.cpp @@ -16,6 +16,7 @@ */ #include "PetAI.h" +#include "AIException.h" #include "Creature.h" #include "Errors.h" #include "Group.h" @@ -45,6 +46,8 @@ int32 PetAI::Permissible(Creature const* creature) PetAI::PetAI(Creature* c) : CreatureAI(c), i_tracker(TIME_INTERVAL_LOOK) { + if (!me->GetCharmInfo()) + throw InvalidAIException("Creature doesn't have a valid charm info"); UpdateAllies(); } @@ -153,7 +156,7 @@ void PetAI::UpdateAI(uint32 diff) if (!spellInfo) continue; - if (me->GetCharmInfo() && me->GetSpellHistory()->HasGlobalCooldown(spellInfo)) + if (me->GetSpellHistory()->HasGlobalCooldown(spellInfo)) continue; // check spell cooldown diff --git a/src/server/game/AI/CreatureAISelector.cpp b/src/server/game/AI/CreatureAISelector.cpp index b7181a35f76..fd298c86ed3 100644 --- a/src/server/game/AI/CreatureAISelector.cpp +++ b/src/server/game/AI/CreatureAISelector.cpp @@ -15,6 +15,7 @@ * with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include "AIException.h" #include "Creature.h" #include "CreatureAISelector.h" #include "CreatureAIFactory.h" @@ -83,8 +84,16 @@ namespace FactorySelector return ASSERT_NOTNULL(sCreatureAIRegistry->GetRegistryItem("PetAI"))->Create(creature); // scriptname in db - if (CreatureAI* scriptedAI = sScriptMgr->GetCreatureAI(creature)) - return scriptedAI; + try + { + if (CreatureAI* scriptedAI = sScriptMgr->GetCreatureAI(creature)) + return scriptedAI; + } + catch (InvalidAIException const& e) + { + TC_LOG_ERROR("entities.unit", "Exception trying to assign script '%s' to Creature (Entry: %u), this Creature will have a default AI. Exception message: %s", + creature->GetScriptName().c_str(), creature->GetEntry(), e.what()); + } return SelectFactory<CreatureAI>(creature)->Create(creature); } |