-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdash_hook.lua
168 lines (130 loc) · 3.15 KB
/
dash_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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
-- dash hook fork
-- upstream licnese: https://github.com/SuperiorServers/dash/blob/master/LICENSE
local debug_info = debug.getinfo
local isfunction = isfunction
local IsValid = IsValid
local getmetatable = getmetatable
local str_mt = getmetatable("")
local hook_callbacks = {}
local hook_index = {}
local hook_id = {}
local removed_something = nil
local function GetTable() -- This function is now slow
local ret = {}
for name, callbacks in pairs(hook_callbacks) do
ret[name] = {}
for index, callback in pairs(callbacks) do
ret[name][hook_id[name][index]] = callback
end
end
return ret
end
local function Exists(name, id)
return (hook_index[name] and hook_index[name][id]) ~= nil
end
local function Call(name, gm, ...)
local callbacks = hook_callbacks[name]
removed_something = nil
if callbacks then
local i = 0
::runhook::
i = i + 1
local v = callbacks[i]
if v then
local a, b, c, d, e, f = v(...)
if removed_something then
i = i - 1
removed_something = nil
end
if a ~= nil then
return a, b, c, d, e, f
end
goto runhook
end
end
if gm == nil then return end
local callback = gm[name]
if callback == nil then return end
return callback(gm, ...)
end
local function Run(name, ...)
return Call(name, GAMEMODE, ...)
end
local function Remove(name, id)
local callbacks = hook_callbacks[name]
if callbacks == nil then return end
local indexes = hook_index[name]
local index = indexes[id]
if index == nil then return end
removed_something = true
local count = #callbacks
if count == index then
callbacks[index] = nil
indexes[id] = nil
hook_id[name][index] = nil
else
local ids = hook_id[name]
callbacks[index] = callbacks[count]
callbacks[count] = nil
local lastid = ids[count]
indexes[id] = nil
indexes[lastid] = index
ids[index] = lastid
ids[count] = nil
end
end
local function Add(name, id, callback)
if isfunction(id) then
callback = id
id = debug_info(callback).short_src
end
if callback == nil then return end
if hook_callbacks[name] == nil then
hook_callbacks[name] = {}
hook_index[name] = {}
hook_id[name] = {}
end
if Exists(name, id) then
Remove(name, id) -- properly simulate hook overwrite behavior
end
local callbacks = hook_callbacks[name]
local indexes = hook_index[name]
if getmetatable(id) ~= str_mt then
local orig = callback
callback = function(...)
if IsValid(id) then
return orig(id, ...)
end
local index = indexes[id]
Remove(name, id)
local nextcallback = callbacks[index]
if nextcallback then
return nextcallback(...)
end
end
end
local index = #callbacks + 1
callbacks[index] = callback
indexes[id] = index
hook_id[name][index] = id
end
if hook and hook.is_dash == nil and debug.getinfo(hook.Add).short_src == "lua/includes/modules/hook.lua" then
for event, listeners in pairs(hook.GetTable()) do
for k, v in pairs(listeners) do
Add(event, k, v)
end
end
end
hook = setmetatable({
Remove = Remove,
GetTable = GetTable,
Exists = Exists,
Add = Add,
Call = Call,
Run = Run,
is_dash = true
}, {
__call = function(self, name, id, callback)
return self.Add(name, id, callback)
end
})