-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathConfig.lua
More file actions
312 lines (253 loc) · 10.7 KB
/
Copy pathConfig.lua
File metadata and controls
312 lines (253 loc) · 10.7 KB
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
-- Config.lua
-- Implements loading and verifying the configuration from the file
--- The configuration
g_Config = {};
-- Static data:
--- Parameters that are required for each gallery:
local g_GalleryRequiredParams =
{
{ Name = "MinX", Type = "number", },
{ Name = "MinZ", Type = "number", },
{ Name = "MaxX", Type = "number", },
{ Name = "MaxZ", Type = "number", },
{ Name = "FillStrategy", Type = "string", },
{ Name = "WorldName", Type = "string", }
} ;
--- Parameters that are optional for each gallery (so that the type is checked):
local g_GalleryOptionalParams =
{
{ Name = "AreaTemplate", Type = "string" },
{ Name = "AreaEdge", Type = "number" },
{ Name = "Biome", Type = "string" },
} ;
local function GetSchematicHighestNonAirBlock(a_Schematic)
for y = a_Schematic:GetSizeY() - 1, 0, -1 do
if (a_Schematic:GetRelBlockType(0, y, 0) ~= E_BLOCK_AIR) then
return y;
end
end
return 0;
end
--- Returns true if the gallery has all the minimum settings it needs
-- a_Index is used instead of gallery name if the name is not present
-- Also loads the gallery's schematic file and calculates helper dimensions
local function CheckGallery(a_Gallery, a_Index)
-- Check if the name is given:
if (a_Gallery.Name == nil) then
LOGWARNING("Gallery #" .. a_Index .. " doesn't have a Name, disabling it.");
return false;
end
-- Check all required parameters by name and type:
for idx, param in ipairs(g_GalleryRequiredParams) do
local p = a_Gallery[param.Name];
if (p == nil) then
LOGWARNING("Gallery \"" .. a_Gallery.Name .. "\" is missing a required parameter '" .. param.Name .."'. Disabling the gallery.");
return false;
end
if (type(p) ~= param.Type) then
LOGWARNING("Gallery \"" .. a_Gallery.Name .. "\"'s parameter \"" .. param.Name .."\" is wrong type. Expected " .. param.Type .. ", got " .. type(p) ..". Disabling the gallery.");
return false;
end
end
-- Check all optional parameters' types:
for idx, param in ipairs(g_GalleryOptionalParams) do
local p = a_Gallery[param.Name];
if (p ~= nil) then
if (type(p) ~= param.Type) then
LOGWARNING("Gallery \"" .. a_Gallery.Name .. "\"'s parameter \"" .. param.Name .."\" is wrong type. Expected " .. param.Type .. ", got " .. type(p) ..". Disabling the gallery.");
return false;
end
end
end
-- Check the FillStrategy param:
local AllowedStrategies = {"x+z+", "x-z+", "x+z-", "x-z-", "z+x+", "z+x-", "z-x+", "z-x-"};
local ReqStrategy = a_Gallery.FillStrategy;
local IsStrategyValid = false;
for idx, strategy in ipairs(AllowedStrategies) do
if (ReqStrategy == strategy) then
IsStrategyValid = true;
end
end
if not(IsStrategyValid) then
LOGWARNING("Gallery \"" .. a_Gallery.Name .. "\"'s FillStrategy is not recognized. The gallery is disabled.");
return false;
end
-- Assign the world:
a_Gallery.World = cRoot:Get():GetWorld(a_Gallery.WorldName);
if (a_Gallery.World == nil) then
LOGWARNING("Gallery \"" .. a_Gallery.Name .. "\" specifies an unknown world '" .. a_Gallery.WorldName .. "\".");
return false;
end
-- Load the schematic, if requested:
local AreaTemplate = a_Gallery["AreaTemplate"];
if (AreaTemplate ~= nil) then
local Schematic = cBlockArea();
if (Schematic == nil) then
LOGWARNING("Cannot create the template schematic representation");
return true;
end
if not(Schematic:LoadFromSchematicFile(AreaTemplate)) then
LOGWARNING("Gallery \"" .. a_Gallery.Name .. "\"'s AreaTemplate failed to load from \"" .. AreaTemplate .. "\".");
return false;
end
a_Gallery.AreaTemplateSchematic = Schematic;
a_Gallery.AreaSizeX = Schematic:GetSizeX();
a_Gallery.AreaSizeZ = Schematic:GetSizeZ();
a_Gallery.AreaTop = Schematic:GetSizeY();
if (a_Gallery.AreaTop < 255) then
a_Gallery.AreaTemplateSchematicTop = cBlockArea();
a_Gallery.AreaTemplateSchematicTop:Create(a_Gallery.AreaSizeX, 255 - a_Gallery.AreaTop, a_Gallery.AreaSizeZ);
else
a_Gallery.AreaTemplateSchematicTop = nil
end
a_Gallery.TeleportCoordY = GetSchematicHighestNonAirBlock(Schematic) + 1;
else
-- If no schematic is given, the area sizes must be specified:
if ((a_Gallery.AreaSizeX == nil) or (a_Gallery.AreaSizeZ == nil)) then
LOGWARNING("Gallery \"" .. a_Gallery.Name .. "\" has neither AreaTemplate nor AreaSizeX / AreaSizeZ set.");
return false;
end
a_Gallery.TeleportCoordY = 256;
end
-- Calculate and check the number of areas per X / Z dimension:
a_Gallery.NumAreasPerX = math.floor((a_Gallery.MaxX - a_Gallery.MinX) / a_Gallery.AreaSizeX);
if (a_Gallery.NumAreasPerX <= 0) then
LOGWARNING("Gallery \"" .. a_Gallery.Name .. "\" has areas wider than will fit in the X direction:" ..
"AreaSizeX = " .. a_Gallery.AreaSizeX .. ", GallerySizeX = " .. tostring(a_Gallery.MaxX - a_Gallery.MinX));
return false;
end
a_Gallery.NumAreasPerZ = math.floor((a_Gallery.MaxZ - a_Gallery.MinZ) / a_Gallery.AreaSizeZ);
if (a_Gallery.NumAreasPerZ <= 0) then
LOGWARNING("Gallery \"" .. a_Gallery.Name .. "\" has areas wider than will fit in the Z direction:" ..
"AreaSizeZ = " .. a_Gallery.AreaSizeZ .. ", GallerySizeZ = " .. tostring(a_Gallery.MaxZ - a_Gallery.MinZ));
return false;
end
a_Gallery.MaxAreaIdx = a_Gallery.NumAreasPerX * a_Gallery.NumAreasPerZ;
-- Apply defaults:
a_Gallery.AreaEdge = a_Gallery.AreaEdge or 2;
if (a_Gallery.AreaSizeX <= a_Gallery.AreaEdge * 2) then
LOGWARNING("Gallery \"" .. a_Gallery.Name .. " has AreaEdge greater than X size: " ..
"AreaEdge = " .. a_Gallery.AreaEdge .. ", GallerySizeX = " .. a_Gallery.AreaSizeX .. ". Gallery is disabled");
return false;
end
if (a_Gallery.AreaSizeZ <= a_Gallery.AreaEdge * 2) then
LOGWARNING("Gallery \"" .. a_Gallery.Name .. " has AreaEdge greater than Z size: " ..
"AreaEdge = " .. a_Gallery.AreaEdge .. ", GallerySizeZ = " .. a_Gallery.AreaSizeZ .. ". Gallery is disabled");
return false;
end
-- Set the gallery's minimum and maximum area coords
-- (that is, minima and maxima rounded to the area coords)
if (a_Gallery.FillStrategy:find("x%+")) then
-- "x+" direction, the areas are calculated from the MinX side of the gallery
a_Gallery.AreaMinX = a_Gallery.MinX;
a_Gallery.AreaMaxX = a_Gallery.MinX + a_Gallery.AreaSizeX * a_Gallery.NumAreasPerX;
else
-- "x-" direction, the areas are calculated from the MaxX side of the gallery
a_Gallery.AreaMinX = a_Gallery.MaxX - a_Gallery.AreaSizeX * a_Gallery.NumAreasPerX;
a_Gallery.AreaMaxX = a_Gallery.MaxX;
end
if (a_Gallery.FillStrategy:find("z%+")) then
-- "z+" direction, the areas are calculated from the MinZ side of the gallery
a_Gallery.AreaMinZ = a_Gallery.MinZ;
a_Gallery.AreaMaxZ = a_Gallery.MinZ + a_Gallery.AreaSizeZ * a_Gallery.NumAreasPerZ;
else
-- "z-" direction, the areas are calculated from the MaxZ side of the gallery
a_Gallery.AreaMinZ = a_Gallery.MaxZ - a_Gallery.AreaSizeZ * a_Gallery.NumAreasPerZ;
a_Gallery.AreaMaxZ = a_Gallery.MaxZ;
end
-- Look up biome, if set, and convert to EMCSBiome enum:
if (a_Gallery.Biome ~= nil) then
local BiomeType = StringToBiome(a_Gallery.Biome);
if (BiomeType == biInvalidBiome) then
LOGWARNING("Gallery " .. a_Gallery.Name .. " has invalid Biome \"" .. a_Gallery.Biome .. "\"; biome support turned off.");
a_Gallery.Biome = nil;
else
a_Gallery.Biome = BiomeType;
end
end
-- All okay
return true;
end
--- Verifies that each gallery has all the minimum settings it needs
-- Returns an array+map of the accepted galleries
local function VerifyGalleries(a_Galleries)
-- Filter out galleries that are not okay:
local GalleriesOK = {}
for idx, gallery in ipairs(a_Galleries) do
if (CheckGallery(gallery, idx)) then
GalleriesOK[gallery.Name] = gallery
table.insert(GalleriesOK, gallery)
end
end
return GalleriesOK
end
--- Checks if g_Config has all the keys it needs, adds defaults for the missing ones
-- Returns the corrected configuration (but changes the one in the parameter as well)
local function VerifyConfig(a_Config)
a_Config.CommandPrefix = a_Config.CommandPrefix or "/gallery"
a_Config.DatabaseEngine = a_Config.DatabaseEngine or "sqlite"
a_Config.DatabaseParams = a_Config.DatabaseParams or {}
-- Check the WebPreview, if it doesn't have all the requirements, set it to nil to disable previewing:
if (a_Config.WebPreview) then
local schematicToPng = a_Config.WebPreview.MCSchematicToPng
if not(schematicToPng) then
LOGINFO("The config doesn't define WebPreview.MCSchematicToPng. Web preview is disabled.")
a_Config.WebPreview = nil
else
if (type(schematicToPng) ~= "table") then
LOGINFO(string.format("The config for WebPreview.MCSchematicToPng is wrong, table expected, got %s. Web preview is disabled.",
type(schematicToPng)
))
a_Config.WebPreview = nil
end
end
end
-- Apply the CommandPrefix - change the actual g_PluginInfo table:
a_Config.CommandPrefix = a_Config.CommandPrefix or "/gallery"
if (a_Config.CommandPrefix ~= "/gallery") then
g_PluginInfo.Commands[a_Config.CommandPrefix] = g_PluginInfo.Commands["/gallery"]
g_PluginInfo.Commands["/gallery"] = nil
end
return a_Config
end
--- Loads the galleries from the config file CONFIG_FILE
function LoadConfig()
if not(cFile:IsFile(CONFIG_FILE)) then
-- No file to read from, bail out with a log message
-- But first copy our example file to the folder, to let the admin know the format:
local PluginFolder = cPluginManager:Get():GetCurrentPlugin():GetLocalFolder()
local ExampleFile = CONFIG_FILE:gsub(".cfg", ".example.cfg");
cFile:Copy(PluginFolder .. "/example.cfg", ExampleFile);
LOGWARNING("The config file '" .. CONFIG_FILE .. "' doesn't exist. An example configuration file '" .. ExampleFile .. "' has been created for you.");
LOGWARNING("No galleries were loaded");
g_Config = VerifyConfig({})
g_Galleries = VerifyGalleries({})
return;
end
-- Load and compile the config file:
local cfg, err = loadfile(CONFIG_FILE);
if (cfg == nil) then
LOGWARNING("Cannot open '" .. CONFIG_FILE .. "': " .. (err or "<unknown error>"));
LOGWARNING("No galleries were loaded");
g_Config = VerifyConfig({})
g_Galleries = VerifyGalleries({})
return;
end
-- Execute the loaded file in a sandbox:
-- This is Lua-5.1-specific and won't work in Lua 5.2!
local Sandbox = {};
setfenv(cfg, Sandbox);
cfg();
-- Retrieve the values we want from the sandbox:
local Galleries, Config = Sandbox.Galleries, Sandbox.Config;
if (Galleries == nil) then
LOGWARNING("Galleries not found in the config file '" .. CONFIG_FILE .. "'. Gallery plugin inactive.");
Galleries = {};
end
if (Config == nil) then
LOGWARNING("Config not found in the config file '" .. CONFIG_FILE .. "'. Using defaults.");
Config = {}; -- Defaults will be inserted by VerifyConfig()
end
g_Config = VerifyConfig(Config)
g_Galleries = VerifyGalleries(Galleries)
end