Skip to content

Commit de80627

Browse files
committed
Update addon structure
- use . notation instead of self reference - make some valiables local
1 parent fbdec4a commit de80627

File tree

1 file changed

+84
-72
lines changed

1 file changed

+84
-72
lines changed
Lines changed: 84 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
-- Core Addon Table
12
MemoryGarbageCollector = {
23
name = "MemoryGarbageCollector",
34
version = "dev",
@@ -7,20 +8,27 @@ MemoryGarbageCollector = {
78
shortName = "MGC",
89
prefix = "|cbf16b9[MGC]|r",
910

11+
-- Cleanup threshold
12+
maxMemoryUsage = 0,
13+
1014
chat = nil,
1115

1216
color = {
1317
main = "bf16b9",
1418
grey = "666666",
1519
},
1620

21+
-- Saved variables configuration
22+
svName = "MemoryGarbageCollectorSV",
23+
svVersion = 1,
1724
config = {}
1825
}
1926

27+
local EM = EVENT_MANAGER
2028
local MGC = MemoryGarbageCollector
2129

22-
-- Cleanup threshold
23-
MGC.maxMemoryUsage = 0
30+
local MaxMemoryUsage = MGC.maxMemoryUsage
31+
local ChatColor = MGC.color
2432

2533
local DEFAULT_SAVED_VARS = {
2634
autoClear = true,
@@ -38,25 +46,27 @@ local GFS = function(STR_NAME, ...)
3846
return SF(GetString(STR_NAME), ...)
3947
end
4048

41-
function MGC:Initialize()
42-
self.config = ZO_SavedVars:NewAccountWide(self.name .. 'SV', 1, nil, DEFAULT_SAVED_VARS)
49+
function MGC.Initialize()
50+
MGC.config = ZO_SavedVars:NewAccountWide(MGC.svName, MGC.svVersion, nil, DEFAULT_SAVED_VARS)
4351

44-
self:InitMenu()
52+
MGC.InitMenu()
4553

4654
-- Start with 15 sec delay and calculate starting memory usage.
4755
zo_callLater(function()
48-
self:refreshSettings()
56+
MGC.refreshSettings()
4957
end, 15000)
5058
end
5159

52-
function MGC:InitMenu()
60+
function MGC.InitMenu()
61+
local config = MGC.config
62+
5363
local LAM = LibAddonMenu2
54-
local panelName = self.name .. "_LAM"
64+
local panelName = MGC.name .. "_LAM"
5565

5666
local panelData = {
5767
type = "panel",
58-
name = self.fullName,
59-
author = self.author,
68+
name = MGC.fullName,
69+
author = MGC.author,
6070
registerForRefresh = true,
6171
registerForDefaults = true,
6272
}
@@ -75,11 +85,11 @@ function MGC:InitMenu()
7585
name = GFS(SI_MGC_AUTO_CLEAR),
7686
tooltip = GFS(SI_MGC_AUTO_CLEAR_TOOLTIP),
7787
getFunc = function()
78-
return self.config.autoClear
88+
return config.autoClear
7989
end,
8090
setFunc = function(v)
81-
self.config.autoClear = v
82-
self:refreshSettings()
91+
config.autoClear = v
92+
MGC.refreshSettings()
8393
end,
8494
default = DEFAULT_SAVED_VARS.autoClear,
8595
},
@@ -91,14 +101,14 @@ function MGC:InitMenu()
91101
max = 60,
92102
decimals = 0,
93103
disabled = function()
94-
return not self.config.autoClear
104+
return not config.autoClear
95105
end,
96106
getFunc = function()
97-
return self.config.refreshRate
107+
return config.refreshRate
98108
end,
99109
setFunc = function(v)
100-
self.config.refreshRate = v
101-
self:refreshSettings()
110+
config.refreshRate = v
111+
MGC.refreshSettings()
102112
end,
103113
default = DEFAULT_SAVED_VARS.refreshRate,
104114
},
@@ -107,14 +117,14 @@ function MGC:InitMenu()
107117
name = GFS(SI_MGC_COMPARISON_METHOD),
108118
tooltip = GFS(SI_MGC_COMPARISON_METHOD_TOOLTIP),
109119
disabled = function()
110-
return not self.config.autoClear
120+
return not config.autoClear
111121
end,
112122
getFunc = function()
113-
return self.config.comparisonMethod
123+
return config.comparisonMethod
114124
end,
115125
setFunc = function(v)
116-
self.config.comparisonMethod = v
117-
self:refreshSettings()
126+
config.comparisonMethod = v
127+
MGC.refreshSettings()
118128
end,
119129
choicesValues = { 1, 2 },
120130
choices = { GFS(SI_MGC_OVERFLOW_RELATIVE), GFS(SI_MGC_OVERFLOW_ABSOLUTE) },
@@ -129,14 +139,14 @@ function MGC:InitMenu()
129139
step = 5,
130140
decimals = 0,
131141
disabled = function()
132-
return not (self.config.autoClear and self.config.comparisonMethod == 1)
142+
return not (config.autoClear and config.comparisonMethod == 1)
133143
end,
134144
getFunc = function()
135-
return self.config.overflowRelative
145+
return config.overflowRelative
136146
end,
137147
setFunc = function(v)
138-
self.config.overflowRelative = v
139-
self:refreshSettings()
148+
config.overflowRelative = v
149+
MGC.refreshSettings()
140150
end,
141151
default = DEFAULT_SAVED_VARS.overflowRelative,
142152
},
@@ -149,14 +159,14 @@ function MGC:InitMenu()
149159
step = 25,
150160
decimals = 0,
151161
disabled = function()
152-
return not (self.config.autoClear and self.config.comparisonMethod == 2)
162+
return not (config.autoClear and config.comparisonMethod == 2)
153163
end,
154164
getFunc = function()
155-
return self.config.overflowAbsolute
165+
return config.overflowAbsolute
156166
end,
157167
setFunc = function(v)
158-
self.config.overflowAbsolute = v
159-
self:refreshSettings()
168+
config.overflowAbsolute = v
169+
MGC.refreshSettings()
160170
end,
161171
default = DEFAULT_SAVED_VARS.overflowAbsolute,
162172
},
@@ -165,14 +175,14 @@ function MGC:InitMenu()
165175
name = GetString(SI_SETTINGSYSTEMPANEL6),
166176
tooltip = GetString(SI_MGC_SHOW_DEBUG_MESSAGES),
167177
disabled = function()
168-
return not self.config.autoClear
178+
return not config.autoClear
169179
end,
170180
getFunc = function()
171-
return self.config.showDebugMessages
181+
return config.showDebugMessages
172182
end,
173183
setFunc = function(v)
174-
self.config.showDebugMessages = v
175-
self:refreshSettings()
184+
config.showDebugMessages = v
185+
MGC.refreshSettings()
176186
end,
177187
default = DEFAULT_SAVED_VARS.showDebugMessages,
178188
},
@@ -182,99 +192,101 @@ function MGC:InitMenu()
182192
LAM:RegisterOptionControls(panelName, optionsTable)
183193
end
184194

