Fix previous commits to be more efficient overall and have better logic. Also try fix clang error.

This commit is contained in:
Rochet2
2014-12-19 14:19:00 +02:00
parent 631e220b31
commit 78d18e8fee
3 changed files with 131 additions and 377 deletions

View File

@@ -30,7 +30,8 @@ public:
functionReference(funcRef), functionReference(funcRef),
isTemporary(shots != 0), isTemporary(shots != 0),
remainingShots(shots) remainingShots(shots)
{} {
}
}; };
typedef std::vector<Binding> FunctionRefVector; typedef std::vector<Binding> FunctionRefVector;
typedef UNORDERED_MAP<int, FunctionRefVector> EventToFunctionsMap; typedef UNORDERED_MAP<int, FunctionRefVector> EventToFunctionsMap;
@@ -49,9 +50,6 @@ public:
// unregisters all registered functions and clears all registered events from the bindings // unregisters all registered functions and clears all registered events from the bindings
virtual void Clear() { }; virtual void Clear() { };
// Updates the counters on all temporary bindings and erases them if the counter would reach 0.
virtual void UpdateTemporaryBindings() { };
}; };
template<typename T> template<typename T>
@@ -74,35 +72,29 @@ public:
Bindings.clear(); Bindings.clear();
} }
void UpdateTemporaryBindings() override // Pushes the function references and updates the counters on the binds and erases them if the counter would reach 0
void PushFuncRefs(lua_State* L, int event_id)
{ {
for (EventToFunctionsMap::iterator itr = Bindings.begin(); itr != Bindings.end();) for (FunctionRefVector::iterator it = Bindings[event_id].begin(); it != Bindings[event_id].end();)
{ {
for (FunctionRefVector::iterator it = itr->second.begin(); it != itr->second.end();) FunctionRefVector::iterator it_old = it++;
{
Binding &b = (*it);
if (b.isTemporary && b.remainingShots == 0)
{
luaL_unref(E.L, LUA_REGISTRYINDEX, b.functionReference);
it = itr->second.erase(it);
}
else
{
it++;
}
}
// If there are no more entries in the vector, erase the vector. lua_rawgeti(L, LUA_REGISTRYINDEX, (it_old->functionReference));
if (itr->second.empty())
if (it_old->isTemporary)
{ {
itr = Bindings.erase(itr); --it_old->remainingShots;
} if (it_old->remainingShots == 0)
else {
{ luaL_unref(L, LUA_REGISTRYINDEX, it_old->functionReference);
itr++; Bindings[event_id].erase(it_old);
}
} }
} }
}
if (Bindings[event_id].empty())
Bindings.erase(event_id);
};
void Insert(int eventId, int funcRef, uint32 shots) // Inserts a new registered event void Insert(int eventId, int funcRef, uint32 shots) // Inserts a new registered event
{ {
@@ -148,48 +140,32 @@ public:
Bindings.clear(); Bindings.clear();
} }
void UpdateTemporaryBindings() override // Pushes the function references and updates the counters on the binds and erases them if the counter would reach 0
void PushFuncRefs(lua_State* L, int event_id, uint32 entry)
{ {
for (EntryToEventsMap::iterator itr = Bindings.begin(); itr != Bindings.end();) for (FunctionRefVector::iterator it = Bindings[entry][event_id].begin(); it != Bindings[entry][event_id].end();)
{ {
for (EventToFunctionsMap::iterator it = itr->second.begin(); it != itr->second.end();) FunctionRefVector::iterator it_old = it++;
{
for (FunctionRefVector::iterator i = it->second.begin(); i != it->second.end();)
{
Binding &b = (*i);
if (b.isTemporary && b.remainingShots == 0)
{
luaL_unref(E.L, LUA_REGISTRYINDEX, b.functionReference);
i = it->second.erase(i);
}
else
{
i++;
}
}
// If there are no more entries in the vector, erase the vector. lua_rawgeti(L, LUA_REGISTRYINDEX, (it_old->functionReference));
if (it->second.empty())
{
it = itr->second.erase(it);
}
else
{
it++;
}
}
// If there are no more vector in the map, erase the map. if (it_old->isTemporary)
if (itr->second.empty())
{ {
itr = Bindings.erase(itr); --it_old->remainingShots;
} if (it_old->remainingShots == 0)
else {
{ luaL_unref(L, LUA_REGISTRYINDEX, it_old->functionReference);
itr++; Bindings[entry][event_id].erase(it_old);
}
} }
} }
}
if (Bindings[entry][event_id].empty())
Bindings[entry].erase(event_id);
if (Bindings[entry].empty())
Bindings.erase(entry);
};
void Insert(uint32 entryId, int eventId, int funcRef, uint32 shots) // Inserts a new registered event void Insert(uint32 entryId, int eventId, int funcRef, uint32 shots) // Inserts a new registered event
{ {

View File

@@ -471,47 +471,33 @@ namespace LuaGlobalFunctions
return 1; return 1;
} }
/** void RegisterEntryHelper(Eluna* E, lua_State* L, int regtype)
* Registers a packet event
*
* <pre>
* enum PacketEvents
* {
* PACKET_EVENT_ON_PACKET_RECEIVE = 5,
* PACKET_EVENT_ON_PACKET_RECEIVE_UNKNOWN = 6,
* PACKET_EVENT_ON_PACKET_SEND = 7,
*
* PACKET_EVENT_COUNT
* };
* </pre>
*
* @param uint32 entry : opcode
* @param uint32 event : packet event Id, refer to PacketEvents above
* @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function"
* @param function function : function to register
*/
int RegisterPacketEvent(Eluna* E, lua_State* L)
{ {
uint32 entry = Eluna::CHECKVAL<uint32>(L, 1); uint32 entry = Eluna::CHECKVAL<uint32>(L, 1);
uint32 ev = Eluna::CHECKVAL<uint32>(L, 2); uint32 ev = Eluna::CHECKVAL<uint32>(L, 2);
uint32 shots; luaL_checktype(L, 3, LUA_TFUNCTION);
lua_pushvalue(L, 3);
if (lua_isfunction(L, 3)) // If the third argument is a function, set shots to 0 for backwards-compatibility. uint32 shots = Eluna::CHECKVAL<uint32>(L, 4, 0);
{
shots = 0;
lua_pushvalue(L, 3);
}
else // Otherwise, shots is the third argument and the function must be the fourth.
{
shots = Eluna::CHECKVAL<uint32>(L, 3);
luaL_checktype(L, 4, LUA_TFUNCTION);
lua_pushvalue(L, 4);
}
int functionRef = luaL_ref(L, LUA_REGISTRYINDEX); int functionRef = luaL_ref(L, LUA_REGISTRYINDEX);
if (functionRef > 0) if (luaL_ref(L, LUA_REGISTRYINDEX) >= 0)
E->Register(HookMgr::REGTYPE_PACKET, entry, ev, functionRef, shots); E->Register(regtype, entry, ev, functionRef, shots);
return 0; else
luaL_argerror(L, 3, "unable to make a ref to function");
}
void RegisterEventHelper(Eluna* E, lua_State* L, int regtype)
{
uint32 ev = Eluna::CHECKVAL<uint32>(L, 1);
luaL_checktype(L, 2, LUA_TFUNCTION);
lua_pushvalue(L, 2);
uint32 shots = Eluna::CHECKVAL<uint32>(L, 3, 0);
int functionRef = luaL_ref(L, LUA_REGISTRYINDEX);
if (luaL_ref(L, LUA_REGISTRYINDEX) >= 0)
E->Register(regtype, 0, ev, functionRef, shots);
else
luaL_argerror(L, 2, "unable to make a ref to function");
} }
/** /**
@@ -575,29 +561,12 @@ namespace LuaGlobalFunctions
* </pre> * </pre>
* *
* @param uint32 event : server event Id, refer to ServerEvents above * @param uint32 event : server event Id, refer to ServerEvents above
* @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function"
* @param function function : function to register * @param function function : function to register
* @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function"
*/ */
int RegisterServerEvent(Eluna* E, lua_State* L) int RegisterServerEvent(Eluna* E, lua_State* L)
{ {
uint32 ev = Eluna::CHECKVAL<uint32>(L, 1); RegisterEventHelper(E, L, HookMgr::REGTYPE_SERVER);
uint32 shots;
if (lua_isfunction(L, 2)) // If the second argument is a function, set shots to 0 for backwards-compatibility.
{
shots = 0;
lua_pushvalue(L, 2);
}
else // Otherwise, shots is the second argument and the function must be the third.
{
shots = Eluna::CHECKVAL<uint32>(L, 2);
luaL_checktype(L, 3, LUA_TFUNCTION);
lua_pushvalue(L, 3);
}
int functionRef = luaL_ref(L, LUA_REGISTRYINDEX);
if (functionRef > 0)
E->Register(HookMgr::REGTYPE_SERVER, 0, ev, functionRef, shots);
return 0; return 0;
} }
@@ -657,29 +626,12 @@ namespace LuaGlobalFunctions
* </pre> * </pre>
* *
* @param uint32 event : [Player] event Id, refer to PlayerEvents above * @param uint32 event : [Player] event Id, refer to PlayerEvents above
* @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function"
* @param function function : function to register * @param function function : function to register
* @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function"
*/ */
int RegisterPlayerEvent(Eluna* E, lua_State* L) int RegisterPlayerEvent(Eluna* E, lua_State* L)
{ {
uint32 ev = Eluna::CHECKVAL<uint32>(L, 1); RegisterEventHelper(E, L, HookMgr::REGTYPE_PLAYER);
uint32 shots;
if (lua_isfunction(L, 2)) // If the second argument is a function, set shots to 0 for backwards-compatibility.
{
shots = 0;
lua_pushvalue(L, 2);
}
else // Otherwise, shots is the second argument and the function must be the third.
{
shots = Eluna::CHECKVAL<uint32>(L, 2);
luaL_checktype(L, 3, LUA_TFUNCTION);
lua_pushvalue(L, 3);
}
int functionRef = luaL_ref(L, LUA_REGISTRYINDEX);
if (functionRef > 0)
E->Register(HookMgr::REGTYPE_PLAYER, 0, ev, functionRef, shots);
return 0; return 0;
} }
@@ -707,29 +659,12 @@ namespace LuaGlobalFunctions
* </pre> * </pre>
* *
* @param uint32 event : [Guild] event Id, refer to GuildEvents above * @param uint32 event : [Guild] event Id, refer to GuildEvents above
* @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function"
* @param function function : function to register * @param function function : function to register
* @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function"
*/ */
int RegisterGuildEvent(Eluna* E, lua_State* L) int RegisterGuildEvent(Eluna* E, lua_State* L)
{ {
uint32 ev = Eluna::CHECKVAL<uint32>(L, 1); RegisterEventHelper(E, L, HookMgr::REGTYPE_GUILD);
uint32 shots;
if (lua_isfunction(L, 2)) // If the second argument is a function, set shots to 0 for backwards-compatibility.
{
shots = 0;
lua_pushvalue(L, 2);
}
else // Otherwise, shots is the second argument and the function must be the third.
{
shots = Eluna::CHECKVAL<uint32>(L, 2);
luaL_checktype(L, 3, LUA_TFUNCTION);
lua_pushvalue(L, 3);
}
int functionRef = luaL_ref(L, LUA_REGISTRYINDEX);
if (functionRef > 0)
E->Register(HookMgr::REGTYPE_GUILD, 0, ev, functionRef, shots);
return 0; return 0;
} }
@@ -752,29 +687,61 @@ namespace LuaGlobalFunctions
* </pre> * </pre>
* *
* @param uint32 event : [Group] event Id, refer to GroupEvents above * @param uint32 event : [Group] event Id, refer to GroupEvents above
* @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function"
* @param function function : function to register * @param function function : function to register
* @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function"
*/ */
int RegisterGroupEvent(Eluna* E, lua_State* L) int RegisterGroupEvent(Eluna* E, lua_State* L)
{ {
uint32 ev = Eluna::CHECKVAL<uint32>(L, 1); RegisterEventHelper(E, L, HookMgr::REGTYPE_GROUP);
uint32 shots; return 0;
}
if (lua_isfunction(L, 2)) // If the second argument is a function, set shots to 0 for backwards-compatibility. /**
{ * Registers a [Battleground] event
shots = 0; *
lua_pushvalue(L, 2); * <pre>
} * enum BGEvents
else // Otherwise, shots is the second argument and the function must be the third. * {
{ * BG_EVENT_ON_START = 1, // (event, bg, bgId, instanceId) - Needs to be added to TC
shots = Eluna::CHECKVAL<uint32>(L, 2); * BG_EVENT_ON_END = 2, // (event, bg, bgId, instanceId, winner) - Needs to be added to TC
luaL_checktype(L, 3, LUA_TFUNCTION); * BG_EVENT_ON_CREATE = 3, // (event, bg, bgId, instanceId) - Needs to be added to TC
lua_pushvalue(L, 3); * BG_EVENT_ON_PRE_DESTROY = 4, // (event, bg, bgId, instanceId) - Needs to be added to TC
} * BG_EVENT_COUNT
* };
* </pre>
*
* @param uint32 event : [Battleground] event Id, refer to BGEvents above
* @param function function : function to register
* @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function"
*/
int RegisterBGEvent(Eluna* E, lua_State* L)
{
RegisterEventHelper(E, L, HookMgr::REGTYPE_BG);
return 0;
}
int functionRef = luaL_ref(L, LUA_REGISTRYINDEX); /**
if (functionRef > 0) * Registers a packet event
E->Register(HookMgr::REGTYPE_GROUP, 0, ev, functionRef, shots); *
* <pre>
* enum PacketEvents
* {
* PACKET_EVENT_ON_PACKET_RECEIVE = 5,
* PACKET_EVENT_ON_PACKET_RECEIVE_UNKNOWN = 6,
* PACKET_EVENT_ON_PACKET_SEND = 7,
*
* PACKET_EVENT_COUNT
* };
* </pre>
*
* @param uint32 entry : opcode
* @param uint32 event : packet event Id, refer to PacketEvents above
* @param function function : function to register
* @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function"
*/
int RegisterPacketEvent(Eluna* E, lua_State* L)
{
RegisterEntryHelper(E, L, HookMgr::REGTYPE_PACKET);
return 0; return 0;
} }
@@ -792,30 +759,12 @@ namespace LuaGlobalFunctions
* *
* @param uint32 menu_id : [Creature] entry Id * @param uint32 menu_id : [Creature] entry Id
* @param uint32 event : [Creature] gossip event Id, refer to GossipEvents above * @param uint32 event : [Creature] gossip event Id, refer to GossipEvents above
* @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function"
* @param function function : function to register * @param function function : function to register
* @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function"
*/ */
int RegisterCreatureGossipEvent(Eluna* E, lua_State* L) int RegisterCreatureGossipEvent(Eluna* E, lua_State* L)
{ {
uint32 entry = Eluna::CHECKVAL<uint32>(L, 1); RegisterEntryHelper(E, L, HookMgr::REGTYPE_CREATURE_GOSSIP);
uint32 ev = Eluna::CHECKVAL<uint32>(L, 2);
uint32 shots;
if (lua_isfunction(L, 3)) // If the third argument is a function, set shots to 0 for backwards-compatibility.
{
shots = 0;
lua_pushvalue(L, 3);
}
else // Otherwise, shots is the third argument and the function must be the fourth.
{
shots = Eluna::CHECKVAL<uint32>(L, 3);
luaL_checktype(L, 4, LUA_TFUNCTION);
lua_pushvalue(L, 4);
}
int functionRef = luaL_ref(L, LUA_REGISTRYINDEX);
if (functionRef > 0)
E->Register(HookMgr::REGTYPE_CREATURE_GOSSIP, entry, ev, functionRef, shots);
return 0; return 0;
} }
@@ -833,30 +782,12 @@ namespace LuaGlobalFunctions
* *
* @param uint32 menu_id : [GameObject] entry Id * @param uint32 menu_id : [GameObject] entry Id
* @param uint32 event : [GameObject] gossip event Id, refer to GossipEvents above * @param uint32 event : [GameObject] gossip event Id, refer to GossipEvents above
* @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function"
* @param function function : function to register * @param function function : function to register
* @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function"
*/ */
int RegisterGameObjectGossipEvent(Eluna* E, lua_State* L) int RegisterGameObjectGossipEvent(Eluna* E, lua_State* L)
{ {
uint32 entry = Eluna::CHECKVAL<uint32>(L, 1); RegisterEntryHelper(E, L, HookMgr::REGTYPE_GAMEOBJECT_GOSSIP);
uint32 ev = Eluna::CHECKVAL<uint32>(L, 2);
uint32 shots;
if (lua_isfunction(L, 3)) // If the third argument is a function, set shots to 0 for backwards-compatibility.
{
shots = 0;
lua_pushvalue(L, 3);
}
else // Otherwise, shots is the third argument and the function must be the fourth.
{
shots = Eluna::CHECKVAL<uint32>(L, 3);
luaL_checktype(L, 4, LUA_TFUNCTION);
lua_pushvalue(L, 4);
}
int functionRef = luaL_ref(L, LUA_REGISTRYINDEX);
if (functionRef > 0)
E->Register(HookMgr::REGTYPE_GAMEOBJECT_GOSSIP, entry, ev, functionRef, shots);
return 0; return 0;
} }
@@ -877,30 +808,12 @@ namespace LuaGlobalFunctions
* *
* @param uint32 entry : [Item] entry Id * @param uint32 entry : [Item] entry Id
* @param uint32 event : [Item] event Id, refer to ItemEvents above * @param uint32 event : [Item] event Id, refer to ItemEvents above
* @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function"
* @param function function : function to register * @param function function : function to register
* @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function"
*/ */
int RegisterItemEvent(Eluna* E, lua_State* L) int RegisterItemEvent(Eluna* E, lua_State* L)
{ {
uint32 entry = Eluna::CHECKVAL<uint32>(L, 1); RegisterEntryHelper(E, L, HookMgr::REGTYPE_ITEM);
uint32 ev = Eluna::CHECKVAL<uint32>(L, 2);
uint32 shots;
if (lua_isfunction(L, 3)) // If the third argument is a function, set shots to 0 for backwards-compatibility.
{
shots = 0;
lua_pushvalue(L, 3);
}
else // Otherwise, shots is the third argument and the function must be the fourth.
{
shots = Eluna::CHECKVAL<uint32>(L, 3);
luaL_checktype(L, 4, LUA_TFUNCTION);
lua_pushvalue(L, 4);
}
int functionRef = luaL_ref(L, LUA_REGISTRYINDEX);
if (functionRef > 0)
E->Register(HookMgr::REGTYPE_ITEM, entry, ev, functionRef, shots);
return 0; return 0;
} }
@@ -918,30 +831,12 @@ namespace LuaGlobalFunctions
* *
* @param uint32 entry : [Item] entry Id * @param uint32 entry : [Item] entry Id
* @param uint32 event : [Item] gossip event Id, refer to GossipEvents above * @param uint32 event : [Item] gossip event Id, refer to GossipEvents above
* @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function"
* @param function function : function to register * @param function function : function to register
* @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function"
*/ */
int RegisterItemGossipEvent(Eluna* E, lua_State* L) int RegisterItemGossipEvent(Eluna* E, lua_State* L)
{ {
uint32 entry = Eluna::CHECKVAL<uint32>(L, 1); RegisterEntryHelper(E, L, HookMgr::REGTYPE_ITEM_GOSSIP);
uint32 ev = Eluna::CHECKVAL<uint32>(L, 2);
uint32 shots;
if (lua_isfunction(L, 3)) // If the third argument is a function, set shots to 0 for backwards-compatibility.
{
shots = 0;
lua_pushvalue(L, 3);
}
else // Otherwise, shots is the third argument and the function must be the fourth.
{
shots = Eluna::CHECKVAL<uint32>(L, 3);
luaL_checktype(L, 4, LUA_TFUNCTION);
lua_pushvalue(L, 4);
}
int functionRef = luaL_ref(L, LUA_REGISTRYINDEX);
if (functionRef > 0)
E->Register(HookMgr::REGTYPE_ITEM_GOSSIP, entry, ev, functionRef, shots);
return 0; return 0;
} }
@@ -959,30 +854,12 @@ namespace LuaGlobalFunctions
* *
* @param uint32 menu_id : [Player] gossip menu Id * @param uint32 menu_id : [Player] gossip menu Id
* @param uint32 event : [Player] gossip event Id, refer to GossipEvents above * @param uint32 event : [Player] gossip event Id, refer to GossipEvents above
* @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function"
* @param function function : function to register * @param function function : function to register
* @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function"
*/ */
int RegisterPlayerGossipEvent(Eluna* E, lua_State* L) int RegisterPlayerGossipEvent(Eluna* E, lua_State* L)
{ {
uint32 menu_id = Eluna::CHECKVAL<uint32>(L, 1); RegisterEntryHelper(E, L, HookMgr::REGTYPE_PLAYER_GOSSIP);
uint32 ev = Eluna::CHECKVAL<uint32>(L, 2);
uint32 shots;
if (lua_isfunction(L, 3)) // If the third argument is a function, set shots to 0 for backwards-compatibility.
{
shots = 0;
lua_pushvalue(L, 3);
}
else // Otherwise, shots is the third argument and the function must be the fourth.
{
shots = Eluna::CHECKVAL<uint32>(L, 3);
luaL_checktype(L, 4, LUA_TFUNCTION);
lua_pushvalue(L, 4);
}
int functionRef = luaL_ref(L, LUA_REGISTRYINDEX);
if (functionRef > 0)
E->Register(HookMgr::REGTYPE_PLAYER_GOSSIP, menu_id, ev, functionRef, shots);
return 0; return 0;
} }
@@ -1035,30 +912,12 @@ namespace LuaGlobalFunctions
* *
* @param uint32 entry : [Creature] entry Id * @param uint32 entry : [Creature] entry Id
* @param uint32 event : [Creature] event Id, refer to CreatureEvents above * @param uint32 event : [Creature] event Id, refer to CreatureEvents above
* @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function"
* @param function function : function to register * @param function function : function to register
* @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function"
*/ */
int RegisterCreatureEvent(Eluna* E, lua_State* L) int RegisterCreatureEvent(Eluna* E, lua_State* L)
{ {
uint32 entry = Eluna::CHECKVAL<uint32>(L, 1); RegisterEntryHelper(E, L, HookMgr::REGTYPE_CREATURE);
uint32 ev = Eluna::CHECKVAL<uint32>(L, 2);
uint32 shots;
if (lua_isfunction(L, 3)) // If the third argument is a function, set shots to 0 for backwards-compatibility.
{
shots = 0;
lua_pushvalue(L, 3);
}
else // Otherwise, shots is the third argument and the function must be the fourth.
{
shots = Eluna::CHECKVAL<uint32>(L, 3);
luaL_checktype(L, 4, LUA_TFUNCTION);
lua_pushvalue(L, 4);
}
int functionRef = luaL_ref(L, LUA_REGISTRYINDEX);
if (functionRef > 0)
E->Register(HookMgr::REGTYPE_CREATURE, entry, ev, functionRef, shots);
return 0; return 0;
} }
@@ -1087,71 +946,12 @@ namespace LuaGlobalFunctions
* *
* @param uint32 entry : [GameObject] entry Id * @param uint32 entry : [GameObject] entry Id
* @param uint32 event : [GameObject] event Id, refer to GameObjectEvents above * @param uint32 event : [GameObject] event Id, refer to GameObjectEvents above
* @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function"
* @param function function : function to register * @param function function : function to register
* @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function"
*/ */
int RegisterGameObjectEvent(Eluna* E, lua_State* L) int RegisterGameObjectEvent(Eluna* E, lua_State* L)
{ {
uint32 entry = Eluna::CHECKVAL<uint32>(L, 1); RegisterEntryHelper(E, L, HookMgr::REGTYPE_GAMEOBJECT);
uint32 ev = Eluna::CHECKVAL<uint32>(L, 2);
uint32 shots;
if (lua_isfunction(L, 3)) // If the third argument is a function, set shots to 0 for backwards-compatibility.
{
shots = 0;
lua_pushvalue(L, 3);
}
else // Otherwise, shots is the third argument and the function must be the fourth.
{
shots = Eluna::CHECKVAL<uint32>(L, 3);
luaL_checktype(L, 4, LUA_TFUNCTION);
lua_pushvalue(L, 4);
}
int functionRef = luaL_ref(L, LUA_REGISTRYINDEX);
if (functionRef > 0)
E->Register(HookMgr::REGTYPE_GAMEOBJECT, entry, ev, functionRef, shots);
return 0;
}
/**
* Registers a [Battleground] event
*
* <pre>
* enum BGEvents
* {
* BG_EVENT_ON_START = 1, // (event, bg, bgId, instanceId) - Needs to be added to TC
* BG_EVENT_ON_END = 2, // (event, bg, bgId, instanceId, winner) - Needs to be added to TC
* BG_EVENT_ON_CREATE = 3, // (event, bg, bgId, instanceId) - Needs to be added to TC
* BG_EVENT_ON_PRE_DESTROY = 4, // (event, bg, bgId, instanceId) - Needs to be added to TC
* BG_EVENT_COUNT
* };
* </pre>
*
* @param uint32 event : [Battleground] event Id, refer to BGEvents above
* @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function"
* @param function function : function to register
*/
int RegisterBGEvent(Eluna* E, lua_State* L)
{
uint32 ev = Eluna::CHECKVAL<uint32>(L, 1);
uint32 shots;
if (lua_isfunction(L, 2)) // If the second argument is a function, set shots to 0 for backwards-compatibility.
{
shots = 0;
lua_pushvalue(L, 2);
}
else // Otherwise, shots is the second argument and the function must be the third.
{
shots = Eluna::CHECKVAL<uint32>(L, 2);
luaL_checktype(L, 3, LUA_TFUNCTION);
lua_pushvalue(L, 3);
}
int functionRef = luaL_ref(L, LUA_REGISTRYINDEX);
if (functionRef > 0)
E->Register(HookMgr::REGTYPE_BG, 0, ev, functionRef, shots);
return 0; return 0;
} }

