-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoLuaMemoryCleaner.lua
More file actions
784 lines (678 loc) · 30.8 KB
/
AutoLuaMemoryCleaner.lua
File metadata and controls
784 lines (678 loc) · 30.8 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
-----------------------------------------------------------
-- Auto Lua Memory Cleaner (@APHONlC) | API: 101049
-----------------------------------------------------------
local ALC = {
name = "AutoLuaMemoryCleaner",
version = "0.0.6",
-- Default settings
defaults = {
enabled = true,
thresholdPC = 400,
thresholdConsole = 85,
fallbackDelayS = 300, -- 5 minutes (300 seconds)
csaEnabled = true,
logEnabled = not IsConsoleUI(),
showUI = true,
uiLocked = false,
uiX = nil,
uiY = nil,
-- Stats
totalCleanups = 0,
totalMBFreed = 0,
lastSessionCleanups = 0,
lastSessionMBFreed = 0,
prevSessionCleanups = 0,
prevSessionMBFreed = 0,
installDate = nil,
versionHistory = {}
},
memState = 0,
isMemCheckQueued = false,
sessionCleanups = 0,
sessionMBFreed = 0,
lastPrioritySaveTime = 0
}
-- LibAddonMenu-2.0 Version
local REQUIRED_LAM_VERSION = 41
-- Check if LibAddonMenu-2.0 is running and get its loaded version
local function GetLAMVersion()
local am = GetAddOnManager()
for i = 1, am:GetNumAddOns() do
local name, _, _, _, _, state = am:GetAddOnInfo(i)
if name == "LibAddonMenu-2.0" and state == ADDON_STATE_ENABLED then
return true, am:GetAddOnVersion(i)
end
end
return false, 0
end
-- Size format
function ALC:FormatMemory(valueMB)
if valueMB >= 1048576 then
return string.format("%.2f TB", valueMB / 1048576)
elseif valueMB >= 1024 then
return string.format("%.2f GB", valueMB / 1024)
else
return string.format("%.2f MB", valueMB)
end
end
-- CSA split
function ALC:SafeCSA(text, customLimit)
if not self.settings.csaEnabled or not CENTER_SCREEN_ANNOUNCE then return end
-- Use the provided limit, or default to 70 if none is provided
local limit = customLimit or 70
if string.len(text) <= limit then
local params = CENTER_SCREEN_ANNOUNCE:CreateMessageParams(CSA_CATEGORY_LARGE_TEXT, SOUNDS.NONE)
params:SetText(text)
params:SetLifespanMS(4000)
CENTER_SCREEN_ANNOUNCE:AddMessageWithParams(params)
return
end
local chunks = {}
local currentChunk = ""
-- Safely split text by words without cutting them in half
for word in string.gmatch(text, "%S+") do
local testStr = (currentChunk == "") and word or (currentChunk .. " " .. word)
if string.len(testStr) > limit and currentChunk ~= "" then
table.insert(chunks, currentChunk)
currentChunk = word
else
currentChunk = testStr
end
end
if currentChunk ~= "" then table.insert(chunks, currentChunk) end
local delayMS = 0
local activeColor = ""
for i, chunk in ipairs(chunks) do
local finalMsg = chunk
-- Add carryover colors and ellipses
if i > 1 then finalMsg = activeColor .. "..." .. finalMsg end
if i < #chunks then finalMsg = finalMsg .. "..." end
-- Scan this chunk from left to right to track the last used color
local idx = 1
while idx <= string.len(chunk) do
local cTag = string.match(chunk, "^|c%x%x%x%x%x%x", idx)
if cTag then
activeColor = cTag
idx = idx + 8
elseif string.sub(chunk, idx, idx + 1) == "|r" then
activeColor = ""
idx = idx + 2
else
idx = idx + 1
end
end
-- Close any unclosed tags to prevent UI bleed
local opens = 0
for _ in string.gmatch(finalMsg, "|c%x%x%x%x%x%x") do opens = opens + 1 end
local closes = 0
for _ in string.gmatch(finalMsg, "|r") do closes = closes + 1 end
if opens > closes then finalMsg = finalMsg .. "|r" end
-- Dispatch with sequenced timing
if delayMS > 0 then
zo_callLater(function()
local params = CENTER_SCREEN_ANNOUNCE:CreateMessageParams(CSA_CATEGORY_LARGE_TEXT, SOUNDS.NONE)
params:SetText(finalMsg)
params:SetLifespanMS(4000)
CENTER_SCREEN_ANNOUNCE:AddMessageWithParams(params)
end, delayMS)
else
local params = CENTER_SCREEN_ANNOUNCE:CreateMessageParams(CSA_CATEGORY_LARGE_TEXT, SOUNDS.NONE)
params:SetText(finalMsg)
params:SetLifespanMS(4000)
CENTER_SCREEN_ANNOUNCE:AddMessageWithParams(params)
end
delayMS = delayMS + 1500
end
end
-- Live statistics
function ALC:GetStatsText()
local currentMB = IsConsoleUI() and GetTotalUserAddOnMemoryPoolUsageMB() or (collectgarbage("count") / 1024)
local memWarning = ""
local luaLimitTxt = ""
if IsConsoleUI() then
luaLimitTxt = "100 MB (Hard Limit)"
if currentMB > 85 then memWarning = "|cFF0000(EXCEEDS CONSOLE LIMIT)|r"
else memWarning = "|c00FF00(Safe)|r" end
else
luaLimitTxt = "Dynamic [512MB] (Auto-Scaling)"
if currentMB > 400 then memWarning = "|cFFA500(High Global Memory)|r"
else memWarning = "|c00FF00(Safe)|r" end
end
local installDate = self.settings.installDate or "Unknown"
local vHistory = self.settings.versionHistory or {self.version}
local vHistoryText = table.concat(vHistory, ", ")
local _, lamVersion = GetLAMVersion()
local lamText = lamVersion > 0 and tostring(lamVersion) or "Not Installed"
return string.format(
"Installed Since: %s\nVersion History: %s\nLibAddonMenu-2.0 Version: %s\nMax Lua Memory: %s\nCurrent Global Memory: %s %s\n\n|c00FF00[Session Statistics]|r\nCleanups Triggered: %d\nMemory Freed: %s\n\n|cFFA500[Previous Session Statistics]|r\nCleanups Triggered: %d\nMemory Freed: %s\n\n|c00FFFF[Lifetime Statistics]|r\nTotal Cleanups: %d\nTotal Memory Freed: %s",
installDate, vHistoryText, lamText, luaLimitTxt, self:FormatMemory(currentMB), memWarning,
self.sessionCleanups, self:FormatMemory(self.sessionMBFreed),
self.settings.prevSessionCleanups or 0, self:FormatMemory(self.settings.prevSessionMBFreed or 0),
self.settings.totalCleanups or 0, self:FormatMemory(self.settings.totalMBFreed or 0)
)
end
-- Data Migration
function ALC:MigrateData()
-- Erase obsolete data
if self.settings then
self.settings.pmOverridden = nil
end
if _G["AutoLuaCleaner"] then
for worldName, worldData in pairs(_G["AutoLuaCleaner"]) do
if type(worldData) == "table" then
for accountName, accountData in pairs(worldData) do
if type(accountData) == "table" then
for profileId, profileData in pairs(accountData) do
if type(profileData) == "table" then
profileData["pmOverridden"] = nil
end
end
end
end
end
end
end
end
-- Memory Cleanup
function ALC:RunCleanup(isEmergency)
self.memState = 1
zo_callLater(function()
local before = collectgarbage("count") / 1024
collectgarbage("collect")
if isEmergency then
collectgarbage("collect")
end
local after = collectgarbage("count") / 1024
local freed = before - after
self.memState = 0
-- ignore zero freed
if freed > 0.01 then
self.sessionCleanups = self.sessionCleanups + 1
self.sessionMBFreed = self.sessionMBFreed + freed
if self.settings then
self.settings.totalCleanups = (self.settings.totalCleanups or 0) + 1
self.settings.totalMBFreed = (self.settings.totalMBFreed or 0) + freed
-- update last session data
self.settings.lastSessionCleanups = self.sessionCleanups
self.settings.lastSessionMBFreed = self.sessionMBFreed
-- save without reloading UI
local now = GetGameTimeMilliseconds()
if (now - self.lastPrioritySaveTime) >= 900000 then
GetAddOnManager():RequestAddOnSavedVariablesPrioritySave(ALC.name)
self.lastPrioritySaveTime = now
end
end
end
local msg = string.format("Memory Freed %s", self:FormatMemory(freed))
if self.settings.logEnabled and CHAT_SYSTEM then
CHAT_SYSTEM:AddMessage("|c00FFFF[ALC]|r " .. msg)
end
self:SafeCSA("|c00FFFF" .. msg .. "|r", 90)
self:UpdateUI()
end, 500)
end
function ALC:TriggerMemoryCheck(checkType, delay)
if not self.settings.enabled then return end
if self.memState == 1 or self.isMemCheckQueued then return end -- Prevent overlapping checks
local currentMB = IsConsoleUI() and GetTotalUserAddOnMemoryPoolUsageMB() or (collectgarbage("count") / 1024)
local threshold = IsConsoleUI() and self.settings.thresholdConsole or self.settings.thresholdPC
if currentMB >= threshold then
local inCombat = IsUnitInCombat and IsUnitInCombat("player")
if inCombat or IsUnitDead("player") then
ALC:UpdateUI() -- Force update to show combat status
return
end
self.isMemCheckQueued = true -- Lock the queue
-- Start delay
zo_callLater(function()
self.isMemCheckQueued = false
if self.memState == 1 then return end
local stillInCombat = IsUnitInCombat and IsUnitInCombat("player")
if stillInCombat or IsUnitDead("player") then
ALC:UpdateUI() -- Force update
return
end
-- Menu Intent Check
if checkType == "Menu" then
local inMenu = SCENE_MANAGER and not (SCENE_MANAGER:IsShowing("hud") or SCENE_MANAGER:IsShowing("hudui"))
if not inMenu then return end
end
-- Memory Check & Execution
local recheckMB = IsConsoleUI() and GetTotalUserAddOnMemoryPoolUsageMB() or (collectgarbage("count") / 1024)
if recheckMB >= threshold then
self:RunCleanup()
EVENT_MANAGER:UnregisterForUpdate(ALC.name .. "_Fallback")
EVENT_MANAGER:RegisterForUpdate(ALC.name .. "_Fallback", self.settings.fallbackDelayS * 1000, function()
ALC:TriggerMemoryCheck("Fallback", 0)
end)
end
end, delay)
else
EVENT_MANAGER:UnregisterForUpdate(ALC.name .. "_Fallback")
self.memState = 0
end
end
-- UI
function ALC:UpdateUIAnchor()
if not self.uiWindow then return end
self.uiWindow:ClearAnchors()
if self.settings.uiX and self.settings.uiY then
self.uiWindow:SetAnchor(TOPLEFT, GuiRoot, TOPLEFT, self.settings.uiX, self.settings.uiY)
else
local xOffset = 0
if _G["PP"] then xOffset = 0.5 end
if IsConsoleUI() then
self.uiWindow:SetAnchor(RIGHT, ZO_Compass, LEFT, -15, 0)
else
self.uiWindow:SetAnchor(RIGHT, ZO_Compass, LEFT, -25 - xOffset, -5)
end
end
end
function ALC:UpdateUI()
if not self.settings.showUI then return end
local currentMB = IsConsoleUI() and GetTotalUserAddOnMemoryPoolUsageMB() or (collectgarbage("count") / 1024)
local color = "|c00FF00"
if currentMB >= 470 then
color = "|cFF0000"
elseif currentMB >= 400 then
color = "|cFFA500"
end
-- UI Combat Status
local title = "|c00FFFF[ALC]|r"
if IsUnitInCombat("player") then
title = "|cFF0000[ALC] (Combat)|r"
end
self.uiLabel:SetText(string.format("%s Memory: %s%s|r", title, color, self:FormatMemory(currentMB)))
self.uiWindow:SetDimensions(self.uiLabel:GetTextWidth() + 20, self.uiLabel:GetTextHeight() + 10)
end
function ALC:UpdateUIScenes()
if not self.hudFragment then return end
local scenes = {"hud", "hudui", "gamepad_hud"}
for _, name in ipairs(scenes) do
local scene = SCENE_MANAGER:GetScene(name)
if scene then
if self.settings.showUI then
scene:AddFragment(self.hudFragment)
else
scene:RemoveFragment(self.hudFragment)
end
end
end
if not self.settings.showUI then
self.uiWindow:SetHidden(true)
end
end
function ALC:CreateUI()
local ui = WINDOW_MANAGER:CreateControl("AutoLuaCleanerUI", GuiRoot, CT_TOPLEVELCONTROL)
ui:SetClampedToScreen(true)
ui:SetMouseEnabled(true)
ui:SetMovable(not self.settings.uiLocked)
ui:SetHidden(true)
self.uiWindow = ui
self:UpdateUIAnchor()
ui:SetHandler("OnMoveStop", function(control)
ALC.settings.uiX = control:GetLeft()
ALC.settings.uiY = control:GetTop()
end)
local bg = WINDOW_MANAGER:CreateControl("AutoLuaCleanerBG", ui, CT_BACKDROP)
bg:SetAnchor(TOPLEFT, ui, TOPLEFT, 0, 0)
bg:SetAnchor(BOTTOMRIGHT, ui, BOTTOMRIGHT, 0, 0)
bg:SetCenterColor(0, 0, 0, 0.6)
bg:SetEdgeColor(0.6, 0.6, 0.6, 0.8)
bg:SetEdgeTexture(nil, 1, 1, 1, 0)
local label = WINDOW_MANAGER:CreateControl("AutoLuaCleanerLabel", ui, CT_LABEL)
if IsInGamepadPreferredMode() then
label:SetFont("ZoFontGamepad22")
else
label:SetFont("ZoFontGameSmall")
end
label:SetColor(1, 1, 1, 1)
label:SetText("[ALC] Loading...")
label:SetAnchor(CENTER, ui, CENTER, 0, 0)
self.uiLabel = label
local lastUpdate = 0
ui:SetHandler("OnUpdate", function(control, time)
if not ALC.settings.showUI then return end
if time - lastUpdate < 1.0 then return end
lastUpdate = time
ALC:UpdateUI()
end)
self.hudFragment = ZO_HUDFadeSceneFragment:New(ui)
self:UpdateUIScenes()
end
-- LibAddonMenu UI Settings
function ALC:BuildMenu()
local isLAMInstalled, lamVersion = GetLAMVersion()
if not isLAMInstalled then return end
if lamVersion < REQUIRED_LAM_VERSION then
zo_callLater(function()
local msg = string.format("|cFFFF00Warning: LibAddonMenu is outdated (v%d). Update to v%d+ for ALC menu.|r", lamVersion, REQUIRED_LAM_VERSION)
if CHAT_SYSTEM then CHAT_SYSTEM:AddMessage("|cFF0000[ALC]|r " .. msg) end
ALC:SafeCSA(msg)
end, 4000)
return
end
local LAM = LibAddonMenu2 or _G["LibAddonMenu"]
if not LAM then return end
local panelData = {
type = "panel",
name = "|c9CD04CAuto Lua Memory Cleaner|r",
displayName = "|c00FFFFAuto Lua Memory Cleaner|r",
author = "@|ca500f3A|r|cb400e6P|r|cc300daH|r|cd200cdO|r|ce100c1NlC|r",
version = self.version,
registerForRefresh = true
}
local optionsData = {}
local consoleCmds = "|c00FFFF/alcon|r (or |cFF0000/alcenable|r) |cFFD700- Toggle Auto Cleanup|r\n\n|c00FFFF/alcui|r (or |cFF0000/alctoggleui|r) |cFFD700- Toggle Memory UI visibility|r\n\n|c00FFFF/alclock|r (or |cFF0000/alcuilock|r) |cFFD700- Lock/Unlock UI dragging|r\n\n|c00FFFF/alcreset|r (or |cFF0000/alcuireset|r) |cFFD700- Reset Memory UI position|r\n\n|c00FFFF/alccsa|r (or |cFF0000/alctogglecsa|r) |cFFD700- Toggle Screen Announcements|r\n\n|c00FFFF/alcclean|r (or |cFF0000/alccleanup|r) |cFFD700- Force manual Lua memory cleanup|r"
local pcCmdsText = "|c00FFFF/alcon|r (or |cFF0000/alcenable|r) |cFFD700- Toggle Auto Cleanup|r\n|c00FFFF/alcui|r (or |cFF0000/alctoggleui|r) |cFFD700- Toggle Memory UI visibility|r\n|c00FFFF/alclock|r (or |cFF0000/alcuilock|r) |cFFD700- Lock/Unlock UI dragging|r\n|c00FFFF/alcreset|r (or |cFF0000/alcuireset|r) |cFFD700- Reset Memory UI position|r\n|c00FFFF/alccsa|r (or |cFF0000/alctogglecsa|r) |cFFD700- Toggle Screen Announcements|r\n|c00FFFF/alclogs|r (or |cFF0000/alcchatlogs|r) |cFFD700- Toggle Chat Logs|r\n|c00FFFF/alcclean|r (or |cFF0000/alccleanup|r) |cFFD700- Force a manual Lua memory cleanup|r"
if IsConsoleUI() then
table.insert(optionsData, { type = "button", name = "|c00FF00ALC LIVE STATS|r", tooltip = function() return ALC:GetStatsText() end, func = function() end, width = "full" })
table.insert(optionsData, { type = "button", name = "|c00FF00COMMANDS INFO|r", tooltip = consoleCmds, func = function() end, width = "full" })
end
local isEU = (GetWorldName() == "EU Megaserver")
if not IsConsoleUI() and not isEU then
table.insert(optionsData, {
type = "button",
name = "|cFFD700DONATE|r to @|ca500f3A|r|cb400e6P|r|cc300daH|r|cd200cdO|r|ce100c1NlC|r",
tooltip = "Opens the in-game mail. Thank you! Donations help support continued development and maintenance.",
func = function()
SCENE_MANAGER:Show("mailSend")
zo_callLater(function()
ZO_MailSendToField:SetText("@APHONlC")
ZO_MailSendSubjectField:SetText("Auto Lua Cleaner Support")
ZO_MailSendBodyField:TakeFocus()
end, 200)
end,
width = "full"
})
table.insert(optionsData, { type = "divider" })
end
table.insert(optionsData, {
type = "checkbox",
name = "Enable Auto Cleanup",
tooltip = "Allows the addon to clean memory automatically when it hits your thresholds.",
getFunc = function() return ALC.settings.enabled end,
setFunc = function(value) ALC.settings.enabled = value end
})
if IsConsoleUI() then
table.insert(optionsData, {
type = "slider",
name = "Console Memory Threshold (MB)",
tooltip = "Triggers a cleanup when console memory hits this amount (Default: 85). Console hard-limit is 100MB.",
min = 10, max = 95, step = 1,
getFunc = function() return ALC.settings.thresholdConsole end,
setFunc = function(value) ALC.settings.thresholdConsole = value end
})
else
table.insert(optionsData, {
type = "slider",
name = "PC Memory Threshold (MB)",
tooltip = "Triggers a cleanup when global Lua memory hits this amount (Default: 400).",
min = 50, max = 800, step = 10,
getFunc = function() return ALC.settings.thresholdPC end,
setFunc = function(value) ALC.settings.thresholdPC = value end
})
end
table.insert(optionsData, {
type = "slider",
name = "Repeat Cleanup Delay (Seconds)",
tooltip = "If memory is still over the threshold after a cleanup, how long should it wait before trying again? (Default: 300s / 5mins)",
min = 30, max = 1200, step = 10,
getFunc = function() return ALC.settings.fallbackDelayS end,
setFunc = function(value) ALC.settings.fallbackDelayS = value end
})
table.insert(optionsData, { type = "divider" })
if not IsConsoleUI() then
table.insert(optionsData, {
type = "checkbox",
name = "Enable Chat Logs",
tooltip = "Shows status messages in your chat window when memory is cleaned.",
getFunc = function() return ALC.settings.logEnabled end,
setFunc = function(value) ALC.settings.logEnabled = value end
})
end
table.insert(optionsData, {
type = "checkbox",
name = "Screen Announcements",
tooltip = "Shows a large text alert on screen when memory is freed.",
getFunc = function() return ALC.settings.csaEnabled end,
setFunc = function(value) ALC.settings.csaEnabled = value end
})
-- CSA Test Button
if GetDisplayName() == "@APHONlC" then
table.insert(optionsData, {
type = "button",
name = "Test Screen Announcements",
tooltip = "Triggers a sequence of all CSA messages to verify formatting, colors, and the long text split logic.",
func = function()
zo_callLater(function()
local warningMsg = "|cFFFF00[ALC] LibAddonMenu is outdated. Update to v41+ for ALC menu.|r"
ALC:SafeCSA(warningMsg, 90)
end, 3000)
end,
width = "full"
})
end
table.insert(optionsData, { type = "divider" })
table.insert(optionsData, {
type = "checkbox",
name = "Show Memory UI",
tooltip = "Displays a draggable on-screen tracker of your current Lua memory usage.",
getFunc = function() return ALC.settings.showUI end,
setFunc = function(value)
ALC.settings.showUI = value
ALC:UpdateUIScenes()
end
})
table.insert(optionsData, {
type = "checkbox",
name = "Lock UI Position",
tooltip = "Prevents the Memory UI from being dragged.",
getFunc = function() return ALC.settings.uiLocked end,
setFunc = function(value)
ALC.settings.uiLocked = value
if ALC.uiWindow then ALC.uiWindow:SetMovable(not value) end
end,
disabled = function() return not ALC.settings.showUI end
})
table.insert(optionsData, {
type = "button",
name = "|cFF0000RESET UI POSITION|r",
tooltip = "Snaps the UI back to the left side of the compass.",
func = function()
ALC.settings.uiX = nil
ALC.settings.uiY = nil
ALC:UpdateUIAnchor()
if ALC.settings.logEnabled and CHAT_SYSTEM then
CHAT_SYSTEM:AddMessage("|c00FFFF[ALC]|r UI Position Reset.")
end
end,
disabled = function() return not ALC.settings.showUI end
})
table.insert(optionsData, { type = "divider" })
table.insert(optionsData, {
type = "button",
name = "|c00FFFFFORCE MANUAL CLEANUP|r",
tooltip = "Instantly clears Lua memory.",
func = function() ALC:RunCleanup() end,
width = "full"
})
if not IsConsoleUI() then
local liveStatsBlock = { type = "submenu", name = "ALC Live Statistics", tooltip = "Live tracking of memory cleanup data.", controls = {
{ type = "description", title = "|c00FFFFLive Statistics|r", text = "Loading statistics...", reference = "ALC_StatsText" }
}}
local commandsInfoBlock = { type = "description", title = "Commands Info", text = pcCmdsText }
table.insert(optionsData, liveStatsBlock)
table.insert(optionsData, commandsInfoBlock)
end
table.insert(optionsData, { type = "divider" })
if IsConsoleUI() then
table.insert(optionsData, {
type = "button",
name = "|cFFD700Buy Me A Coffee|r",
tooltip = "Thank you! Donations help support continued development and maintenance!\n\nLink: https://buymeacoffee.com/aph0nlc",
func = function() end,
width = "full"
})
table.insert(optionsData, {
type = "button",
name = "|cFF0000BUG REPORT|r",
tooltip = "Found an issue? Report it on ESOUI or Github:\n\nhttps://www.esoui.com/portal.php?id=360&a=listbugs",
func = function() end,
width = "full"
})
else
table.insert(optionsData, {
type = "button",
name = "|cFFD700Buy Me A Coffee|r",
tooltip = "Thank you! Donations help support continued development and maintenance! Opens a secure link to my Buy Me A Coffee page in your default web browser.",
func = function()
RequestOpenUnsafeURL("https://buymeacoffee.com/aph0nlc")
end,
width = "full"
})
table.insert(optionsData, {
type = "button",
name = "|cFF0000BUG REPORT|r",
tooltip = "Found an issue? Opens the Bug Portal on ESOUI in your default web browser.",
func = function()
RequestOpenUnsafeURL("https://www.esoui.com/portal.php?id=360&a=listbugs")
end,
width = "full"
})
end
LAM:RegisterAddonPanel("AutoLuaCleanerOptions", panelData)
LAM:RegisterOptionControls("AutoLuaCleanerOptions", optionsData)
end
function ALC:IntegrateWithPermMemento()
local pmCore = _G["PermMementoCore"]
if pmCore and type(pmCore) == "table" and pmCore.settings then
pmCore.settings.autoCleanup = false
pmCore.settings.csaCleanupEnabled = false
EVENT_MANAGER:UnregisterForUpdate(pmCore.name .. "_MemFallback")
end
end
-- Initialization
function ALC:Init(eventCode, addOnName)
if addOnName ~= self.name then return end
EVENT_MANAGER:UnregisterForEvent(self.name, EVENT_ADD_ON_LOADED)
local world = GetWorldName() or "Default"
self.settings = ZO_SavedVars:NewAccountWide("AutoLuaCleaner", 1, "AccountWide", self.defaults, world)
-- Run Data Migration first
self:MigrateData()
-- Manage Session Statistics for previous session data
self.settings.prevSessionCleanups = self.settings.lastSessionCleanups or 0
self.settings.prevSessionMBFreed = self.settings.lastSessionMBFreed or 0
self.settings.lastSessionCleanups = 0
self.settings.lastSessionMBFreed = 0
if not self.settings.installDate then
local d = GetDate()
if d and type(d) == "number" then d = tostring(d) end
if d and string.len(d) == 8 then
self.settings.installDate = string.sub(d, 1, 4) .. "/" .. string.sub(d, 5, 6) .. "/" .. string.sub(d, 7, 8)
else
self.settings.installDate = GetDateStringFromTimestamp(GetTimeStamp())
end
end
if not self.settings.versionHistory then self.settings.versionHistory = {} end
local vLen = #self.settings.versionHistory
if vLen == 0 or self.settings.versionHistory[vLen] ~= self.version then
table.insert(self.settings.versionHistory, self.version)
if #self.settings.versionHistory > 3 then table.remove(self.settings.versionHistory, 1) end
end
self:CreateUI()
self:BuildMenu()
if not IsConsoleUI() then
EVENT_MANAGER:RegisterForUpdate(ALC.name .. "_StatsUpdate", 1000, function()
local statsCtrl = _G["ALC_StatsText"]
if statsCtrl and statsCtrl.desc and not statsCtrl:IsHidden() then
statsCtrl.desc:SetText(ALC:GetStatsText())
end
end)
end
EVENT_MANAGER:RegisterForEvent(self.name, EVENT_PLAYER_ACTIVATED, function()
self:IntegrateWithPermMemento()
self:TriggerMemoryCheck("ZoneLoad", 5000)
end)
EVENT_MANAGER:RegisterForEvent(self.name .. "_CombatState", EVENT_PLAYER_COMBAT_STATE, function(eventCode, inCombat)
if not inCombat then self:TriggerMemoryCheck("CombatEnd", 3000) end
end)
EVENT_MANAGER:RegisterForEvent(self.name .. "_LowMem", EVENT_LUA_LOW_MEMORY, function()
self:RunCleanup(true)
end)
if SCENE_MANAGER then
SCENE_MANAGER:RegisterCallback("SceneStateChanged", function(scene, oldState, newState)
if newState == SCENE_SHOWN then
if scene.name ~= "hud" and scene.name ~= "hudui" then
self:TriggerMemoryCheck("Menu", 6000) -- Updated to 6 seconds
end
end
end)
end
-- Slash Commands
SLASH_COMMANDS["/alc"] = function(extra)
local cmd = extra:lower()
if cmd == "" then
local cmds = "|c00FF00Available ALC Commands:|r\n"
cmds = cmds .. "|c00FFFF/alcon|r (or |cFF0000/alcenable|r) |cFFD700- Toggle Auto Cleanup|r\n"
cmds = cmds .. "|c00FFFF/alcui|r (or |cFF0000/alctoggleui|r) |cFFD700- Toggle Memory UI visibility|r\n"
cmds = cmds .. "|c00FFFF/alclock|r (or |cFF0000/alcuilock|r) |cFFD700- Lock/Unlock UI dragging|r\n"
cmds = cmds .. "|c00FFFF/alcreset|r (or |cFF0000/alcuireset|r) |cFFD700- Reset Memory UI position|r\n"
cmds = cmds .. "|c00FFFF/alccsa|r (or |cFF0000/alctogglecsa|r) |cFFD700- Toggle Screen Announcements|r\n"
if not IsConsoleUI() then
cmds = cmds .. "|c00FFFF/alclogs|r (or |cFF0000/alcchatlogs|r) |cFFD700- Toggle Chat Logs|r\n"
end
cmds = cmds .. "|c00FFFF/alcclean|r (or |cFF0000/alccleanup|r) |cFFD700- Force a manual Lua memory cleanup|r"
if CHAT_SYSTEM then CHAT_SYSTEM:AddMessage("|c00FFFF[ALC]|r\n" .. cmds) end
return
end
end
SLASH_COMMANDS["/autoluaclean"] = SLASH_COMMANDS["/alc"]
-- Command Aliases
SLASH_COMMANDS["/alcon"] = function()
self.settings.enabled = not self.settings.enabled
local status = self.settings.enabled and "|c00FF00ON|r" or "|cFF0000OFF|r"
if CHAT_SYSTEM then CHAT_SYSTEM:AddMessage("|c00FFFF[ALC]|r Auto Cleanup: " .. status) end
end
SLASH_COMMANDS["/alcenable"] = SLASH_COMMANDS["/alcon"]
SLASH_COMMANDS["/alcui"] = function()
self.settings.showUI = not self.settings.showUI
self:UpdateUIScenes()
local status = self.settings.showUI and "|c00FF00VISIBLE|r" or "|cFF0000HIDDEN|r"
if CHAT_SYSTEM then CHAT_SYSTEM:AddMessage("|c00FFFF[ALC]|r Memory UI: " .. status) end
end
SLASH_COMMANDS["/alctoggleui"] = SLASH_COMMANDS["/alcui"]
SLASH_COMMANDS["/alclock"] = function()
self.settings.uiLocked = not self.settings.uiLocked
if self.uiWindow then self.uiWindow:SetMovable(not self.settings.uiLocked) end
local status = self.settings.uiLocked and "|cFF0000LOCKED|r" or "|c00FF00UNLOCKED|r"
if CHAT_SYSTEM then CHAT_SYSTEM:AddMessage("|c00FFFF[ALC]|r UI Position: " .. status) end
end
SLASH_COMMANDS["/alcuilock"] = SLASH_COMMANDS["/alclock"]
SLASH_COMMANDS["/alcreset"] = function()
self.settings.uiX = nil
self.settings.uiY = nil
self:UpdateUIAnchor()
if CHAT_SYSTEM then CHAT_SYSTEM:AddMessage("|c00FFFF[ALC]|r UI Position Reset.") end
end
SLASH_COMMANDS["/alcuireset"] = SLASH_COMMANDS["/alcreset"]
SLASH_COMMANDS["/alccsa"] = function()
self.settings.csaEnabled = not self.settings.csaEnabled
local status = self.settings.csaEnabled and "|c00FF00ON|r" or "|cFF0000OFF|r"
if CHAT_SYSTEM then CHAT_SYSTEM:AddMessage("|c00FFFF[ALC]|r Screen Announcements: " .. status) end
end
SLASH_COMMANDS["/alctogglecsa"] = SLASH_COMMANDS["/alccsa"]
if not IsConsoleUI() then
SLASH_COMMANDS["/alclogs"] = function()
self.settings.logEnabled = not self.settings.logEnabled
local status = self.settings.logEnabled and "|c00FF00ON|r" or "|cFF0000OFF|r"
if CHAT_SYSTEM then CHAT_SYSTEM:AddMessage("|c00FFFF[ALC]|r Chat Logs: " .. status) end
end
SLASH_COMMANDS["/alcchatlogs"] = SLASH_COMMANDS["/alclogs"]
end
SLASH_COMMANDS["/alcclean"] = function()
self:RunCleanup()
end
SLASH_COMMANDS["/alccleanup"] = SLASH_COMMANDS["/alcclean"]
end
EVENT_MANAGER:RegisterForEvent(ALC.name, EVENT_ADD_ON_LOADED, function(...) ALC:Init(...) end)