From c8809f15c2d89910fe58e7ceb987446e20fc4395 Mon Sep 17 00:00:00 2001 From: Kargatum Date: Thu, 9 Dec 2021 20:58:23 +0700 Subject: [PATCH] feat(Core/ScriptMgr): correct execute bool hooks (#9574) --- src/server/game/Scripting/ScriptMgr.cpp | 66 ++++++++++++++------- src/server/game/Scripting/ScriptMgrMacros.h | 10 ++-- 2 files changed, 48 insertions(+), 28 deletions(-) diff --git a/src/server/game/Scripting/ScriptMgr.cpp b/src/server/game/Scripting/ScriptMgr.cpp index 13a7faec2..6925fd23d 100644 --- a/src/server/game/Scripting/ScriptMgr.cpp +++ b/src/server/game/Scripting/ScriptMgr.cpp @@ -781,10 +781,12 @@ bool ScriptMgr::OnGossipHello(Player* player, Creature* creature) ASSERT(player); ASSERT(creature); - if (GetReturnBoolScripts(false, [&](AllCreatureScript* script) + auto ret = IsValidBoolScript([&](AllCreatureScript* script) { return script->CanCreatureGossipHello(player, creature); - })) + }); + + if (ret && *ret) { return true; } @@ -799,10 +801,12 @@ bool ScriptMgr::OnGossipSelect(Player* player, Creature* creature, uint32 sender ASSERT(player); ASSERT(creature); - if (GetReturnBoolScripts(false, [&](AllCreatureScript* script) + auto ret = IsValidBoolScript([&](AllCreatureScript* script) { return script->CanCreatureGossipSelect(player, creature, sender, action); - })) + }); + + if (ret && *ret) { return true; } @@ -817,10 +821,12 @@ bool ScriptMgr::OnGossipSelectCode(Player* player, Creature* creature, uint32 se ASSERT(creature); ASSERT(code); - if (GetReturnBoolScripts(false, [&](AllCreatureScript* script) + auto ret = IsValidBoolScript([&](AllCreatureScript* script) { return script->CanCreatureGossipSelectCode(player, creature, sender, action, code); - })) + }); + + if (ret && *ret) { return true; } @@ -835,10 +841,12 @@ bool ScriptMgr::OnQuestAccept(Player* player, Creature* creature, Quest const* q ASSERT(creature); ASSERT(quest); - if (GetReturnBoolScripts(false, [&](AllCreatureScript* script) + auto ret = IsValidBoolScript([&](AllCreatureScript* script) { return script->CanCreatureQuestAccept(player, creature, quest); - })) + }); + + if (ret && *ret) { return true; } @@ -876,10 +884,12 @@ bool ScriptMgr::OnQuestReward(Player* player, Creature* creature, Quest const* q ASSERT(creature); ASSERT(quest); - if (GetReturnBoolScripts(false, [&](AllCreatureScript* script) + auto ret = IsValidBoolScript([&](AllCreatureScript* script) { return script->CanCreatureQuestReward(player, creature, quest, opt); - })) + }); + + if (ret && *ret) { return true; } @@ -946,10 +956,12 @@ bool ScriptMgr::OnGossipHello(Player* player, GameObject* go) ASSERT(player); ASSERT(go); - if (GetReturnBoolScripts(false, [&](AllGameObjectScript* script) + auto ret = IsValidBoolScript([&](AllGameObjectScript* script) { return script->CanGameObjectGossipHello(player, go); - })) + }); + + if (ret && *ret) { return true; } @@ -964,10 +976,12 @@ bool ScriptMgr::OnGossipSelect(Player* player, GameObject* go, uint32 sender, ui ASSERT(player); ASSERT(go); - if (GetReturnBoolScripts(false, [&](AllGameObjectScript* script) + auto ret = IsValidBoolScript([&](AllGameObjectScript* script) { return script->CanGameObjectGossipSelect(player, go, sender, action); - })) + }); + + if (ret && *ret) { return true; } @@ -982,10 +996,12 @@ bool ScriptMgr::OnGossipSelectCode(Player* player, GameObject* go, uint32 sender ASSERT(go); ASSERT(code); - if (GetReturnBoolScripts(false, [&](AllGameObjectScript* script) + auto ret = IsValidBoolScript([&](AllGameObjectScript* script) { return script->CanGameObjectGossipSelectCode(player, go, sender, action, code); - })) + }); + + if (ret && *ret) { return true; } @@ -1000,10 +1016,12 @@ bool ScriptMgr::OnQuestAccept(Player* player, GameObject* go, Quest const* quest ASSERT(go); ASSERT(quest); - if (GetReturnBoolScripts(false, [&](AllGameObjectScript* script) + auto ret = IsValidBoolScript([&](AllGameObjectScript* script) { return script->CanGameObjectQuestAccept(player, go, quest); - })) + }); + + if (ret && *ret) { return true; } @@ -1019,10 +1037,12 @@ bool ScriptMgr::OnQuestReward(Player* player, GameObject* go, Quest const* quest ASSERT(go); ASSERT(quest); - if (GetReturnBoolScripts(false, [&](AllGameObjectScript* script) + auto ret = IsValidBoolScript([&](AllGameObjectScript* script) { return script->CanGameObjectQuestReward(player, go, quest, opt); - })) + }); + + if (ret && *ret) { return true; } @@ -1129,10 +1149,12 @@ bool ScriptMgr::OnAreaTrigger(Player* player, AreaTrigger const* trigger) ASSERT(player); ASSERT(trigger); - if (GetReturnBoolScripts(false, [&](ElunaScript* script) + auto ret = IsValidBoolScript([&](ElunaScript* script) { return script->CanAreaTrigger(player, trigger); - })) + }); + + if (ret && *ret) { return false; } diff --git a/src/server/game/Scripting/ScriptMgrMacros.h b/src/server/game/Scripting/ScriptMgrMacros.h index b4ac46f83..1ac730548 100644 --- a/src/server/game/Scripting/ScriptMgrMacros.h +++ b/src/server/game/Scripting/ScriptMgrMacros.h @@ -21,20 +21,18 @@ #include "ScriptMgr.h" template -inline bool GetReturnBoolScripts(bool ret, TCallBack&& callback) +inline Optional IsValidBoolScript(TCallBack&& callback) { if (ScriptRegistry::ScriptPointerList.empty()) - return ret; - - bool needReturn = !ret; + return {}; for (auto const& [scriptID, script] : ScriptRegistry::ScriptPointerList) { if (callback(script)) - return needReturn; + return true; } - return ret; + return false; } template