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 75a6a7a0ad)
This commit is contained in:
Shauren
2020-08-20 15:49:59 +02:00
parent 4064b22dd7
commit e81b5838a6

View File

@@ -20,6 +20,8 @@
#include "Common.h"
#include "ObjectGuid.h"
#include "Tuples.h"
#include "Types.h"
#include <vector>
class AccountMgr;
@@ -1195,33 +1197,50 @@ class TC_GAME_API ScriptMgr
std::string _currentContext;
};
template <class S>
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>(#spell_script)
template<typename T>
using is_SpellScript = std::is_base_of<SpellScript, T>;
template <class A>
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>(#aura_script)
template<typename T>
using is_AuraScript = std::is_base_of<AuraScript, T>;
}
template <class S, class A>
template <typename... Ts>
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<Trinity::SpellScripts::is_SpellScript, Ts...>;
using AuraScriptType = typename Trinity::find_type_if_t<Trinity::SpellScripts::is_AuraScript, Ts...>;
using ArgsType = typename Trinity::find_type_if_t<Trinity::is_tuple, Ts...>;
public:
GenericSpellAndAuraScriptLoader(char const* name, ArgsType&& args) : SpellScriptLoader(name), _args(std::move(args)) { }
private:
SpellScript* GetSpellScript() const override
{
if constexpr (!std::is_same_v<SpellScriptType, Trinity::find_type_end>)
return Trinity::new_from_tuple<SpellScriptType>(_args);
else
return nullptr;
}
AuraScript* GetAuraScript() const override
{
if constexpr (!std::is_same_v<AuraScriptType, Trinity::find_type_end>)
return Trinity::new_from_tuple<AuraScriptType>(_args);
else
return nullptr;
}
ArgsType _args;
};
#define RegisterSpellAndAuraScriptPair(spell_script, aura_script) new GenericSpellAndAuraScriptLoader<spell_script, aura_script>(#spell_script)
#define RegisterSpellScriptWithArgs(spell_script, script_name, ...) new GenericSpellAndAuraScriptLoader<spell_script, decltype(std::make_tuple(__VA_ARGS__))>(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_1, script_2, decltype(std::make_tuple(__VA_ARGS__))>(script_name, std::make_tuple(__VA_ARGS__))
#define RegisterSpellAndAuraScriptPair(script_1, script_2) RegisterSpellAndAuraScriptPairWithArgs(script_1, script_2, #script_1)
template <class AI>
class GenericCreatureScript : public CreatureScript