This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracked.lua
executable file
·288 lines (235 loc) · 7.59 KB
/
tracked.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
-------------------------------------
-- vars --------------
-- Vars Tracking --------
--
local vars = LibStub( 'AceAddon-3.0' ):GetAddon( 'vars' )
local tracked = vars:NewModule( 'tracked' )
local utility = LibStub:GetLibrary( 'utility' )
-- parent persistence reference
--
-- returns table
function tracked:_geParenttDB( )
return vars:getDB( )
end
-- persistence namespace
--
-- returns table
function tracked:getNameSpace( )
return self:_geParenttDB( ):GetNamespace(
self:GetName( )
)[ 'profile' ]
end
-- copy configuration
--
-- returns table
function tracked:getConfig( )
local persistence = self:getNameSpace( )
-- force initial reset
local v_hash = vars[ 'build_info' ][ 'tocversion' ] .. '05'
if persistence[ v_hash ] == nil then
--vars:wipeDB( )
persistence[ v_hash ] = true
end
-- already built
if persistence[ 'tracked' ] ~= nil and next( persistence[ 'tracked' ] ) ~= nil then
return persistence[ 'tracked' ]
end
-- needs building
local known_vars = vars:buildConfig( )
persistence[ 'tracked' ] = { }
for category, category_rows in pairs( known_vars ) do
for i, row in pairs( category_rows ) do
if persistence[ 'tracked' ][ category ] == nil then
persistence[ 'tracked' ][ category ] = { }
end
tinsert( persistence[ 'tracked' ][ category ], {
help = row[ 'help' ],
command = row[ 'command' ],
category = row[ 'category' ],
scriptContents = row[ 'scriptContents' ],
commandType = row[ 'commandType' ],
info = row[ 'info' ],
tracked = row[ 'tracked' ],
value = row[ 'value' ],
} )
end
end
return persistence[ 'tracked' ]
end
-- refresh configuration
-- for a given changeset comprised of category|rowindex|var|value
-- determine if an update is needed within the persistence
--
-- returns bool, table
function tracked:refreshConfig( ui_obj, changeset )
local persistence = self:getConfig( )
local updated = false
--self:_geParenttDB( ):ResetDB( )
if changeset ~= nil then
local default_value = changeset[ 'default_value' ] -- from changeset
local updated_value = changeset[ 'new_value' ] -- from changeset
local current_value = changeset[ 'current_value' ] -- from persistence
local evaluation = current_value ~= '' and current_value ~= updated_value
if evaluation then
persistence[ changeset[ 'category' ] ][ changeset[ 'index' ] ][ 'info'][ 'value' ] = updated_value
updated = true
end
if ui_obj ~= nil then
-- @todo: tremove() call needed to cause the ui registry a rebuild for this value
--ui_obj[ 'registry'][ changeset[ 'category' ] .. '|' .. changeset[ 'command' ] ] = nil
end
persistence[ changeset[ 'category' ] ][ changeset[ 'index' ] ][ 'tracked' ] = evaluation
end
return updated, persistence
end
-- queue configuration
--
-- return bool
function tracked:queueConfig( category, var, value )
local changeset = { }
for cat, rows in pairs( self:getConfig( ) ) do
if category == cat then
for i, row in pairs( rows ) do
if row[ 'command' ] == var then
changeset = {
category = category,
index = i,
command = row[ 'command' ],
default_value = strlower( tostring( row[ 'info' ][ 'defaultValue' ] ) ),
current_value = strlower( tostring( row[ 'info' ][ 'value' ] ) ),
new_value = strlower( tostring( value ) ),
}
if not tracked[ 'queue' ] then
tracked[ 'queue' ] = { }
end
tinsert( tracked[ 'queue' ], changeset )
end
end
end
end
return changeset ~= nil
end
-- update system
-- CVar and stats application
--
-- returns bool, number, string
function tracked:applyConfig( ui_obj, category )
local is_tracked = false
local tracked_count = 0
local message = ''
local updated = false
local known_vars = self:getConfig( )
-- determine how many are modified already
for cat, rows in pairs( known_vars ) do
if type( rows ) == 'table' then
for i, row in pairs( rows ) do
if row[ 'tracked' ] == true then
tracked_count = tracked_count + 1
end
end
end
end
-- initialize changes
local changeset = tracked[ 'queue' ][ next( tracked[ 'queue' ] ) ]
-- do not allow empty changes
if changeset[ 'new_value' ] == nil then
message = 'cannot update'
vars:warn( message )
return false, tracked_count, message
end
-- do not apply combat protected during combat
if tContains( vars:getProtected( 'combat' ), changeset[ 'command' ] ) ~= false and InCombatLockdown( ) == true then
message = changeset[ 'command' ] .. ' can only be modified outside of combat'
vars:warn( message )
return false, tracked_count, message
end
-- apply changes
vars:notify( 'updating ' .. changeset[ 'command' ] .. '...' )
SetCVar( changeset[ 'command' ], changeset[ 'new_value' ] )
-- check for error
if strlower( tostring( GetCVar( changeset[ 'command' ] ) ) ) ~= changeset[ 'new_value' ] then
message = 'failed'
vars:error( message )
return false, tracked_count, message
end
-- maintenance
updated = true
self:refreshConfig( ui_obj, changeset )
local new_value_not_default = changeset[ 'new_value' ] ~= changeset[ 'default_value' ]
if new_value_not_default == true and is_tracked == false then
tracked_count = tracked_count + 1
elseif new_value_not_default == false and is_tracked == true then
tracked_count = tracked_count - 1
end
vars:notify( 'updated from: ' .. changeset[ 'current_value' ] .. ' to: ' .. changeset[ 'new_value' ] )
self[ 'queue' ] = { }
local persistence = self:getNameSpace( )
if persistence[ 'options' ][ 'reloadgx' ] and updated then
RestartGx( )
end
if persistence[ 'options' ][ 'reloadui' ] and updated then
ReloadUI( )
end
return updated, tracked_count, message
end
-- mark config rows as modified/tracked or not
--
-- return string
function tracked:indicate( value )
if value == true then
return 'modified'
else
return 'default'
end
end
-- toggle synch to blizz hq
--
-- return bool
function tracked:cloudSync( state )
local current_state = GetCVar( 'synchronizeConfig' )
if state ~= current_state then
SetCVar( 'synchronizeConfig', state )
if GetCVar( 'synchronizeConfig' ) ~= state then
return false
else
return true
end
end
end
-- setup tracking
--
-- return void
function tracked:init( )
self:getConfig( )
end
-- register persistence
--
-- returns void
function tracked:OnInitialize( )
local defaults = { }
defaults[ 'profile' ] = { }
defaults[ 'profile' ][ 'tracked' ] = nil
defaults[ 'profile' ][ 'search' ] = { }
defaults[ 'profile' ][ 'options' ] = { }
defaults[ 'profile' ][ 'search' ][ 'category_filter' ] = 'Game'
defaults[ 'profile' ][ 'search' ][ 'staus_filter' ] = 'all'
defaults[ 'profile' ][ 'search' ][ 'text' ] = nil
defaults[ 'profile' ][ 'search' ][ 'sort_direction' ] = 'asc'
defaults[ 'profile' ][ 'search' ][ 'remember' ] = true
defaults[ 'profile' ][ 'options' ] = { }
defaults[ 'profile' ][ 'options' ][ 'reloadgx' ] = true
defaults[ 'profile' ][ 'options' ][ 'reloadui' ] = false
defaults[ 'profile' ][ 'options' ][ 'cloudsync' ] = true
defaults[ 'profile' ][ 'db' ] = { }
defaults[ 'profile' ][ 'db' ][ 'was_reset' ] = false
self:_geParenttDB( ):RegisterNamespace(
self:GetName( ), defaults
)
self:Enable( )
end
-- activated module handler
--
-- returns void
function tracked:OnEnable( )
self:init( )
end