-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathCVarsManager.lua
240 lines (205 loc) · 7.95 KB
/
CVarsManager.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
local ADDON_NAME, Addon = ...
local ThreatPlates = Addon.ThreatPlates
---------------------------------------------------------------------------------------------------
-- Functions for changing and savely restoring CVars (mostly after login/logout/reload UI)
---------------------------------------------------------------------------------------------------
-- Lua APIs
local tostring, string_format = tostring, string.format
-- WoW APIs
local GetCVar, GetCVarDefault, GetCVarBool = GetCVar, GetCVarDefault, C_CVar.GetCVarBool
-- ThreatPlates APIs
local L = ThreatPlates.L
local _G =_G
-- Global vars/functions that we don't upvalue since they might get hooked, or upgraded
-- List them here for Mikk's FindGlobals script
-- GLOBALS: SetCVar
Addon.CVars = {}
local CVars = Addon.CVars
local COMBAT_PROTECTED = {
-- Miscellaneous
uiScale = true,
-- Nameplate CVars
nameplateLargeBottomInset = true,
nameplateLargeTopInset = true,
nameplateMaxAlpha = true,
nameplateMaxDistance = true,
nameplateMinAlpha = true,
nameplateMotion = true,
nameplateMotionSpeed = true,
nameplateOccludedAlphaMult = true,
nameplateOtherBottomInset = true,
nameplateOtherTopInset = true,
nameplateOverlapH = true,
nameplateOverlapV = true,
nameplateResourceOnTarget = true,
nameplateSelectedAlpha = true,
nameplateNotSelectedAlpha = true,
nameplateTargetBehindMaxDistance = true,
-- Nameplate Visibility CVars
nameplateShowAll = true,
nameplateShowFriends = true,
nameplateShowEnemies = true,
nameplateShowEnemyGuardians = true,
nameplateShowEnemyMinions = true,
nameplateShowEnemyMinus = true,
nameplateShowEnemyPets = true,
nameplateShowEnemyTotems = true,
nameplateShowFriendlyGuardians = true,
nameplateShowFriendlyMinions = true,
nameplateShowFriendlyNPCs = true,
nameplateShowFriendlyPets = true,
nameplateShowFriendlyTotems = true,
nameplateShowOnlyNames = true,
-- Soft Target CVars
SoftTargetForce = true,
SoftTargetEnemy = true,
SoftTargetNameplateEnemy = true,
SoftTargetIconEnemy = true,
SoftTargetNameplateFriend = true,
SoftTargetIconFriend = true,
SoftTargetInteract = true,
SoftTargetNameplateInteract = true,
SoftTargetIconInteract = true,
SoftTargetIconGameObject = true,
SoftTargetLowPriorityIcons = true,
-- Name CVars
UnitNameFriendlyPlayerName = true,
UnitNameFriendlyPetName = true,
UnitNameFriendlyGuardianName = true,
UnitNameFriendlyTotemName = true,
UnitNameFriendlyMinionName = true,
}
---------------------------------------------------------------------------------------------------
-- Initialize CVars for Threat Plates
---------------------------------------------------------------------------------------------------
-- Set these CVars after login/reloading the UI
function CVars:Initialize(cvar, value)
-- Fix for: Friendly Nameplates Name Only Mode in Raids and Dungeons for Patch 10.0.5
-- https://www.wowhead.com/de/news/update-solution-to-friendly-nameplates-name-only-mode-in-raids-and-dungeons-for-331178
-- /script C_CVar.RegisterCVar("nameplateShowOnlyNames")
-- Registering only has to be done once, but the value seems to be reset to 0 after every reload
if C_CVar.GetCVar("nameplateShowOnlyNames") == nil then
C_CVar.RegisterCVar("nameplateShowOnlyNames")
end
-- ! The CVar nameplateShowOnlyNames is not persistently stored by WoW, so we have to restore its value
-- ! after every login/reloading the UI.
self:SetBool("nameplateShowOnlyNames", Addon.db.profile.BlizzardSettings.Names.ShowOnlyNames)
-- Sync internal settings with Blizzard CVars
-- SetCVar("ShowClassColorInNameplate", 1)
-- local db = Addon.db.profile.threat
-- -- Required for threat/aggro detection
-- if db.ON and (GetCVar("threatWarning") ~= 3) then
-- SetCVar("threatWarning", 3)
-- elseif not db.ON and (GetCVar("threatWarning") ~= 0) then
-- SetCVar("threatWarning", 0)
-- end
end
---------------------------------------------------------------------------------------------------
--
---------------------------------------------------------------------------------------------------
local function SetConsoleVariable(cvar, value)
-- Store in settings to be able to restore it later, but don't overwrite an existing value unless the current value
-- is different from the backup value and the new value TP wants to set. In that case, the CVars was changed since
-- last login with TP by the player or another addon.
local db = Addon.db.profile.CVarsBackup
value = tostring(value) -- convert to string, otherwise the following comparisons would compare numbers with strings
local current_value = GetCVar(cvar)
local backup_value = db[cvar]
if (value ~= current_value) and (current_value ~= backup_value) then
db[cvar] = current_value
end
_G.SetCVar(cvar, value)
end
function CVars:Set(cvar, value)
if COMBAT_PROTECTED[cvar] then
Addon:CallbackWhenOoC(function()
SetConsoleVariable(cvar, value)
end, L["Unable to change the following console variable while in combat: "] .. cvar .. ". ")
else
SetConsoleVariable(cvar, value)
end
end
function CVars:SetBool(cvar, value)
self:Set(cvar, (value and 1) or 0)
end
function CVars:SetToDefault(cvar)
if COMBAT_PROTECTED[cvar] then
Addon:CallbackWhenOoC(function()
_G.SetCVar(cvar, GetCVarDefault(cvar))
Addon.db.profile.CVarsBackup[cvar] = nil
end, L["Unable to change the following console variable while in combat: "] .. cvar .. ". ")
else
_G.SetCVar(cvar, GetCVarDefault(cvar))
Addon.db.profile.CVarsBackup[cvar] = nil
end
end
function CVars:Overwrite(cvar, value)
if COMBAT_PROTECTED[cvar] then
Addon:CallbackWhenOoC(function()
_G.SetCVar(cvar, value)
end, L["Unable to change the following console variable while in combat: "] .. cvar .. ". ")
else
_G.SetCVar(cvar, value)
end
end
function CVars:OverwriteBool(cvar, value)
self:Overwrite(cvar, (value and 1) or 0)
end
function CVars:RestoreFromProfile(cvar)
local db = Addon.db.profile.CVarsBackup
if db[cvar] then
_G.SetCVar(cvar, db[cvar])
db[cvar] = nil
end
end
--function CVars:RestoreAllFromProfile()
-- local db = Addon.db.profile.CVarsBackup
--
-- for cvar, value in pairs(db) do
-- _G.SetCVar(cvar, value)
-- db[cvar] = nil
-- end
--end
function CVars:Get(cvar)
return GetCVar(cvar)
end
function CVars:GetAsNumber(cvar)
local value = GetCVar(cvar)
local numeric_value = tonumber(value)
if not numeric_value then
Addon.Logging.Warning(string_format(L["CVar %s has an invalid value: %s. The value must be a number. Using the default value for this CVar instead."], cvar, value))
numeric_value = tonumber(GetCVarDefault(cvar))
end
return numeric_value
end
function CVars:GetAsBool(cvar)
return GetCVarBool(cvar)
end
---------------------------------------------------------------------------------------------------
--
---------------------------------------------------------------------------------------------------
-- From addon: AdvancedInterfaceOptions
function CVars:CVarExists(cvar)
return not not select(2, pcall(function() return addon.GetCVarInfo(cvar) end))
end
local RESET_TO_DEFAULT = {
"nameplateOtherTopInset", "nameplateOtherBottomInset", "nameplateLargeTopInset", "nameplateLargeBottomInset",
"nameplateMotion", "nameplateMotionSpeed", "nameplateOverlapH", "nameplateOverlapV",
"nameplateMaxDistance", "nameplateTargetBehindMaxDistance",
"nameplateShowOnlyNames",
"clampTargetNameplateToScreen",
"nameplateResourceOnTarget",
-- "nameplateGlobalScale" -- Reset it to 1, if it get's somehow corrupted
-- Action Target
"SoftTargetEnemy", "SoftTargetNameplateEnemy", "SoftTargetIconEnemy",
"SoftTargetFriend", "SoftTargetNameplateFriend", "SoftTargetIconFriend",
"SoftTargetInteract", "SoftTargetNameplateInteract", "SoftTargetIconInteract",
"SoftTargetIconGameObject", "SoftTargetLowPriorityIcons",
}
function CVars:ResetToDefaults()
for k, v in pairs(RESET_TO_DEFAULT) do
if self:CVarExists(k) then
self:SetToDefault(v)
end
end
end