diff --git a/ElunaTemplate.h b/ElunaTemplate.h index 828234ce9d..b7dd673953 100644 --- a/ElunaTemplate.h +++ b/ElunaTemplate.h @@ -154,6 +154,26 @@ class ElunaObjectImpl : public ElunaObject TRACKABLE_PTR_NAMESPACE unique_weak_ptr _obj; }; +template <> +class ElunaObjectImpl : public ElunaObject +{ +public: + ElunaObjectImpl(Eluna* E, Aura* obj, char const* tname); + + void* GetObj() const override { return _obj.lock().get(); } + bool IsValid() const override + { + // aura references are not invalidated when their owner (player) changes map + // owner reference must be checked additionally to ensure scripts don't store + // and access auras of players on another map (possibly updating in another thread) + return !_obj.expired() && !_owner.expired(); + } + +private: + TRACKABLE_PTR_NAMESPACE unique_weak_ptr _obj; + TRACKABLE_PTR_NAMESPACE unique_weak_ptr _owner; +}; + template class ElunaObjectValueImpl : public ElunaObject { diff --git a/LuaFunctions.cpp b/LuaFunctions.cpp index fbc10532ef..874d382555 100644 --- a/LuaFunctions.cpp +++ b/LuaFunctions.cpp @@ -50,6 +50,11 @@ TRACKABLE_PTR_NAMESPACE unique_weak_ptr GetWeakPtrFor(Spell const* obj) { TRACKABLE_PTR_NAMESPACE unique_weak_ptr GetWeakPtrFor(Vehicle const* obj) { return obj->GetWeakPtr(); } #endif +ElunaObjectImpl::ElunaObjectImpl(Eluna* E, Aura* obj, char const* tname) : ElunaObject(E, tname), + _obj(GetWeakPtrFor(obj)), _owner(GetWeakPtrFor(obj->GetOwner())) +{ +} + // Template by Mud from http://stackoverflow.com/questions/4484437/lua-integer-type/4485511#4485511 template<> int ElunaTemplate::Add(lua_State* L) { Eluna* E = Eluna::GetEluna(L); E->Push(E->CHECKVAL(1) + E->CHECKVAL(2)); return 1; } template<> int ElunaTemplate::Substract(lua_State* L) { Eluna* E = Eluna::GetEluna(L); E->Push(E->CHECKVAL(1) - E->CHECKVAL(2)); return 1; }