Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print(("HNG"):rep(500))
- local pairs = pairs
- local Error = Error
- local ErrorNoHalt = ErrorNoHalt
- local pcall = pcall
- local tostring = tostring
- local concommand = concommand
- local PrintTable = PrintTable
- local CLIENT = CLIENT
- local Msg = Msg
- local type = type
- local ValidEntity=ValidEntity
- local debug=debug
- local table=table
- local SysTime = SysTime
- local math = math
- module("hook")
- -- Local variables
- local Hooks = {}
- local Benchmarks = {}
- -- Exposed Functions
- function GetTable()
- return Hooks
- end
- function GetBenchmark()
- local tbl = table.Copy(Benchmarks)
- for name, hook in pairs(tbl) do
- if table.Count(hook) ~= 0 then
- for unique, value in pairs(hook) do
- if value.time and value.count and value.count ~= 0 then
- value.average = math.Round((value.time / value.count)*1000, 9)
- value.time = math.Round(value.time * 1000, 9)
- if value.average == 0 then
- tbl[name][unique] = nil
- end
- elseif table.Count(tbl[name][unique]) == 0 then
- tbl[name][unique] = nil
- end
- end
- end
- if table.Count(hook) == 0 then
- tbl[name] = nil
- end
- end
- return tbl
- end
- function Add( event_name, name, func )
- if (Hooks[ event_name ] == nil) then
- Hooks[ event_name ] = {}
- end
- Hooks[ event_name ][ name ] = func
- if Benchmarks[ event_name ] == nil then
- Benchmarks[ event_name ] = {}
- end
- Benchmarks[ event_name ][ name ] = {}
- end
- function Remove( event_name, name )
- Hooks[ event_name ][ name ] = nil
- Benchmarks[ event_name ][ name ] = nil
- end
- local b, rA, rB, rC, rD, rE, rF, rG, rH
- local HookTable, GamemodeFunction
- local time_took, ProfileTable -- I have no idea what I should call Benchmark[event_name]
- function Call( name, gm, ... )
- HookTable = Hooks[ name ]
- ProfileTable = Benchmarks[ name ]
- if HookTable then
- for k, v in pairs( HookTable ) do
- time_took = SysTime()
- -- Call hook function
- b, rA, rB, rC, rD, rE, rF, rG, rH = pcall( v, ... )
- if ProfileTable and ProfileTable[k] then
- ProfileTable[k].time = (ProfileTable[k].time or 0) + (SysTime() - time_took)
- ProfileTable[k].count = (ProfileTable[k].count or 0) + 1
- end
- if not b then
- HookTable[ k ] = nil -- remove this hook
- ErrorNoHalt("[Hook] "..tostring(name)..":"..tostring(k).." removed: "..tostring(rA).."\n")
- else
- -- Allow hooks to override return values
- if rA ~= nil then
- return rA, rB, rC, rD, rE, rF, rG, rH
- end
- end
- end
- end
- if gm then
- GamemodeFunction = gm[ name ]
- if not GamemodeFunction then return end
- -- This calls the actual gamemode function - after all the hooks have had chance to override
- b, rA, rB, rC, rD, rE, rF, rG, rH = pcall( GamemodeFunction, gm, ... )
- if not b then
- gm[ name .. "_ERRORCOUNT" ] = (gm[ name .. "_ERRORCOUNT" ] or 0) + 1
- ErrorNoHalt( tostring(rA) .. "(Hook: "..tostring(name)..")\n" )
- return end
- return rA, rB, rC, rD, rE, rF, rG, rH
- end
- end
- ------------------------
- -- Useful hook dump :v
- ------------------------
- concommand.Add('dump_hooks_ex',function(pl,_,params)
- if SERVER and ValidEntity( pl ) then return end
- local hookname=params[1]
- if hookname then
- else
- local h=0
- for k,v in pairs(GetTable()) do
- MsgN("> "..k.." ("..table.Count(v).." hooks):")
- for name,func in pairs(v) do
- h=h+1
- Msg(" \""..name.."\" \t "..tostring(debug.getinfo(func).source)..":")
- Msg(" Line:"..tostring(debug.getinfo(func).linedefined)..'\n')
- end
- MsgN()
- end
- Msg("\n==== Total hooks: "..h.." ====")
- end
- end)
- ------------------------
- -- Additions
- ------------------------
- local DisabledHooks=false
- local Backup={}
- function DisableHooks()
- if DisabledHooks then ErrorNoHalt "Already disabled hooks" return end
- DisabledHooks=true
- Backup=table.Copy(GetTable())
- table.Empty(GetTable())
- end
- function EnableHooks()
- if not DisabledHooks then ErrorNoHalt "No hooks to enable" return end
- DisabledHooks = false
- table.Merge( GetTable(), Backup )
- table.Empty( Backup )
- end
- concommand.Add('hooks_disable_all',function(pl)
- if SERVER and ValidEntity( pl ) then return end -- Prevent major exploit
- DisableHooks()
- end)
- concommand.Add('hooks_enable_all',function(pl)
- if SERVER and ValidEntity( pl ) then return end -- Prevent major exploit
- EnableHooks()
- end)
Advertisement
Add Comment
Please, Sign In to add comment