-
Notifications
You must be signed in to change notification settings - Fork 2
/
Config.lua
112 lines (79 loc) · 2.47 KB
/
Config.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
local g_DefaultConfig =
{
General =
{
MaxRange = 10,
Shape = "circle",
},
Chunk =
{
ChunkComposition = "61x7;1x8",
},
}
function LoadConfig()
local FilePath = cPluginManager:Get():GetCurrentPlugin():GetLocalFolder() .. "/config.cfg"
if (not cFile:IsFile(FilePath)) then
LOGWARNING("[LimitWorld] The config file doesn't exist. LimitWorld will write and use the default settings")
WriteDefaultSettings(FilePath)
LoadDefaultSettings()
return
end
local FileContent = cFile:ReadWholeFile(FilePath)
if (FileContent == "") then
LOGWARNING("[LimitWorld] The config file is empty. LimitWorld will write and use the default settings")
WriteDefaultSettings(FilePath)
LoadDefaultSettings()
return
end
local Loader, Error = loadstring("return {" .. FileContent .. "}")
if (not Loader) then
LOGWARNING("[LimitWorld] There is a problem in the config file. LimitWorld will use the default settings")
LoadDefaultSettings()
return
end
local Result, ConfigTable, Error = pcall(Loader)
if (not Result) then
LOGWARNING("[LimitWorld] There is a problem in the config file. LimitWorld will use the default settings")
LoadDefaultSettings()
end
if (not ConfigTable.General) then
LOGWARNING("[LimitWorld] General tab not found in config. Using defaults")
LoadDefaultSettings()
end
if (type(ConfigTable.General.MaxRange) ~= 'number') then
if (type(tonumber(ConfigTable.General.MaxRange)) ~= 'number') then
LOGWARNING("[LimitWorld] MaxRange isn't a number. Changing to default (" .. g_DefaultConfig.General.MaxRange .. ")")
ConfigTable.General.MaxRange = g_DefaultConfig.General.MaxRange
end
ConfigTable.General.MaxRange = tonumber(ConfigTable.General.MaxRange)
end
g_Config = ConfigTable
end
function LoadDefaultSettings()
g_Config = g_DefaultConfig
end
function WriteDefaultSettings(a_Path)
local NumTabs = 0
local File = io.open(a_Path, "w")
local function WriteTable(a_Table, a_Name)
if (a_Name) then
File:write(a_Name, " = \n", string.rep("\t", NumTabs))
File:write("{\n")
end
for Key, Value in pairs(a_Table) do
if (type(Value) == 'table') then
WriteTable(Value, Key)
NumTabs = NumTabs + 1
else
local StringToFormat = type(Value) == 'string' and "%s = '%s',\n" or "%s = %s,\n"
File:write(string.rep("\t", NumTabs + 1), StringToFormat:format(Key, Value))
end
end
if (a_Name) then
File:write("},\n\n")
end
NumTabs = NumTabs - 1
end
WriteTable(g_DefaultConfig)
File:close()
end