Skip to content

Commit 39b4231

Browse files
authored
Separate ElunaTemplate and Method registration (#499)
* Split Template functions and method registry * Implement Custom Method template files
1 parent 889f552 commit 39b4231

File tree

8 files changed

+333
-194
lines changed

8 files changed

+333
-194
lines changed

ElunaTemplate.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright (C) 2010 - 2024 Eluna Lua Engine <https://elunaluaengine.github.io/>
3+
* This program is free software licensed under GPL version 3
4+
* Please see the included DOCS/LICENSE.md for more information
5+
*/
6+
7+
// Eluna
8+
#include "LuaEngine.h"
9+
#include "ElunaIncludes.h"
10+
#include "ElunaTemplate.h"
11+
#include "ElunaUtility.h"
12+
13+
#if defined TRACKABLE_PTR_NAMESPACE
14+
ElunaConstrainedObjectRef<Aura> GetWeakPtrFor(Aura const* obj)
15+
{
16+
#if defined ELUNA_TRINITY
17+
Map* map = obj->GetOwner()->GetMap();
18+
#elif defined ELUNA_CMANGOS
19+
Map* map = obj->GetTarget()->GetMap();
20+
#endif
21+
return { obj->GetWeakPtr(), map };
22+
}
23+
ElunaConstrainedObjectRef<BattleGround> GetWeakPtrFor(BattleGround const* obj) { return { obj->GetWeakPtr(), obj->GetBgMap() }; }
24+
ElunaConstrainedObjectRef<Group> GetWeakPtrFor(Group const* obj) { return { obj->GetWeakPtr(), nullptr }; }
25+
ElunaConstrainedObjectRef<Guild> GetWeakPtrFor(Guild const* obj) { return { obj->GetWeakPtr(), nullptr }; }
26+
ElunaConstrainedObjectRef<Map> GetWeakPtrFor(Map const* obj) { return { obj->GetWeakPtr(), obj }; }
27+
ElunaConstrainedObjectRef<Object> GetWeakPtrForObjectImpl(Object const* obj)
28+
{
29+
if (obj->isType(TYPEMASK_WORLDOBJECT))
30+
return { obj->GetWeakPtr(), static_cast<WorldObject const*>(obj)->GetMap() };
31+
32+
if (obj->GetTypeId() == TYPEID_ITEM)
33+
if (Player const* player = static_cast<Item const*>(obj)->GetOwner())
34+
return { obj->GetWeakPtr(), player->GetMap() };
35+
36+
// possibly dangerous item
37+
return { obj->GetWeakPtr(), nullptr };
38+
}
39+
ElunaConstrainedObjectRef<Quest> GetWeakPtrFor(Quest const* obj) { return { obj->GetWeakPtr(), nullptr }; }
40+
ElunaConstrainedObjectRef<Spell> GetWeakPtrFor(Spell const* obj) { return { obj->GetWeakPtr(), obj->GetCaster()->GetMap() }; }
41+
#if ELUNA_EXPANSION >= EXP_WOTLK
42+
ElunaConstrainedObjectRef<Vehicle> GetWeakPtrFor(Vehicle const* obj)
43+
{
44+
#if defined ELUNA_TRINITY
45+
Map* map = obj->GetBase()->GetMap();
46+
#elif defined ELUNA_CMANGOS
47+
Map* map = obj->GetOwner()->GetMap();
48+
#endif
49+
return { obj->GetWeakPtr(), map };
50+
}
51+
#endif
52+
#endif
53+
54+
template<> inline int ElunaTemplate<unsigned long long>::Add(lua_State* L) { return ElunaTemplateHelper<unsigned long long>::PerformOp(L, std::plus()); }
55+
template<> inline int ElunaTemplate<unsigned long long>::Subtract(lua_State* L) { return ElunaTemplateHelper<unsigned long long>::PerformOp(L, std::minus()); }
56+
template<> inline int ElunaTemplate<unsigned long long>::Multiply(lua_State* L) { return ElunaTemplateHelper<unsigned long long>::PerformOp(L, std::multiplies()); }
57+
template<> inline int ElunaTemplate<unsigned long long>::Divide(lua_State* L) { return ElunaTemplateHelper<unsigned long long>::PerformOp(L, std::divides()); }
58+
template<> inline int ElunaTemplate<unsigned long long>::Mod(lua_State* L) { return ElunaTemplateHelper<unsigned long long>::PerformOp(L, std::modulus()); }
59+
template<> inline int ElunaTemplate<unsigned long long>::Equal(lua_State* L) { return ElunaTemplateHelper<unsigned long long>::PerformOp(L, std::equal_to()); }
60+
template<> inline int ElunaTemplate<unsigned long long>::Less(lua_State* L) { return ElunaTemplateHelper<unsigned long long>::PerformOp(L, std::less()); }
61+
template<> inline int ElunaTemplate<unsigned long long>::LessOrEqual(lua_State* L) { return ElunaTemplateHelper<unsigned long long>::PerformOp(L, std::less_equal()); }
62+
template<> inline int ElunaTemplate<unsigned long long>::ToString(lua_State* L) { return ElunaTemplateHelper<unsigned long long>::ToString(L); }
63+
template<> inline int ElunaTemplate<unsigned long long>::Pow(lua_State* L) { return ElunaTemplateHelper<unsigned long long>::Pow(L); }
64+
65+
template<> inline int ElunaTemplate<long long>::Add(lua_State* L) { return ElunaTemplateHelper<long long>::PerformOp(L, std::plus()); }
66+
template<> inline int ElunaTemplate<long long>::Subtract(lua_State* L) { return ElunaTemplateHelper<long long>::PerformOp(L, std::minus()); }
67+
template<> inline int ElunaTemplate<long long>::Multiply(lua_State* L) { return ElunaTemplateHelper<long long>::PerformOp(L, std::multiplies()); }
68+
template<> inline int ElunaTemplate<long long>::Divide(lua_State* L) { return ElunaTemplateHelper<long long>::PerformOp(L, std::divides()); }
69+
template<> inline int ElunaTemplate<long long>::Mod(lua_State* L) { return ElunaTemplateHelper<long long>::PerformOp(L, std::modulus()); }
70+
template<> inline int ElunaTemplate<long long>::UnaryMinus(lua_State* L) { return ElunaTemplateHelper<long long>::PerformOp(L, std::negate()); }
71+
template<> inline int ElunaTemplate<long long>::Equal(lua_State* L) { return ElunaTemplateHelper<long long>::PerformOp(L, std::equal_to()); }
72+
template<> inline int ElunaTemplate<long long>::Less(lua_State* L) { return ElunaTemplateHelper<long long>::PerformOp(L, std::less()); }
73+
template<> inline int ElunaTemplate<long long>::LessOrEqual(lua_State* L) { return ElunaTemplateHelper<long long>::PerformOp(L, std::less_equal()); }
74+
template<> inline int ElunaTemplate<long long>::ToString(lua_State* L) { return ElunaTemplateHelper<long long>::ToString(L); }
75+
template<> inline int ElunaTemplate<long long>::Pow(lua_State* L) { return ElunaTemplateHelper<long long>::Pow(L); }
76+
77+
template<> inline int ElunaTemplate<ObjectGuid>::Equal(lua_State* L) { Eluna* E = Eluna::GetEluna(L); E->Push(E->CHECKVAL<ObjectGuid>(1) == E->CHECKVAL<ObjectGuid>(2)); return 1; }
78+
template<> inline int ElunaTemplate<ObjectGuid>::ToString(lua_State* L)
79+
{
80+
Eluna* E = Eluna::GetEluna(L);
81+
#if defined ELUNA_TRINITY
82+
E->Push(E->CHECKVAL<ObjectGuid>(1).ToString());
83+
#else
84+
E->Push(E->CHECKVAL<ObjectGuid>(1).GetString());
85+
#endif
86+
return 1;
87+
}

LuaEngine.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ extern "C"
2727
// Additional lua libraries
2828
};
2929

