Created
August 15, 2018 06:33
-
-
Save sanny-io/0f491f08578a063f619e0f09295f9839 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local base = _G | |
| require("string") | |
| require("table") | |
| module("config") | |
| settings = {} | |
| function loadFunction(fct) | |
| local old_mt = base.getmetatable(settings) | |
| local loader = { active = true } | |
| base.setmetatable(settings, | |
| { | |
| __index = function(table, key) | |
| local t = {} | |
| base.setmetatable(t, base.getmetatable(table)) | |
| if loader.active and base.rawget(table, key) == nil then base.rawset(table, key, t) end | |
| return base.rawget(table, key) | |
| end, | |
| }) | |
| base.setfenv(fct, settings) | |
| local ret, err = base.pcall(fct) | |
| loader.active = false | |
| if ret then return true | |
| else return false, err end | |
| end | |
| function loadString(str) | |
| local fct = base.loadstring(str) | |
| if fct then | |
| return loadFunction(fct) | |
| end | |
| return nil, "Could not load string" | |
| end | |
| function loadFile(file) | |
| local fct = base.loadfile(file) | |
| if fct then | |
| return loadFunction(fct) | |
| end | |
| return nil, "Could not load file" | |
| end | |
| load = loadFile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment