One way is to hook the function. LibAddonMenu uses Prehook defined by the api. Writing your own hook is pretty simple, you just use a closure. A closure is a function that returns a function and has local variables used by that returned function. A simple example is:
Lua Code:
function GetPrint(text)
local text = text
return function () print(text) end
end
PrintHello = GetPrint("Hello")
PrintHello()
GetPrint returns a function that prints whatever text you passed to GetPrint. The actual parameter will be gone when that returned function executes, but the local will still be there. So hooking a function is simply:
Lua Code:
function Hook(funcname, before, after)
local origfunc = _G[funcname]
local before = before
local after = after
return function ( ... )
before( ... )
local parms = {origfunc( ... )}
after( ... )
return unpack(parms)
end
function mybefore( ... )
print("hello")
end
function myafter( ... )
print("world")
end
SomeFunc = Hook("SomeFunc", mybefore, myafter)
There's a lot of possible variations on that depending upon what, exactly, you're wanting to do. As an example you could use before to modify parms in which case you would pass it's return value as the parameters to the original function. Similarly after could modify results from the original function. So:
Lua Code:
return after(origfunc(before(...)))
You could also skip calling the original when it's called with particular parameters. The main caveat is be sure the hook can handle being called while the hook is executing. The function you hook may call itself as well as your code directly or indirectly calling it.