This commit is contained in:
Lars Mueller 2020-02-28 22:12:05 +01:00
parent b0b68c39bd
commit 358cdf5084
2 changed files with 41 additions and 43 deletions

@ -33,23 +33,7 @@ if _VERSION then
end end
end end
-- MT shorthands local function loadfile_exports(filename)
mt = minetest
MT = mt
-- TODO automatically know current mod
-- get modpath wrapper
function get_resource(modname, resource)
return minetest.get_modpath(modname) .. "/" .. resource
end
-- get resource + dofile
function include(modname, file)
dofile(get_resource(modname, file))
end
function loadfile_exports(filename)
local env = setmetatable({}, {__index = _G, __call = _G}) local env = setmetatable({}, {__index = _G, __call = _G})
local file = assert(loadfile(filename)) local file = assert(loadfile(filename))
setfenv(file, env) setfenv(file, env)
@ -57,33 +41,10 @@ function loadfile_exports(filename)
return env return env
end end
-- loadfile with table env -- TODO automatically know current mod
function include_class(classname, filename)
_G[classname] = setmetatable(_G[classname] or {}, {__index = _G, __call = _G})
local class = assert(loadfile(filename))
setfenv(class, _G[classname])
class()
return _G[classname]
end
-- runs main.lua in table env
function include_mod(modname)
include_class(modname, get_resource(modname, "main.lua"))
end
function extend_mod(modname, filename)
include_class(modname, get_resource(modname, filename .. ".lua"))
end
function extend_mod_string(modname, string)
_G[modname] = setmetatable(_G[modname] or {}, {__index = _G, __call = _G})
local string = assert(loadstring(string))
setfenv(string, _G[modname])
string()
return _G[modname]
end
local components = { local components = {
mod = {},
class = {class = "global"}, class = {class = "global"},
conf = {conf = "global"}, conf = {conf = "global"},
data = {data = "global"}, data = {data = "global"},
@ -93,7 +54,7 @@ local components = {
number = {number_ext = "global"}, number = {number_ext = "global"},
player = {player_ext = "global"}, player = {player_ext = "global"},
table = {table_ext = "global"}, table = {table_ext = "global"},
text = {string_ext = "global"}, text = {string_ext = "global", string = "local"},
threading = {threading_ext = "global"} threading = {threading_ext = "global"}
} }
@ -111,4 +72,6 @@ for component, additional in pairs(components) do
end end
end end
modlib.mod.loadfile_exports = loadfile_exports
_ml = modlib _ml = modlib

35
mod.lua Normal file

@ -0,0 +1,35 @@
-- get modpath wrapper
function get_resource(modname, resource)
return minetest.get_modpath(modname) .. "/" .. resource
end
-- get resource + dofile
function include(modname, file)
dofile(get_resource(modname, file))
end
-- loadfile with table env
function include_class(classname, filename)
_G[classname] = setmetatable(_G[classname] or {}, {__index = _G, __call = _G})
local class = assert(loadfile(filename))
setfenv(class, _G[classname])
class()
return _G[classname]
end
-- runs main.lua in table env
function include_mod(modname)
include_class(modname, get_resource(modname, "main.lua"))
end
function extend_mod(modname, filename)
include_class(modname, get_resource(modname, filename .. ".lua"))
end
function extend_mod_string(modname, string)
_G[modname] = setmetatable(_G[modname] or {}, {__index = _G, __call = _G})
local string = assert(loadstring(string))
setfenv(string, _G[modname])
string()
return _G[modname]
end