30-
extern void RegisterFunctions(Eluna* E);
30+
extern void RegisterMethods(Eluna* E);
3131

3232
void Eluna::_ReloadEluna()
3333
{
@@ -146,7 +146,7 @@ void Eluna::OpenLua()
146146
luaL_openlibs(L);
147147

148148
// Register methods and functions
149-
RegisterFunctions(this);
149+
RegisterMethods(this);
150150

151151
// get require paths
152152
const std::string& requirepath = sElunaLoader->GetRequirePath();

LuaFunctions.cpp

Lines changed: 0 additions & 192 deletions
This file was deleted.

methods/CMangos/CustomMethods.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (C) 2010 - 2024 Eluna Lua Engine <https://elunaluaengine.github.io/>
3+
* This program is free software licensed under GPL version 3
4+
* Please see the included DOCS/LICENSE.md for more information
5+
*/
6+
7+
// This file is used for custom Lua methods, without needing to edit the existing method header files.
8+
// This can also be used to override default methods without needing to edit existing methods.
9+
// It follows the same structure as any other method header, except you can use RegisterCustomFunction
10+
// to register multiple method tables in a single file.
11+
12+
#include "ElunaTemplate.h"
13+
#include "ElunaIncludes.h"
14+
15+
#ifndef CUSTOMMETHODS_H
16+
#define CUSTOMMETHODS_H
17+
18+
namespace LuaCustom
19+
{
20+
// See the CustomMethods header file in the TC method directory for an example.
21+
inline void RegisterCustomMethods([[maybe_unused]] Eluna* E)
22+
{
23+
24+
};
25+
};
26+
27+
#endif

methods/Mangos/CustomMethods.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (C) 2010 - 2024 Eluna Lua Engine <https://elunaluaengine.github.io/>
3+
* This program is free software licensed under GPL version 3
4+
* Please see the included DOCS/LICENSE.md for more information
5+
*/
6+
7+
// This file is used for custom Lua methods, without needing to edit the existing method header files.
8+
// This can also be used to override default methods without needing to edit existing methods.
9+
// It follows the same structure as any other method header, except you can use RegisterCustomFunction
10+
// to register multiple method tables in a single file.
11+
12+
#include "ElunaTemplate.h"
13+
#include "ElunaIncludes.h"
14+
15+
#ifndef CUSTOMMETHODS_H
16+
#define CUSTOMMETHODS_H
17+
18+
namespace LuaCustom
19+
{
20+
// See the CustomMethods header file in the TC method directory for an example.
21+
inline void RegisterCustomMethods([[maybe_unused]] Eluna* E)
22+
{
23+
24+
};
25+
};
26+
27+
#endif

0 commit comments

Comments
 (0)