View File

@@ -51,17 +51,7 @@ using namespace HookMgr;
const char* _LuaBindType = this->BINDMAP->groupName; \ const char* _LuaBindType = this->BINDMAP->groupName; \
uint32 _LuaEvent = EVENT; \ uint32 _LuaEvent = EVENT; \
int _LuaStackTop = lua_gettop(L); \ int _LuaStackTop = lua_gettop(L); \
bool _LuaTemporariesDied = false; \ this->BINDMAP->PushFuncRefs(L, _LuaEvent); \
for (size_t i = 0; i < this->BINDMAP->Bindings[_LuaEvent].size(); ++i) \
{ \
if (this->BINDMAP->Bindings[_LuaEvent][i].isTemporary) \
{ \
this->BINDMAP->Bindings[_LuaEvent][i].remainingShots--; \
if (this->BINDMAP->Bindings[_LuaEvent][i].remainingShots == 0) \
_LuaTemporariesDied = true; \
} \
lua_rawgeti(L, LUA_REGISTRYINDEX, (this->BINDMAP->Bindings[_LuaEvent][i].functionReference)); \
} \
int _LuaFuncTop = lua_gettop(L); \ int _LuaFuncTop = lua_gettop(L); \
int _LuaFuncCount = _LuaFuncTop-_LuaStackTop; \ int _LuaFuncCount = _LuaFuncTop-_LuaStackTop; \
Eluna::Push(L, _LuaEvent); Eluna::Push(L, _LuaEvent);
@@ -95,17 +85,7 @@ using namespace HookMgr;
const char* _LuaBindType = _LuaBind->groupName; \ const char* _LuaBindType = _LuaBind->groupName; \
uint32 _LuaEvent = EVENT; \ uint32 _LuaEvent = EVENT; \
int _LuaStackTop = lua_gettop(L); \ int _LuaStackTop = lua_gettop(L); \
bool _LuaTemporariesDied = false; \ this->BINDMAP->PushFuncRefs(L, _LuaEvent, ENTRY); \
for (size_t i = 0; i < this->BINDMAP->Bindings[ENTRY][_LuaEvent].size(); ++i) \
{ \
if (this->BINDMAP->Bindings[ENTRY][_LuaEvent][i].isTemporary) \
{ \
this->BINDMAP->Bindings[ENTRY][_LuaEvent][i].remainingShots--; \
if (this->BINDMAP->Bindings[ENTRY][_LuaEvent][i].remainingShots == 0) \
_LuaTemporariesDied = true; \
} \
lua_rawgeti(L, LUA_REGISTRYINDEX, (this->BINDMAP->Bindings[ENTRY][_LuaEvent][i].functionReference)); \
} \
int _LuaFuncTop = lua_gettop(L); \ int _LuaFuncTop = lua_gettop(L); \
int _LuaFuncCount = _LuaFuncTop-_LuaStackTop; \ int _LuaFuncCount = _LuaFuncTop-_LuaStackTop; \
Eluna::Push(L, _LuaEvent); Eluna::Push(L, _LuaEvent);
@@ -141,8 +121,6 @@ using namespace HookMgr;
ELUNA_LOG_ERROR("[Eluna]: Ending event %u for %s, stack top was %i and was supposed to be between %i and %i. Report to devs", _LuaEvent, _LuaBindType, lua_gettop(L), _LuaStackTop, _LuaStackTop + _LuaFuncCount * _LuaReturnValues); \ ELUNA_LOG_ERROR("[Eluna]: Ending event %u for %s, stack top was %i and was supposed to be between %i and %i. Report to devs", _LuaEvent, _LuaBindType, lua_gettop(L), _LuaStackTop, _LuaStackTop + _LuaFuncCount * _LuaReturnValues); \
} \ } \
lua_settop(L, _LuaStackTop); \ lua_settop(L, _LuaStackTop); \
if (_LuaTemporariesDied) \
_LuaBind->UpdateTemporaryBindings(); \
if (!this->event_level) \ if (!this->event_level) \
this->InvalidateObjects(); // Invalidate objects on outermost hook call this->InvalidateObjects(); // Invalidate objects on outermost hook call