-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.lua
161 lines (122 loc) · 5.4 KB
/
Main.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
--[[
Config Library by Exunys © CC0 1.0 Universal (2023)
https://github.com/Exunys
]]
local HttpService, ConfigLibrary = game:GetService("HttpService"), {}
ConfigLibrary.Encode = function(Table)
assert(Table, "ConfigLibrary.Encode => Parameter \"Table\" is missing!")
assert(type(Table) == "table", "ConfigLibrary.Encode => Parameter \"Table\" must be of type <table>. Type given: <"..type(Table)..">")
if Table and type(Table) == "table" then
return HttpService:JSONEncode(Table)
end
end
ConfigLibrary.Decode = function(Content)
assert(Content, "ConfigLibrary.Decode => Parameter \"Content\" is missing!")
assert(type(Content) == "string", "ConfigLibrary.Decode => Parameter \"Content\" must be of type <string>. Type given: <"..type(Content)..">")
return HttpService:JSONDecode(Content)
end
ConfigLibrary.Recursive = function(self, Table, Callback)
assert(Table, "ConfigLibrary.Recursive => Parameter \"Table\" is missing!")
assert(Callback, "ConfigLibrary.Recursive => Parameter \"Callback\" is missing!")
assert(type(Table) == "table", "ConfigLibrary.Recursive => Parameter \"Table\" must be of type <table>. Type given: <"..type(Table)..">")
assert(type(Callback) == "function", "ConfigLibrary.Recursive => Parameter \"Callback\" must be of type <string>. Type given: <"..type(Callback)..">")
for i, v in next, Table do
Callback(i, v)
if type(v) == "table" then
self:Recursive(v, Callback)
end
end
end
ConfigLibrary.EditValue = function(Value)
if typeof(Value) == "Color3" then
return "Color3_("..math.floor(Value.R * 255)..", "..math.floor(Value.G * 255)..", "..math.floor(Value.B * 255)..")"
elseif typeof(Value) == "Vector3" or typeof(Value) == "Vector2" or typeof(Value) == "CFrame" then
return typeof(Value).."_("..tostring(Value)..")"
elseif typeof(Value) == "EnumItem" then
return "EnumItem_("..string.match(tostring(Value), "Enum%.(.+)")..")"
end
return Value
end
ConfigLibrary.RestoreValue = function(Value)
if type(Value) == "string" then
local Type, Content = string.match(Value, "(%w+)_%((.+)%)")
if Type == "Color3" then
Content = string.split(Content, ", ")
for i, v in next, Content do
Content[i] = tonumber(v)
end
return Color3.fromRGB(unpack(Content))
elseif Type == "Vector3" or Type == "Vector2" or Type == "CFrame" then
Content = string.split(Content, ", ")
for i, v in next, Content do
Content[i] = tonumber(v)
end
return getfenv()[Type].new(unpack(Content))
elseif Type == "EnumItem" then
return loadstring("return Enum."..Content)()
end
end
return Value
end
ConfigLibrary.CloneTable = function(self, Object, Seen)
if type(Object) ~= "table" then return Object end
if Seen and Seen[Object] then return Seen[Object] end
local LocalSeen = Seen or {}
local Result = setmetatable({}, getmetatable(Object))
LocalSeen[Object] = Result
for i, v in next, Object do
Result[self:CloneTable(i, LocalSeen)] = self:CloneTable(v, LocalSeen)
end
return Result
end
ConfigLibrary.ConvertValues = function(self, Data, Method)
assert(Data, "ConfigLibrary.ConvertValues => Parameter \"Data\" is missing!")
assert(Method, "ConfigLibrary.ConvertValues => Parameter \"Method\" is missing!")
assert(type(Data) == "table", "ConfigLibrary.ConvertValues => Parameter \"Data\" must be of type <table>. Type given: <"..type(Data)..">")
assert(type(Method) == "string", "ConfigLibrary.ConvertValues => Parameter \"Method\" must be of type <string>. Type given: <"..type(Method)..">")
local Passed, Stack = {[Data] = true}, {Data}
repeat
local Current = table.remove(Stack) -- "Pop"
for i, v in next, Current do
if type(v) == "table" and not Passed[v] then
Passed[v] = true
Stack[#Stack + 1] = v -- "Push" to stack
else
Current[i] = self[Method.."Value"](v)
end
end
until #Stack == 0
return Data
end
ConfigLibrary.SaveConfig = function(self, Path, Data)
assert(Path, "ConfigLibrary.SaveConfig => Parameter \"Path\" is missing!")
assert(Data, "ConfigLibrary.SaveConfig => Parameter \"Data\" is missing!")
assert(type(Path) == "string", "ConfigLibrary.SaveConfig => Parameter \"Path\" must be of type <string>. Type given: <"..type(Path)..">")
assert(type(Data) == "table", "ConfigLibrary.SaveConfig => Parameter \"Data\" must be of type <table>. Type given: <"..type(Data)..">")
local Result = self.Encode(self:ConvertValues(self:CloneTable(Data), "Edit"))
if select(2, pcall(function() readfile(Path) end)) then
self.CreatePath(self, Path, Result)
end
writefile(Path, Result)
end
ConfigLibrary.LoadConfig = function(self, Path)
assert(Path, "ConfigLibrary.LoadConfig => Parameter \"Path\" is missing!")
assert(type(Path) == "string", "ConfigLibrary.LoadConfig => Parameter \"Path\" must be of type <string>. Type given: <"..type(Path)..">")
return self:ConvertValues(self.Decode(readfile(Path)), "Restore")
end
ConfigLibrary.CreatePath = function(self, Path, Content)
assert(Path, "ConfigLibrary.CreatePath => Parameter \"Path\" is missing!")
assert(type(Path) == "string", "ConfigLibrary.CreatePath => Parameter \"Path\" must be of type <string>. Type given: <"..type(Path)..">")
local Folders, Destination, File = string.split(Path, "/"), ""
File = Folders[#Folders]; table.remove(Folders)
for i = 1, #Folders do
Destination = Destination..Folders[i].."/"
if not isfolder(Destination) then
makefolder(Destination)
end
end
if not isfile(Destination..File) then
writefile(Destination..File, Content or "")
end
end
return ConfigLibrary