init.lua
local IE = minetest.request_insecure_environment()
local has_IE = IE ~= nil
dslib = {}
local dslib_version = "0.3.0"
dslib.version = dslib_version
dslib.internal = {}
if has_IE then
IE.dslib_ie = {} IE.dslib_ie.version = dslib_version
IE.dslib_ie.internal = {}
end
local submodules = {
["dslib:raw_buffer"] = {
path = "src/raw_buffer.lua",
needs_IE = true,
sha256sum = "5baf39067bb42b7ddacbb413db5ec11a785f4e990005f6a9a6336ca014127811",
experimental = true,
},
["dslib:endian_helpers"] = {
path = "src/endian_helpers.lua",
needs_IE = false,
experimental = true,
},
["dslib:new_luajit_stuff"] = {
path = "src/new_luajit_stuff.lua",
needs_IE = true,
sha256sum = "33a70464f995aa4280e942d4bd1bdf368145c9580ef973eacd46ffe43de32ac2",
experimental = true,
},
["dslib:start_end"] = {
path = "src/start_end.lua",
needs_IE = false,
experimental = true,
},
["dslib:rotnum"] = {
path = "src/rotnum.lua",
needs_IE = false,
experimental = true,
},
["dslib:fmt"] = {
path = "src/fmt.lua",
needs_IE = false,
experimental = true,
},
}
local skip_sha256_sums = false
if has_IE then end
dslib.internal.load_experimental_untrusted_modules = true
if skip_sha256_sums then
minetest.log("warning", "dslib: skip_sha256_sums is set to true.")
end
if has_IE and IE.dslib_ie.internal.load_experimental_trusted_modules then
minetest.log("warning", "dslib: load_experimental_trusted_modules is set to true.")
end
local error = has_IE and IE.error or _G.error local assert = has_IE and IE.assert or _G.assert
local pairs = has_IE and IE.pairs or _G.pairs
local type = has_IE and IE.type or _G.type
local string_format = has_IE and IE.string.format or _G.string.format
local dslib_modpath = minetest.get_modpath("dslib")
assert(type(dslib_modpath) == "string")
local mmodules = _G.dofile(dslib_modpath .. "/src/mmodules.lua")
mmodules.add_module_by_value("dslib:mmodules", mmodules)
dslib.mrequire = mmodules.mrequire
while has_IE do function IE.dslib_ie.internal.require_with_IE_env(...)
IE.debug.sethook()
local old_thread_env = IE.getfenv(0)
local old_string_metatable = IE.debug.getmetatable("")
IE.setfenv(0, IE)
IE.debug.setmetatable("", {__index = IE.string})
local ok, ret = IE.pcall(IE.require, ...)
IE.setfenv(0, old_thread_env)
IE.debug.setmetatable("", old_string_metatable)
if not ok then
error(ret)
end
return ret
end
local ffi = IE.dslib_ie.internal.require_with_IE_env("ffi") if not ffi then
break
end
IE.dslib_ie.internal.ffi = ffi
local ssl = ffi.load("ssl", false)
ffi.cdef([[
unsigned char *SHA256(const unsigned char *d, size_t n, unsigned char *md);
]])
local SHA256_DIGEST_LENGTH = 32
function IE.dslib_ie.internal.sha256sum(str)
IE.assert(IE.type(str) == "string")
local s_sum = ssl.SHA256(str, #str, nil)
local sum_t = {}
for i = 0, SHA256_DIGEST_LENGTH - 1 do
sum_t[i+1] = IE.bit.tohex(s_sum[i], 2)
end
return IE.table.concat(sum_t)
end
break
end
local function my_module_loader(module_name)
local module_info = submodules[module_name]
if not module_info then
error("can not happen")
end
if module_info.experimental then
if not module_info.needs_IE then
if not dslib.internal.load_experimental_untrusted_modules then
return false, "Tried to load (untrusted) experimental module."
end
else
if not IE.dslib_ie.internal.load_experimental_trusted_modules then
return false, "Tried to load (trusted) experimental module."
end
end
end
local path = dslib_modpath .. "/" .. module_info.path
if not module_info.needs_IE then
return assert(_G.loadfile(path))({})
end
IE.debug.sethook()
if not has_IE then
return false, "Module needs the insecure environment, but dslib hasn't."
end
local file = IE.io.open(path, "r")
local code = file:read("*a")
file:close()
assert(type(code) == "string")
if not skip_sha256_sums then
if not module_info.sha256sum then
error(string_format("sha256-sum missing for trusted module.",
module_name))
end
local hash = IE.dslib_ie.internal.sha256sum(code)
if hash ~= module_info.sha256sum then
error(string_format("Wrong sha256-sum (%s instead of %s).",
hash, module_info.sha256sum))
end
end
local chunkname_path = path
if #chunkname_path > 20 then
chunkname_path = "..."..IE.string.sub(chunkname_path, -20)
end
local chunkname = string_format("[%s] %s", module_name, chunkname_path)
return assert(IE.loadstring(code, chunkname))({IE = IE})
end
for module_name, _ in pairs(submodules) do
mmodules.add_module_by_loader(module_name, my_module_loader)
end