CapsAdmin

Untitled

Mar 10th, 2011
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.42 KB | None | 0 0
  1. hook = hook or {} local hook = hook
  2.  
  3. HOOK_DESTROY = "??|___HOOK_DESTROY___|??" -- what
  4.  
  5. hook.active = {}
  6. hook.errors = {}
  7. hook.profil = {}
  8. hook.destroy_tag = HOOK_DESTROY
  9.  
  10. hook.profiler_enabled = false
  11.  
  12. function hook.Add(event, unique, func, on_error)   
  13.     unique = unique == nil and event .. "_WITH_NO_HOOK_NAME_" .. tostring(func) or unique  
  14.    
  15.     hook.active[event] = hook.active[event] or {}
  16.     hook.active[event][unique] = {
  17.         func = func,
  18.         on_error = on_error,
  19.     }
  20. end
  21.  
  22. function hook.Remove(event, unique)
  23.     if unique ~= nil and hook.active[event] and hook.active[event][unique] then
  24.         hook.active[event][unique] = nil
  25.     else
  26.         Msg(Format("Tried to remove non existing hook '%s:%s'", event, tostring(unique)))
  27.     end
  28. end
  29.  
  30. function hook.GetTable()
  31.     return hook.active
  32. end
  33.  
  34.  
  35. local status, a,b,c,d,e,f,g,h
  36. local time = 0
  37.  
  38. local type = type
  39. local SysTime = SysTime
  40. local pairs = pairs
  41. local pcall = pcall
  42.  
  43. function hook.Call(event, gm, ...)
  44.  
  45.     if hook.active[event] then
  46.         for unique, data in pairs(hook.active[event]) do
  47.            
  48.             if hook.profiler_enabled == true then
  49.                 hook.profil[event] = hook.profil[event] or {}
  50.                 hook.profil[event][unique] = hook.profil[event][unique] or {}
  51.                                
  52.                 time = SysTime()
  53.             end
  54.        
  55.             status, a,b,c,d,e,f,g,h = pcall(data.func, ...)
  56.            
  57.             if a == hook.destroy_tag then
  58.                 hook.Remove(event, unique)
  59.             break end
  60.            
  61.             if hook.profiler_enabled == true then
  62.                 hook.profil[event][unique].time = (hook.profil[event][unique].time or 0) + (SysTime() - time)
  63.                 hook.profil[event][unique].count = (hook.profil[event][unique].count or 0) + 1
  64.             end
  65.            
  66.             if status == false then
  67.                 if data.on_error then
  68.                     if data.on_error(a) == false then
  69.                         hook.Remove(event, unique)
  70.                     end
  71.                 else
  72.                     hook.errors[event] = hook.errors[event] or {}
  73.                     table.insert(hook.errors[event], {unique = unique, error = a, time = os.date("*t")})
  74.                     ErrorNoHalt("[Hook] " .. tostring(event) .. ":" .. tostring(unique) .. " removed: " .. tostring(a) .. "\n")
  75.                     hook.Remove(event, unique)
  76.                 end
  77.                 break
  78.             end
  79.            
  80.             if a or b or c or d or e or f or g or h then
  81.                 return a,b,c,d,e,f,g,h
  82.             end
  83.            
  84.         end
  85.     end
  86.    
  87.     if type(gm) == "table" and type(gm[event]) == "function" then
  88.    
  89.         status, a,b,c,d,e,f,g,h = pcall(gm[event], gm, ...)
  90.        
  91.         if status == false then
  92.             hook.errors["GAMEMODE"] = hook.errors["GAMEMODE"] or {}
  93.             table.insert(hook.errors["GAMEMODE"], {event = event, unique = unique, error = a, time = os.date("*t")})
  94.             ErrorNoHalt("[GAMEMODE] " .. tostring(event) .. " errored: " .. tostring(a) .. "\n")
  95.         return nil end
  96.        
  97.         if a or b or c or d or e or f or g or h then
  98.             return a,b,c,d,e,f,g,h
  99.         end
  100.        
  101.     end
  102.    
  103. end
  104.  
  105. function hook.GetErrorHistory()
  106.     return hook.errors
  107. end
  108.  
  109. function hook.GetProfilerHistory()
  110.     local new = {}
  111.    
  112.     for event, hooks in pairs(hook.profil) do
  113.         for unique, _data in pairs(hooks) do
  114.             if table.Count(_data) ~= 0 and _data.time ~= 0 and _data.count ~= 0 then
  115.                 local data = {}
  116.                
  117.                 data.event = event
  118.                 data.average =  math.Round((_data.time / _data.count) * 1000, 9)                       
  119.                 local info = debug.getinfo(hook.GetTable()[event][unique].func)
  120.                 data.hook = event
  121.                 data.source = info.short_src:gsub("\\", "/")
  122.                 data.line_defined = info.linedefined
  123.                 data.times_ran = _data.count
  124.                
  125.                 if data.average ~= 0 then
  126.                     table.insert(new, data)
  127.                 end
  128.             end
  129.         end
  130.     end
  131.    
  132.     table.SortByMember(new, "average")
  133.    
  134.     return new
  135. end
  136.  
  137. function hook.EnableProfiler()
  138.     hook.profiler_enabled = true
  139. end
  140.  
  141. function hook.DisableProfiler()
  142.     hook.profiler_enabled = false
  143. end
  144.  
  145. function hook.DisableAll()
  146.     if not hook.enabled then
  147.         ErrorNoHalt("Hooks are already disabled.")
  148.     else
  149.         hook.enabled = true
  150.         hook.__backup_hooks = table.Copy(hook.GetTable())
  151.         table.Empty(hook.GetTable())
  152.     end
  153. end
  154.  
  155. function hook.EnableAll()
  156.     if hook.enabled then
  157.         ErrorNoHalt("Hooks are already enabled.")
  158.     else
  159.         hook.enabled = false
  160.         table.Merge( hook.GetTable(), hook.__backup_hooks )
  161.         hook.__backup_hooks = nil
  162.     end
  163. end
  164.  
  165. function hook.Dump()
  166.     local h=0
  167.     for k,v in pairs(hook.GetTable()) do
  168.         Msg("> "..k.." ("..table.Count(v).." hooks):\n")
  169.         for name,data in pairs(v) do
  170.             h=h+1
  171.             Msg("   \""..name.."\" \t "..tostring(debug.getinfo(data.func).source)..":")
  172.             Msg(" Line:"..tostring(debug.getinfo(data.func).linedefined)..'\n')
  173.         end
  174.         Msg"\n"
  175.     end
  176.     Msg("\n>>> Total hooks: "..h..".\n")
  177. end
Advertisement
Add Comment
Please, Sign In to add comment