From 846487acaa9f4b604af0628bca5685be6815f9ba Mon Sep 17 00:00:00 2001 From: Kargatum Date: Sun, 5 Dec 2021 03:33:02 +0700 Subject: [PATCH] fix(Core/ScriptMgr): Cleanup few scripts (#9497) --- src/server/game/Scripting/ScriptMgr.cpp | 132 +++++++++----------- src/server/game/Scripting/ScriptMgrMacros.h | 43 +++++++ 2 files changed, 103 insertions(+), 72 deletions(-) diff --git a/src/server/game/Scripting/ScriptMgr.cpp b/src/server/game/Scripting/ScriptMgr.cpp index 2567527b6..8fb3627c1 100644 --- a/src/server/game/Scripting/ScriptMgr.cpp +++ b/src/server/game/Scripting/ScriptMgr.cpp @@ -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(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(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(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(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(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(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(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(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(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(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(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(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(false, [&](ElunaScript* script) + { + return script->CanAreaTrigger(player, trigger); + })) { return false; } diff --git a/src/server/game/Scripting/ScriptMgrMacros.h b/src/server/game/Scripting/ScriptMgrMacros.h index 8bce6ecce..b4ac46f83 100644 --- a/src/server/game/Scripting/ScriptMgrMacros.h +++ b/src/server/game/Scripting/ScriptMgrMacros.h @@ -15,6 +15,44 @@ * with this program. If not, see . */ +#ifndef _SCRIPT_MGR_MACRO_H_ +#define _SCRIPT_MGR_MACRO_H_ + +#include "ScriptMgr.h" + +template +inline bool GetReturnBoolScripts(bool ret, TCallBack&& callback) +{ + if (ScriptRegistry::ScriptPointerList.empty()) + return ret; + + bool needReturn = !ret; + + for (auto const& [scriptID, script] : ScriptRegistry::ScriptPointerList) + { + if (callback(script)) + return needReturn; + } + + return ret; +} + +template +inline void GetReturnIndexScripts([[maybe_unused]] T* ret, TCallBack&& callback) +{ + if (ScriptRegistry::ScriptPointerList.empty()) + return; + + for (auto const& [scriptID, script] : ScriptRegistry::ScriptPointerList) + { + if (T* scriptAI = callback(script)) + { + ret = scriptAI; + break; + } + } +} + // Utility macros to refer to the script registry. #define SCR_REG_MAP(T) ScriptRegistry::ScriptMap #define SCR_REG_ITR(T) ScriptRegistry::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::GetScriptById(I); \ if (!V) \ return; + #define GET_SCRIPT_RET(T, I, V, R) \ T* V = ScriptRegistry::GetScriptById(I); \ if (!V) \ return R; + +#endif // _SCRIPT_MGR_MACRO_H_