-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoLuaMemoryCleaner.lua
More file actions
3337 lines (2942 loc) · 136 KB
/
AutoLuaMemoryCleaner.lua
File metadata and controls
3337 lines (2942 loc) · 136 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
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Auto Lua Memory Cleaner
-- LICENSE
-- Copyright 2025-2026 @APHONlC
-- Licensed under the Apache License, Version 2.0.
-- TESTED: 2026-03-26 | Release v0.0.8 | APIVersion: 101049 | LAM2 v41
local is_release_build = true
local REQUIRED_LAM_VERSION = 41
local REQUIRED_LCA_VERSION = 7060
local stat_fps_total = 0
local stat_fps_count = 0
local function IsConsoleUI()
return IsInGamepadPreferredMode()
end
local ALC = {
name = "AutoLuaMemoryCleaner",
version = "0.0.8",
defaults = {
is_enabled = true,
threshold_pc = 400,
threshold_console = 60,
fallback_delay_sec = 300,
is_csa_enabled = true,
is_log_enabled = false,
show_ui = false,
is_ui_locked = false,
ui_x = nil,
ui_y = nil,
track_stats = false,
is_migrated_008 = false,
has_shown_lib_warning_008 = false,
total_cleanups = 0,
total_mb_freed = 0,
last_session_cleanups = 0,
last_session_mb_freed = 0,
prev_session_cleanups = 0,
prev_session_mb_freed = 0,
install_date = nil,
is_graph_enabled = false,
is_stats_log_enabled = false,
session_history = {},
graph_x = nil,
graph_y = nil,
is_graph_locked = false,
is_graph_detached = false,
is_graph_global = false,
is_ui_global = false,
is_session_global = false,
version_history = {},
show_mem_ui_bar = false,
show_graph_diags = true,
lite_mode = false,
track_fps = false,
track_ping = false,
track_memory_gains = false,
track_frametime = false,
show_session_ui = false,
session_ui_x = nil,
session_ui_y = nil,
is_session_locked = false,
session_track_peak = false,
session_track_avg = false,
session_track_final = false,
session_track_cleaned = false,
prev_session_perf = {
ft_peak = 0,
ft_avg = 0,
ft_final = 0,
ft_ticks = 0,
fps_loss_max = 0,
fps_avg = 0
},
is_profiler_enabled = false,
can_profile_self = false,
include_esoprofiler = false,
exclude_libs = false,
saved_profiler_data = {},
specific_lib_excludes = {},
specific_addon_excludes = {}
},
mem_state = 0,
is_mem_check_queued = false,
session_cleanups = 0,
session_mb_freed = 0,
last_priority_save_time = 0,
last_ui_update = 0,
is_scene_callback_registered = false,
scene_callback_fn = nil,
ui_update_fn = nil,
is_profiling = false,
frametime_avg_accumulator = 0,
frametime_ticks = 0
}
local graph_window = nil
local max_points = 60
local last_graph_mb = 0
local session_peak_mb = 0
local graph_labels = {}
local graph_segments = {}
local graph_grid_lines = {}
local graph_labels_x = {}
local graph_grid_lines_v = {}
local graph_latency_dots = {}
local graph_frametime_dots = {}
local graph_labels_lat = {}
local graph_latency_pool = {}
local graph_frametime_pool = {}
local diag_labels = {}
local graph_last_diag_time = 0
local graph_last_sec_mb = 0
local stat_ticks = 0
local stat_total_ping = 0
local stat_baseline_fps = 0
local stat_fps_stable_ticks = 0
local stat_last_fps_raw = 0
local stat_baseline_ping = 0
local stat_ping_stable_ticks = 0
local stat_last_ping_raw = 0
local session_window = nil
local history_label = nil
local stat_session_ticks = 0
local stat_session_fps_loss_max = 0
local stat_session_fps_loss_final = 0
local stat_session_ping_max = 0
local stat_session_ping_final = 0
local stat_session_ping_total = 0
local stat_session_kb_max = 0
local stat_session_kb_final = 0
local stat_session_kb_total = 0
local stat_session_mb_max = 0
local stat_session_mb_final = 0
local stat_session_mb_total = 0
local stat_session_fps_loss_total = 0
local stat_session_current_mb_total = 0
local stat_session_ft_max = 0
local stat_session_ft_final = 0
local stat_session_ft_total = 0
local stat_session_fps_total = 0
local stat_session_fps_final = 0
local function format_dynamic_gain(mbValue)
local value = mbValue
if value < 1 then
value = value * 1024
return string.format("%.2f KB", value)
elseif value < 1024 then
return string.format("%.2f MB", value)
elseif value < 1024 * 1024 then
value = value / 1024
return string.format("%.2f GB", value)
else
value = value / (1024 * 1024)
return string.format("%.2f TB", value)
end
end
local function capitalizeAddonName(str)
return (str:gsub("^%l", string.upper))
end
function ALC.format_time(ms_val)
if not ms_val then return "0 ms" end
if ms_val < 1000 then return string.format("%.1f ms", ms_val) end
local sec = ms_val / 1000
if sec < 60 then return string.format("%.1f sec", sec) end
local mins = sec / 60
if mins < 60 then return string.format("%.1f min", mins) end
local hrs = mins / 60
return string.format("%.2f hr", hrs)
end
function ALC.get_hybrid_memory_data()
return collectgarbage("count") / 1024
end
function ALC.get_console_pool_mb()
return GetTotalUserAddOnMemoryPoolUsageMB() or 0
end
function ALC.get_alc_color(val, is_spike_mb)
local limit = IsConsoleUI() and ALC.settings.threshold_console or ALC.settings.threshold_pc
local pct = math.min((val / limit) * 100, 100)
if is_spike_mb then
if pct >= 90 then return 1, 0, 0, "|cFF0000"
elseif pct >= 75 then return 1, 0.5, 0, "|cFFA500"
else return 0, 1, 0, "|c00FF00" end
else
if val >= (IsConsoleUI() and 100 or 512) then return 1, 0, 0, "|cFF0000"
elseif val >= (IsConsoleUI() and 60 or 320) then return 1, 0.65, 0, "|cFFA500"
else return 0, 1, 0, "|c00FF00" end
end
end
function ALC.get_gradient_color(block_mb, peak_mb)
if IsConsoleUI() then
if block_mb < 60 then return 0, 1, 0
elseif block_mb < 100 then return 1, 0.65, 0
else return 1, 0, 0 end
else
if block_mb < 320 then return 0, 1, 0
elseif block_mb < 449 then return 1, 0.65, 0
else return 1, 0, 0 end
end
end
function ALC.get_main_label_color(mbCurrent, thresholdMB)
local pct = math.min((mbCurrent / thresholdMB) * 100, 100)
local r, g, b
if pct < 80 then r, g, b = 0, 1, 0
elseif pct < 95 then r, g, b = 1, 0.65, 0
else r, g, b = 1, 0, 0 end
local r255, g255, b255 = r*255, g*255, b*255
return string.format("%02x%02x%02x", r255, g255, b255), r, g, b
end
function ALC.get_fixed_hard_cap_color(mbCurrent)
if IsConsoleUI() then
if mbCurrent >= 100 then return "|cFF0000"
elseif mbCurrent >= 60 then return "|cFFA500"
else return "|c00FF00" end
else
if mbCurrent >= 512 then return "|cFF0000"
elseif mbCurrent >= 320 then return "|cFFA500"
else return "|c00FF00" end
end
end
function ALC.set_tag_alignment(l, y_off)
l:ClearAnchors()
l:SetAnchor(LEFT, graph_window, TOPLEFT, 305, y_off - 6)
end
function ALC.check_session_peak()
if not ALC.settings.is_stats_log_enabled then return end
local current_mb = ALC.get_hybrid_memory_data()
if current_mb > session_peak_mb then session_peak_mb = current_mb end
end
function ALC.get_settings_library()
local am = GetAddOnManager()
local lam_v, lam_e = 0, false
local lca_v, lca_e = 0, false
for i = 1, am:GetNumAddOns() do
local name, _, _, _, _, state = am:GetAddOnInfo(i)
local is_en = (state == ADDON_STATE_ENABLED)
if name == "LibAddonMenu-2.0" then
local v = am:GetAddOnVersion(i)
if is_en then
lam_v = math.max(lam_v, v); lam_e = true
elseif not lam_e then
lam_v = math.max(lam_v, v)
end
end
if name == "LibCombatAlerts" then
local v = am:GetAddOnVersion(i)
if is_en then
lca_v = math.max(lca_v, v); lca_e = true
elseif not lca_e then
lca_v = math.max(lca_v, v)
end
end
end
return lam_v, lam_e, lca_v, lca_e
end
function ALC.calculate_ram_overhead_recursive(t, seen_map, depth)
seen_map = seen_map or {}
depth = depth or 0
if depth > 20 then return 0, 0, 0, 0 end
if seen_map[t] then return 0, 0, 0, 0 end
seen_map[t] = true
local keys, raw_bytes, total_refs = 0, 0, 0
local array_slots, hash_slots, nested_ib = 0, 0, 0
local instruction_limit = 0
for k, v in pairs(t) do
instruction_limit = instruction_limit + 1
if instruction_limit > 5000 then break end
keys = keys + 1; total_refs = total_refs + 1
if type(k) == "string" then raw_bytes = raw_bytes + #k + 16 end
local v_type = type(v)
if v_type == "string" then raw_bytes = raw_bytes + #v + 16
elseif v_type == "number" then raw_bytes = raw_bytes + 8
elseif v_type == "boolean" then raw_bytes = raw_bytes + 8
elseif v_type == "table" then
local rb, tr, k2, ib2 = ALC.calculate_ram_overhead_recursive(v, seen_map, depth + 1)
raw_bytes = raw_bytes + rb; total_refs = total_refs + tr
keys = keys + k2; nested_ib = nested_ib + ib2
end
if type(k) == "number" and k > 0 and k % 1 == 0 then array_slots = array_slots + 1
else hash_slots = hash_slots + 1 end
end
total_refs = total_refs + 1
local function next_power_2(n)
local p = 1
while p < n do p = p * 2 end; return p
end
local allocated_array_slots = next_power_2(array_slots)
local allocated_hash_slots = next_power_2(hash_slots)
local invisible_structure_bytes = (allocated_array_slots * 8) + (allocated_hash_slots * 24) + 16 + nested_ib
return raw_bytes, total_refs, keys, invisible_structure_bytes
end
function ALC.get_true_ram_footprint(t)
local rb, tr, k, ib = ALC.calculate_ram_overhead_recursive(t, {}, 0)
return (rb + (k * 8) + ib)
end
function ALC.get_high_precision_sv_disk_size(t)
local function count_sv_chars_recursive(t, depth, seen)
if type(t) ~= "table" or seen[t] or depth > 20 then return 0 end
seen[t] = true
local chars = 0
local instruction_limit = 0
for k, v in pairs(t) do
instruction_limit = instruction_limit + 1
if instruction_limit > 5000 then break end
chars = chars + (depth * 4)
local k_type = type(k)
if k_type == "string" then chars = chars + #k + 7
elseif k_type == "number" then chars = chars + 10 end
local v_type = type(v)
if v_type == "string" then chars = chars + #v + 4
elseif v_type == "number" then chars = chars + 10
elseif v_type == "boolean" then chars = chars + (v and 4 or 5) + 2
elseif v_type == "table" then
chars = chars + 1 + (depth * 4) + 2
chars = chars + count_sv_chars_recursive(v, depth + 1, seen)
chars = chars + (depth * 4) + 3
end
end
return chars
end
return count_sv_chars_recursive(t, 1, {}) + 20
end
function ALC.format_memory(value_mb)
if value_mb >= 1048576 then return string.format("%.2f TB", value_mb / 1048576)
elseif value_mb >= 1024 then return string.format("%.2f GB", value_mb / 1024)
elseif value_mb >= 1 then return string.format("%.2f MB", value_mb)
else return string.format("%d KB", math.floor(value_mb * 1024)) end
end
function ALC.refresh_stats_tracker()
if not ALC.settings then return end
if ALC.settings.track_stats and not IsConsoleUI() then
EVENT_MANAGER:RegisterForUpdate(ALC.name .. "_StatsUpdate", 1000, function()
local stats_ctrl = _G["ALC_StatsText"]
if stats_ctrl and stats_ctrl.desc and not stats_ctrl:IsHidden() then
stats_ctrl.desc:SetText(ALC.get_stats_text())
end
end)
else
EVENT_MANAGER:UnregisterForUpdate(ALC.name .. "_StatsUpdate")
end
end
function ALC.toggle_core_events()
if ALC.settings.is_enabled then
EVENT_MANAGER:RegisterForEvent(ALC.name .. "_CombatState", EVENT_PLAYER_COMBAT_STATE,
function(event_code, in_combat)
if not in_combat then ALC.trigger_memory_check("CombatEnd", 3000) end
end
)
EVENT_MANAGER:RegisterForEvent(ALC.name .. "_LowMem", EVENT_LUA_LOW_MEMORY,
function()
if IsConsoleUI() then
ALC.run_manual_cleanup()
end
end
)
EVENT_MANAGER:RegisterForUpdate(ALC.name .. "_AutoSweep", 5000,
function()
if not IsPlayerMoving() then
ALC.trigger_memory_check("Idle", 1000)
else
ALC.trigger_memory_check("AutoSweep", 0)
end
end
)
if SCENE_MANAGER and not ALC.is_scene_callback_registered then
ALC.scene_callback_fn = function(scene, old_state, new_state)
if new_state == SCENE_SHOWN then
local s_name = scene:GetName()
if s_name ~= "hud" and s_name ~= "hudui" and s_name ~= "gamepad_hud" then
ALC.trigger_memory_check("Menu", 6000)
end
end
end
SCENE_MANAGER:RegisterCallback("SceneStateChanged", ALC.scene_callback_fn)
ALC.is_scene_callback_registered = true
end
else
EVENT_MANAGER:UnregisterForEvent(ALC.name .. "_CombatState", EVENT_PLAYER_COMBAT_STATE)
EVENT_MANAGER:UnregisterForEvent(ALC.name .. "_LowMem", EVENT_LUA_LOW_MEMORY)
EVENT_MANAGER:UnregisterForUpdate(ALC.name .. "_IdleCheck")
EVENT_MANAGER:UnregisterForUpdate(ALC.name .. "_AutoSweep")
if SCENE_MANAGER and ALC.is_scene_callback_registered then
SCENE_MANAGER:UnregisterCallback("SceneStateChanged", ALC.scene_callback_fn)
ALC.is_scene_callback_registered = false
end
EVENT_MANAGER:UnregisterForUpdate(ALC.name .. "_Fallback")
ALC.mem_state = 0
ALC.is_mem_check_queued = false
end
end
function ALC.toggle_ui_update()
if not ALC.ui_window then return end
if ALC.settings.show_ui then
ALC.ui_window:SetHandler("OnUpdate", ALC.ui_update_fn)
else
ALC.ui_window:SetHandler("OnUpdate", nil)
end
if ALC.settings.is_graph_enabled and ALC.settings.show_ui then
if not graph_window then ALC.build_graph_ui() end
EVENT_MANAGER:RegisterForUpdate("ALC_GraphTick", 250, function()
ALC.update_graph_visuals()
end)
else
EVENT_MANAGER:UnregisterForUpdate("ALC_GraphTick")
end
ALC.update_ui_scenes()
end
function ALC.safe_csa(text, custom_limit)
if not ALC.settings.is_csa_enabled or not CENTER_SCREEN_ANNOUNCE then return end
local limit = custom_limit 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 current_chunk = ""
for word in string.gmatch(text, "%S+") do
local test_str = (current_chunk == "") and word or (current_chunk .. " " .. word)
if string.len(test_str) > limit and current_chunk ~= "" then
table.insert(chunks, current_chunk)
current_chunk = word
else
current_chunk = test_str
end
end
if current_chunk ~= "" then table.insert(chunks, current_chunk) end
local delay_ms = 0
local active_color = ""
for i, chunk in ipairs(chunks) do
local final_msg = chunk
if i > 1 then final_msg = active_color .. "..." .. final_msg end
if i < #chunks then final_msg = final_msg .. "..." end
local idx = 1
while idx <= string.len(chunk) do
local c_tag = string.match(chunk, "^|c%x%x%x%x%x%x", idx)
if c_tag then
active_color = c_tag; idx = idx + 8
elseif string.sub(chunk, idx, idx + 1) == "|r" then
active_color = ""; idx = idx + 2
else
idx = idx + 1
end
end
local opens = 0
for _ in string.gmatch(final_msg, "|c%x%x%x%x%x%x") do opens = opens + 1 end
local closes = 0
for _ in string.gmatch(final_msg, "|r") do closes = closes + 1 end
if opens > closes then final_msg = final_msg .. "|r" end
if delay_ms > 0 then
zo_callLater(function()
local params = CENTER_SCREEN_ANNOUNCE:CreateMessageParams(
CSA_CATEGORY_LARGE_TEXT, SOUNDS.NONE
)
params:SetText(final_msg)
params:SetLifespanMS(4000)
CENTER_SCREEN_ANNOUNCE:AddMessageWithParams(params)
end, delay_ms)
else
local params = CENTER_SCREEN_ANNOUNCE:CreateMessageParams(
CSA_CATEGORY_LARGE_TEXT, SOUNDS.NONE
)
params:SetText(final_msg)
params:SetLifespanMS(4000)
CENTER_SCREEN_ANNOUNCE:AddMessageWithParams(params)
end
delay_ms = delay_ms + 1500
end
end
function ALC.get_stats_text()
if not ALC.settings.track_stats then
return "Tracking DISABLED. Enable 'Track Statistics' or type /alcstats to view live data."
end
local current_mb = ALC.get_hybrid_memory_data()
local mem_warning = ""
local lua_limit_txt = ""
if IsConsoleUI() then
lua_limit_txt = "100 MB (Hard Limit)"
mem_warning = current_mb > 70 and "|cFF0000(EXCEEDS CONSOLE LIMIT)|r" or "|c00FF00(Safe)|r"
else
lua_limit_txt = "Dynamic [512MB] (Auto-Scaling)"
mem_warning = current_mb > 400 and "|cFFA500(High Global Memory)|r" or "|c00FF00(Safe)|r"
end
local install_date = ALC.settings.install_date or "Unknown"
local v_history = table.concat(ALC.settings.version_history or {ALC.version}, ", ")
local lam_ver, lam_en, lca_ver, lca_en = ALC.get_settings_library()
local function get_lib_str(ver, en, name, req)
if ver == 0 then return string.format("|cFF0000%s (Missing)|r", name) end
if not en then return string.format("|cFF0000%s (Disabled - v%d)|r", name, ver) end
if ver < req then return string.format("|cFF0000%s (v%d) (PLEASE UPDATE %s TO v%d)|r", name, ver, name, req) end
return string.format("|c00FF00%s (v%d)|r", name, ver)
end
local lib_text = get_lib_str(lam_ver, lam_en, "LAM2", REQUIRED_LAM_VERSION) .. " | " .. get_lib_str(lca_ver, lca_en, "LCA", REQUIRED_LCA_VERSION)
local pc_data_str = ""
if not IsConsoleUI() then
local mem_raw = ALC.get_true_ram_footprint(ALC)
local mem_mb = mem_raw / (1024 * 1024)
local mem_kb = mem_raw / 1024
local mem_str = (mem_mb >= 1) and string.format("%.2f MB", mem_mb) or string.format("%.2f KB", mem_kb)
local sv_prec_bytes = ALC.get_high_precision_sv_disk_size(_G["AutoLuaCleaner"] or {})
local sv_size_kb = sv_prec_bytes / 1024
local sv_size_mb = sv_size_kb / 1024
local sv_str = (sv_size_mb >= 1) and string.format("%.2f MB", sv_size_mb) or string.format("%.2f KB", sv_size_kb)
local sv_health = _G["AutoLuaCleaner"] and "|c00FF00Healthy|r" or "|cFF0000Corrupted|r"
local sv_warn = (sv_size_kb > 5000) and "|cFFA500(Large File)|r" or "|c00FF00(Safe)|r"
pc_data_str = string.format("Data Footprint: %s (Estimated)\nSavedVariable Disk Size: %s (%s) %s\n", mem_str, sv_str, sv_health, sv_warn)
end
local prof_txt = ""
local p_data = ALC.settings.saved_profiler_data
if p_data and p_data[1] and p_data[1].peak and p_data[1].peak > 0 then
prof_txt = string.format(
"\n\n|c00FFFF[Last Profiler Scan]|r\nPeak: %.1fms by [%s]",
p_data[1].peak,
p_data[1].name
)
end
local num_errors = ALC.settings.error_logs and #ALC.settings.error_logs or 0
local err_str = num_errors > 0 and string.format("|cFF0000%d (Check Logs)|r", num_errors) or "|c00FF000 (Healthy)|r"
return string.format(
"Installed Since: %s\nVersion History: %s\nActive Library: %s\n" ..
"Max Lua Memory: %s\nCurrent Global Memory: %s %s\n%s" ..
"Active Errors: %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%s",
install_date, v_history, lib_text, lua_limit_txt, ALC.format_memory(current_mb),
mem_warning, pc_data_str, err_str, ALC.session_cleanups, ALC.format_memory(ALC.session_mb_freed),
ALC.settings.prev_session_cleanups or 0,
ALC.format_memory(ALC.settings.prev_session_mb_freed or 0),
ALC.settings.total_cleanups or 0,
ALC.format_memory(ALC.settings.total_mb_freed or 0),
prof_txt
)
end
function ALC.migrate_data()
if ALC.settings then
ALC.settings.pmOverridden = nil
if not ALC.settings.is_migrated_008 then
ALC.settings.is_migrated_008 = true
end
if ALC.settings.threshold_console == 85 or ALC.settings.threshold_console == 70 then
ALC.settings.threshold_console = 60
end
end
if _G["AutoLuaCleaner"] then
for world_name, world_data in pairs(_G["AutoLuaCleaner"]) do
if type(world_data) == "table" then
for account_name, account_data in pairs(world_data) do
if type(account_data) == "table" then
for profile_id, profile_data in pairs(account_data) do
if type(profile_data) == "table" then
profile_data["pmOverridden"] = nil
end
end
end
end
end
end
end
end
function ALC.create_mover(target, label_text)
local mover = WINDOW_MANAGER:CreateControl(nil, GuiRoot, CT_TOPLEVELCONTROL)
mover:SetDimensions(target:GetWidth(), target:GetHeight())
mover:SetAnchor(CENTER, target, CENTER, 0, 0)
mover:SetDrawTier(DT_HIGH)
mover:SetDrawLayer(DL_OVERLAY)
mover:SetDrawLevel(9999)
mover:SetMouseEnabled(true)
mover:SetMovable(true)
mover:SetClampedToScreen(true)
local bg = WINDOW_MANAGER:CreateControl(nil, mover, CT_BACKDROP)
bg:SetAnchorFill(mover)
bg:SetCenterColor(0, 0.5, 0.7, 0.32)
bg:SetEdgeColor(0, 0.5, 0.7, 1)
bg:SetEdgeTexture('', 8, 1, 0)
local lbl = WINDOW_MANAGER:CreateControl(nil, mover, CT_LABEL)
lbl:SetAnchorFill(mover)
local font_mover = IsInGamepadPreferredMode() and "ZoFontGamepad27" or "ZoFontWinH5"
lbl:SetFont(font_mover)
lbl:SetColor(1, 0.82, 0, 0.9)
lbl:SetHorizontalAlignment(TEXT_ALIGN_CENTER)
lbl:SetVerticalAlignment(TEXT_ALIGN_CENTER)
lbl:SetText(label_text)
mover.target = target
mover:SetHandler("OnMoveStop", function(self)
self.target:ClearAnchors()
self.target:SetAnchor(TOPLEFT, GuiRoot, TOPLEFT, self:GetLeft(), self:GetTop())
end)
return mover
end
function ALC.unlock_ui()
if ALC.is_ui_unlocked then return end
ALC.is_ui_unlocked = true
if SCENE_MANAGER then SCENE_MANAGER:Show("hud") end
ALC.orig_pos = {
ui = {x = ALC.settings.ui_x, y = ALC.settings.ui_y},
graph = {x = ALC.settings.graph_x, y = ALC.settings.graph_y, detached = ALC.settings.is_graph_detached},
session = {x = ALC.settings.session_ui_x, y = ALC.settings.session_ui_y}
}
local cur_scene = SCENE_MANAGER:GetCurrentScene()
if cur_scene then
if ALC.ui_window and cur_scene:HasFragment(ALC.hud_fragment) then
ALC.ui_window:SetHidden(false)
end
if graph_window and cur_scene:HasFragment(ALC.graph_fragment) then
graph_window:SetHidden(false)
end
if session_window and cur_scene:HasFragment(ALC.session_fragment) then
session_window:SetHidden(false)
end
end
ALC.movers = {}
if ALC.ui_window then table.insert(ALC.movers, ALC.create_mover(ALC.ui_window, "Memory UI")) end
if graph_window then table.insert(ALC.movers, ALC.create_mover(graph_window, "Graph UI")) end
if session_window then table.insert(ALC.movers, ALC.create_mover(session_window, "Session UI")) end
ALC.references = {}
local function create_ref(target, text)
if not target or target:IsHidden() or target:GetWidth() == 0 or target:GetHeight() == 0 then return end
if not target.SetName then return end
local ref = WINDOW_MANAGER:CreateControl(nil, GuiRoot, CT_TOPLEVELCONTROL)
ref:SetDimensions(target:GetWidth(), target:GetHeight())
ref:SetAnchor(CENTER, target, CENTER, 0, 0)
ref:SetDrawTier(DT_HIGH)
ref:SetDrawLayer(DL_OVERLAY)
ref:SetDrawLevel(9998)
ref:SetMouseEnabled(false)
ref:SetMovable(false)
local bg = WINDOW_MANAGER:CreateControl(nil, ref, CT_BACKDROP)
bg:SetAnchorFill(ref)
bg:SetCenterColor(0, 0.4, 0.6, 0.3)
bg:SetEdgeColor(0, 0.4, 0.6, 1)
bg:SetEdgeTexture('', 8, 1, 0)
local lbl = WINDOW_MANAGER:CreateControl(nil, ref, CT_LABEL)
lbl:SetAnchorFill(ref)
lbl:SetFont(IsInGamepadPreferredMode() and "ZoFontGamepad27" or "ZoFontWinH5")
lbl:SetColor(1, 1, 1, 0.8)
lbl:SetHorizontalAlignment(TEXT_ALIGN_CENTER)
lbl:SetVerticalAlignment(TEXT_ALIGN_CENTER)
lbl:SetText(text)
table.insert(ALC.references, ref)
end
local function scanHUD(parent)
if not parent or not parent:IsHidden() or not parent.GetNumChildren then return end
for i = 1, parent:GetNumChildren() do
local child = parent:GetChild(i)
if child and child:GetType() == CT_TOPLEVELCONTROL and not child:IsHidden() then
local name = child:GetName()
local alc_match = string.match(name, "^ALC") or string.match(name, "^AutoLuaMemoryCleaner")
local system_match = string.match(name, "^ZO") or string.match(name, "^GuiRoot")
if not alc_match and not system_match then
create_ref(child, name or "Addon UI")
end
end
end
end
local hud_scene = IsInGamepadPreferredMode() and "gamepad_hud" or "hud"
local scene = SCENE_MANAGER:GetScene(hud_scene)
if scene then
local fragments = scene:GetFragments()
if fragments then
for _, frag in ipairs(fragments) do
if frag.GetControl then
local control = frag:GetControl()
if control and not control:IsHidden() and control.SetName then
local name = control:GetName() or frag:GetName()
if name and string.len(name) > 0 then
create_ref(control, name)
end
end
end
end
end
end
scanHUD(GuiRoot)
local dialog_id = "ALC_UI_UNLOCK_PROMPT"
if not ESO_Dialogs[dialog_id] then
ESO_Dialogs[dialog_id] = {
canQueue = true,
gamepadInfo = { dialogType = GAMEPAD_DIALOGS.BASIC },
title = { text = "|c00FFFFALC UI Unlocked|r" },
mainText = { text = "Move the UI elements to your desired locations.\n\nPress Accept to Save.\nPress Decline to Cancel." },
buttons = {
{ text = "Save", keybind = "DIALOG_PRIMARY", callback = function() ALC.lock_ui(true) end },
{ text = "Cancel", keybind = "DIALOG_NEGATIVE", callback = function() ALC.lock_ui(false) end }
}
}
end
zo_callLater(function()
if IsInGamepadPreferredMode() then
ZO_Dialogs_ShowGamepadDialog(dialog_id)
else
ZO_Dialogs_ShowDialog(dialog_id)
end
end, 500)
end
function ALC.lock_ui(save)
if not ALC.is_ui_unlocked then return end
ALC.is_ui_unlocked = false
for _, mover in ipairs(ALC.movers) do
mover:SetHidden(true)
end
ALC.movers = {}
if ALC.references then
for _, ref in ipairs(ALC.references) do
ref:SetHidden(true)
end
ALC.references = {}
end
if save then
if ALC.ui_window then
ALC.settings.ui_x = ALC.ui_window:GetLeft()
ALC.settings.ui_y = ALC.ui_window:GetTop()
end
if graph_window then
ALC.settings.graph_x = graph_window:GetLeft()
ALC.settings.graph_y = graph_window:GetTop()
ALC.settings.is_graph_detached = true
end
if session_window then
ALC.settings.session_ui_x = session_window:GetLeft()
ALC.settings.session_ui_y = session_window:GetTop()
end
if CHAT_SYSTEM then CHAT_SYSTEM:AddMessage("|c00FFFF[ALC]|r UI Positions Saved.") end
else
ALC.settings.ui_x = ALC.orig_pos.ui.x
ALC.settings.ui_y = ALC.orig_pos.ui.y
ALC.settings.graph_x = ALC.orig_pos.graph.x
ALC.settings.graph_y = ALC.orig_pos.graph.y
ALC.settings.is_graph_detached = ALC.orig_pos.graph.detached
ALC.settings.session_ui_x = ALC.orig_pos.session.x
ALC.settings.session_ui_y = ALC.orig_pos.session.y
ALC.update_ui_anchor()
ALC.update_graph_anchor()
ALC.update_session_anchor()
if CHAT_SYSTEM then CHAT_SYSTEM:AddMessage("|c00FFFF[ALC]|r UI Positioning Cancelled.") end
end
ALC.toggle_ui_update()
if session_window then
session_window:SetHidden(not ALC.settings.show_session_ui)
end
end
function ALC.run_manual_cleanup(force_feedback)
ALC.mem_state = 1
ALC.last_cleanup_time = GetGameTimeMilliseconds()
zo_callLater(function()
local before_lua = collectgarbage("count") / 1024
local before_pool = ALC.get_console_pool_mb()
for i = 1, 2 do collectgarbage("collect") end
zo_callLater(function()
local after_lua = collectgarbage("count") / 1024
local after_pool = ALC.get_console_pool_mb()
local freed = before_lua - after_lua
local freed_pool = before_pool - after_pool
ALC.mem_state = 0
if freed > 0.001 then
ALC.session_cleanups = ALC.session_cleanups + 1
ALC.session_mb_freed = ALC.session_mb_freed + freed
if ALC.settings and ALC.settings.track_stats then
ALC.settings.total_cleanups = (ALC.settings.total_cleanups or 0) + 1
ALC.settings.total_mb_freed = (ALC.settings.total_mb_freed or 0) + freed
ALC.settings.last_session_cleanups = ALC.session_cleanups
ALC.settings.last_session_mb_freed = ALC.session_mb_freed
local now = GetGameTimeMilliseconds()
if (now - ALC.last_priority_save_time) >= 900000 then
GetAddOnManager():RequestAddOnSavedVariablesPrioritySave(ALC.name)
ALC.last_priority_save_time = now
end
end
local msg = ""
if IsConsoleUI() then
msg = string.format("Mem Pool Freed: %s (LUA Mem: %s)", ALC.format_memory(math.max(freed_pool, 0)), ALC.format_memory(freed))
else
msg = string.format("LUA Mem Freed: %s (Mem Pool: %s)", ALC.format_memory(freed), ALC.format_memory(math.max(freed_pool, 0)))
end
if ALC.settings.is_log_enabled and CHAT_SYSTEM then
CHAT_SYSTEM:AddMessage("|c00FFFF[ALC]|r " .. msg)
end
ALC.safe_csa("|c00FFFF" .. msg .. "|r", 90)
elseif force_feedback then
local msg = IsConsoleUI()
and "Mem Pool Freed: 0 MB (Console Pool Locked)"
or "LUA Mem Freed: 0 MB (Already Clean)"
if ALC.settings.is_log_enabled and CHAT_SYSTEM then
CHAT_SYSTEM:AddMessage("|c00FFFF[ALC]|r " .. msg)
end
ALC.safe_csa("|c00FFFF" .. msg .. "|r", 90)
end
if ALC.settings.show_ui then ALC.update_ui() end
end, 200)
end, 500)
end
function ALC.trigger_memory_check(check_type, delay)
if not ALC.settings.is_enabled then return end
if ALC.mem_state == 1 or ALC.is_mem_check_queued then return end
local now_ms = GetGameTimeMilliseconds()
local fallback_ms = ALC.settings.fallback_delay_sec * 1000
if (now_ms - (ALC.last_cleanup_time or 0)) < fallback_ms then return end
local current_pool = ALC.get_hybrid_memory_data()
local current_lua = collectgarbage("count") / 1024
local limit_threshold = IsConsoleUI() and ALC.settings.threshold_console or ALC.settings.threshold_pc
if current_pool >= limit_threshold or (IsConsoleUI() and current_lua >= limit_threshold) then
local in_combat = IsUnitInCombat and IsUnitInCombat("player")
if in_combat or IsUnitDead("player") then
if ALC.settings.show_ui then ALC.update_ui() end
return
end
ALC.is_mem_check_queued = true
zo_callLater(function()
ALC.is_mem_check_queued = false
if ALC.mem_state == 1 then return end
local still_in_combat = IsUnitInCombat and IsUnitInCombat("player")
if still_in_combat or IsUnitDead("player") then
if ALC.settings.show_ui then ALC.update_ui() end
return
end
if check_type == "Menu" then
local is_hud = SCENE_MANAGER:IsShowing("hud")
local is_hudui = SCENE_MANAGER:IsShowing("hudui")
local in_menu = SCENE_MANAGER and not (is_hud or is_hudui)
if not in_menu then return end
end
local recheck_pool = ALC.get_hybrid_memory_data()
local recheck_lua = collectgarbage("count") / 1024
if recheck_pool >= limit_threshold or (IsConsoleUI() and recheck_lua >= limit_threshold) then
ALC.run_manual_cleanup()
EVENT_MANAGER:UnregisterForUpdate(ALC.name .. "_Fallback")
EVENT_MANAGER:RegisterForUpdate(ALC.name .. "_Fallback",
ALC.settings.fallback_delay_sec * 1000,
function() ALC.trigger_memory_check("Fallback", 0) end
)
end
end, delay)
else
EVENT_MANAGER:UnregisterForUpdate(ALC.name .. "_Fallback")
ALC.mem_state = 0
end
end
function ALC.update_graph_visuals()
local alc = ALC
if not alc.settings.is_graph_enabled or not graph_window or graph_window:IsHidden() then
return
end
if alc.settings.lite_mode and not alc.settings.show_graph_diags then return end
alc.graph_data = alc.graph_data or {}
local is_lite = alc.settings.lite_mode
local current_mb = IsConsoleUI() and alc.get_console_pool_mb() or alc.get_hybrid_memory_data()
local current_lat = alc.settings.track_ping and GetLatency() or 0
local fps = (alc.settings.track_fps or alc.settings.track_frametime) and GetFramerate() or 0
local current_ft = fps > 0 and math.floor(1000 / fps) or 0
local delta_mb = current_mb - last_graph_mb
local gain_pop = (delta_mb > 0) and 1.0 or 0.3
last_graph_mb = current_mb
local drawing_peak_mb = IsConsoleUI() and 100 or 512
if not is_lite then
local peak_mb = IsConsoleUI() and 100 or 512
for _, v in ipairs(alc.graph_data) do
if v > peak_mb then peak_mb = v end
end
if current_mb > peak_mb then peak_mb = current_mb end
table.insert(alc.graph_data, current_mb)
if #alc.graph_data > max_points then table.remove(alc.graph_data, 1) end
if alc.settings.track_ping then
table.insert(graph_latency_pool, current_lat)
if #graph_latency_pool > max_points then table.remove(graph_latency_pool, 1) end
end
if alc.settings.track_frametime then
table.insert(graph_frametime_pool, current_ft)
if #graph_frametime_pool > max_points then table.remove(graph_frametime_pool, 1) end
end
local lat_markers = IsConsoleUI()
and {0, 50, 100, 150, 200, 250, 300, 350}
or {0, 20, 60, 100, 125, 250, 375, 750, 1000}
local function get_active_row(val, markers, max_rows)
if val <= markers[1] then return 1 end
if val >= markers[#markers] then return max_rows end
for i = 1, #markers - 1 do
if val >= markers[i] and val <= markers[i+1] then
local frac = (val - markers[i]) / (markers[i+1] - markers[i])
local start_row = ((i - 1) / (#markers - 1)) * max_rows
local end_row = (i / (#markers - 1)) * max_rows
local row = start_row + (frac * (end_row - start_row))
local floorVal = math.floor(row)
local fracPart = row - floorVal
local roundedRow = (fracPart >= 0.5) and (floorVal + 1) or floorVal
return math.max(roundedRow, 1)