Python1320

Infinite loop protection glua gmod

Jun 1st, 2014
882
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.37 KB | None | 0 0
  1. local Now=SysTime
  2. local debug=debug
  3. local table=table
  4. local backthen=Now()
  5. local counter=0
  6. local c={}
  7. local function infinitetrack()
  8.     if counter>10 then
  9.         debug.sethook()
  10.         counter=0
  11.         ErrorNoHalt"Infinite loop detected: "
  12.         debug.Trace()
  13.         local calls={}
  14.         local prev
  15.         for k,v in pairs(c) do
  16.             local line = tostring(v.func).."\t line: \t"..tostring(v.currentline)
  17.             if prev==line then
  18.                 prev=line
  19.                 line="^"
  20.             else
  21.                 prev=line
  22.             end
  23.             calls[k]=k..': \t'..line
  24.         end
  25.         ErrorNoHalt("\nLast 11 calls:\n"..table.concat(calls,'\n')..'\n')
  26.         c={}
  27.         error("Time limit exceeded",2)
  28.     end
  29.     counter=counter+1
  30.     local info = debug.getinfo(2)
  31.     if info then table.insert(c,info) end
  32.        
  33. end
  34.  
  35. local f=function()
  36.     if Now()-backthen>7 then -- enjoy your 7 seconds freeze
  37.         counter=0
  38.         debug.sethook(infinitetrack,"",1)
  39.     end
  40. end
  41.  
  42. local function load()
  43.     hook.Add("Think","infinite_loop_protection",function()
  44.         backthen=Now()
  45.         debug.sethook(f,"",2^25)
  46.     end)
  47. end
  48.  
  49. concommand.Add("allow_infinite_loop",function(pl)
  50.     if SERVER and IsValid(pl) then return end
  51.     hook.Remove("Think","infinite_loop_protection")
  52.     debug.sethook()
  53. end)
  54.  
  55. concommand.Add("enable_infinite_loop_protection",function(pl)
  56.     if SERVER and IsValid(pl) then return end
  57.     load()
  58. end)
  59.  
  60. if GetConVarNumber("developer") == 666 then
  61.     print("Ah loading this crap anyway")
  62.     load()
  63. end
Advertisement
Add Comment
Please, Sign In to add comment