-
Notifications
You must be signed in to change notification settings - Fork 0
/
PerformanceMonitor.lua
174 lines (149 loc) · 4.84 KB
/
PerformanceMonitor.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
local addonName, addon = ...
local L = addon.L
local ldb = LibStub:GetLibrary("LibDataBroker-1.1")
local ldbi = LibStub:GetLibrary('LibDBIcon-1.0')
local AceTimer = LibStub("AceTimer-3.0")
local timer = nil;
local function showConfig()
InterfaceOptionsFrame_OpenToCategory(addonName)
InterfaceOptionsFrame_OpenToCategory(addonName)
end
local function normal(text)
if not text then return "" end
return NORMAL_FONT_COLOR_CODE..text..FONT_COLOR_CODE_CLOSE;
end
local function red(text)
if not text then return "" end
return RED_FONT_COLOR_CODE..text..FONT_COLOR_CODE_CLOSE;
end
local function yellow(text)
if not text then return "" end
return YELLOW_FONT_COLOR_CODE..text..FONT_COLOR_CODE_CLOSE;
end
local function green(text)
if not text then return "" end
return GREEN_FONT_COLOR_CODE..text..FONT_COLOR_CODE_CLOSE;
end
local function highlight(text)
if not text then return "" end
return HIGHLIGHT_FONT_COLOR_CODE..text..FONT_COLOR_CODE_CLOSE;
end
local function muted(text)
if not text then return "" end
return DISABLED_FONT_COLOR_CODE..text..FONT_COLOR_CODE_CLOSE;
end
-- Init & config panel
do
local eventFrame = CreateFrame("Frame", nil, InterfaceOptionsFramePanelContainer)
eventFrame:SetScript("OnEvent", function(self, event, loadedAddon)
if loadedAddon ~= addonName then return end
self:UnregisterEvent("ADDON_LOADED")
if type(PerformanceMonitorSettings) ~= "table" then PerformanceMonitorSettings = {currencies={},minimap={hide=false}} end
local sv = PerformanceMonitorSettings
if type(sv.minimap) ~= "table" then sv.minimap = {hide=false} end
if type(sv.showFPS) ~= "boolean" then sv.showFPS = true end
if type(sv.showLatency) ~= "boolean" then sv.showLatency = true end
if type(sv.showLatencyWorld) ~= "boolean" then sv.showLatencyWorld = false end
if type(sv.showMem) ~= "boolean" then sv.showMem = true end
addon.db = sv
ldbi:Register(addonName, addon.dataobj, addon.db.minimap)
self:SetScript("OnEvent", nil)
end)
eventFrame:RegisterEvent("ADDON_LOADED")
addon.frame = eventFrame
end
-- data text
do
local f = CreateFrame("frame")
local text = "..loading.."
local tooltip = ""
local dataobj = ldb:NewDataObject("PerformanceMonitor", {
type = "data source",
icon = "Interface\\AddOns\\PerformanceMonitor\\PerformanceMonitor",
text = text,
OnEnter = function(frame)
GameTooltip:SetOwner(frame, "ANCHOR_NONE")
GameTooltip:SetPoint("TOPLEFT", frame, "BOTTOMLEFT")
GameTooltip:ClearLines()
addon:updateTooltip(frame)
GameTooltip:Show()
end,
OnLeave = function()
GameTooltip:Hide()
end,
OnClick = function(self, button)
showConfig()
end,
})
addon.dataobj = dataobj
local function fmtLabel(text)
if not text then return "" end
text = normal(text);
return " "..normal(text)..": ";
end
local function updateText()
local fps = GetFramerate();
local _, _, latencyHome, latencyWorld = GetNetStats();
local memory, gcThreshold = gcinfo();
local function getFPS()
local function color(v)
if (v >= 60) then
return green(format('%2.1f', v))
elseif (v >= 30) then
return yellow(format('%2.1f', v))
end
return red(format('%2.1f', v))
end
return fmtLabel(L.labelFPS)..color(fps)
end
local function getLatency()
local t = fmtLabel(L.labelLatency)
local function color(v)
if (v <= 300) then
return green(format('%d', v)..'ms')
elseif (v <= 600) then
return yellow(format('%d', v)..'ms')
end
return red(format('%d', v)..'ms')
end
if (addon.db.showLatency) then
t = t..color(latencyHome)
end
if (addon.db.showLatency and addon.db.showLatencyWorld) then
t = t.." "
end
if (addon.db.showLatencyWorld) then
t = t..color(latencyWorld)
end
return t
end
local function getMemoryUsage()
local function color(v)
-- @todo thresholds?
return highlight(format('%.3f', v / 1024).." MB")
end
return fmtLabel(L.labelMem)..color(memory)
end
local text = "";
if (addon.db.showFPS) then text = text..getFPS() end
if (addon.db.showLatency or addon.db.showLatencyWorld) then
text = text..getLatency()
end
if (addon.db.showMem) then text = text..getMemoryUsage() end
dataobj.text = text;
end
function addon:updateTooltip()
GameTooltip:AddLine(L["PerformanceMonitor"].."\n")
end
function addon:setDB(key, value)
addon.db[key] = value
updateText()
end
f:RegisterEvent("PLAYER_ENTERING_WORLD");
f:SetScript("OnEvent", function(self, event)
if not timer then
-- @todo should this only run when the datatext is displayed?
timer = AceTimer.ScheduleRepeatingTimer("PerformanceMonitor", updateText, 1.5 )
end
end)
end