Allow temporary event handlers for all events.

This allows you to register a handler to only fire N times.

Dynamic event handler binding/unbinding is now possible.

For backwards compatibility, if the number of shots is missing, the event
handler will never be removed from the event.
This commit is contained in:
Patman64
2014-12-18 22:09:34 -05:00
parent f819245308
commit 8c40dd1692
5 changed files with 340 additions and 96 deletions

View File

@@ -491,17 +491,30 @@ namespace LuaGlobalFunctions
*
* @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 ev = Eluna::CHECKVAL<uint32>(L, 2);
luaL_checktype(L, 3, LUA_TFUNCTION);
lua_pushvalue(L, 3);
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_PACKET, entry, ev, functionRef);
E->Register(HookMgr::REGTYPE_PACKET, entry, ev, functionRef, shots);
return 0;
}
@@ -566,16 +579,29 @@ namespace LuaGlobalFunctions
* </pre>
*
* @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
*/
int RegisterServerEvent(Eluna* E, lua_State* L)
{
uint32 ev = Eluna::CHECKVAL<uint32>(L, 1);
luaL_checktype(L, 2, LUA_TFUNCTION);
lua_pushvalue(L, 2);
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);
E->Register(HookMgr::REGTYPE_SERVER, 0, ev, functionRef, shots);
return 0;
}
@@ -635,16 +661,29 @@ namespace LuaGlobalFunctions
* </pre>
*
* @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
*/
int RegisterPlayerEvent(Eluna* E, lua_State* L)
{
uint32 ev = Eluna::CHECKVAL<uint32>(L, 1);
luaL_checktype(L, 2, LUA_TFUNCTION);
lua_pushvalue(L, 2);
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);
E->Register(HookMgr::REGTYPE_PLAYER, 0, ev, functionRef, shots);
return 0;
}
@@ -672,16 +711,29 @@ namespace LuaGlobalFunctions
* </pre>
*
* @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
*/
int RegisterGuildEvent(Eluna* E, lua_State* L)
{
uint32 ev = Eluna::CHECKVAL<uint32>(L, 1);
luaL_checktype(L, 2, LUA_TFUNCTION);
lua_pushvalue(L, 2);
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);
E->Register(HookMgr::REGTYPE_GUILD, 0, ev, functionRef, shots);
return 0;
}
@@ -704,16 +756,29 @@ namespace LuaGlobalFunctions
* </pre>
*
* @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
*/
int RegisterGroupEvent(Eluna* E, lua_State* L)
{
uint32 ev = Eluna::CHECKVAL<uint32>(L, 1);
luaL_checktype(L, 2, LUA_TFUNCTION);
lua_pushvalue(L, 2);
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_GROUP, 0, ev, functionRef);
E->Register(HookMgr::REGTYPE_GROUP, 0, ev, functionRef, shots);
return 0;
}
@@ -731,17 +796,30 @@ namespace LuaGlobalFunctions
*
* @param uint32 menu_id : [Creature] entry Id
* @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
*/
int RegisterCreatureGossipEvent(Eluna* E, lua_State* L)
{
uint32 entry = Eluna::CHECKVAL<uint32>(L, 1);
uint32 ev = Eluna::CHECKVAL<uint32>(L, 2);
luaL_checktype(L, 3, LUA_TFUNCTION);
lua_pushvalue(L, 3);
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);
E->Register(HookMgr::REGTYPE_CREATURE_GOSSIP, entry, ev, functionRef, shots);
return 0;
}
@@ -759,17 +837,30 @@ namespace LuaGlobalFunctions
*
* @param uint32 menu_id : [GameObject] entry Id
* @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
*/
int RegisterGameObjectGossipEvent(Eluna* E, lua_State* L)
{
uint32 entry = Eluna::CHECKVAL<uint32>(L, 1);
uint32 ev = Eluna::CHECKVAL<uint32>(L, 2);
luaL_checktype(L, 3, LUA_TFUNCTION);
lua_pushvalue(L, 3);
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);
E->Register(HookMgr::REGTYPE_GAMEOBJECT_GOSSIP, entry, ev, functionRef, shots);
return 0;
}
@@ -790,17 +881,30 @@ namespace LuaGlobalFunctions
*
* @param uint32 entry : [Item] entry Id
* @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
*/
int RegisterItemEvent(Eluna* E, lua_State* L)
{
uint32 entry = Eluna::CHECKVAL<uint32>(L, 1);
uint32 ev = Eluna::CHECKVAL<uint32>(L, 2);
luaL_checktype(L, 3, LUA_TFUNCTION);
lua_pushvalue(L, 3);
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);
E->Register(HookMgr::REGTYPE_ITEM, entry, ev, functionRef, shots);
return 0;
}
@@ -818,17 +922,30 @@ namespace LuaGlobalFunctions
*
* @param uint32 entry : [Item] entry Id
* @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
*/
int RegisterItemGossipEvent(Eluna* E, lua_State* L)
{
uint32 entry = Eluna::CHECKVAL<uint32>(L, 1);
uint32 ev = Eluna::CHECKVAL<uint32>(L, 2);
luaL_checktype(L, 3, LUA_TFUNCTION);
lua_pushvalue(L, 3);
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);
E->Register(HookMgr::REGTYPE_ITEM_GOSSIP, entry, ev, functionRef, shots);
return 0;
}
@@ -846,17 +963,30 @@ namespace LuaGlobalFunctions
*
* @param uint32 menu_id : [Player] gossip menu Id
* @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
*/
int RegisterPlayerGossipEvent(Eluna* E, lua_State* L)
{
uint32 menu_id = Eluna::CHECKVAL<uint32>(L, 1);
uint32 ev = Eluna::CHECKVAL<uint32>(L, 2);
luaL_checktype(L, 3, LUA_TFUNCTION);
lua_pushvalue(L, 3);
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);
E->Register(HookMgr::REGTYPE_PLAYER_GOSSIP, menu_id, ev, functionRef, shots);
return 0;
}
@@ -909,17 +1039,30 @@ namespace LuaGlobalFunctions
*
* @param uint32 entry : [Creature] entry Id
* @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
*/
int RegisterCreatureEvent(Eluna* E, lua_State* L)
{
uint32 entry = Eluna::CHECKVAL<uint32>(L, 1);
uint32 ev = Eluna::CHECKVAL<uint32>(L, 2);
luaL_checktype(L, 3, LUA_TFUNCTION);
lua_pushvalue(L, 3);
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);
E->Register(HookMgr::REGTYPE_CREATURE, entry, ev, functionRef, shots);
return 0;
}
@@ -948,17 +1091,30 @@ namespace LuaGlobalFunctions
*
* @param uint32 entry : [GameObject] entry Id
* @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
*/
int RegisterGameObjectEvent(Eluna* E, lua_State* L)
{
uint32 entry = Eluna::CHECKVAL<uint32>(L, 1);
uint32 ev = Eluna::CHECKVAL<uint32>(L, 2);
luaL_checktype(L, 3, LUA_TFUNCTION);
lua_pushvalue(L, 3);
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);
E->Register(HookMgr::REGTYPE_GAMEOBJECT, entry, ev, functionRef, shots);
return 0;
}
@@ -977,16 +1133,29 @@ namespace LuaGlobalFunctions
* </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);
luaL_checktype(L, 2, LUA_TFUNCTION);
lua_pushvalue(L, 2);
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);
E->Register(HookMgr::REGTYPE_BG, 0, ev, functionRef, shots);
return 0;
}