fix(Core/ScriptMgr): Cleanup few scripts (#9497)

This commit is contained in:
Kargatum
2021-12-05 03:33:02 +07:00
committed by GitHub
parent 725da843e4
commit 846487acaa
2 changed files with 103 additions and 72 deletions

View File

@@ -26,6 +26,7 @@
#include "ObjectMgr.h"
#include "OutdoorPvPMgr.h"
#include "Player.h"
#include "ScriptMgrMacros.h"
#include "ScriptSystem.h"
#include "ScriptedGossip.h"
#include "SmartAI.h"
@@ -34,6 +35,7 @@
#include "Transport.h"
#include "Vehicle.h"
#include "WorldPacket.h"
#include "ScriptMgrMacros.h"
struct TSpellSummary
{
@@ -41,8 +43,6 @@ struct TSpellSummary
uint8 Effects; // set of enum SelectEffect
}*SpellSummary;
#include "ScriptMgrMacros.h"
ScriptMgr::ScriptMgr()
: _scriptCount(0),
_scheduledScripts(0),
@@ -120,6 +120,10 @@ void ScriptMgr::Unload()
SCR_CLEAR(ArenaScript);
SCR_CLEAR(CommandSC);
SCR_CLEAR(DatabaseScript);
SCR_CLEAR(AllCreatureScript);
SCR_CLEAR(AllItemScript);
SCR_CLEAR(AllGameObjectScript);
SCR_CLEAR(ElunaScript);
#undef SCR_CLEAR
@@ -778,12 +782,10 @@ bool ScriptMgr::OnGossipHello(Player* player, Creature* creature)
ASSERT(player);
ASSERT(creature);
bool ret = false;
FOR_SCRIPTS_RET(AllCreatureScript, itr, end, ret) // return true by default if not scripts
if (itr->second->CanCreatureGossipHello(player, creature))
ret = true; // we change ret value only when scripts return false
if (ret)
if (GetReturnBoolScripts<AllCreatureScript>(false, [&](AllCreatureScript* script)
{
return script->CanCreatureGossipHello(player, creature);
}))
{
return true;
}
@@ -798,12 +800,10 @@ bool ScriptMgr::OnGossipSelect(Player* player, Creature* creature, uint32 sender
ASSERT(player);
ASSERT(creature);
bool ret = false;
FOR_SCRIPTS_RET(AllCreatureScript, itr, end, ret) // return true by default if not scripts
if (itr->second->CanCreatureGossipSelect(player, creature, sender, action))
ret = true; // we change ret value only when scripts return false
if (ret)
if (GetReturnBoolScripts<AllCreatureScript>(false, [&](AllCreatureScript* script)
{
return script->CanCreatureGossipSelect(player, creature, sender, action);
}))
{
return true;
}
@@ -818,12 +818,10 @@ bool ScriptMgr::OnGossipSelectCode(Player* player, Creature* creature, uint32 se
ASSERT(creature);
ASSERT(code);
bool ret = false;
FOR_SCRIPTS_RET(AllCreatureScript, itr, end, ret) // return true by default if not scripts
if (itr->second->CanCreatureGossipSelectCode(player, creature, sender, action, code))
ret = true; // we change ret value only when scripts return false
if (ret)
if (GetReturnBoolScripts<AllCreatureScript>(false, [&](AllCreatureScript* script)
{
return script->CanCreatureGossipSelectCode(player, creature, sender, action, code);
}))
{
return true;
}
@@ -838,12 +836,10 @@ bool ScriptMgr::OnQuestAccept(Player* player, Creature* creature, Quest const* q
ASSERT(creature);
ASSERT(quest);
bool ret = false;
FOR_SCRIPTS_RET(AllCreatureScript, itr, end, ret) // return true by default if not scripts
if (itr->second->CanCreatureQuestAccept(player, creature, quest))
ret = true; // we change ret value only when scripts return false
if (ret)
if (GetReturnBoolScripts<AllCreatureScript>(false, [&](AllCreatureScript* script)
{
return script->CanCreatureQuestAccept(player, creature, quest);
}))
{
return true;
}
@@ -881,12 +877,10 @@ bool ScriptMgr::OnQuestReward(Player* player, Creature* creature, Quest const* q
ASSERT(creature);
ASSERT(quest);
bool ret = false;
FOR_SCRIPTS_RET(AllCreatureScript, itr, end, ret) // return true by default if not scripts
if (itr->second->CanCreatureQuestReward(player, creature, quest, opt))
ret = true; // we change ret value only when scripts return false
if (ret)
if (GetReturnBoolScripts<AllCreatureScript>(false, [&](AllCreatureScript* script)
{
return script->CanCreatureQuestReward(player, creature, quest, opt);
}))
{
return true;
}
@@ -911,8 +905,11 @@ CreatureAI* ScriptMgr::GetCreatureAI(Creature* creature)
ASSERT(creature);
CreatureAI* ret = nullptr;
FOR_SCRIPTS_RET(AllCreatureScript, itr, end, ret) // return true by default if not scripts
ret = itr->second->GetCreatureAI(creature); // we change ret value only when scripts return false
GetReturnIndexScripts<AllCreatureScript>(ret, [creature](AllCreatureScript* script)
{
return script->GetCreatureAI(creature);
});
if (ret)
{
@@ -950,12 +947,10 @@ bool ScriptMgr::OnGossipHello(Player* player, GameObject* go)
ASSERT(player);
ASSERT(go);
bool ret = false;
FOR_SCRIPTS_RET(AllGameObjectScript, itr, end, ret)
if (itr->second->CanGameObjectGossipHello(player, go))
ret = true;
if (ret)
if (GetReturnBoolScripts<AllGameObjectScript>(false, [&](AllGameObjectScript* script)
{
return script->CanGameObjectGossipHello(player, go);
}))
{
return true;
}
@@ -970,12 +965,10 @@ bool ScriptMgr::OnGossipSelect(Player* player, GameObject* go, uint32 sender, ui
ASSERT(player);
ASSERT(go);
bool ret = false;
FOR_SCRIPTS_RET(AllGameObjectScript, itr, end, ret)
if (itr->second->CanGameObjectGossipSelect(player, go, sender, action))
ret = true;
if (ret)
if (GetReturnBoolScripts<AllGameObjectScript>(false, [&](AllGameObjectScript* script)
{
return script->CanGameObjectGossipSelect(player, go, sender, action);
}))
{
return true;
}
@@ -990,12 +983,10 @@ bool ScriptMgr::OnGossipSelectCode(Player* player, GameObject* go, uint32 sender
ASSERT(go);
ASSERT(code);
bool ret = false;
FOR_SCRIPTS_RET(AllGameObjectScript, itr, end, ret)
if (itr->second->CanGameObjectGossipSelectCode(player, go, sender, action, code))
ret = true;
if (ret)
if (GetReturnBoolScripts<AllGameObjectScript>(false, [&](AllGameObjectScript* script)
{
return script->CanGameObjectGossipSelectCode(player, go, sender, action, code);
}))
{
return true;
}
@@ -1010,12 +1001,10 @@ bool ScriptMgr::OnQuestAccept(Player* player, GameObject* go, Quest const* quest
ASSERT(go);
ASSERT(quest);
bool ret = false;
FOR_SCRIPTS_RET(AllGameObjectScript, itr, end, ret)
if (itr->second->CanGameObjectQuestAccept(player, go, quest))
ret = true;
if (ret)
if (GetReturnBoolScripts<AllGameObjectScript>(false, [&](AllGameObjectScript* script)
{
return script->CanGameObjectQuestAccept(player, go, quest);
}))
{
return true;
}
@@ -1031,12 +1020,10 @@ bool ScriptMgr::OnQuestReward(Player* player, GameObject* go, Quest const* quest
ASSERT(go);
ASSERT(quest);
bool ret = false;
FOR_SCRIPTS_RET(AllGameObjectScript, itr, end, ret)
if (itr->second->CanGameObjectQuestReward(player, go, quest, opt))
ret = true;
if (ret)
if (GetReturnBoolScripts<AllGameObjectScript>(false, [&](AllGameObjectScript* script)
{
return script->CanGameObjectQuestReward(player, go, quest, opt);
}))
{
return true;
}
@@ -1111,8 +1098,11 @@ GameObjectAI* ScriptMgr::GetGameObjectAI(GameObject* go)
ASSERT(go);
GameObjectAI* ret = nullptr;
FOR_SCRIPTS_RET(AllGameObjectScript, itr, end, ret)
ret = itr->second->GetGameObjectAI(go);
GetReturnIndexScripts<AllGameObjectScript>(ret, [go](AllGameObjectScript* script)
{
return script->GetGameObjectAI(go);
});
if (ret)
{
@@ -1140,12 +1130,10 @@ bool ScriptMgr::OnAreaTrigger(Player* player, AreaTrigger const* trigger)
ASSERT(player);
ASSERT(trigger);
bool ret = false;
FOR_SCRIPTS_RET(ElunaScript, itr, end, ret) // return true by default if not scripts
if (itr->second->CanAreaTrigger(player, trigger))
ret = true; // we change ret value only when scripts return false
if (ret)
if (GetReturnBoolScripts<ElunaScript>(false, [&](ElunaScript* script)
{
return script->CanAreaTrigger(player, trigger);
}))
{
return false;
}

View File

@@ -15,6 +15,44 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _SCRIPT_MGR_MACRO_H_
#define _SCRIPT_MGR_MACRO_H_
#include "ScriptMgr.h"
template<typename ScriptName, typename TCallBack>
inline bool GetReturnBoolScripts(bool ret, TCallBack&& callback)
{
if (ScriptRegistry<ScriptName>::ScriptPointerList.empty())
return ret;
bool needReturn = !ret;
for (auto const& [scriptID, script] : ScriptRegistry<ScriptName>::ScriptPointerList)
{
if (callback(script))
return needReturn;
}
return ret;
}
template<class ScriptName, class T, typename TCallBack>
inline void GetReturnIndexScripts([[maybe_unused]] T* ret, TCallBack&& callback)
{
if (ScriptRegistry<ScriptName>::ScriptPointerList.empty())
return;
for (auto const& [scriptID, script] : ScriptRegistry<ScriptName>::ScriptPointerList)
{
if (T* scriptAI = callback(script))
{
ret = scriptAI;
break;
}
}
}
// Utility macros to refer to the script registry.
#define SCR_REG_MAP(T) ScriptRegistry<T>::ScriptMap
#define SCR_REG_ITR(T) ScriptRegistry<T>::ScriptMapIterator
@@ -25,11 +63,13 @@
if (!SCR_REG_LST(T).empty()) \
for (SCR_REG_ITR(T) C = SCR_REG_LST(T).begin(); \
C != SCR_REG_LST(T).end(); ++C)
#define FOR_SCRIPTS_RET(T, C, E, R) \
if (SCR_REG_LST(T).empty()) \
return R; \
for (SCR_REG_ITR(T) C = SCR_REG_LST(T).begin(); \
C != SCR_REG_LST(T).end(); ++C)
#define FOREACH_SCRIPT(T) \
FOR_SCRIPTS(T, itr, end) \
itr->second
@@ -39,7 +79,10 @@
T* V = ScriptRegistry<T>::GetScriptById(I); \
if (!V) \
return;
#define GET_SCRIPT_RET(T, I, V, R) \
T* V = ScriptRegistry<T>::GetScriptById(I); \
if (!V) \
return R;
#endif // _SCRIPT_MGR_MACRO_H_