PageRenderTime 264ms CodeModel.GetById 101ms app.highlight 6ms RepoModel.GetById 155ms app.codeStats 0ms

/Tukui/modules/datatext/system.lua

http://github.com/Asphyxia/Tukui
Lua | 114 lines | 90 code | 15 blank | 9 comment | 14 complexity | 34639b99fee17f4e144a86ce32e24bcc MD5 | raw file
  1--------------------------------------------------------------------
  2-- System Stats
  3--------------------------------------------------------------------
  4local T, C, L = unpack(select(2, ...)) -- Import Functions/Constants, Config, Locales
  5
  6if not C["datatext"].system or C["datatext"].system == 0 then return end
  7
  8local Stat = CreateFrame("Frame")
  9Stat:RegisterEvent("PLAYER_ENTERING_WORLD")
 10Stat:SetFrameStrata("HIGH")
 11Stat:SetFrameLevel(3)
 12Stat:EnableMouse(true)
 13Stat.tooltip = false
 14
 15local Text  = TukuiInfoLeft:CreateFontString(nil, "OVERLAY")
 16Text:SetFont(C.media.pixelfont, C["datatext"].fontsize, "MONOCHROMEOUTLINE")
 17T.PP(C["datatext"].system, Text)
 18
 19local bandwidthString = "%.2f Mbps"
 20local percentageString = "%.2f%%"
 21
 22local kiloByteString = "%d"..T.datacolor.." kb"
 23local megaByteString = "%.2f"..T.datacolor.." mb"
 24
 25local function formatMem(memory)
 26	local mult = 10^1
 27	if memory > 999 then
 28		local mem = ((memory/1024) * mult) / mult
 29		return string.format(megaByteString, mem)
 30	else
 31		local mem = (memory * mult) / mult
 32		return string.format(kiloByteString, mem)
 33	end
 34end
 35
 36local memoryTable = {}
 37
 38local function RebuildAddonList(self)
 39	local addOnCount = GetNumAddOns()
 40	if (addOnCount == #memoryTable) or self.tooltip == true then return end
 41
 42	-- Number of loaded addons changed, create new memoryTable for all addons
 43	memoryTable = {}
 44	for i = 1, addOnCount do
 45		memoryTable[i] = { i, select(2, GetAddOnInfo(i)), 0, IsAddOnLoaded(i) }
 46	end
 47	self:SetAllPoints(Text)
 48end
 49
 50local function UpdateMemory()
 51	-- Update the memory usages of the addons
 52	UpdateAddOnMemoryUsage()
 53	-- Load memory usage in table
 54	local addOnMem = 0
 55	local totalMemory = 0
 56	for i = 1, #memoryTable do
 57		addOnMem = GetAddOnMemoryUsage(memoryTable[i][1])
 58		memoryTable[i][3] = addOnMem
 59		totalMemory = totalMemory + addOnMem
 60	end
 61	-- Sort the table to put the largest addon on top
 62	table.sort(memoryTable, function(a, b)
 63		if a and b then
 64			return a[3] > b[3]
 65		end
 66	end)
 67	
 68	return totalMemory
 69end
 70
 71local int = 10
 72
 73local function Update(self, t)
 74	int = int - t
 75	
 76	if int < 0 then
 77		RebuildAddonList(self)
 78		local total = UpdateMemory()
 79		Text:SetText(formatMem(total))
 80		int = 10
 81	end
 82end
 83
 84Stat:SetScript("OnMouseDown", function () collectgarbage("collect") Update(Stat, 10) end)
 85Stat:SetScript("OnEnter", function(self)
 86	if not InCombatLockdown() then
 87		self.tooltip = true
 88		local bandwidth = GetAvailableBandwidth()
 89		local anchor, panel, xoff, yoff = T.DataTextTooltipAnchor(Text)
 90		GameTooltip:SetOwner(panel, anchor, xoff, yoff)
 91		GameTooltip:ClearLines()
 92		if bandwidth ~= 0 then
 93			-- GetAvailableBandwidth() func is bugged as always, commenting for now
 94			-- GameTooltip:AddDoubleLine(L.datatext_bandwidth , string.format(bandwidthString, bandwidth),0.69, 0.31, 0.31,0.84, 0.75, 0.65)
 95			GameTooltip:AddDoubleLine(L.datatext_download , string.format(percentageString, GetDownloadedPercentage() *100),0.69, 0.31, 0.31, 0.84, 0.75, 0.65)
 96			GameTooltip:AddLine(" ")
 97		end
 98		local totalMemory = UpdateMemory()
 99		GameTooltip:AddDoubleLine(L.datatext_totalmemusage, formatMem(totalMemory), 0.69, 0.31, 0.31,0.84, 0.75, 0.65)
100		GameTooltip:AddLine(" ")
101		for i = 1, #memoryTable do
102			if (memoryTable[i][4]) then
103				local red = memoryTable[i][3] / totalMemory
104				local green = 1 - red
105				GameTooltip:AddDoubleLine(memoryTable[i][2], formatMem(memoryTable[i][3]), 1, 1, 1, red, green + .5, 0)
106			end						
107		end
108		GameTooltip:Show()
109	end
110end)
111Stat:SetScript("OnLeave", function(self) self.tooltip = false GameTooltip:Hide() end)
112Stat:SetScript("OnUpdate", Update)
113Stat:SetScript("OnEvent", function(self, event) collectgarbage("collect") end)
114Update(Stat, 10)