-
Notifications
You must be signed in to change notification settings - Fork 1
/
loadSettings.lua
51 lines (42 loc) · 1.37 KB
/
loadSettings.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
local exertion = select(1, ...) or exertion;
local MOD_SETTINGS_FILE =
exertion.MOD_PATH .. "/settings.lua";
local WORLD_SETTINGS_FILE =
minetest.get_worldpath() .. "/" .. exertion.MOD_NAME .. "_settings.lua";
local settings = {};
local function loadSettingsFile(filePath)
-- test for existence/readability
local file = io.open(filePath, 'r');
if not file then return nil; end;
file:close();
local chunk, err = loadfile(filePath);
return chunk or error(err);
end;
local modSettingsFunc = loadSettingsFile(MOD_SETTINGS_FILE);
local worldSettingsFunc = loadSettingsFile(WORLD_SETTINGS_FILE);
if not modSettingsFunc and not worldSettingsFunc then return settings; end;
-- Setting any "global" variable in the settings files actually modifies the
-- settings table (unless the variable is accessed through another existing
-- table like _G).
local settingsEnv =
setmetatable(
{},
{
__index = function(self, key)
local v = settings[key];
if v ~= nil then return v; else return _G[key]; end;
end,
__newindex = function(self, key, value)
settings[key] = value;
return true;
end,
});
if modSettingsFunc then
setfenv(modSettingsFunc, settingsEnv);
modSettingsFunc(settings);
end;
if worldSettingsFunc then
setfenv(worldSettingsFunc, settingsEnv);
worldSettingsFunc(settings);
end;
return settings;