185-
function MGC:refreshSettings()
186-
MGC:calcMaxMemory()
187-
MGC:setRefreshTimer()
195+
function MGC.refreshSettings()
196+
MGC.calcMaxMemory()
197+
MGC.setRefreshTimer()
188198
end
189199

190-
function MGC:setRefreshTimer()
200+
function MGC.setRefreshTimer()
191201
local eventName = MGC.name .. "_Auto"
192202
EVENT_MANAGER:UnregisterForUpdate(eventName)
193203

194204
if MGC.config.autoClear == true then
195205
local refreshSeconds = MGC.config.refreshRate * 60 * 1000 -- min to seconds
196206
EVENT_MANAGER:RegisterForUpdate(eventName, refreshSeconds, function()
197-
MGC:checkGarbage()
207+
MGC.checkGarbage()
198208
end)
199209
end
200210
end
201211

202-
function MGC:calcMaxMemory(memory)
212+
function MGC.calcMaxMemory(memory)
213+
if not MGC.config.autoClear then
214+
return
215+
end
216+
203217
if not memory then
204-
memory = self:currentMemory()
218+
memory = MGC.currentMemory()
205219
end
206220

207221
local max = 1024
208-
if self.config.comparisonMethod == 1 then
209-
max = memory * (1 + self.config.overflowRelative / 100)
222+
if MGC.config.comparisonMethod == 1 then
223+
max = memory * (1 + MGC.config.overflowRelative / 100)
210224
else
211-
max = memory + self.config.overflowAbsolute
225+
max = memory + MGC.config.overflowAbsolute
212226
end
213227

