View Single Post
01-07-13, 04:14 PM   #4
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
There are a couple issues I'm seeing. First of all, it's not a good idea to replace the metatable of a frame unless you absolutely need to. Also, you're creating an extra frame that you're not using for any specific purpose that needs a frame. Taking these issues in hand and some further optimizations, here is the code that I'd propose.

Lua Code:
  1. --  Settings
  2. local MaxDisplay=5;
  3. local UpdateSpeed=1;
  4.  
  5. --  Init table
  6. local AddonData={};
  7. for i=1,GetNumAddOns() do
  8.     AddonData[i]={ID=i,Name=GetAddOnInfo(i),Value=0};
  9. end
  10.  
  11. --  Format time func
  12. local function AddCPULine(tooltip,name,secs)
  13.     if secs > 3600 then
  14.         tooltip:AddDoubleLine(name,format('%.2f h',secs/3600),1,1,1,1,0.2,0.2);
  15.     elseif secs > 60 then
  16.         tooltip:AddDoubleLine(name,format('%.2f m',secs/60),1,1,1,1,1,0.2);
  17.     elseif secs >= 1 then
  18.         tooltip:AddDoubleLine(name,format('%.1f s',secs),1,1,1,0.2,1,0.2);
  19.     elseif secs > 0 then
  20.         tooltip:AddDoubleLine(name,format('%.1f ms',secs*1000),1,1,1,0.2,1,0.2);
  21.     end
  22. end
  23.  
  24. --  Format memory func
  25. local function AddMemoryLine(tooltip,name, size)
  26.     if size>1000 then
  27.         tooltip:AddDoubleLine(name,format('%.2f mb',size/1000),1,1,1,1,1,0.2);
  28.     elseif size>0 then
  29.         tooltip:AddDoubleLine(name,format('%.2f kb',size),1,1,1,0.2,1,0.2);
  30.     end
  31. end
  32.  
  33. --  GameTooltip calls this OnUpdate if it exists
  34. local LastUpdate=0;
  35. function MiniMapTrackingButton:UpdateTooltip()
  36. --  FPS and latency
  37.     local down,up,latency=GetNetStats();
  38.     local fps=format('FPS: %.1f',GetFramerate());
  39.     local net=format('Ping: %d ms',latency);
  40.  
  41.     GameTooltip:SetOwner(self,"ANCHOR_BOTTOMLEFT",0,self:GetHeight());
  42.     GameTooltip:ClearLines();
  43.     GameTooltip:AddDoubleLine(fps,net,1,1,1,1,1,1);
  44.     GameTooltip:AddLine("--------------------------------------------------");
  45.  
  46. --  Update throttle
  47.     local now=GetTime();
  48.     if now-LastUpdate>UpdateSpeed then
  49.         local watchingCPU=(GetCVar("scriptProfile")=="1" and not IsModifierKeyDown());
  50.  
  51. --      Request game client to update internal cache
  52.         if watchingCPU then
  53.             UpdateAddOnCPUUsage();
  54.         else
  55.             UpdateAddOnMemoryUsage();
  56.         end
  57.  
  58. --      Update data
  59.         local total=0;
  60.         for i,j in ipairs(AddonData) do
  61.             local val=(watchingCPU and GetAddOnCPUUsage(j.ID)/1000 or GetAddOnMemoryUsage(j.ID));
  62.             j.Value=val;
  63.             total=total+val;
  64.         end
  65.  
  66. --      Sort data
  67.         for i=1,#AddonData-1 do
  68.             local max,id=0;
  69.             for j=i+1,#AddonData do
  70.                 local val=AddonData[j].Value;
  71.                 if val>max then max,id=val,j; end
  72.             end
  73.  
  74.             if AddonData[i].Value<max then
  75.                 AddonData[i],AddonData[id]=AddonData[id],AddonData[i];
  76.             end
  77.         end
  78.  
  79.         if watchingCPU then
  80.             GameTooltip:AddLine("Addon CPU Usage");
  81.             GameTooltip:AddLine("--------------------------------------------------");
  82.  
  83.             for i,j in ipairs(AddonData) do
  84.                 if i>MaxDisplay then break; end
  85.                 AddCPULine(GameTooltip,j.Name,j.Value);
  86.             end
  87.             GameTooltip:AddLine("--------------------------------------------------");
  88.             AddCPULine(GameTooltip,"Total",total);
  89.         else
  90.             GameTooltip:AddLine("Addon Memory Usage");
  91.             GameTooltip:AddLine("--------------------------------------------------");
  92.  
  93.             for i,j in ipairs(AddonData) do
  94.                 if i>MaxDisplay then break; end
  95.                 AddMemoryLine(GameTooltip,j.Name,j.Value);
  96.             end
  97.             GameTooltip:AddLine("--------------------------------------------------");
  98.             AddMemoryLine(GameTooltip,"Total",total);
  99.         end
  100.     end
  101.     GameTooltip:Show()
  102. end
  103.  
  104. MiniMapTrackingButton:SetScript("OnEnter",function(self)
  105.     GameTooltip:SetOwner(self,"ANCHOR_BOTTOMLEFT",0,self:GetHeight());
  106.     self:UpdateTooltip();
  107. end);
  108.  
  109. MiniMapTrackingButton:SetScript("OnLeave",function() GameTooltip:Hide(); end);
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 01-08-13 at 02:54 PM.
  Reply With Quote