forked from mstudio45/LinoriaLib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSaveManager.lua
496 lines (395 loc) · 16.8 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
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
local cloneref = (cloneref or clonereference or function(instance: any) return instance end)
local httpService = cloneref(game:GetService('HttpService'))
local isfolder, isfile, listfiles = isfolder, isfile, listfiles;
if typeof(copyfunction) == "function" then
-- Fix is_____ functions for shitsploits, those functions should never error, only return a boolean.
local
isfolder_copy,
isfile_copy,
listfiles_copy = copyfunction(isfolder), copyfunction(isfile), copyfunction(listfiles);
local isfolder_success, isfolder_error = pcall(function()
return isfolder_copy("test" .. tostring(math.random(1000000, 9999999)))
end);
if isfolder_success == false or typeof(isfolder_error) ~= "boolean" then
isfolder = function(folder)
local success, data = pcall(isfolder_copy, folder)
return (if success then data else false)
end;
isfile = function(file)
local success, data = pcall(isfile_copy, file)
return (if success then data else false)
end;
listfiles = function(folder)
local success, data = pcall(listfiles_copy, folder)
return (if success then data else {})
end;
end
end
local SaveManager = {} do
SaveManager.Folder = 'LinoriaLibSettings'
SaveManager.SubFolder = ''
SaveManager.Ignore = {}
SaveManager.Library = nil
SaveManager.Parser = {
Toggle = {
Save = function(idx, object)
return { type = 'Toggle', idx = idx, value = object.Value }
end,
Load = function(idx, data)
local object = SaveManager.Library.Toggles[idx]
if object and object.Value ~= data.value then
object:SetValue(data.value)
end
end,
},
Slider = {
Save = function(idx, object)
return { type = 'Slider', idx = idx, value = tostring(object.Value) }
end,
Load = function(idx, data)
local object = SaveManager.Library.Options[idx]
if object and object.Value ~= data.value then
object: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)
local object = SaveManager.Library.Options[idx]
if object and object.Value ~= data.value then
object: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 SaveManager.Library.Options[idx] then
SaveManager.Library.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 SaveManager.Library.Options[idx] then
SaveManager.Library.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)
local object = SaveManager.Library.Options[idx]
if object and object.Value ~= data.text and type(data.text) == 'string' then
SaveManager.Library.Options[idx]:SetValue(data.text)
end
end,
},
}
function SaveManager:SetLibrary(library)
self.Library = library
end
function SaveManager:IgnoreThemeSettings()
self:SetIgnoreIndexes({
"BackgroundColor", "MainColor", "AccentColor", "OutlineColor", "FontColor", -- themes
"ThemeManager_ThemeList", 'ThemeManager_CustomThemeList', 'ThemeManager_CustomThemeName', -- themes
"VideoLink",
})
end
--// Folders \\--
function SaveManager:CheckSubFolder(createFolder)
if typeof(self.SubFolder) ~= "string" or self.SubFolder == "" then return false end
if createFolder == true then
if not isfolder(self.Folder .. "/settings/" .. self.SubFolder) then
makefolder(self.Folder .. "/settings/" .. self.SubFolder)
end
end
return true
end
function SaveManager:GetPaths()
local paths = {}
local parts = self.Folder:split('/')
for idx = 1, #parts do
local path = table.concat(parts, '/', 1, idx)
if not table.find(paths, path) then paths[#paths + 1] = path end
end
paths[#paths + 1] = self.Folder .. '/themes'
paths[#paths + 1] = self.Folder .. '/settings'
if self:CheckSubFolder(false) then
local subFolder = self.Folder .. "/settings/" .. self.SubFolder
parts = subFolder:split('/')
for idx = 1, #parts do
local path = table.concat(parts, '/', 1, idx)
if not table.find(paths, path) then paths[#paths + 1] = path end
end
end
return paths
end
function SaveManager:BuildFolderTree()
local paths = self:GetPaths()
for i = 1, #paths do
local str = paths[i]
if isfolder(str) then continue end
makefolder(str)
end
end
function SaveManager:CheckFolderTree()
if isfolder(self.Folder) then return end
SaveManager:BuildFolderTree()
task.wait(0.1)
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:SetSubFolder(folder)
self.SubFolder = folder;
self:BuildFolderTree()
end
--// Save, Load, Delete, Refresh \\--
function SaveManager:Save(name)
if (not name) then
return false, 'no config file is selected'
end
SaveManager:CheckFolderTree()
local fullPath = self.Folder .. '/settings/' .. name .. '.json'
if SaveManager:CheckSubFolder(true) then
fullPath = self.Folder .. "/settings/" .. self.SubFolder .. "/" .. name .. '.json'
end
local data = {
objects = {}
}
for idx, toggle in next, self.Library.Toggles do
if not toggle.Type then continue end
if not self.Parser[toggle.Type] then continue end
if self.Ignore[idx] then continue end
table.insert(data.objects, self.Parser[toggle.Type].Save(idx, toggle))
end
for idx, option in next, self.Library.Options do
if not option.Type then continue end
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
SaveManager:CheckFolderTree()
local file = self.Folder .. '/settings/' .. name .. '.json'
if SaveManager:CheckSubFolder(true) then
file = self.Folder .. "/settings/" .. self.SubFolder .. "/" .. name .. '.json'
end
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 not option.type then continue end
if not self.Parser[option.type] then continue end
task.spawn(self.Parser[option.type].Load, option.idx, option) -- task.spawn() so the config loading wont get stuck.
end
return true
end
function SaveManager:Delete(name)
if (not name) then
return false, 'no config file is selected'
end
local file = self.Folder .. '/settings/' .. name .. '.json'
if SaveManager:CheckSubFolder(true) then
file = self.Folder .. "/settings/" .. self.SubFolder .. "/" .. name .. '.json'
end
if not isfile(file) then return false, 'invalid file' end
local success = pcall(delfile, file)
if not success then return false, 'delete file error' end
return true
end
function SaveManager:RefreshConfigList()
local success, data = pcall(function()
SaveManager:CheckFolderTree()
local list = {}
local out = {}
if SaveManager:CheckSubFolder(true) then
list = listfiles(self.Folder .. "/settings/" .. self.SubFolder)
else
list = listfiles(self.Folder .. "/settings")
end
if typeof(list) ~= "table" then list = {} end
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)
if (not success) then
if self.Library then
self.Library:Notify('Failed to load config list: ' .. tostring(data))
else
warn('Failed to load config list: ' .. tostring(data))
end
return {}
end
return data
end
--// Auto Load \\--
function SaveManager:GetAutoloadConfig()
SaveManager:CheckFolderTree()
local autoLoadPath = self.Folder .. "/settings/autoload.txt"
if SaveManager:CheckSubFolder(true) then
autoLoadPath = self.Folder .. "/settings/" .. self.SubFolder .. "/autoload.txt"
end
if isfile(autoLoadPath) then
local successRead, name = pcall(readfile, autoLoadPath)
if not successRead then
return "none"
end
name = tostring(name)
return if name == "" then "none" else name
end
return "none"
end
function SaveManager:LoadAutoloadConfig()
SaveManager:CheckFolderTree()
local autoLoadPath = self.Folder .. "/settings/autoload.txt"
if SaveManager:CheckSubFolder(true) then
autoLoadPath = self.Folder .. "/settings/" .. self.SubFolder .. "/autoload.txt"
end
if isfile(autoLoadPath) then
local successRead, name = pcall(readfile, autoLoadPath)
if not successRead then
return self.Library:Notify('Failed to load autoload config: write file error')
end
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:SaveAutoloadConfig(name)
SaveManager:CheckFolderTree()
local autoLoadPath = self.Folder .. "/settings/autoload.txt"
if SaveManager:CheckSubFolder(true) then
autoLoadPath = self.Folder .. "/settings/" .. self.SubFolder .. "/autoload.txt"
end
local success = pcall(writefile, autoLoadPath, name)
if not success then return false, 'write file error' end
return true, ""
end
function SaveManager:DeleteAutoLoadConfig()
SaveManager:CheckFolderTree()
local autoLoadPath = self.Folder .. "/settings/autoload.txt"
if SaveManager:CheckSubFolder(true) then
autoLoadPath = self.Folder .. "/settings/" .. self.SubFolder .. "/autoload.txt"
end
local success = pcall(delfile, autoLoadPath)
if not success then return false, 'delete file error' end
return true, ""
end
--// GUI \\--
function SaveManager:BuildConfigSection(tab)
assert(self.Library, 'Must set SaveManager.Library')
local section = tab:AddRightGroupbox('Configuration')
section:AddInput('SaveManager_ConfigName', { Text = 'Config name' })
section:AddButton('Create config', function()
local name = self.Library.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 create config: ' .. err)
end
self.Library:Notify(string.format('Created config %q', name))
self.Library.Options.SaveManager_ConfigList:SetValues(self:RefreshConfigList())
self.Library.Options.SaveManager_ConfigList:SetValue(nil)
end)
section:AddDivider()
section:AddDropdown('SaveManager_ConfigList', { Text = 'Config list', Values = self:RefreshConfigList(), AllowNull = true })
section:AddButton('Load config', function()
local name = self.Library.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 = self.Library.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('Delete config', function()
local name = self.Library.Options.SaveManager_ConfigList.Value
local success, err = self:Delete(name)
if not success then
return self.Library:Notify('Failed to delete config: ' .. err)
end
self.Library:Notify(string.format('Deleted config %q', name))
self.Library.Options.SaveManager_ConfigList:SetValues(self:RefreshConfigList())
self.Library.Options.SaveManager_ConfigList:SetValue(nil)
end)
section:AddButton('Refresh list', function()
self.Library.Options.SaveManager_ConfigList:SetValues(self:RefreshConfigList())
self.Library.Options.SaveManager_ConfigList:SetValue(nil)
end)
section:AddButton('Set as autoload', function()
local name = self.Library.Options.SaveManager_ConfigList.Value
local success, err = self:SaveAutoloadConfig(name)
if not success then
return self.Library:Notify('Failed to set autoload config: ' .. err)
end
SaveManager.AutoloadLabel:SetText('Current autoload config: ' .. name)
self.Library:Notify(string.format('Set %q to auto load', name))
end)
section:AddButton('Reset autoload', function()
local success, err = self:DeleteAutoLoadConfig()
if not success then
return self.Library:Notify('Failed to set autoload config: ' .. err)
end
self.Library:Notify('Set autoload to none')
SaveManager.AutoloadLabel:SetText('Current autoload config: none')
end)
self.AutoloadLabel = section:AddLabel("Current autoload config: " .. self:GetAutoloadConfig(), true)
-- self:LoadAutoloadConfig()
self:SetIgnoreIndexes({ 'SaveManager_ConfigList', 'SaveManager_ConfigName' })
end
SaveManager:BuildFolderTree()
end
return SaveManager