/Tukui/modules/databars/memory.lua

http://github.com/Asphyxia/Tukui · Lua · 123 lines · 101 code · 18 blank · 4 comment · 19 complexity · 2ff54f6fd97290bf336010a000869661 MD5 · raw file

  1. local T, C, L = unpack(select(2, ...)) -- Import: T - functions, constants, variables; C - config; L - locales
  2. local ADDON_NAME, ns = ...
  3. local oUF = ns.oUF or oUF
  4. ns._Objects = {}
  5. ns._Headers = {}
  6. if not C["databars"].memory or C["databars"].memory == 0 then return end
  7. local barNum = C["databars"].memory
  8. local barTex = C.media.normTex
  9. T.databars[barNum]:Show()
  10. local Stat = CreateFrame("Frame", nil, T.databars[barNum])
  11. Stat:EnableMouse(true)
  12. Stat:SetFrameStrata("BACKGROUND")
  13. Stat:SetFrameLevel(4)
  14. Stat.tooltip = false
  15. Stat:ClearAllPoints()
  16. Stat:SetAllPoints(T.databars[barNum])
  17. local StatusBar = T.databars[barNum].statusbar
  18. local Text = T.databars[barNum].text
  19. local bandwidthString = "%.2f Mbps"
  20. local percentageString = "%.2f%%"
  21. local kiloByteString = "%d"..T.datacolor.." kb"
  22. local megaByteString = "%.2f"..T.datacolor.." mb"
  23. local function formatMem(memory)
  24. local mult = 10^1
  25. if memory > 999 then
  26. local mem = ((memory/1024) * mult) / mult
  27. return string.format(megaByteString, mem)
  28. else
  29. local mem = (memory * mult) / mult
  30. return string.format(kiloByteString, mem)
  31. end
  32. end
  33. local memoryTable = {}
  34. local function RebuildAddonList(self)
  35. local addOnCount = GetNumAddOns()
  36. if (addOnCount == #memoryTable) or self.tooltip == true then return end
  37. -- Number of loaded addons changed, create new memoryTable for all addons
  38. memoryTable = {}
  39. for i = 1, addOnCount do
  40. memoryTable[i] = { i, select(2, GetAddOnInfo(i)), 0, IsAddOnLoaded(i) }
  41. end
  42. self:SetAllPoints(T.databars[barNum])
  43. end
  44. local function UpdateMemory()
  45. -- Update the memory usages of the addons
  46. UpdateAddOnMemoryUsage()
  47. -- Load memory usage in table
  48. local addOnMem = 0
  49. local totalMemory = 0
  50. for i = 1, #memoryTable do
  51. addOnMem = GetAddOnMemoryUsage(memoryTable[i][1])
  52. memoryTable[i][3] = addOnMem
  53. totalMemory = totalMemory + addOnMem
  54. end
  55. -- Sort the table to put the largest addon on top
  56. table.sort(memoryTable, function(a, b)
  57. if a and b then
  58. return a[3] > b[3]
  59. end
  60. end)
  61. return totalMemory
  62. end
  63. local int = 10
  64. local function Update(self, t)
  65. int = int - t
  66. if int < 0 then
  67. RebuildAddonList(self)
  68. local total = UpdateMemory()
  69. Text:SetText(formatMem(total))
  70. StatusBar:SetMinMaxValues(0,10000)
  71. StatusBar:SetValue(total)
  72. StatusBar:SetStatusBarColor( 41/255, 79/255, 155/255 )
  73. int = 10
  74. end
  75. end
  76. Stat:SetScript("OnMouseDown", function () collectgarbage("collect") Update(Stat, 10) end)
  77. Stat:SetScript("OnEnter", function(self)
  78. if not InCombatLockdown() then
  79. self.tooltip = true
  80. local bandwidth = GetAvailableBandwidth()
  81. local xoff, yoff = T.DataBarTooltipAnchor(barNum)
  82. GameTooltip:SetOwner(T.databars[barNum], "ANCHOR_BOTTOMRIGHT", xoff, yoff)
  83. GameTooltip:ClearLines()
  84. if bandwidth ~= 0 then
  85. GameTooltip:AddDoubleLine(L.datatext_bandwidth , string.format(bandwidthString, bandwidth),0.69, 0.31, 0.31,0.84, 0.75, 0.65)
  86. GameTooltip:AddDoubleLine(L.datatext_download , string.format(percentageString, GetDownloadedPercentage() *100),0.69, 0.31, 0.31, 0.84, 0.75, 0.65)
  87. GameTooltip:AddLine(" ")
  88. end
  89. local totalMemory = UpdateMemory()
  90. GameTooltip:AddDoubleLine(L.datatext_totalmemusage, formatMem(totalMemory), 0.69, 0.31, 0.31,0.84, 0.75, 0.65)
  91. GameTooltip:AddLine(" ")
  92. for i = 1, #memoryTable do
  93. if (memoryTable[i][4]) then
  94. local red = memoryTable[i][3] / totalMemory
  95. local green = 1 - red
  96. GameTooltip:AddDoubleLine(memoryTable[i][2], formatMem(memoryTable[i][3]), 1, 1, 1, red, green + .5, 0)
  97. end
  98. end
  99. GameTooltip:Show()
  100. end
  101. end)
  102. Stat:SetScript("OnLeave", function(self) self.tooltip = false GameTooltip:Hide() end)
  103. Stat:SetScript("OnUpdate", Update)
  104. Stat:SetScript("OnEvent", function(self, event) collectgarbage("collect") end)
  105. Update(Stat, 10)