-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWago.lua
298 lines (246 loc) · 8.5 KB
/
Wago.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
local addonName, ATOM = ...
local Module = ATOM:NewModule('Wago')
local CopyIntoTable, CompressData, DecompressData, GetField
local GetAddon, SelectAddon, SelectAddonProfile, SelectImportBtn, SelectExportBtn, UpdateDisabledStates
function Module:ShowWindow()
Module.minwidth = 720
Module.minheight = 460
local AceGUI = LibStub('AceGUI-3.0')
local window = AceGUI:Create('Frame')
window.frame:SetClampedToScreen(true)
window.frame:SetFrameStrata('MEDIUM')
window.frame:Raise()
window.content:SetFrameStrata('MEDIUM')
window.content:Raise()
window:SetTitle('ATOM Import/Export')
window:SetStatusText('')
window:SetLayout('Fill')
window:SetWidth(Module.minwidth)
window:SetHeight(Module.minheight)
window:SetAutoAdjustHeight(true)
window:SetCallback('OnClose', function(widget)
AceGUI:Release(widget)
end)
Module.window = window
_G['ATOM_WagoWindow'] = window.frame
tinsert(UISpecialFrames, 'ATOM_WagoWindow')
local group1 = AceGUI:Create('SimpleGroup')
group1:SetLayout('List')
group1:SetFullWidth(true)
group1:SetFullHeight(true)
window:AddChild(group1)
local group2 = AceGUI:Create('SimpleGroup')
group2:SetLayout('Flow')
group2:SetFullWidth(true)
group1:AddChild(group2)
local supportedAddons = {}
if GetAddon('Dominos') then
supportedAddons['Dominos.db'] = 'Dominos'
end
local addonDropdown = AceGUI:Create('Dropdown')
addonDropdown:SetMultiselect(false)
addonDropdown:SetLabel('Select addon:')
addonDropdown:SetWidth(200)
addonDropdown:SetList(supportedAddons)
addonDropdown:SetCallback('OnValueChanged', SelectAddon)
window.addonDropdown = addonDropdown
group2:AddChild(addonDropdown)
local profileDropdown = AceGUI:Create('Dropdown')
profileDropdown:SetMultiselect(false)
profileDropdown:SetLabel('Select profile:')
profileDropdown:SetWidth(200)
profileDropdown:SetDisabled(true)
profileDropdown:SetCallback('OnValueChanged', SelectAddonProfile)
window.profileDropdown = profileDropdown
group2:AddChild(profileDropdown)
local textBox = AceGUI:Create('MultiLineEditBox')
textBox:SetLabel('')
textBox:SetMaxLetters(0)
textBox:SetNumLines(20)
textBox:SetHeight(290)
textBox:DisableButton(true)
textBox:SetFullWidth(true)
textBox:SetText('')
textBox:SetCallback('OnTextChanged', SelectAddonProfile)
window.textBox = textBox
group1:AddChild(textBox)
local group3 = AceGUI:Create('SimpleGroup')
group3:SetLayout('Flow')
group3:SetFullWidth(true)
group1:AddChild(group3)
local importBtn = AceGUI:Create('Button')
importBtn:SetText('Import')
importBtn:SetWidth(110)
importBtn:SetDisabled(true)
importBtn:SetCallback('OnClick', SelectImportBtn)
window.importBtn = importBtn
group3:AddChild(importBtn)
local exportBtn = AceGUI:Create('Button')
exportBtn:SetText('Export')
exportBtn:SetWidth(110)
exportBtn:SetDisabled(true)
exportBtn:SetCallback('OnClick', SelectExportBtn)
window.exportBtn = exportBtn
group3:AddChild(exportBtn)
hooksecurefunc(window, 'OnWidthSet', function(widget, width)
if widget == window and width < Module.minwidth then
window:SetWidth(Module.minwidth)
end
end)
hooksecurefunc(window, 'OnHeightSet', function(widget, height)
if widget == window then
if height < Module.minheight then
window:SetHeight(Module.minheight)
else
textBox:SetHeight(height - Module.minheight + 298)
end
end
end)
addonDropdown:SetValue('Dominos.db')
SelectAddon(addonDropdown, 'OnValueChanged', 'Dominos.db')
end
function GetAddon(addon)
if _G[addon] then
return _G[addon]
end
return LibStub('AceAddon-3.0'):GetAddon(addon, true)
end
function GetField(field)
local var
var = _G
for name in string.gmatch(field, '[%w_]+') do
if var == _G then
var = GetAddon(name)
elseif type(var) == 'table' then
var = var[name]
else
var = nil
end
end
return var
end
function SelectAddon(widget, event, key)
local database = GetField(key)
local profiles = {}
local currentProfile
if database then
currentProfile = database:GetCurrentProfile()
for i, name in pairs(database:GetProfiles()) do
if name == currentProfile then
profiles[name] = name .. ' (current)'
else
profiles[name] = name
end
end
end
Module.window.profileDropdown:SetList(profiles)
if #profiles then
Module.window.profileDropdown:SetDisabled(false)
else
Module.window.profileDropdown:SetDisabled(true)
end
if currentProfile then
Module.window.profileDropdown:SetValue(currentProfile)
SelectAddonProfile(Module.window.profileDropdown, 'OnValueChanged', currentProfile)
end
end
function SelectAddonProfile()
local payload = Module.window.textBox:GetText()
local profile = Module.window.addonDropdown:GetValue()
if profile and payload ~= '' then
Module.window.importBtn:SetDisabled(false)
Module.window.exportBtn:SetDisabled(true)
elseif profile then
Module.window.importBtn:SetDisabled(true)
Module.window.exportBtn:SetDisabled(false)
else
Module.window.importBtn:SetDisabled(true)
Module.window.exportBtn:SetDisabled(true)
end
end
function SelectImportBtn()
local database = GetField(Module.window.addonDropdown:GetValue())
local profileName = Module.window.profileDropdown:GetValue()
local profile = DecompressData(Module.window.textBox:GetText())
if database and profile then
database:SetProfile(profileName)
database:ResetProfile(false, true)
CopyIntoTable(database.profile, profile)
Module.window.textBox:GetText('')
Module.window:SetStatusText('Profile imported')
UpdateDisabledStates()
ReloadUI()
else
Module.window:SetStatusText('Failed to import profile')
end
end
function SelectExportBtn()
local database = GetField(Module.window.addonDropdown:GetValue())
local profileName = Module.window.profileDropdown:GetValue()
local content
if database and database.profiles[profileName] then
content = CompressData(database.profiles[profileName])
end
if content then
Module.window.textBox:SetFocus()
Module.window.textBox:SetText(content)
Module.window.textBox:HighlightText()
Module.window:SetStatusText('Profile exported')
UpdateDisabledStates()
else
Module.window:SetStatusText('Failed to export profile')
end
end
function UpdateDisabledStates()
Module.window.addonDropdown:SetDisabled(true)
Module.window.profileDropdown:SetDisabled(true)
Module.window.importBtn:SetDisabled(true)
Module.window.exportBtn:SetDisabled(true)
end
function CopyIntoTable(dst, src)
for key, value in pairs(src) do
if (key ~= '__index') then
if (type(value) == 'table') then
dst[key] = dst[key] or {}
CopyIntoTable(dst[key], src[key])
else
dst[key] = value
end
end
end
return dst
end
function CompressData(data)
local LibDeflate = LibStub:GetLibrary('LibDeflate')
local LibAceSerializer = LibStub:GetLibrary('AceSerializer-3.0')
if LibDeflate and LibAceSerializer then
local serializedData = LibAceSerializer:Serialize(data)
if not serializedData then
return nil
end
local compressedData = LibDeflate:CompressDeflate(serializedData, { level = 9 })
if not compressedData then
return nil
end
return LibDeflate:EncodeForPrint(compressedData)
end
end
function DecompressData(encodedData)
local LibDeflate = LibStub:GetLibrary('LibDeflate')
local LibAceSerializer = LibStub:GetLibrary('AceSerializer-3.0')
if LibDeflate and LibAceSerializer then
local compressedData = LibDeflate:DecodeForPrint(encodedData)
if not compressedData then
return nil
end
local serializedData = LibDeflate:DecompressDeflate(compressedData)
if not serializedData then
return nil
end
local deserialized, data = LibAceSerializer:Deserialize(serializedData)
if not deserialized then
return nil
end
return data
end
end