Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] master from ElunaLuaEngine:master #92

Merged
merged 16 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions ElunaTemplate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright (C) 2010 - 2024 Eluna Lua Engine <https://elunaluaengine.github.io/>
* This program is free software licensed under GPL version 3
* Please see the included DOCS/LICENSE.md for more information
*/

// Eluna
#include "LuaEngine.h"
#include "ElunaIncludes.h"
#include "ElunaTemplate.h"
#include "ElunaUtility.h"

#if defined TRACKABLE_PTR_NAMESPACE
ElunaConstrainedObjectRef<Aura> 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<BattleGround> GetWeakPtrFor(BattleGround const* obj) { return { obj->GetWeakPtr(), obj->GetBgMap() }; }
ElunaConstrainedObjectRef<Group> GetWeakPtrFor(Group const* obj) { return { obj->GetWeakPtr(), nullptr }; }
ElunaConstrainedObjectRef<Guild> GetWeakPtrFor(Guild const* obj) { return { obj->GetWeakPtr(), nullptr }; }
ElunaConstrainedObjectRef<Map> GetWeakPtrFor(Map const* obj) { return { obj->GetWeakPtr(), obj }; }
ElunaConstrainedObjectRef<Object> GetWeakPtrForObjectImpl(Object const* obj)
{
if (obj->isType(TYPEMASK_WORLDOBJECT))
return { obj->GetWeakPtr(), static_cast<WorldObject const*>(obj)->GetMap() };

if (obj->GetTypeId() == TYPEID_ITEM)
if (Player const* player = static_cast<Item const*>(obj)->GetOwner())
return { obj->GetWeakPtr(), player->GetMap() };

// possibly dangerous item
return { obj->GetWeakPtr(), nullptr };
}
ElunaConstrainedObjectRef<Quest> GetWeakPtrFor(Quest const* obj) { return { obj->GetWeakPtr(), nullptr }; }
ElunaConstrainedObjectRef<Spell> GetWeakPtrFor(Spell const* obj) { return { obj->GetWeakPtr(), obj->GetCaster()->GetMap() }; }
#if ELUNA_EXPANSION >= EXP_WOTLK
ElunaConstrainedObjectRef<Vehicle> 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<unsigned long long>::Add(lua_State* L) { return ElunaTemplateHelper<unsigned long long>::PerformOp(L, std::plus()); }
template<> inline int ElunaTemplate<unsigned long long>::Subtract(lua_State* L) { return ElunaTemplateHelper<unsigned long long>::PerformOp(L, std::minus()); }
template<> inline int ElunaTemplate<unsigned long long>::Multiply(lua_State* L) { return ElunaTemplateHelper<unsigned long long>::PerformOp(L, std::multiplies()); }
template<> inline int ElunaTemplate<unsigned long long>::Divide(lua_State* L) { return ElunaTemplateHelper<unsigned long long>::PerformOp(L, std::divides()); }
template<> inline int ElunaTemplate<unsigned long long>::Mod(lua_State* L) { return ElunaTemplateHelper<unsigned long long>::PerformOp(L, std::modulus()); }
template<> inline int ElunaTemplate<unsigned long long>::Equal(lua_State* L) { return ElunaTemplateHelper<unsigned long long>::PerformOp(L, std::equal_to()); }
template<> inline int ElunaTemplate<unsigned long long>::Less(lua_State* L) { return ElunaTemplateHelper<unsigned long long>::PerformOp(L, std::less()); }
template<> inline int ElunaTemplate<unsigned long long>::LessOrEqual(lua_State* L) { return ElunaTemplateHelper<unsigned long long>::PerformOp(L, std::less_equal()); }
template<> inline int ElunaTemplate<unsigned long long>::ToString(lua_State* L) { return ElunaTemplateHelper<unsigned long long>::ToString(L); }
template<> inline int ElunaTemplate<unsigned long long>::Pow(lua_State* L) { return ElunaTemplateHelper<unsigned long long>::Pow(L); }

