Python1320

Garbage Collector Util

Nov 18th, 2011
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.56 KB | None | 0 0
  1. local collectgarbage=collectgarbage
  2. local ScrW=ScrW
  3. local Round=math.Round
  4. local surface=surface or math
  5. local draw=draw or math
  6.  
  7. local DrawRect=surface.DrawRect
  8. local SetDrawColor=surface.SetDrawColor
  9. local SimpleText=draw.SimpleText
  10. local TEXT_ALIGN_CENTER=TEXT_ALIGN_CENTER
  11. local SetFont=surface.SetFont
  12. local SetTextPos=surface.SetTextPos
  13. local SetTextColor=surface.SetTextColor
  14. local GetTextSize=surface.GetTextSize
  15. local DrawText=surface.DrawText
  16.  
  17. if SERVER then
  18.     local times=0
  19.     hook.Add('Think',1,function()
  20.         umsg.Start("GC")
  21.             umsg.Float(collectgarbage"count")
  22.         umsg.End()
  23.        
  24.         if times==0 then return end
  25.        
  26.         for i=1,times do
  27.             collectgarbage"step"
  28.         end
  29.        
  30.     end)
  31.  
  32.     function SetGC(n)
  33.         times=tonumber(n) or times
  34.     end
  35.     return
  36. end
  37.  
  38. local kb_s
  39. usermessage.Hook("GC",function( umsg )
  40.     kb_s=umsg:ReadFloat()
  41. end)
  42.  
  43. local max1,max2=16,16
  44. local col=Color(255,255,255,255)
  45. local PAINT=function(pnl)
  46.     local W=pnl and pnl:GetWide() or ScrW()
  47.     local oy=1
  48.    
  49.     local kb=collectgarbage"count"
  50.     SetDrawColor(0,0,0,100)
  51.     DrawRect(0,oy,W,10)
  52.     SetDrawColor(0,0,255,100)
  53.    
  54.     local frac=(kb/1024)
  55.    
  56.    
  57.     max1=max1<frac and frac or max1
  58.     DrawRect(0,oy,W*(frac/max1),10)
  59.     local txt=(Round(kb/1024)).." MB"
  60.    
  61.     SetFont"Trebuchet18"
  62.     SetTextColor(255,255,255,255)
  63.     local w,h=GetTextSize(txt)
  64.     SetTextPos( W*(frac/max1)-w*0.5 ,oy-h*0.20)
  65.     DrawText(txt)
  66.  
  67.     oy=12
  68.    
  69.     if not kb_s then return end
  70.    
  71.     kb=kb_s
  72.     SetDrawColor(0,0,0,100)
  73.     DrawRect(0,oy,W,10)
  74.     SetDrawColor(200,50,50,100)
  75.    
  76.     frac=(kb/1024)
  77.    
  78.     max2=max2<frac and frac or max2
  79.    
  80.     DrawRect(0,oy,W*(frac/max2),10)
  81.     txt=(Round(kb/1024*100)/100).." MB"
  82.    
  83.     local w,h=GetTextSize(txt)
  84.     SetTextPos( W*(frac/max2)-w*0.5 ,oy-h*0.20)
  85.     DrawText(txt)
  86.    
  87. end
  88.  
  89. if ValidPanel(_G.GCVIS) then
  90.     _G.GCVIS:Remove()
  91.     _G.GCVIS=nil
  92. end
  93. _G.GCVIS=vgui.Create'EditablePanel'
  94. local a=_G.GCVIS
  95.  
  96. a:SetSize(ScrW(),64-10)
  97. a:SetPos(0,0)
  98. a:SetVisible(true)
  99. a.Paint=PAINT
  100. function a:OnMouseReleased(mc)
  101.     if mc==MOUSE_RIGHT then self:Remove() end
  102. --  if times==0 then hook.Remove("Think","GC") end
  103. end
  104.  
  105. local times=0
  106. hook.Add("Think","GC",function()
  107.     if times==0 then return end
  108.     for i=1,times do
  109.         collectgarbage"step"
  110.     end
  111. end)
  112.  
  113. local w=vgui.Create('DNumberWang',a)
  114. w:SetMin(0)
  115. w:SetMax(4000)
  116. w:SetPos(10,a:GetTall()-w:GetTall())
  117. w:SetWide(100)
  118. function w:OnValueChanged()
  119.     local num = math.Round(w:GetValue())
  120.     times=num  
  121. end
  122.  
  123. function SetGC(n)
  124.     times=tonumber(n) or times
  125.     w:SetValue(times)
  126. end
  127.  
Advertisement
Add Comment
Please, Sign In to add comment