[bmx] Lua Hooks by Galaxy613 [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:39

Previous topic - Next topic

BlitzBot

Title : Lua Hooks
Author : Galaxy613
Posted : 1+ years ago

Description : After reading up on the built-in BMX event and hook system, I saw that it wouldn't really work, at least not efficently, with calling Lua functions when hooks are hit/checked. So I made a new Type called "LuaHooks" that control Hooks and stuff.

This files ASSUMES that it will be included into a project with Lua a setup.


Code :
Code (blitzmax) Select
Rem

Lua_hooks.bmx Rev 1.0
Written by 'Galaxy613' on 1/23/2009


For use in Lua Scripts to Hook Lua Functions into Abstract Hooks which can be called
anytime within the BlitzMax code.

End Rem

'#Region Register Lua Functions
lua_register(LuaState, "AddHook", AddHook_luahook)
lua_register(LuaState, "UnHook", UnHook_luahook)

lua_register(LuaState, "CallHook", CallHook_luahook)

lua_register(LuaState, "RegisterHook", RegisterHookName_luahook)
lua_register(LuaState, "UnregisterHook", UnregisterHookName_luahook)
'#End Region

Type LuaHook
Global LuaHookList:TList = CreateList()
Global LuaHookNameList:TList = CreateList()

Field HookName:String, LuaFuncName:String

Method SetNames(Hnme:String, LFnme:String)
HookName = Hnme
LuaFuncName = LFnme
End Method

Function CheckAll()
For hookname:String = EachIn LuaHookNameList
For hook:LuaHook = EachIn LuaHookList
If hook.HookName = HookName Then
lua_getfield(LuaState, LUA_GLOBALSINDEX, hook.LuaFuncName)
lua_call(LuaState, 0, 0)
EndIf
Next
Next
End Function

Function Check(HookName:String)
For hook:LuaHook = EachIn LuaHookList
If hook.HookName = HookName Then
lua_getfield(LuaState, LUA_GLOBALSINDEX, hook.LuaFuncName)
lua_call(LuaState, 0, 0)
EndIf
Next
End Function

Function RegisterHookName(HookName:String)
ListAddLast LuaHookNameList, HookName
End Function

Function UnregisterHookName(HookName:String)
For hooknme:String = EachIn LuaHookNameList
If hooknme = HookName Then
ListRemove LuaHookNameList, hooknme
EndIf
Next
End Function

Function Register(LuaFuncName:String, HookName:String)
Local tmp:LuaHook = New LuaHook
tmp.HookName = HookName
tmp.LuaFuncName = LuaFuncName
ListAddLast LuaHookList, tmp
'ListAddLast LuaHookList, (New LuaHook).SetNames(HookName,LuaFuncName) ' This isn't working..?
End Function

Function Unregister(LuaFuncName:String, HookName:String)
For hook:LuaHook = EachIn LuaHookList
If hook.HookName = HookName And hook.LuaFuncName = LuaFuncName Then
ListRemove LuaHookList, hook
EndIf
Next
End Function
End Type

'#Region Lua Functions
Function RegisterHookName_luahook(LuaState:Byte Ptr)
Local HookName:String = luaL_checkstring(LuaState, 1)

LuaHook.RegisterHookName(HookName)

Return 0
End Function

Function UnregisterHookName_luahook(LuaState:Byte Ptr)
Local HookName:String = luaL_checkstring(LuaState, 1)

LuaHook.UnregisterHookName(HookName)

Return 0
End Function

Function CallHook_luahook(LuaState:Byte Ptr)
Local HookName:String = luaL_checkstring(LuaState, 1)

LuaHook.Check(HookName)

Return 0
End Function

Function AddHook_luahook(LuaState:Byte Ptr)
Local HookName:String = luaL_checkstring(LuaState, 1)
Local HookFunc:String = luaL_checkstring(LuaState, 2)

LuaHook.Register(HookFunc, HookName)

Return 0
End Function

Function UnHook_luahook(LuaState:Byte Ptr)
Local HookName:String = luaL_checkstring(LuaState, 1)
Local HookFunc:String = luaL_checkstring(LuaState, 2)

LuaHook.Unregister(HookFunc, HookName)

Return 0
End Function
'#End Region


Comments :


Galaxy613(Posted 1+ years ago)

 Oh, and I'm sorry if someone already made something like this before. I honestly didn't bother to look and it was a fun exercise anyways...


markcw(Posted 1+ years ago)

 Well there's Noels' <a href="codearcsf042.html?code=1580" target="_blank">LuaInterface</a> and <a href="codearcse88b.html?code=2277" target="_blank">LuaReflection</a> which use axe.lua.


N(Posted 1+ years ago)

 Not really the same thing, plus as a learning exercise I think he did well.