-
Notifications
You must be signed in to change notification settings - Fork 11
/
hook_expanded.lua
111 lines (90 loc) · 2.7 KB
/
hook_expanded.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
-- https://github.com/Be1zebub/Small-GLua-Things/blob/master/hook_expanded.lua
hook._GetTable = hook._GetTable or hook.GetTable
function hook.GetTable(event)
if event then
return hook._GetTable()[event] or {}
else
return hook._GetTable()
end
end
function hook.Get(event, identifier)
return hook.GetTable(event)[identifier]
end
function hook.Exists(event, identifier)
return tobool(hook.Get(event, identifier))
end
function hook.Once(event, callback, identifier)
identifier = identifier or debug.traceback()
hook.Add(event, identifier, function()
hook.Remove(event, identifier)
callback()
end)
end
for ClassName, Class in pairs(debug.getregistry()) do
if FindMetaTable(ClassName) == nil or Class.IsValid == nil then continue end
function Class:AddHook(event, callback)
hook.Add(event, self, callback)
if self.HooksTable == nil then
self.HooksTable = {}
local old = self.OnRemove
self.OnRemove = function()
for event in pairs(self.HooksTable) do
hook.Remove(event, self)
end
if old then old(self) end
end
end
self.HooksTable[eventName] = true
end
function Class:RemoveHook(event)
hook.Remove(event, self)
self.HooksTable[eventName] = nil
end
end
hook.Paused = hook.Paused or {}
function hook.Pause(event, identifier)
if hook.Paused[event] == nil then hook.Paused[event] = {} end
local hooks = hook.GetTable()
if identifier then
if hook.Paused[event][identifier] then return end
if isstring(identifier) or (identifier.IsValid and identifier:IsValid()) then
hook.Paused[event][identifier] = (hooks[event] or {})[identifier]
end
hooks[event][identifier] = nil
else
for identifier in pairs(hooks[event] or {}) do
if hook.Paused[event][identifier] then continue end
if isstring(identifier) or (identifier.IsValid and identifier:IsValid()) then
hook.Paused[event][identifier] = (hooks[event] or {})[identifier]
end
end
hooks[event] = nil
end
end
function hook.PauseAll()
for event in pairs(hook.GetTable()) do
hook.Pause(event)
end
end
function hook.UnPause(event, identifier)
if hook.Paused[event] == nil then return end
if identifier then
if hook.Paused[event][identifier] == nil then return end
if isstring(identifier) or (identifier.IsValid and identifier:IsValid()) then
hook.Add(event, identifier, hook.Paused[event][identifier])
end
hook.Paused[event][identifier] = nil
else
for identifier in pairs(hook.Paused[event]) do
if isstring(identifier) or (identifier.IsValid and identifier:IsValid()) then
hook.Add(event, identifier, hook.Paused[event][identifier])
end
hook.Paused[event][identifier] = nil
end
end
end
function hook.UnPauseAll()
for event in pairs(hook.Paused) do
hook.UnPause(event)
end
end