forked from violin-suzutsuki/LinoriaLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SaveManager.lua
274 lines (221 loc) · 7.27 KB
/
SaveManager.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
local httpService = game:GetService('HttpService')
local SaveManager = {} do
SaveManager.Folder = 'LinoriaLibSettings'
SaveManager.Ignore = {}
SaveManager.Parser = {
Toggle = {
Save = function(idx, object)
return { type = 'Toggle', idx = idx, value = object.Value }
end,
Load = function(idx, data)
if Toggles[idx] then
Toggles[idx]:SetValue(data.value)
end
end,
},
Slider = {
Save = function(idx, object)
return { type = 'Slider', idx = idx, value = tostring(object.Value) }
end,
Load = function(idx, data)
if Options[idx] then
Options[idx]:SetValue(data.value)
end
end,
},
Dropdown = {
Save = function(idx, object)
return { type = 'Dropdown', idx = idx, value = object.Value, mutli = object.Multi }
end,
Load = function(idx, data)
if Options[idx] then
Options[idx]:SetValue(data.value)
end
end,
},
ColorPicker = {
Save = function(idx, object)
return { type = 'ColorPicker', idx = idx, value = object.Value:ToHex(), transparency = object.Transparency }
end,
Load = function(idx, data)
if Options[idx] then
Options[idx]:SetValueRGB(Color3.fromHex(data.value), data.transparency)
end
end,
},
KeyPicker = {
Save = function(idx, object)
return { type = 'KeyPicker', idx = idx, mode = object.Mode, key = object.Value }
end,
Load = function(idx, data)
if Options[idx] then
Options[idx]:SetValue({ data.key, data.mode })
end
end,
},
Input = {
Save = function(idx, object)
return { type = 'Input', idx = idx, text = object.Value }
end,
Load = function(idx, data)
if Options[idx] and type(data.text) == 'string' then
Options[idx]:SetValue(data.text)
end
end,
},
}
function SaveManager:SetIgnoreIndexes(list)
for _, key in next, list do
self.Ignore[key] = true
end
end
function SaveManager:SetFolder(folder)
self.Folder = folder;
self:BuildFolderTree()
end
function SaveManager:Save(name)
if (not name) then
return false, 'no config file is selected'
end
local fullPath = self.Folder .. '/settings/' .. name .. '.json'
local data = {
objects = {}
}
for idx, toggle in next, Toggles do
if self.Ignore[idx] then continue end
table.insert(data.objects, self.Parser[toggle.Type].Save(idx, toggle))
end
for idx, option in next, Options do
if not self.Parser[option.Type] then continue end
if self.Ignore[idx] then continue end
table.insert(data.objects, self.Parser[option.Type].Save(idx, option))
end
local success, encoded = pcall(httpService.JSONEncode, httpService, data)
if not success then
return false, 'failed to encode data'
end
writefile(fullPath, encoded)
return true
end
function SaveManager:Load(name)
if (not name) then
return false, 'no config file is selected'
end
local file = self.Folder .. '/settings/' .. name .. '.json'
if not isfile(file) then return false, 'invalid file' end
local success, decoded = pcall(httpService.JSONDecode, httpService, readfile(file))
if not success then return false, 'decode error' end
for _, option in next, decoded.objects do
if self.Parser[option.type] then
task.spawn(function() self.Parser[option.type].Load(option.idx, option) end) -- task.spawn() so the config loading wont get stuck.
end
end
return true
end
function SaveManager:IgnoreThemeSettings()
self:SetIgnoreIndexes({
"BackgroundColor", "MainColor", "AccentColor", "OutlineColor", "FontColor", -- themes
"ThemeManager_ThemeList", 'ThemeManager_CustomThemeList', 'ThemeManager_CustomThemeName', -- themes
})
end
function SaveManager:BuildFolderTree()
local paths = {
self.Folder,
self.Folder .. '/themes',
self.Folder .. '/settings'
}
for i = 1, #paths do
local str = paths[i]
if not isfolder(str) then
makefolder(str)
end
end
end
function SaveManager:RefreshConfigList()
local list = listfiles(self.Folder .. '/settings')
local out = {}
for i = 1, #list do
local file = list[i]
if file:sub(-5) == '.json' then
-- i hate this but it has to be done ...
local pos = file:find('.json', 1, true)
local start = pos
local char = file:sub(pos, pos)
while char ~= '/' and char ~= '\\' and char ~= '' do
pos = pos - 1
char = file:sub(pos, pos)
end
if char == '/' or char == '\\' then
table.insert(out, file:sub(pos + 1, start - 1))
end
end
end
return out
end
function SaveManager:SetLibrary(library)
self.Library = library
end
function SaveManager:LoadAutoloadConfig()
if isfile(self.Folder .. '/settings/autoload.txt') then
local name = readfile(self.Folder .. '/settings/autoload.txt')
local success, err = self:Load(name)
if not success then
return self.Library:Notify('Failed to load autoload config: ' .. err)
end
self.Library:Notify(string.format('Auto loaded config %q', name))
end
end
function SaveManager:BuildConfigSection(tab)
assert(self.Library, 'Must set SaveManager.Library')
local section = tab:AddRightGroupbox('Configuration')
section:AddInput('SaveManager_ConfigName', { Text = 'Config name' })
section:AddDropdown('SaveManager_ConfigList', { Text = 'Config list', Values = self:RefreshConfigList(), AllowNull = true })
section:AddDivider()
section:AddButton('Create config', function()
local name = Options.SaveManager_ConfigName.Value
if name:gsub(' ', '') == '' then
return self.Library:Notify('Invalid config name (empty)', 2)
end
local success, err = self:Save(name)
if not success then
return self.Library:Notify('Failed to save config: ' .. err)
end
self.Library:Notify(string.format('Created config %q', name))
Options.SaveManager_ConfigList:SetValues(self:RefreshConfigList())
Options.SaveManager_ConfigList:SetValue(nil)
end):AddButton('Load config', function()
local name = Options.SaveManager_ConfigList.Value
local success, err = self:Load(name)
if not success then
return self.Library:Notify('Failed to load config: ' .. err)
end
self.Library:Notify(string.format('Loaded config %q', name))
end)
section:AddButton('Overwrite config', function()
local name = Options.SaveManager_ConfigList.Value
local success, err = self:Save(name)
if not success then
return self.Library:Notify('Failed to overwrite config: ' .. err)
end
self.Library:Notify(string.format('Overwrote config %q', name))
end)
section:AddButton('Refresh list', function()
Options.SaveManager_ConfigList:SetValues(self:RefreshConfigList())
Options.SaveManager_ConfigList:SetValue(nil)
end)
section:AddButton('Set as autoload', function()
local name = Options.SaveManager_ConfigList.Value
writefile(self.Folder .. '/settings/autoload.txt', name)
SaveManager.AutoloadLabel:SetText('Current autoload config: ' .. name)
self.Library:Notify(string.format('Set %q to auto load', name))
end)
SaveManager.AutoloadLabel = section:AddLabel('Current autoload config: none', true)
if isfile(self.Folder .. '/settings/autoload.txt') then
local name = readfile(self.Folder .. '/settings/autoload.txt')
SaveManager.AutoloadLabel:SetText('Current autoload config: ' .. name)
end
SaveManager:SetIgnoreIndexes({ 'SaveManager_ConfigList', 'SaveManager_ConfigName' })
end
SaveManager:BuildFolderTree()
end
return SaveManager