Python1320

Untitled

Oct 19th, 2010
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.38 KB | None | 0 0
  1. print(("HNG"):rep(500))
  2.  
  3. local pairs = pairs
  4. local Error = Error
  5. local ErrorNoHalt = ErrorNoHalt
  6. local pcall = pcall
  7. local tostring = tostring
  8. local concommand = concommand
  9. local PrintTable = PrintTable
  10. local CLIENT = CLIENT
  11. local Msg = Msg
  12. local type = type
  13. local ValidEntity=ValidEntity
  14. local debug=debug
  15. local table=table
  16. local SysTime = SysTime
  17. local math = math
  18.  
  19. module("hook")
  20.  
  21. -- Local variables
  22. local Hooks = {}
  23. local Benchmarks = {}
  24.  
  25. -- Exposed Functions
  26.  
  27. function GetTable()
  28.     return Hooks
  29. end
  30.  
  31. function GetBenchmark()
  32.     local tbl = table.Copy(Benchmarks)
  33.     for name, hook in pairs(tbl) do
  34.         if table.Count(hook) ~= 0 then
  35.             for unique, value in pairs(hook) do
  36.                 if value.time and value.count and value.count ~= 0 then
  37.                     value.average =  math.Round((value.time / value.count)*1000, 9)
  38.                     value.time = math.Round(value.time * 1000, 9)
  39.                    
  40.                     if value.average == 0 then
  41.                         tbl[name][unique] = nil
  42.                     end
  43.                 elseif table.Count(tbl[name][unique]) == 0 then
  44.                     tbl[name][unique] = nil
  45.                 end
  46.             end
  47.         end
  48.         if table.Count(hook) == 0 then
  49.             tbl[name] = nil
  50.         end
  51.     end
  52.     return tbl
  53. end
  54.  
  55. function Add( event_name, name, func )
  56.  
  57.     if (Hooks[ event_name ] == nil) then
  58.         Hooks[ event_name ] = {}
  59.     end
  60.  
  61.     Hooks[ event_name ][ name ] = func
  62.    
  63.     if Benchmarks[ event_name ] == nil then
  64.         Benchmarks[ event_name ] = {}
  65.     end
  66.    
  67.     Benchmarks[ event_name ][ name ] = {}
  68. end
  69.  
  70. function Remove( event_name, name )
  71.     Hooks[ event_name ][ name ] = nil
  72.     Benchmarks[ event_name ][ name ] = nil
  73. end
  74.  
  75. local b, rA, rB, rC, rD, rE, rF, rG, rH
  76. local HookTable, GamemodeFunction
  77. local time_took, ProfileTable -- I have no idea what I should call Benchmark[event_name]
  78.  
  79. function Call( name, gm, ... )
  80.  
  81.     HookTable = Hooks[ name ]
  82.     ProfileTable = Benchmarks[ name ]
  83.  
  84.     if HookTable then
  85.    
  86.         for k, v in pairs( HookTable ) do
  87.  
  88.            
  89.             time_took = SysTime()
  90.            
  91.             -- Call hook function
  92.             b, rA, rB, rC, rD, rE, rF, rG, rH = pcall( v, ... )
  93.            
  94.             if ProfileTable and ProfileTable[k] then
  95.                 ProfileTable[k].time = (ProfileTable[k].time or 0) + (SysTime() - time_took)
  96.                 ProfileTable[k].count = (ProfileTable[k].count or 0) + 1
  97.             end
  98.            
  99.            
  100.             if not b then
  101.            
  102.                 HookTable[ k ] = nil -- remove this hook
  103.                 ErrorNoHalt("[Hook] "..tostring(name)..":"..tostring(k).." removed: "..tostring(rA).."\n")
  104.            
  105.             else
  106.            
  107.                 -- Allow hooks to override return values
  108.                 if rA ~= nil then
  109.                     return rA, rB, rC, rD, rE, rF, rG, rH
  110.                 end
  111.                
  112.             end
  113.            
  114.         end
  115.     end
  116.    
  117.     if gm then
  118.    
  119.         GamemodeFunction = gm[ name ]
  120.         if not GamemodeFunction then return end
  121.        
  122.         -- This calls the actual gamemode function - after all the hooks have had chance to override
  123.         b, rA, rB, rC, rD, rE, rF, rG, rH = pcall( GamemodeFunction, gm, ... )
  124.        
  125.         if not b then
  126.             gm[ name .. "_ERRORCOUNT" ] = (gm[ name .. "_ERRORCOUNT" ] or 0) + 1
  127.             ErrorNoHalt( tostring(rA) .. "(Hook: "..tostring(name)..")\n" )
  128.            
  129.         return end
  130.        
  131.         return rA, rB, rC, rD, rE, rF, rG, rH
  132.        
  133.     end
  134.    
  135.    
  136. end
  137.  
  138.  
  139. ------------------------
  140. -- Useful hook dump :v
  141. ------------------------
  142. concommand.Add('dump_hooks_ex',function(pl,_,params)
  143.     if SERVER and ValidEntity( pl ) then return end
  144.    
  145.     local hookname=params[1]
  146.     if hookname then
  147.        
  148.     else
  149.         local h=0
  150.         for k,v in pairs(GetTable()) do
  151.             MsgN("> "..k.." ("..table.Count(v).." hooks):")
  152.             for name,func in pairs(v) do
  153.                 h=h+1
  154.                 Msg("   \""..name.."\" \t "..tostring(debug.getinfo(func).source)..":")
  155.                 Msg(" Line:"..tostring(debug.getinfo(func).linedefined)..'\n')
  156.             end
  157.             MsgN()
  158.         end
  159.         Msg("\n==== Total hooks: "..h.." ====")
  160.     end
  161. end)
  162.  
  163.  
  164. ------------------------
  165. -- Additions
  166. ------------------------
  167. local DisabledHooks=false
  168.  
  169. local Backup={}
  170.  
  171. function DisableHooks()
  172.     if DisabledHooks then ErrorNoHalt "Already disabled hooks" return end
  173.     DisabledHooks=true
  174.     Backup=table.Copy(GetTable())
  175.     table.Empty(GetTable())
  176. end
  177.  
  178. function EnableHooks()
  179.     if not DisabledHooks then ErrorNoHalt "No hooks to enable" return end
  180.     DisabledHooks = false
  181.     table.Merge( GetTable(), Backup )
  182.     table.Empty( Backup )
  183. end
  184.  
  185.  
  186. concommand.Add('hooks_disable_all',function(pl)
  187.     if SERVER and ValidEntity( pl ) then return end -- Prevent major exploit
  188.     DisableHooks()
  189. end)
  190.  
  191. concommand.Add('hooks_enable_all',function(pl)
  192.     if SERVER and ValidEntity( pl ) then return end -- Prevent major exploit
  193.     EnableHooks()
  194. end)
Advertisement
Add Comment
Please, Sign In to add comment