template<> inline int ElunaTemplate<long long>::Add(lua_State* L) { return ElunaTemplateHelper<long long>::PerformOp(L, std::plus()); }
template<> inline int ElunaTemplate<long long>::Subtract(lua_State* L) { return ElunaTemplateHelper<long long>::PerformOp(L, std::minus()); }
template<> inline int ElunaTemplate<long long>::Multiply(lua_State* L) { return ElunaTemplateHelper<long long>::PerformOp(L, std::multiplies()); }
template<> inline int ElunaTemplate<long long>::Divide(lua_State* L) { return ElunaTemplateHelper<long long>::PerformOp(L, std::divides()); }
template<> inline int ElunaTemplate<long long>::Mod(lua_State* L) { return ElunaTemplateHelper<long long>::PerformOp(L, std::modulus()); }
template<> inline int ElunaTemplate<long long>::UnaryMinus(lua_State* L) { return ElunaTemplateHelper<long long>::PerformOp(L, std::negate()); }
template<> inline int ElunaTemplate<long long>::Equal(lua_State* L) { return ElunaTemplateHelper<long long>::PerformOp(L, std::equal_to()); }
template<> inline int ElunaTemplate<long long>::Less(lua_State* L) { return ElunaTemplateHelper<long long>::PerformOp(L, std::less()); }
template<> inline int ElunaTemplate<long long>::LessOrEqual(lua_State* L) { return ElunaTemplateHelper<long long>::PerformOp(L, std::less_equal()); }
template<> inline int ElunaTemplate<long long>::ToString(lua_State* L) { return ElunaTemplateHelper<long long>::ToString(L); }
template<> inline int ElunaTemplate<long long>::Pow(lua_State* L) { return ElunaTemplateHelper<long long>::Pow(L); }

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; }
template<> inline int ElunaTemplate<ObjectGuid>::ToString(lua_State* L)
{
Eluna* E = Eluna::GetEluna(L);
#if defined ELUNA_TRINITY
E->Push(E->CHECKVAL<ObjectGuid>(1).ToString());
#else
E->Push(E->CHECKVAL<ObjectGuid>(1).GetString());
#endif
return 1;
}
48 changes: 46 additions & 2 deletions ElunaTemplate.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class ElunaTemplate
lua_setfield(L, metatable, "__add");

// make new indexes saved to methods
lua_pushcfunction(L, Substract);
lua_pushcfunction(L, Subtract);
lua_setfield(L, metatable, "__sub");

// make new indexes saved to methods
Expand Down Expand Up @@ -477,7 +477,7 @@ class ElunaTemplate
static int ArithmeticError(lua_State* L) { return luaL_error(L, "attempt to perform arithmetic on a %s value", tname); }
static int CompareError(lua_State* L) { return luaL_error(L, "attempt to compare %s", tname); }
static int Add(lua_State* L) { return ArithmeticError(L); }
static int Substract(lua_State* L) { return ArithmeticError(L); }
static int Subtract(lua_State* L) { return ArithmeticError(L); }
static int Multiply(lua_State* L) { return ArithmeticError(L); }
static int Divide(lua_State* L) { return ArithmeticError(L); }
static int Mod(lua_State* L) { return ArithmeticError(L); }
Expand All @@ -496,4 +496,48 @@ class ElunaTemplate

template<typename T> const char* ElunaTemplate<T>::tname = NULL;

template <typename T>
class ElunaTemplateHelper
{
public:
static int PerformOp(lua_State* L, std::function<T(T, T)> op)
{
Eluna* E = Eluna::GetEluna(L);
T val1 = E->CHECKVAL<T>(1);
T val2 = E->CHECKVAL<T>(2);
E->Push(op(val1, val2));
return 1;
}

static int PerformOp(lua_State* L, std::function<T(T)> op)
{
Eluna* E = Eluna::GetEluna(L);

T val = E->CHECKVAL<T>(1);
E->Push(op(val));
return 1;
}

static int ToString(lua_State* L)
{
Eluna* E = Eluna::GetEluna(L);

T val = E->CHECKVAL<T>(1);
std::ostringstream ss;
ss << val;
E->Push(ss.str());
return 1;
}

static int Pow(lua_State* L)
{
Eluna* E = Eluna::GetEluna(L);

T val1 = E->CHECKVAL<T>(1);
T val2 = E->CHECKVAL<T>(2);
E->Push(static_cast<T>(powl(static_cast<long double>(val1), static_cast<long double>(val2))));
return 1;
}
};

#endif
4 changes: 2 additions & 2 deletions LuaEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extern "C"
// Additional lua libraries
};

extern void RegisterFunctions(Eluna* E);
extern void RegisterMethods(Eluna* E);

void Eluna::_ReloadEluna()
{
Expand Down Expand Up @@ -146,7 +146,7 @@ void Eluna::OpenLua()
luaL_openlibs(L);

// Register methods and functions
RegisterFunctions(this);
RegisterMethods(this);

// get require paths
const std::string& requirepath = sElunaLoader->GetRequirePath();
Expand Down
Loading
Loading