-
Notifications
You must be signed in to change notification settings - Fork 18
/
Config.lua
180 lines (121 loc) · 4.27 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
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
-- Config.lua
-- Contains the functions to initialize and write the configuration file.
g_Config = {}
-- Create an environment for the config loader where the admin can use item names directly without quotes.
local g_LoaderEnv = {}
for Key, Value in pairs(_G) do
if (Key:match("^E_.*")) then
g_LoaderEnv[ItemTypeToString(Value)] = Value
end
end
local g_ConfigDefault =
[[
WandItem = woodenaxe,
Limits =
{
ButcherRadius = -1,
MaxBrushRadius = 5,
DisallowedBlocks = {6, 7, 14, 15, 16, 26, 27, 28, 29, 39, 31, 32, 33, 34, 36, 37, 38, 39, 40, 46, 50, 51, 56, 59, 69, 73, 74, 75, 76, 77, 81, 83},
},
Defaults =
{
ButcherRadius = 20,
},
NavigationWand =
{
Item = compass,
MaxDistance = 120,
TeleportNoHit = true,
},
Scripting =
{
-- If true it logs an error when a craftscript failed
Debug = false,
-- The amount of seconds that a script may be active. Any longer and the script will be aborted.
-- If negative the time a script can run is unlimited.
MaxExecutionTime = 5,
},
Schematics =
{
OverrideExistingFiles = true,
},
Updates =
{
CheckForUpdates = true,
NumAttempts = 3,
ShowMessageWhenUpToDate = true,
DownloadNewerVersion = true,
},
Storage =
{
-- If set to true the selection of a player will be remembered once he leaves.
RememberPlayerSelection = true,
-- If WorldEdit needs to change a format in the database the database will be backuped first before changing.
-- This doesn't mean when adding or removing data the database will be backed up. Only when the used database is outdated.
BackupDatabaseWhenUpdating = true,
}
]]
-- Writes the default configuration to a_Path
local function WriteDefaultConfiguration(a_Path)
LOGWARNING("Default configuration written to \"" .. a_Path .. "\"")
local File = io.open(a_Path, "w")
File:write(g_ConfigDefault)
File:close()
end
-- Returns the default configuration table. This can be directly used in g_Config
local function GetDefaultConfigurationTable()
-- Load the default config
local Loader = loadstring("return {" .. g_ConfigDefault .. "}")
-- Apply the environment to the configloader.
setfenv(Loader, g_LoaderEnv)
return Loader()
end
-- Sets g_Config to the default configuration
local function LoadDefaultConfiguration()
LOGWARNING("The default configuration will be used.")
g_Config = GetDefaultConfigurationTable()
end
-- Finds from an error message where the error occurred.
local function FindErrorPosition(a_ErrorMessage)
local ErrorPosition = a_ErrorMessage:match(":(.-):") or 0
return ErrorPosition
end
-- Loads the configuration from the given path. If it doesn't exist, or if there is an error in the config file it will load the defaults.
function InitializeConfiguration(a_Path)
local ConfigContent = cFile:ReadWholeFile(a_Path)
-- The configuration file doesn't exist or is empty. Write and load the default value
if (ConfigContent == "") then
WriteDefaultConfiguration(a_Path)
LoadDefaultConfiguration()
return
end
-- Load the content of the config file. Place brackets around it to make the whole thing a table.
-- Also, return the table when executed
local ConfigLoader, Error = loadstring("return {" .. ConfigContent .. "}")
if (not ConfigLoader) then
local ErrorPosition = FindErrorPosition(Error)
LOGWARNING("Error in the configuration file near line " .. ErrorPosition)
LoadDefaultConfiguration()
return
end
-- Apply the environment to the configloader.
setfenv(ConfigLoader, g_LoaderEnv)
-- Execute the loader. It returns true + the configuration if it executed properly. Else it returns false with the error message.
local Succes, Result = pcall(ConfigLoader)
if (not Succes) then
local ErrorPosition = FindErrorPosition(Result)
LOGWARNING("Error in the configuration file at line " .. ErrorPosition)
LoadDefaultConfiguration()
return
end
-- Merge the configuration with the default configuration.
-- When the admin missed something in the configuration it will be set to the default value.
local DefaultConfig = GetDefaultConfigurationTable()
table.merge(Result, DefaultConfig)
-- Make a dictionary out of the array of disallowed blocks.
if (Result.Limits and (type(Result.Limits.DisallowedBlocks) == "table")) then
Result.Limits.DisallowedBlocks = table.todictionary(Result.Limits.DisallowedBlocks)
end
-- Set the g_Config table.
g_Config = Result
end