214228
-- Round to 5 MB
215229
max = math.ceil(max / 5) * 5
216230

217-
self:sendDebugMessage(GFS(SI_MGC_MEMORY_INIT_MAX, max))
218-
self.maxMemoryUsage = max
231+
MGC.sendDebugMessage(GFS(SI_MGC_MEMORY_INIT_MAX, max))
232+
MaxMemoryUsage = max
219233
end
220234

221-
function MGC:currentMemory()
235+
function MGC.currentMemory()
222236
return math.ceil(collectgarbage("count") / 1024)
223237
end
224238

225-
function MGC:checkGarbage()
239+
function MGC.checkGarbage()
226240
if IsUnitInCombat("player") then
227241
return
228242
end
229243

230-
local limit = self.maxMemoryUsage or 1024;
231-
local current = self:currentMemory()
244+
local limit = MaxMemoryUsage or 1024;
245+
local current = MGC.currentMemory()
232246

233247
-- Check currently used memory for overflow limit.
234-
self:sendDebugMessage(GFS(SI_MGC_MEMORY_OVERFLOW_DEBUG, current, limit))
248+
MGC.sendDebugMessage(GFS(SI_MGC_MEMORY_OVERFLOW_DEBUG, current, limit))
249+
235250
if current <= limit then
236251
return
237252
end
238253

239254
-- Run garbage collect when overflow is reached.
240255
collectgarbage("collect")
241256

242-
local after = self:currentMemory()
243-
self:calcMaxMemory(after)
257+
local after = MGC.currentMemory()
258+
MGC.calcMaxMemory(after)
244259

245-
self:chatMessage(GFS(SI_MGC_MEMORY_OVERFLOW_REACHED, current, after, current - after))
260+
MGC.chatMessage(GFS(SI_MGC_MEMORY_OVERFLOW_REACHED, current, after, current - after))
246261
end
247262

248263
-- Send message to game chat
249-
function MGC:chatMessage(message)
264+
function MGC.chatMessage(message)
250265
if LibChatMessage then
251-
if not self.chat then
252-
self.chat = LibChatMessage(self.shortName, self.shortName)
266+
if not MGC.chat then
267+
MGC.chat = LibChatMessage(MGC.shortName, MGC.shortName)
253268
end
254269

255-
local formatted = SF("|c%s[%s]|r %s", self.color.grey, GetTimeString(), message)
256-
self.chat:SetTagColor(self.color.main):Printf(formatted)
270+
local formatted = SF("|c%s[%s]|r %s", ChatColor.grey, GetTimeString(), message)
271+
MGC.chat:SetTagColor(ChatColor.main):Printf(formatted)
257272
else
258-
local formatted = SF("%s |c%s[%s]|r %s", self.prefix, self.color.grey, GetTimeString(), message)
273+
local formatted = SF("%s |c%s[%s]|r %s", MGC.prefix, ChatColor.grey, GetTimeString(), message)
259274
CHAT_SYSTEM:AddMessage(formatted)
260275
end
261276
end
262277

263278
-- Send debug message to game chat
264-
function MGC:sendDebugMessage(message)
265-
if self.config.showDebugMessages then
266-
self:chatMessage(message)
279+
function MGC.sendDebugMessage(message)
280+
if MGC.config.showDebugMessages then
281+
MGC.chatMessage(message)
267282
end
268283
end
269284

270-
function MGC.OnAddOnLoaded(_, addonName)
271-
if addonName ~= MGC.name then
272-
return
273-
end
274-
275-
EVENT_MANAGER:UnregisterForEvent(MGC.name, EVENT_ADD_ON_LOADED)
285+
-- Addon Initialize
286+
EM:RegisterForEvent(MGC.name, EVENT_ADD_ON_LOADED, function(_, name)
287+
if name == MGC.name then
288+
MGC.Initialize()
276289

277-
MGC:Initialize()
278-
end
279-
280-
EVENT_MANAGER:RegisterForEvent(MGC.name, EVENT_ADD_ON_LOADED, MGC.OnAddOnLoaded)
290+
EM:UnregisterForEvent(MGC.name, EVENT_ADD_ON_LOADED)
291+
end
292+
end)

0 commit comments

Comments
 (0)