modlib/mod.lua

55 lines
1.6 KiB
Lua
Raw Normal View History

2020-02-28 22:12:05 +01:00
-- get modpath wrapper
function get_resource(modname, resource)
if not resource then
resource = modname
modname = minetest.get_current_modname()
end
2020-02-28 22:12:05 +01:00
return minetest.get_modpath(modname) .. "/" .. resource
end
-- get resource + dofile
function include(modname, file)
if not file then
file = modname
modname = minetest.get_current_modname()
end
2020-02-28 22:12:05 +01:00
dofile(get_resource(modname, file))
end
function include_env(file_or_string, env, is_string)
setfenv(assert((is_string and loadstring or loadfile)(file_or_string)), env)()
end
function create_namespace(namespace_name, parent_namespace)
namespace_name = namespace_name or minetest.get_current_modname()
2020-03-23 20:20:43 +01:00
parent_namespace = parent_namespace or _G
local namespace = setmetatable({}, {__index = parent_namespace})
2020-11-15 11:28:05 +01:00
-- should use rawset if MT's strictness wasn't disabled in init.lua
parent_namespace[namespace_name] = namespace
return namespace
end
-- formerly extend_mod
function extend(modname, file)
if not file then
file = modname
modname = minetest.get_current_modname()
end
include_env(get_resource(modname, file .. ".lua"), _G[modname])
2020-02-28 22:12:05 +01:00
end
-- runs main.lua in table env
2020-02-29 12:55:02 +01:00
-- formerly include_mod
function init(modname)
modname = modname or minetest.get_current_modname()
create_namespace(modname)
extend(modname, "main")
2020-02-28 22:12:05 +01:00
end
2020-02-29 12:55:02 +01:00
function extend_string(modname, string)
if not string then
string = modname
modname = minetest.get_current_modname()
end
include_env(string, _G[modname], true)
2020-02-28 22:12:05 +01:00
end