Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- hook = hook or {} local hook = hook
- HOOK_DESTROY = "??|___HOOK_DESTROY___|??" -- what
- hook.active = {}
- hook.errors = {}
- hook.profil = {}
- hook.destroy_tag = HOOK_DESTROY
- hook.profiler_enabled = false
- function hook.Add(event, unique, func, on_error)
- unique = unique == nil and event .. "_WITH_NO_HOOK_NAME_" .. tostring(func) or unique
- hook.active[event] = hook.active[event] or {}
- hook.active[event][unique] = {
- func = func,
- on_error = on_error,
- }
- end
- function hook.Remove(event, unique)
- if unique ~= nil and hook.active[event] and hook.active[event][unique] then
- hook.active[event][unique] = nil
- else
- Msg(Format("Tried to remove non existing hook '%s:%s'", event, tostring(unique)))
- end
- end
- function hook.GetTable()
- return hook.active
- end
- local status, a,b,c,d,e,f,g,h
- local time = 0
- local type = type
- local SysTime = SysTime
- local pairs = pairs
- local pcall = pcall
- function hook.Call(event, gm, ...)
- if hook.active[event] then
- for unique, data in pairs(hook.active[event]) do
- if hook.profiler_enabled == true then
- hook.profil[event] = hook.profil[event] or {}
- hook.profil[event][unique] = hook.profil[event][unique] or {}
- time = SysTime()
- end
- status, a,b,c,d,e,f,g,h = pcall(data.func, ...)
- if a == hook.destroy_tag then
- hook.Remove(event, unique)
- break end
- if hook.profiler_enabled == true then
- hook.profil[event][unique].time = (hook.profil[event][unique].time or 0) + (SysTime() - time)
- hook.profil[event][unique].count = (hook.profil[event][unique].count or 0) + 1
- end
- if status == false then
- if data.on_error then
- if data.on_error(a) == false then
- hook.Remove(event, unique)
- end
- else
- hook.errors[event] = hook.errors[event] or {}
- table.insert(hook.errors[event], {unique = unique, error = a, time = os.date("*t")})
- ErrorNoHalt("[Hook] " .. tostring(event) .. ":" .. tostring(unique) .. " removed: " .. tostring(a) .. "\n")
- hook.Remove(event, unique)
- end
- break
- end
- if a or b or c or d or e or f or g or h then
- return a,b,c,d,e,f,g,h
- end
- end
- end
- if type(gm) == "table" and type(gm[event]) == "function" then
- status, a,b,c,d,e,f,g,h = pcall(gm[event], gm, ...)
- if status == false then
- hook.errors["GAMEMODE"] = hook.errors["GAMEMODE"] or {}
- table.insert(hook.errors["GAMEMODE"], {event = event, unique = unique, error = a, time = os.date("*t")})
- ErrorNoHalt("[GAMEMODE] " .. tostring(event) .. " errored: " .. tostring(a) .. "\n")
- return nil end
- if a or b or c or d or e or f or g or h then
- return a,b,c,d,e,f,g,h
- end
- end
- end
- function hook.GetErrorHistory()
- return hook.errors
- end
- function hook.GetProfilerHistory()
- local new = {}
- for event, hooks in pairs(hook.profil) do
- for unique, _data in pairs(hooks) do
- if table.Count(_data) ~= 0 and _data.time ~= 0 and _data.count ~= 0 then
- local data = {}
- data.event = event
- data.average = math.Round((_data.time / _data.count) * 1000, 9)
- local info = debug.getinfo(hook.GetTable()[event][unique].func)
- data.hook = event
- data.source = info.short_src:gsub("\\", "/")
- data.line_defined = info.linedefined
- data.times_ran = _data.count
- if data.average ~= 0 then
- table.insert(new, data)
- end
- end
- end
- end
- table.SortByMember(new, "average")
- return new
- end
- function hook.EnableProfiler()
- hook.profiler_enabled = true
- end
- function hook.DisableProfiler()
- hook.profiler_enabled = false
- end
- function hook.DisableAll()
- if not hook.enabled then
- ErrorNoHalt("Hooks are already disabled.")
- else
- hook.enabled = true
- hook.__backup_hooks = table.Copy(hook.GetTable())
- table.Empty(hook.GetTable())
- end
- end
- function hook.EnableAll()
- if hook.enabled then
- ErrorNoHalt("Hooks are already enabled.")
- else
- hook.enabled = false
- table.Merge( hook.GetTable(), hook.__backup_hooks )
- hook.__backup_hooks = nil
- end
- end
- function hook.Dump()
- local h=0
- for k,v in pairs(hook.GetTable()) do
- Msg("> "..k.." ("..table.Count(v).." hooks):\n")
- for name,data in pairs(v) do
- h=h+1
- Msg(" \""..name.."\" \t "..tostring(debug.getinfo(data.func).source)..":")
- Msg(" Line:"..tostring(debug.getinfo(data.func).linedefined)..'\n')
- end
- Msg"\n"
- end
- Msg("\n>>> Total hooks: "..h..".\n")
- end
Advertisement
Add Comment
Please, Sign In to add comment