-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnifiedProfileManager.lua
621 lines (563 loc) · 20.9 KB
/
UnifiedProfileManager.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
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
local name, ns = ...;
local AceDB = LibStub('AceDB-3.0');
local AceDBOptions = LibStub('AceDBOptions-3.0');
local AceConfig = LibStub('AceConfig-3.0');
local AceConfigDialog = LibStub('AceConfigDialog-3.0');
-- LibDualSpec does not load on non-SoD classic era
local LibDualSpec = LibStub('LibDualSpec-1.0', true);
local function SortAddons(name1, name2)
return strcmputf8i(StripHyperlinks(name1), StripHyperlinks(name2)) < 0;
end
local currentCharacterName = UnitName('player')..' - '..GetRealmName();
local DEFAULT_OPTION_KEY = 'Default';
local L;
do
L = {
choose_sub = 'Select one of your currently available profiles.',
default = 'Default',
};
local LOCALE = GetLocale();
if LOCALE == 'deDE' then
L['choose_sub'] = 'Wählt ein bereits vorhandenes Profil aus.';
L['default'] = 'Standard';
elseif LOCALE == 'frFR' then
L['choose_sub'] = 'Permet de choisir un des profils déjà disponibles.';
L['default'] = 'Défaut';
elseif LOCALE == 'koKR' then
L['choose_sub'] = '현재 이용할 수 있는 프로필 중 하나를 선택합니다.';
L['default'] = '기본값';
elseif LOCALE == 'esES' or LOCALE == 'esMX' then
L['choose_sub'] = 'Selecciona uno de los perfiles disponibles.';
L['default'] = 'Por defecto';
elseif LOCALE == 'zhTW' then
L['choose_sub'] = '從當前可用的設定檔裡面選擇一個。';
L['default'] = '預設';
elseif LOCALE == 'zhCN' then
L['choose_sub'] = '从当前可用的配置文件里面选择一个。';
L['default'] = '默认';
elseif LOCALE == 'ruRU' then
L['choose_sub'] = 'Выбор одного из уже доступных профилей.';
L['default'] = 'По умолчанию';
elseif LOCALE == 'itIT' then
L['choose_sub'] = 'Seleziona uno dei profili attualmente disponibili.';
L['default'] = 'Predefinito';
elseif LOCALE == 'ptBR' then
L['choose_sub'] = 'Selecione um de seus perfis atualmente disponíveis.';
L['default'] = 'Padrão';
end
end
--- @class UnifiedProfileManager: FRAME
local UPM = CreateFrame('FRAME');
UPM:SetScript('OnEvent', function(self, event, ...)
return self[event](self, ...);
end);
UPM:RegisterEvent('ADDON_LOADED');
do
function UnifiedProfileManager_OnAddonCompartmentClick()
UPM:OpenConfigUI();
end
function UnifiedProfileManager_OnAddonCompartmentEnter(_, button)
GameTooltip:SetOwner(button, 'ANCHOR_RIGHT');
GameTooltip:SetText('Unified Profile Manager');
GameTooltip:AddLine(CreateAtlasMarkup('NPE_LeftClick', 18, 18) .. ' to manage your profiles', 1, 1, 1);
GameTooltip:Show();
end
function UnifiedProfileManager_OnAddonCompartmentLeave()
GameTooltip:Hide();
end
end
function UPM:ADDON_LOADED()
if NumyProfiler then
--- @type NumyProfiler
local NumyProfiler = NumyProfiler;
NumyProfiler:WrapModules(name, 'Main', self);
end
UPM:UnregisterEvent('ADDON_LOADED');
UnifiedProfileManagerDB = UnifiedProfileManagerDB or {};
self.db = UnifiedProfileManagerDB;
local defaults = {
hideAddonsSetToDefault = false,
hideAddonsWithAllAltsUsingSameProfile = false,
hideAltsMatchingCurrentCharacter = false,
};
for property, value in pairs(defaults) do
if self.db[property] == nil then
self.db[property] = value;
end
end
AceConfig:RegisterOptionsTable(name, self:GetOptionsTable(true));
local panel, category = AceConfigDialog:AddToBlizOptions(name, name);
local ignoreHook = false;
panel:HookScript('OnShow', function()
if ignoreHook then return; end
ignoreHook = true;
AceConfig:RegisterOptionsTable(name, self:GetOptionsTable());
panel:Hide();
panel:Show();
RunNextFrame(function() ignoreHook = false; end);
end);
_G.SLASH_UNIFIED_PROFILE_MANAGER1 = '/upm';
_G.SLASH_UNIFIED_PROFILE_MANAGER2 = '/profiles';
SlashCmdList['UNIFIED_PROFILE_MANAGER'] = function() UPM:OpenConfigUI(); end;
end
function UPM:OpenConfigUI()
AceConfig:RegisterOptionsTable(name, self:GetOptionsTable());
AceConfigDialog:Open(name);
local container = AceConfigDialog.OpenFrames[name];
if not container or not container.frame then return; end
container:SetTitle('Unified Profile Manager');
container.SetTitle = nop;
local frame = container.frame;
frame:SetMovable(true);
frame:SetScript('OnMouseDown', function(self)
self:StartMoving();
end);
frame:SetScript('OnMouseUp', function(self)
self:StopMovingOrSizing();
end);
frame.ClearAllPoints = nop;
frame.SetPoint = nop;
end
UPM.resultCache = {};
function UPM:FindGlobal(item)
if not self.resultCache[item] then
for k, v in pairs(_G) do
if item == v then
self.resultCache[item] = k;
break;
end
end
end
return self.resultCache[item];
end
function UPM:IsBlacklistedDB(db)
-- some addons include debug code for LibDualSpec, which creates a database, we don't show it, cause it's nonsense and confusing to the player
-- we scan between minors 15 and 100, for arbitrary reasons, at July 2024 we're at minor 24.
for minor = 15, 100 do
if db.sv == _G[('LibDualSpec-1.0-%d test'):format(minor)] then
return true;
end
end
return false;
end
function UPM:GetDuplicateAddons()
local duplicateAddons = {};
local addonNames = {};
for db, _ in pairs(AceDB.db_registry) do
if not db.parent then
local addonName = self:GetAddonNameForDB(db);
if addonNames[addonName] then
duplicateAddons[addonName] = true;
end
addonNames[addonName] = true;
end
end
return duplicateAddons;
end
UPM.dbCache = {};
function UPM:GetAddonNameForDB(db)
if not self.dbCache[db] then
local _, addonName = issecurevariable(db, 'sv');
_, addonName = C_AddOns.GetAddOnInfo(addonName);
self.dbCache[db] = addonName;
end
return self.dbCache[db];
end
local altHandlerPrototype = {};
do
local CHARACTER_MAGIC_KEY = '%character%';
local CHARACTER_REALM_MAGIC_KEY = '%character_realm%';
local defaultProfilesProto = {
[DEFAULT_OPTION_KEY] = L['default'],
};
local defaultProfilesOrder = {
DEFAULT_OPTION_KEY,
CHARACTER_MAGIC_KEY,
CHARACTER_REALM_MAGIC_KEY,
};
local classNameFormat = '|Tinterface/icons/classicon_%s:16|t %s';
for classID = 1, GetNumClasses() do
local className, classFilename = GetClassInfo(classID);
if className then
defaultProfilesProto[classFilename] = classNameFormat:format(classFilename, className);
table.insert(defaultProfilesOrder, classFilename);
end
end
local defaultProfileCache = {};
function altHandlerPrototype:GetDefaultProfilesForCharacter(characterName)
if defaultProfileCache[characterName] then
return defaultProfileCache[characterName];
end
if characterName == '-' then return defaultProfilesProto; end
local defaultProfiles = Mixin({
[characterName] = characterName,
}, defaultProfilesProto);
local realm = characterName:match(' %- (.+)');
if realm then
defaultProfiles[realm] = realm;
end
defaultProfileCache[characterName] = defaultProfiles;
return defaultProfiles;
end
function altHandlerPrototype:ListProfiles(info)
local db = self.db;
local characterName = info.arg;
local profiles = {};
for profile, _ in pairs(db.sv.profiles) do
profiles[profile] = profile;
end
for k, v in pairs(self:GetDefaultProfilesForCharacter(characterName)) do
profiles[k] = v;
end
return profiles;
end
function altHandlerPrototype:ListOrderedProfiles(info)
local db = self.db;
local characterName = info.arg;
local isAll = characterName == '-';
local realm = characterName:match(' %- (.+)');
local defaultProfiles = self:GetDefaultProfilesForCharacter(characterName);
local orderedProfileKeys = {};
for profile, _ in pairs(db.sv.profiles) do
if not defaultProfiles[profile] then
table.insert(orderedProfileKeys, profile);
end
end
table.sort(orderedProfileKeys);
local orderedProfiles = {};
for _, v in ipairs(defaultProfilesOrder) do
if not isAll and v == CHARACTER_MAGIC_KEY then
v = characterName;
elseif not isAll and v == CHARACTER_REALM_MAGIC_KEY then
v = realm;
end
if defaultProfiles[v] then
table.insert(orderedProfiles, v);
end
end
for _, profile in ipairs(orderedProfileKeys) do
table.insert(orderedProfiles, profile);
end
return orderedProfiles;
end
function altHandlerPrototype:GetCurrentProfile(info)
local db = self.db;
local characterName = info.arg;
local isAll = characterName == '-';
if isAll then
local foundProfile
for character, profile in pairs(db.sv.profileKeys) do
if not foundProfile then
foundProfile = profile;
elseif profile ~= foundProfile and character ~= currentCharacterName then
return nil;
end
end
return foundProfile;
end
local currentProfile = db.sv and db.sv.profileKeys and db.sv.profileKeys[characterName];
return currentProfile;
end
function altHandlerPrototype:SetProfile(info, profile)
local db = self.db;
local characterName = info.arg;
local isAll = characterName == '-';
if isAll then
for character, _ in pairs(db.sv.profileKeys) do
if character ~= currentCharacterName then
db.sv.profileKeys[character] = profile;
end
end
else
db.sv.profileKeys[characterName] = profile;
end
end
function altHandlerPrototype:IsHidden(info)
local db = self.db;
local characterName = info.arg;
local currentCharactersProfile = db.sv.profileKeys[currentCharacterName];
local profile = db.sv.profileKeys[characterName];
if UPM.db.hideAltsMatchingCurrentCharacter and currentCharactersProfile == profile then
return true;
end
return false;
end
end
UPM.altHandlers = {};
function UPM:MakeAltOptions(db)
if not db.sv or not db.sv.profileKeys or not next(db.sv.profileKeys) then
return nil;
end
local altHandler = self.altHandlers[db] or {db = db};
Mixin(altHandler, altHandlerPrototype);
local increment = CreateCounter(1);
local group = {
type = 'group',
name = 'Character Profiles',
inline = true,
order = -1, -- last
args = {
applyToAll = {
name = 'Apply to all alts',
desc = L['choose_sub'] .. ' This applies to all characters, except the current character!',
type = 'select',
order = increment(),
arg = '-',
get = 'GetCurrentProfile',
set = 'SetProfile',
values = 'ListProfiles',
sorting = 'ListOrderedProfiles',
},
hideAltsMatchingCurrentCharacter = {
type = 'toggle',
name = 'Hide alts matching current character',
desc = ('Hide alts with the same profile as %s'):format(currentCharacterName),
order = increment(),
get = function()
return self.db.hideAltsMatchingCurrentCharacter;
end,
set = function(info, value)
self.db.hideAltsMatchingCurrentCharacter = value;
end,
width = 'double',
},
header = {
type = 'header',
name = '',
order = increment(),
},
},
handler = altHandler,
}
local option = {
name = '', -- character name
desc = L['choose_sub'],
type = 'select',
order = 1, -- order by character name
arg = '', -- character name
get = 'GetCurrentProfile',
set = 'SetProfile',
values = 'ListProfiles',
sorting = 'ListOrderedProfiles',
hidden = 'IsHidden',
};
local orderedCharacterNames = {};
for characterName, _ in pairs(db.sv.profileKeys) do
table.insert(orderedCharacterNames, tostring(characterName));
end
table.sort(orderedCharacterNames);
orderedCharacterNames = tInvert(orderedCharacterNames);
local offset = increment();
local i = 1;
for characterName, _ in pairs(db.sv.profileKeys) do
characterName = tostring(characterName);
if characterName ~= currentCharacterName then
i = i + 1;
local charOption = CopyTable(option, true);
charOption.name = characterName;
charOption.arg = characterName;
charOption.order = (orderedCharacterNames[characterName] or i) + offset;
group.args['char'..i] = charOption;
end
end
if not next(group.args) then
return nil;
end
return group;
end
--- @param tbl source table
--- @param ignoredValue value that is copied raw, not recursively
--- @param copies table to store copies of tables to avoid infinite recursion
local function DeepCopyTable(tbl, ignoredValue, copies)
copies = copies or {}
local copy = {};
if copies[tbl] then
return copies[tbl];
end
copies[tbl] = copy;
for k, v in pairs(tbl) do
if type(v) == 'table' and v ~= ignoredValue then
copy[k] = DeepCopyTable(v, ignoredValue, copies);
else
copy[k] = v;
end
end
return copy;
end
function UPM:GetUnusedProfiles(db)
local profiles = db.sv.profiles;
local profileKeys = db.sv.profileKeys;
local usedProfiles = {};
for _, profile in pairs(profileKeys) do
usedProfiles[profile] = true;
end
local unusedProfiles = {};
for profile, _ in pairs(profiles) do
if not usedProfiles[profile] then
table.insert(unusedProfiles, profile);
end
end
return unusedProfiles;
end
function UPM:GetAceDBOptionsTable(db)
local options = DeepCopyTable(AceDBOptions:GetOptionsTable(db), db);
local isLibDualSpec = LibDualSpec and LibDualSpec.registry[db] and true or false;
if isLibDualSpec then
LibDualSpec:EnhanceOptions(options, db);
end
return options;
end
function UPM:GetOptionsTable(skipAddons)
local increment = CreateCounter(1);
local function getOption(info)
return self.db[info[#info]];
end
local function setOption(info, value)
self.db[info[#info]] = value;
end
local options = {
type = 'group',
args = {
['allProfiles'] = {
type = 'group',
order = 0,
name = 'All Addons',
desc = 'Overview of all addon profiles',
args = {
desc = {
type = 'description',
name = 'You can change the profile for all addons here. Some addons may need you to reload the UI after switching, to avoid issues',
order = increment(),
},
hideAddonsSetToDefault = {
type = 'toggle',
name = 'Hide Addons Set to ' .. L['default'],
desc = ('Hide addons with their profile set to the %s profile'):format(L['default']),
order = increment(),
get = getOption,
set = setOption,
width = 'double',
},
hideAddonsWithAllAltsUsingSameProfile = {
type = 'toggle',
name = 'Hide Addons where all alts use the same profile',
desc = 'Hide addons where all characters use the same profile',
order = increment(),
get = getOption,
set = setOption,
width = 'double',
},
hideAddonsSetToCharProfile = {
type = 'toggle',
name = ('Hide Addons Set to %s'):format(currentCharacterName),
desc = ('Hide addons with their profile set to %s'):format(currentCharacterName),
order = increment(),
get = getOption,
set = setOption,
width = 'double',
},
reloadUI = {
type = 'execute',
name = 'Reload UI',
order = increment(),
width = 'full',
func = ReloadUI,
},
addons = {
type = 'group',
name = 'Addon Profiles',
inline = true,
order = increment(),
args = {},
}
},
}
},
}
if skipAddons then
return options;
end
local addonNames = {};
local addonOrder = {};
local function getOrder(info)
local addonName = info.option.name;
return addonOrder[addonName] or -1;
end
local function prune(info)
local db = info.arg;
local unusedProfiles = self:GetUnusedProfiles(db);
for _, profile in ipairs(unusedProfiles) do
db.sv.profiles[profile] = nil;
end
end
local function pruneConfirmation(info)
local db = info.arg;
local unusedProfiles = self:GetUnusedProfiles(db);
if #unusedProfiles == 0 then
return false;
end
return ('Are you sure you want to remove %d unused profiles?\n%s'):format(#unusedProfiles, table.concat(unusedProfiles, '\n'));
end
local function pruneDisabled(info)
return #self:GetUnusedProfiles(info.arg) == 0;
end
local addons = options.args.allProfiles.args.addons;
local duplicateAddons = self:GetDuplicateAddons();
for db, _ in pairs(AceDB.db_registry) do
if not db.parent and not self:IsBlacklistedDB(db) then
local i = increment();
local addonName = self:GetAddonNameForDB(db);
if duplicateAddons[addonName] then
local savedVariableName = self:FindGlobal(db.sv);
addonName = addonName .. (savedVariableName and WHITE_FONT_COLOR:WrapTextInColorCode(' ('..savedVariableName..')') or '');
end
table.insert(addonNames, addonName);
local option = self:GetAceDBOptionsTable(db);
option.order = getOrder;
option.name = addonName;
option.inline = false;
option.args.alts = self:MakeAltOptions(db);
option.args.prune = {
type = 'execute',
name = 'Prune',
desc = 'Remove all profiles that are not used by any character',
confirm = pruneConfirmation,
disabled = pruneDisabled,
order = -2,
arg = db,
func = prune,
};
options.args['profiles'..i] = option;
local choose = CopyTable(option.args.choose);
choose.order = getOrder;
choose.handler = option.handler;
choose.name = addonName;
choose.hidden = function()
local currentProfile = choose.handler:GetCurrentProfile(choose);
if self.db.hideAddonsSetToDefault and currentProfile == DEFAULT_OPTION_KEY then
return true;
end
if self.db.hideAddonsSetToCharProfile and currentProfile == currentCharacterName then
return true;
end
if self.db.hideAddonsWithAllAltsUsingSameProfile then
local lastSeenProfile;
for _, v in pairs(choose.handler.db.sv.profileKeys) do
if not lastSeenProfile then
lastSeenProfile = v;
elseif lastSeenProfile ~= v then
return false;
end
end
return true;
end
end
addons.args['profiles'..i] = choose;
end
end
table.sort(addonNames, SortAddons);
for i, addonName in ipairs(addonNames) do
addonOrder[addonName] = i;
end
return options;
end