From e81b5838a6780a73946047c627583cdb837df58d Mon Sep 17 00:00:00 2001 From: Shauren Date: Thu, 20 Aug 2020 15:49:59 +0200 Subject: Core/Scripts: Unify RegisterSpellScript and RegisterAuraScript macros to do the same thing and implemented passing custom arguments to spell script classes (cherry picked from commit 75a6a7a0ad48874e75dd71f766dad3707341bb32) --- src/server/game/Scripting/ScriptMgr.h | 61 +++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/server/game/Scripting/ScriptMgr.h b/src/server/game/Scripting/ScriptMgr.h index 5f25655d2bc..0e2c48f4e06 100644 --- a/src/server/game/Scripting/ScriptMgr.h +++ b/src/server/game/Scripting/ScriptMgr.h @@ -20,6 +20,8 @@ #include "Common.h" #include "ObjectGuid.h" +#include "Tuples.h" +#include "Types.h" #include class AccountMgr; @@ -1195,33 +1197,50 @@ class TC_GAME_API ScriptMgr std::string _currentContext; }; -template -class GenericSpellScriptLoader : public SpellScriptLoader +namespace Trinity::SpellScripts { - public: - GenericSpellScriptLoader(char const* name) : SpellScriptLoader(name) { } - SpellScript* GetSpellScript() const override { return new S(); } -}; -#define RegisterSpellScript(spell_script) new GenericSpellScriptLoader(#spell_script) + template + using is_SpellScript = std::is_base_of; -template -class GenericAuraScriptLoader : public SpellScriptLoader -{ - public: - GenericAuraScriptLoader(char const* name) : SpellScriptLoader(name) { } - AuraScript* GetAuraScript() const override { return new A(); } -}; -#define RegisterAuraScript(aura_script) new GenericAuraScriptLoader(#aura_script) + template + using is_AuraScript = std::is_base_of; +} -template +template class GenericSpellAndAuraScriptLoader : public SpellScriptLoader { - public: - GenericSpellAndAuraScriptLoader(char const* name) : SpellScriptLoader(name) { } - SpellScript* GetSpellScript() const override { return new S(); } - AuraScript* GetAuraScript() const override { return new A(); } + using SpellScriptType = typename Trinity::find_type_if_t; + using AuraScriptType = typename Trinity::find_type_if_t; + using ArgsType = typename Trinity::find_type_if_t; + +public: + GenericSpellAndAuraScriptLoader(char const* name, ArgsType&& args) : SpellScriptLoader(name), _args(std::move(args)) { } + +private: + SpellScript* GetSpellScript() const override + { + if constexpr (!std::is_same_v) + return Trinity::new_from_tuple(_args); + else + return nullptr; + } + + AuraScript* GetAuraScript() const override + { + if constexpr (!std::is_same_v) + return Trinity::new_from_tuple(_args); + else + return nullptr; + } + + ArgsType _args; }; -#define RegisterSpellAndAuraScriptPair(spell_script, aura_script) new GenericSpellAndAuraScriptLoader(#spell_script) + +#define RegisterSpellScriptWithArgs(spell_script, script_name, ...) new GenericSpellAndAuraScriptLoader(script_name, std::make_tuple(__VA_ARGS__)) +#define RegisterSpellScript(spell_script) RegisterSpellScriptWithArgs(spell_script, #spell_script) +#define RegisterAuraScript(aura_script) RegisterSpellScriptWithArgs(aura_script, #aura_script) +#define RegisterSpellAndAuraScriptPairWithArgs(script_1, script_2, script_name, ...) new GenericSpellAndAuraScriptLoader(script_name, std::make_tuple(__VA_ARGS__)) +#define RegisterSpellAndAuraScriptPair(script_1, script_2) RegisterSpellAndAuraScriptPairWithArgs(script_1, script_2, #script_1) template class GenericCreatureScript : public CreatureScript -- cgit v1.2.3