-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook.lua
57 lines (43 loc) · 2.04 KB
/
hook.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
local Hook = {}
Hook.debug = false
local hooks = {}
local function _print( ... )
if not Hook.debug then return end
print( ... )
end
-- > Hook: methods
function Hook.Add( event, id, func )
if not event or not type(event) == "string" then error( "#1 argument must be valid and be a string", 2 ) end
if not id or not type(id) == "string" then error( "#2 argument must be valid and be a string", 2 ) end
if not func or not type(func) == "function" then error( "#3 argument must be valid", 2 ) end
if not hooks[event] then hooks[event] = {} end
hooks[event][id] = func
_print( ("Add hook with event '%s' and id '%s'"):format( event, id ) )
end
function Hook.Remove( event, id )
if not event or not type(event) == "string" then error( "#1 argument must be valid and be a string", 2 ) end
if not id or not type(id) == "string" then error( "#2 argument must be valid and be a string", 2 ) end
if not hooks[event] or not hooks[event][id] then return _print( ( "Hook with event '%s' and id '%s' doesn't exists" ):format( event, id ) ) end
hooks[event][id] = nil
_print( ("Remove hook with event '%s' and id '%s'"):format( event, id ) )
end
function Hook.Call( event, id, ... )
if not event or not type(event) == "string" then error( "#1 argument must be valid and be a string", 2 ) end
if not hooks[event] then return _print( ( "Hooks with event '%s' don't exists" ):format( event ) ) end
if hooks[event][id] then
hooks[event][id]( ... )
_print( ("Call hook with event '%s' and id '%s'"):format( event, id ) )
elseif not id then
_print( ("Call hooks with event '%s'"):format( event ) )
for _, v in pairs( hooks[event] ) do
v( ... )
end
else
_print( ("There is no hooks with event '%s' and id '%s'"):format( event, id ) )
end
end
function Hook.SetDebug( state )
if state == nil or not type(state) == "boolean" then error( "#1 argument must be valid and be a boolean", 2 ) end
Hook.debug = state
end
return Hook