From 13850ee523dbec1f72150480da170754868ce799 Mon Sep 17 00:00:00 2001 From: Foereaper Date: Sun, 10 Nov 2024 03:35:37 +0100 Subject: [PATCH] Move method registration to its own file Also rename LuaFunctions to ElunaTemplate now that it only contains template related code --- ElunaTemplate.cpp | 85 +++++++++++++++++++ LuaFunctions.cpp | 197 -------------------------------------------- methods/Methods.cpp | 113 +++++++++++++++++++++++++ 3 files changed, 198 insertions(+), 197 deletions(-) create mode 100644 ElunaTemplate.cpp delete mode 100644 LuaFunctions.cpp create mode 100644 methods/Methods.cpp diff --git a/ElunaTemplate.cpp b/ElunaTemplate.cpp new file mode 100644 index 0000000000..7d831ceb32 --- /dev/null +++ b/ElunaTemplate.cpp @@ -0,0 +1,85 @@ +/* +* Copyright (C) 2010 - 2024 Eluna Lua Engine +* This program is free software licensed under GPL version 3 +* Please see the included DOCS/LICENSE.md for more information +*/ + +// Eluna +#include "ElunaIncludes.h" +#include "ElunaTemplate.h" + +#if defined TRACKABLE_PTR_NAMESPACE +ElunaConstrainedObjectRef GetWeakPtrFor(Aura const* obj) +{ +#if defined ELUNA_TRINITY + Map* map = obj->GetOwner()->GetMap(); +#elif defined ELUNA_CMANGOS + Map* map = obj->GetTarget()->GetMap(); +#endif + return { obj->GetWeakPtr(), map }; +} +ElunaConstrainedObjectRef GetWeakPtrFor(BattleGround const* obj) { return { obj->GetWeakPtr(), obj->GetBgMap() }; } +ElunaConstrainedObjectRef GetWeakPtrFor(Group const* obj) { return { obj->GetWeakPtr(), nullptr }; } +ElunaConstrainedObjectRef GetWeakPtrFor(Guild const* obj) { return { obj->GetWeakPtr(), nullptr }; } +ElunaConstrainedObjectRef GetWeakPtrFor(Map const* obj) { return { obj->GetWeakPtr(), obj }; } +ElunaConstrainedObjectRef GetWeakPtrForObjectImpl(Object const* obj) +{ + if (obj->isType(TYPEMASK_WORLDOBJECT)) + return { obj->GetWeakPtr(), static_cast(obj)->GetMap() }; + + if (obj->GetTypeId() == TYPEID_ITEM) + if (Player const* player = static_cast(obj)->GetOwner()) + return { obj->GetWeakPtr(), player->GetMap() }; + + // possibly dangerous item + return { obj->GetWeakPtr(), nullptr }; +} +ElunaConstrainedObjectRef GetWeakPtrFor(Quest const* obj) { return { obj->GetWeakPtr(), nullptr }; } +ElunaConstrainedObjectRef GetWeakPtrFor(Spell const* obj) { return { obj->GetWeakPtr(), obj->GetCaster()->GetMap() }; } +#if ELUNA_EXPANSION >= EXP_WOTLK +ElunaConstrainedObjectRef GetWeakPtrFor(Vehicle const* obj) +{ +#if defined ELUNA_TRINITY + Map* map = obj->GetBase()->GetMap(); +#elif defined ELUNA_CMANGOS + Map* map = obj->GetOwner()->GetMap(); +#endif + return { obj->GetWeakPtr(), map }; +} +#endif +#endif + +template<> inline int ElunaTemplate::Add(lua_State* L) { return PerformBinaryOp(L, std::plus()); } +template<> inline int ElunaTemplate::Subtract(lua_State* L) { return PerformBinaryOp(L, std::minus()); } +template<> inline int ElunaTemplate::Multiply(lua_State* L) { return PerformBinaryOp(L, std::multiplies()); } +template<> inline int ElunaTemplate::Divide(lua_State* L) { return PerformBinaryOp(L, std::divides()); } +template<> inline int ElunaTemplate::Mod(lua_State* L) { return PerformBinaryOp(L, std::modulus()); } +template<> inline int ElunaTemplate::Equal(lua_State* L) { return PerformBinaryOp(L, std::equal_to()); } +template<> inline int ElunaTemplate::Less(lua_State* L) { return PerformBinaryOp(L, std::less()); } +template<> inline int ElunaTemplate::LessOrEqual(lua_State* L) { return PerformBinaryOp(L, std::less_equal()); } +template<> inline int ElunaTemplate::ToString(lua_State* L) { return ToStringHelper(L); } +template<> inline int ElunaTemplate::Pow(lua_State* L) { return PowHelper(L); } + +template<> inline int ElunaTemplate::Add(lua_State* L) { return PerformBinaryOp(L, std::plus()); } +template<> inline int ElunaTemplate::Subtract(lua_State* L) { return PerformBinaryOp(L, std::minus()); } +template<> inline int ElunaTemplate::Multiply(lua_State* L) { return PerformBinaryOp(L, std::multiplies()); } +template<> inline int ElunaTemplate::Divide(lua_State* L) { return PerformBinaryOp(L, std::divides()); } +template<> inline int ElunaTemplate::Mod(lua_State* L) { return PerformBinaryOp(L, std::modulus()); } +template<> inline int ElunaTemplate::UnaryMinus(lua_State* L) { return PerformUnaryOp(L, std::negate()); } +template<> inline int ElunaTemplate::Equal(lua_State* L) { return PerformBinaryOp(L, std::equal_to()); } +template<> inline int ElunaTemplate::Less(lua_State* L) { return PerformBinaryOp(L, std::less()); } +template<> inline int ElunaTemplate::LessOrEqual(lua_State* L) { return PerformBinaryOp(L, std::less_equal()); } +template<> inline int ElunaTemplate::ToString(lua_State* L) { return ToStringHelper(L); } +template<> inline int ElunaTemplate::Pow(lua_State* L) { return PowHelper(L); } + +template<> inline int ElunaTemplate::Equal(lua_State* L) { Eluna* E = Eluna::GetEluna(L); E->Push(E->CHECKVAL(1) == E->CHECKVAL(2)); return 1; } +template<> inline int ElunaTemplate::ToString(lua_State* L) +{ + Eluna* E = Eluna::GetEluna(L); +#if defined ELUNA_TRINITY + E->Push(E->CHECKVAL(1).ToString()); +#else + E->Push(E->CHECKVAL(1).GetString()); +#endif + return 1; +} diff --git a/LuaFunctions.cpp b/LuaFunctions.cpp deleted file mode 100644 index df9522df28..0000000000 --- a/LuaFunctions.cpp +++ /dev/null @@ -1,197 +0,0 @@ -/* -* Copyright (C) 2010 - 2024 Eluna Lua Engine -* This program is free software licensed under GPL version 3 -* Please see the included DOCS/LICENSE.md for more information -*/ - -extern "C" -{ -#include "lua.h" -}; - -// Eluna -#include "LuaEngine.h" -#include "ElunaEventMgr.h" -#include "ElunaIncludes.h" -#include "ElunaTemplate.h" -#include "ElunaUtility.h" - -// Method includes -#include "GlobalMethods.h" -#include "ObjectMethods.h" -#include "WorldObjectMethods.h" -#include "UnitMethods.h" -#include "PlayerMethods.h" -#include "CreatureMethods.h" -#include "GroupMethods.h" -#include "GuildMethods.h" -#include "GameObjectMethods.h" -#include "ElunaQueryMethods.h" -#include "AuraMethods.h" -#include "ItemMethods.h" -#include "WorldPacketMethods.h" -#include "SpellMethods.h" -#include "QuestMethods.h" -#include "MapMethods.h" -#include "CorpseMethods.h" -#include "VehicleMethods.h" -#include "BattleGroundMethods.h" - -#if defined TRACKABLE_PTR_NAMESPACE -ElunaConstrainedObjectRef GetWeakPtrFor(Aura const* obj) -{ -#if defined ELUNA_TRINITY - Map* map = obj->GetOwner()->GetMap(); -#elif defined ELUNA_CMANGOS - Map* map = obj->GetTarget()->GetMap(); -#endif - return { obj->GetWeakPtr(), map }; -} -ElunaConstrainedObjectRef GetWeakPtrFor(BattleGround const* obj) { return { obj->GetWeakPtr(), obj->GetBgMap() }; } -ElunaConstrainedObjectRef GetWeakPtrFor(Group const* obj) { return { obj->GetWeakPtr(), nullptr }; } -ElunaConstrainedObjectRef GetWeakPtrFor(Guild const* obj) { return { obj->GetWeakPtr(), nullptr }; } -ElunaConstrainedObjectRef GetWeakPtrFor(Map const* obj) { return { obj->GetWeakPtr(), obj }; } -ElunaConstrainedObjectRef GetWeakPtrForObjectImpl(Object const* obj) -{ - if (obj->isType(TYPEMASK_WORLDOBJECT)) - return { obj->GetWeakPtr(), static_cast(obj)->GetMap() }; - - if (obj->GetTypeId() == TYPEID_ITEM) - if (Player const* player = static_cast(obj)->GetOwner()) - return { obj->GetWeakPtr(), player->GetMap() }; - - // possibly dangerous item - return { obj->GetWeakPtr(), nullptr }; -} -ElunaConstrainedObjectRef GetWeakPtrFor(Quest const* obj) { return { obj->GetWeakPtr(), nullptr }; } -ElunaConstrainedObjectRef GetWeakPtrFor(Spell const* obj) { return { obj->GetWeakPtr(), obj->GetCaster()->GetMap() }; } -#if ELUNA_EXPANSION >= EXP_WOTLK -ElunaConstrainedObjectRef GetWeakPtrFor(Vehicle const* obj) -{ -#if defined ELUNA_TRINITY - Map* map = obj->GetBase()->GetMap(); -#elif defined ELUNA_CMANGOS - Map* map = obj->GetOwner()->GetMap(); -#endif - return { obj->GetWeakPtr(), map }; -} -#endif -#endif - -void RegisterFunctions(Eluna* E) -{ - ElunaTemplate<>::SetMethods(E, LuaGlobalFunctions::GlobalMethods); - - ElunaTemplate::Register(E, "Object"); - ElunaTemplate::SetMethods(E, LuaObject::ObjectMethods); - - ElunaTemplate::Register(E, "WorldObject"); - ElunaTemplate::SetMethods(E, LuaObject::ObjectMethods); - ElunaTemplate::SetMethods(E, LuaWorldObject::WorldObjectMethods); - - ElunaTemplate::Register(E, "Unit"); - ElunaTemplate::SetMethods(E, LuaObject::ObjectMethods); - ElunaTemplate::SetMethods(E, LuaWorldObject::WorldObjectMethods); - ElunaTemplate::SetMethods(E, LuaUnit::UnitMethods); - - ElunaTemplate::Register(E, "Player"); - ElunaTemplate::SetMethods(E, LuaObject::ObjectMethods); - ElunaTemplate::SetMethods(E, LuaWorldObject::WorldObjectMethods); - ElunaTemplate::SetMethods(E, LuaUnit::UnitMethods); - ElunaTemplate::SetMethods(E, LuaPlayer::PlayerMethods); - - ElunaTemplate::Register(E, "Creature"); - ElunaTemplate::SetMethods(E, LuaObject::ObjectMethods); - ElunaTemplate::SetMethods(E, LuaWorldObject::WorldObjectMethods); - ElunaTemplate::SetMethods(E, LuaUnit::UnitMethods); - ElunaTemplate::SetMethods(E, LuaCreature::CreatureMethods); - - ElunaTemplate::Register(E, "GameObject"); - ElunaTemplate::SetMethods(E, LuaObject::ObjectMethods); - ElunaTemplate::SetMethods(E, LuaWorldObject::WorldObjectMethods); - ElunaTemplate::SetMethods(E, LuaGameObject::GameObjectMethods); - - ElunaTemplate::Register(E, "Corpse"); - ElunaTemplate::SetMethods(E, LuaObject::ObjectMethods); - ElunaTemplate::SetMethods(E, LuaWorldObject::WorldObjectMethods); - ElunaTemplate::SetMethods(E, LuaCorpse::CorpseMethods); - - ElunaTemplate::Register(E, "Item"); - ElunaTemplate::SetMethods(E, LuaObject::ObjectMethods); - ElunaTemplate::SetMethods(E, LuaItem::ItemMethods); - -#if ELUNA_EXPANSION >= EXP_WOTLK - ElunaTemplate::Register(E, "Vehicle"); - ElunaTemplate::SetMethods(E, LuaVehicle::VehicleMethods); -#endif - - ElunaTemplate::Register(E, "Group"); - ElunaTemplate::SetMethods(E, LuaGroup::GroupMethods); - - ElunaTemplate::Register(E, "Guild"); - ElunaTemplate::SetMethods(E, LuaGuild::GuildMethods); - - ElunaTemplate::Register(E, "Aura"); - ElunaTemplate::SetMethods(E, LuaAura::AuraMethods); - - ElunaTemplate::Register(E, "Spell"); - ElunaTemplate::SetMethods(E, LuaSpell::SpellMethods); - - ElunaTemplate::Register(E, "Quest"); - ElunaTemplate::SetMethods(E, LuaQuest::QuestMethods); - - ElunaTemplate::Register(E, "Map"); - ElunaTemplate::SetMethods(E, LuaMap::MapMethods); - - ElunaTemplate::Register(E, "BattleGround"); - ElunaTemplate::SetMethods(E, LuaBattleGround::BattleGroundMethods); - - ElunaTemplate::Register(E, "WorldPacket"); - ElunaTemplate::SetMethods(E, LuaPacket::PacketMethods); - - ElunaTemplate::Register(E, "ElunaQuery"); - ElunaTemplate::SetMethods(E, LuaQuery::QueryMethods); - - ElunaTemplate::Register(E, "long long"); - - ElunaTemplate::Register(E, "unsigned long long"); - - ElunaTemplate::Register(E, "ObjectGuid"); - - LuaVal::Register(E->L); -} - -template<> int ElunaTemplate::Add(lua_State* L) { return PerformBinaryOp(L, std::plus()); } -template<> int ElunaTemplate::Subtract(lua_State* L) { return PerformBinaryOp(L, std::minus()); } -template<> int ElunaTemplate::Multiply(lua_State* L) { return PerformBinaryOp(L, std::multiplies()); } -template<> int ElunaTemplate::Divide(lua_State* L) { return PerformBinaryOp(L, std::divides()); } -template<> int ElunaTemplate::Mod(lua_State* L) { return PerformBinaryOp(L, std::modulus()); } -template<> int ElunaTemplate::Equal(lua_State* L) { return PerformBinaryOp(L, std::equal_to()); } -template<> int ElunaTemplate::Less(lua_State* L) { return PerformBinaryOp(L, std::less()); } -template<> int ElunaTemplate::LessOrEqual(lua_State* L) { return PerformBinaryOp(L, std::less_equal()); } -template<> int ElunaTemplate::ToString(lua_State* L) { return ToStringHelper(L); } -template<> int ElunaTemplate::Pow(lua_State* L) { return PowHelper(L); } - -template<> int ElunaTemplate::Add(lua_State* L) { return PerformBinaryOp(L, std::plus()); } -template<> int ElunaTemplate::Subtract(lua_State* L) { return PerformBinaryOp(L, std::minus()); } -template<> int ElunaTemplate::Multiply(lua_State* L) { return PerformBinaryOp(L, std::multiplies()); } -template<> int ElunaTemplate::Divide(lua_State* L) { return PerformBinaryOp(L, std::divides()); } -template<> int ElunaTemplate::Mod(lua_State* L) { return PerformBinaryOp(L, std::modulus()); } -template<> int ElunaTemplate::UnaryMinus(lua_State* L) { return PerformUnaryOp(L, std::negate()); } -template<> int ElunaTemplate::Equal(lua_State* L) { return PerformBinaryOp(L, std::equal_to()); } -template<> int ElunaTemplate::Less(lua_State* L) { return PerformBinaryOp(L, std::less()); } -template<> int ElunaTemplate::LessOrEqual(lua_State* L) { return PerformBinaryOp(L, std::less_equal()); } -template<> int ElunaTemplate::ToString(lua_State* L) { return ToStringHelper(L); } -template<> int ElunaTemplate::Pow(lua_State* L) { return PowHelper(L); } - -template<> int ElunaTemplate::Equal(lua_State* L) { Eluna* E = Eluna::GetEluna(L); E->Push(E->CHECKVAL(1) == E->CHECKVAL(2)); return 1; } -template<> int ElunaTemplate::ToString(lua_State* L) -{ - Eluna* E = Eluna::GetEluna(L); -#if defined ELUNA_TRINITY - E->Push(E->CHECKVAL(1).ToString()); -#else - E->Push(E->CHECKVAL(1).GetString()); -#endif - return 1; -} diff --git a/methods/Methods.cpp b/methods/Methods.cpp new file mode 100644 index 0000000000..caeffdd120 --- /dev/null +++ b/methods/Methods.cpp @@ -0,0 +1,113 @@ +/* +* Copyright (C) 2010 - 2024 Eluna Lua Engine +* This program is free software licensed under GPL version 3 +* Please see the included DOCS/LICENSE.md for more information +*/ + +// Eluna +#include "ElunaIncludes.h" +#include "ElunaTemplate.h" + +// Method includes +#include "GlobalMethods.h" +#include "ObjectMethods.h" +#include "WorldObjectMethods.h" +#include "UnitMethods.h" +#include "PlayerMethods.h" +#include "CreatureMethods.h" +#include "GroupMethods.h" +#include "GuildMethods.h" +#include "GameObjectMethods.h" +#include "ElunaQueryMethods.h" +#include "AuraMethods.h" +#include "ItemMethods.h" +#include "WorldPacketMethods.h" +#include "SpellMethods.h" +#include "QuestMethods.h" +#include "MapMethods.h" +#include "CorpseMethods.h" +#include "VehicleMethods.h" +#include "BattleGroundMethods.h" + +void RegisterFunctions(Eluna* E) +{ + ElunaTemplate<>::SetMethods(E, LuaGlobalFunctions::GlobalMethods); + + ElunaTemplate::Register(E, "Object"); + ElunaTemplate::SetMethods(E, LuaObject::ObjectMethods); + + ElunaTemplate::Register(E, "WorldObject"); + ElunaTemplate::SetMethods(E, LuaObject::ObjectMethods); + ElunaTemplate::SetMethods(E, LuaWorldObject::WorldObjectMethods); + + ElunaTemplate::Register(E, "Unit"); + ElunaTemplate::SetMethods(E, LuaObject::ObjectMethods); + ElunaTemplate::SetMethods(E, LuaWorldObject::WorldObjectMethods); + ElunaTemplate::SetMethods(E, LuaUnit::UnitMethods); + + ElunaTemplate::Register(E, "Player"); + ElunaTemplate::SetMethods(E, LuaObject::ObjectMethods); + ElunaTemplate::SetMethods(E, LuaWorldObject::WorldObjectMethods); + ElunaTemplate::SetMethods(E, LuaUnit::UnitMethods); + ElunaTemplate::SetMethods(E, LuaPlayer::PlayerMethods); + + ElunaTemplate::Register(E, "Creature"); + ElunaTemplate::SetMethods(E, LuaObject::ObjectMethods); + ElunaTemplate::SetMethods(E, LuaWorldObject::WorldObjectMethods); + ElunaTemplate::SetMethods(E, LuaUnit::UnitMethods); + ElunaTemplate::SetMethods(E, LuaCreature::CreatureMethods); + + ElunaTemplate::Register(E, "GameObject"); + ElunaTemplate::SetMethods(E, LuaObject::ObjectMethods); + ElunaTemplate::SetMethods(E, LuaWorldObject::WorldObjectMethods); + ElunaTemplate::SetMethods(E, LuaGameObject::GameObjectMethods); + + ElunaTemplate::Register(E, "Corpse"); + ElunaTemplate::SetMethods(E, LuaObject::ObjectMethods); + ElunaTemplate::SetMethods(E, LuaWorldObject::WorldObjectMethods); + ElunaTemplate::SetMethods(E, LuaCorpse::CorpseMethods); + + ElunaTemplate::Register(E, "Item"); + ElunaTemplate::SetMethods(E, LuaObject::ObjectMethods); + ElunaTemplate::SetMethods(E, LuaItem::ItemMethods); + +#if ELUNA_EXPANSION >= EXP_WOTLK + ElunaTemplate::Register(E, "Vehicle"); + ElunaTemplate::SetMethods(E, LuaVehicle::VehicleMethods); +#endif + + ElunaTemplate::Register(E, "Group"); + ElunaTemplate::SetMethods(E, LuaGroup::GroupMethods); + + ElunaTemplate::Register(E, "Guild"); + ElunaTemplate::SetMethods(E, LuaGuild::GuildMethods); + + ElunaTemplate::Register(E, "Aura"); + ElunaTemplate::SetMethods(E, LuaAura::AuraMethods); + + ElunaTemplate::Register(E, "Spell"); + ElunaTemplate::SetMethods(E, LuaSpell::SpellMethods); + + ElunaTemplate::Register(E, "Quest"); + ElunaTemplate::SetMethods(E, LuaQuest::QuestMethods); + + ElunaTemplate::Register(E, "Map"); + ElunaTemplate::SetMethods(E, LuaMap::MapMethods); + + ElunaTemplate::Register(E, "BattleGround"); + ElunaTemplate::SetMethods(E, LuaBattleGround::BattleGroundMethods); + + ElunaTemplate::Register(E, "WorldPacket"); + ElunaTemplate::SetMethods(E, LuaPacket::PacketMethods); + + ElunaTemplate::Register(E, "ElunaQuery"); + ElunaTemplate::SetMethods(E, LuaQuery::QueryMethods); + + ElunaTemplate::Register(E, "long long"); + + ElunaTemplate::Register(E, "unsigned long long"); + + ElunaTemplate::Register(E, "ObjectGuid"); + + LuaVal::Register(E->L); +}