Redo environments

This commit is contained in:
Lars Mueller 2021-06-17 19:45:08 +02:00
parent 0315a77696
commit 4175974def
30 changed files with 264 additions and 116 deletions

16
b3d.lua

@ -1,10 +1,11 @@
-- Localize globals
local assert, error, getfenv, math, modlib, next, pairs, rawget, setmetatable, table = assert, error, getfenv, math, modlib, next, pairs, rawget, setmetatable, table
local assert, error, math, modlib, next, pairs, setmetatable, table = assert, error, math, modlib, next, pairs, setmetatable, table
local class = getfenv(1)
local metatable = {__index = function(_self, key)
return rawget(class, key)
end}
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
local metatable = {__index = _ENV}
--! experimental
--+ Reads a single BB3D chunk from a stream
@ -363,4 +364,7 @@ function get_animated_bone_properties(self, keyframe, interpolate)
end
get_props(self.node)
return bone_properties
end
end
-- Export environment
return _ENV

@ -1,6 +1,10 @@
-- Localize globals
local assert, math = assert, math
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
-- All little endian
--+ Reads doubles (f64) or floats (f32)
@ -115,4 +119,7 @@ end
function write_double(write_byte, number)
return write_float(write_byte, number, nil, true)
end
end
-- Export environment
return _ENV

@ -1,15 +1,16 @@
-- Localize globals
local assert, error, getfenv, ipairs, math, modlib, next, pairs, rawget, setmetatable, string, table, unpack = assert, error, getfenv, ipairs, math, modlib, next, pairs, rawget, setmetatable, string, table, unpack
local assert, error, ipairs, math, modlib, next, pairs, setmetatable, string, table, type, unpack = assert, error, ipairs, math, modlib, next, pairs, setmetatable, string, table, type, unpack
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
--! experimental
local bluon = getfenv(1)
local no_op = modlib.func.no_op
local write_float = modlib.binary.write_float
local metatable = {__index = function(_self, key)
return rawget(bluon, key)
end}
local metatable = {__index = _ENV}
function new(self)
return setmetatable(self or {}, metatable)
@ -326,4 +327,7 @@ function read(self, stream)
return
end
return _read(type)
end
end
-- Export environment
return _ENV

@ -1,6 +1,10 @@
-- Localize globals
local assert, dump, error, ipairs, minetest, modlib, pairs, pcall, table, tonumber, type = assert, dump, error, ipairs, minetest, modlib, pairs, pcall, table, tonumber, type
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
-- not deprecated
function build_tree(dict)
local tree = {}
@ -296,4 +300,7 @@ function check_constraints(value, constraints)
error(possible_errors)
end
end
end
end
-- Export environment
return _ENV

@ -1,6 +1,10 @@
-- Localize globals
local _G, debug, getfenv, table, tostring = _G, debug, getfenv, table, tostring
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
function variables(stacklevel)
stacklevel = (stacklevel or 1) + 1
local locals = {}
@ -43,4 +47,7 @@ function stack(stacklevel)
stacklevel = stacklevel + 1
end
return stack
end
end
-- Export environment
return _ENV

@ -1,6 +1,10 @@
-- Localize globals
local io, minetest, modlib, string = io, minetest, modlib, string
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
function read(filename)
local file = io.open(filename, "r")
if file == nil then return nil end
@ -107,4 +111,7 @@ end
function process_bridge_start(name, command, os_execute)
local bridge = process_bridges[name]
os_execute(string.format(command, bridge.output, bridge.input, bridge.logs))
end
end
-- Export environment
return _ENV

@ -1,6 +1,10 @@
-- Localize globals
local error, modlib, unpack = error, modlib, unpack
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
no_op = function() end
function curry(func, ...)
@ -113,4 +117,7 @@ end
function or_(a, b)
return a or b
end
end
-- Export environment
return _ENV

@ -1,7 +1,11 @@
-- Localize globals
local getfenv, math, setmetatable, table = getfenv, math, setmetatable, table
local math, setmetatable, table = math, setmetatable, table
local metatable = {__index = getfenv(1)}
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
local metatable = {__index = _ENV}
function less_than(a, b) return a < b end
@ -50,4 +54,7 @@ function pop(self)
end
heapify(1)
return value
end
end
-- Export environment
return _ENV

@ -1,6 +1,5 @@
-- Localize globals
local _G, _VERSION, assert, debug, dump, error, load, loadfile, minetest, pairs, rawget, rawset, setmetatable, table, type = _G, _VERSION, assert, debug, dump, error, load, loadfile, minetest, pairs, rawget, rawset, setmetatable, table, type
local _G, _VERSION, assert, debug, dump, error, load, loadfile, minetest, pairs, rawget, rawset, setmetatable, table = _G, _VERSION, assert, debug, dump, error, load, loadfile, minetest, pairs, rawget, rawset, setmetatable, table
-- Lua version check
if _VERSION then
@ -58,33 +57,40 @@ for _, file in pairs{
"persistence",
"debug"
} do
modules[file] = file
modules[file] = true
end
if minetest then
modules.minetest = {
"misc",
"collisionboxes",
"liquid",
"raycast",
"wielditem_change",
"colorspec",
"schematic"
}
for _, file in pairs{
"minetest",
"log",
"player",
-- deprecated
"conf"
} do
modules[file] = file
modules[file] = true
end
end
local load_module, get_resource, loadfile_exports
local parent_dir
if not minetest then
local init_path = arg and arg[0]
parent_dir = init_path and init_path:match"^.[/\\]" or ""
end
-- only used if Minetest is available
local function get_resource(modname, resource, ...)
if not resource then
resource = modname
modname = minetest.get_current_modname()
end
return table.concat({minetest.get_modpath(modname), resource, ...}, modlib.dir_delim)
end
modlib = setmetatable({
-- TODO bump on release
version = 69,
modname = minetest and minetest.get_current_modname(),
-- TODO
dir_delim = rawget(_G, "DIR_DELIM") or "/",
_RG = setmetatable({}, {
__index = function(_, index)
@ -101,55 +107,19 @@ modlib = setmetatable({
end
}, {
__index = function(self, module_name)
local files = modules[module_name]
local environment
if type(files) == "string" then
environment = load_module(files)
elseif files then
environment = loadfile_exports(get_resource(self.modname, module_name, files[1] .. ".lua"))
for index = 2, #files do
self.mod.include_env(get_resource(self.modname, module_name, files[index] .. ".lua"), environment)
end
else
if not modules[module_name] then
-- module not available, return nil
return
end
local module = {}
for key, value in pairs(environment) do
-- Shallow copy. Doesn't use `modlib.table.shallowcopy` as that is part of a module, too.
module[key] = value
end
self[module_name] = module
return module
local environment = dofile(minetest
and get_resource(self.modname, module_name .. ".lua")
or (parent_dir .. module_name .. ".lua"))
self[module_name] = environment
return environment
end
})
function get_resource(modname, resource, ...)
if not resource then
resource = modname
modname = minetest.get_current_modname()
end
return table.concat({minetest.get_modpath(modname), resource, ...}, modlib.dir_delim)
end
function loadfile_exports(filename)
local env = setmetatable({}, {__index = _G})
local file = assert(loadfile(filename))
setfenv(file, env)
file()
return env
end
local parent_dir
if not minetest then
local init_path = arg and arg[0]
parent_dir = init_path and init_path:match"^.[/\\]" or ""
end
function load_module(module_name)
local file = module_name .. ".lua"
return loadfile_exports(minetest and get_resource(modlib.modname, file) or (parent_dir .. file))
end
modlib.mod = minetest and loadfile_exports(get_resource(modlib.modname, "mod.lua"))
modlib.mod = minetest and dofile(get_resource(modlib.modname, "mod.lua"))
-- Aliases
modlib.string = modlib.text
@ -157,15 +127,15 @@ modlib.number = modlib.math
if minetest then
modlib.conf.build_setting_tree()
modlib.mod.get_resource = get_resource
modlib.mod.loadfile_exports = loadfile_exports
end
_ml = modlib
modlib.mod.include"test.lua"
--[[
--modlib.mod.include"test.lua"
]]
-- TODO verify localizations suffice
return modlib

@ -1,10 +1,11 @@
-- Localize globals
local assert, getfenv, math, modlib, rawget, setmetatable, table, unpack = assert, getfenv, math, modlib, rawget, setmetatable, table, unpack
local assert, math, modlib, setmetatable, table, unpack = assert, math, modlib, setmetatable, table, unpack
local class = getfenv(1)
local metatable = {__index = function(_self, key)
return rawget(class, key)
end}
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
local metatable = {__index = _ENV}
distance = modlib.vector.distance
@ -57,4 +58,7 @@ function get_nearest_neighbor(self, vector)
return nearest_neighbor, min_distance
end
-- TODO insertion & deletion + rebalancing
-- TODO insertion & deletion + rebalancing
-- Export environment
return _ENV

@ -1,6 +1,10 @@
-- Localize globals
local ipairs, minetest, modlib, os, pairs, table = ipairs, minetest, modlib, os, pairs, table
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
-- Log helpers - write to log, force writing to file
minetest.mkdir(minetest.get_worldpath() .. "/logs")
channels = {}
@ -77,3 +81,7 @@ minetest.register_on_shutdown(
write_all_to_file()
end
)
-- Export environment
return _ENV

@ -1,6 +1,10 @@
-- Localize globals
local math, minetest, modlib, os, string, table = math, minetest, modlib, os, string, table
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
-- Make random random
math.randomseed(minetest and minetest.get_us_time() or os.time() + os.clock())
for _ = 1, 100 do math.random() end
@ -71,4 +75,7 @@ function fround(number)
return sign * math.huge
end
return sign * powexp * (leading - mantissa / 0x800000)
end
end
-- Export environment
return _ENV

12
minetest.lua Normal file

@ -0,0 +1,12 @@
local _ENV = setmetatable({}, {__index = _G})
local function load(filename)
assert(loadfile(modlib.mod.get_resource(modlib.modname, "minetest", filename .. ".lua")))(_ENV)
end
load"misc"
load"collisionboxes"
load"liquid"
load"raycast"
load"wielditem_change"
load"colorspec"
load"schematic"
return _ENV

@ -1,6 +1,10 @@
-- Localize globals
local assert, ipairs, math, minetest, pairs, table, type, vector = assert, ipairs, math, minetest, pairs, table, type, vector
-- Set environment
local _ENV = ...
setfenv(1, _ENV)
-- Minetest allows shorthand collisionbox = {...} instead of {{...}}
local function get_collisionboxes(box_or_boxes)
return type(box_or_boxes[1]) == "number" and {box_or_boxes} or box_or_boxes

@ -1,6 +1,10 @@
-- Localize globals
local assert, error, math, minetest, setmetatable, tonumber, type = assert, error, math, minetest, setmetatable, tonumber, type
-- Set environment
local _ENV = ...
setfenv(1, _ENV)
-- As in src/util/string.cpp
named_colors = {
aliceblue = 0xf0f8ff,

@ -1,6 +1,10 @@
-- Localize globals
local minetest, modlib, pairs = minetest, modlib, pairs
-- Set environment
local _ENV = ...
setfenv(1, _ENV)
liquid_level_max = 8
--+ Calculates the corner levels of a flowingliquid node
--> 4 corner levels from -0.5 to 0.5 as list of `modlib.vector`

@ -1,6 +1,10 @@
-- Localize globals
local assert, minetest, modlib, next, pairs, string, table, type = assert, minetest, modlib, next, pairs, string, table, type
-- Set environment
local _ENV = ...
setfenv(1, _ENV)
max_wear = 2 ^ 16 - 1
function override(function_name, function_builder)
local func = minetest[function_name]

@ -1,6 +1,10 @@
-- Localize globals
local assert, math, minetest, modlib, pairs, setmetatable, vector = assert, math, minetest, modlib, pairs, setmetatable, vector
-- Set environment
local _ENV = ...
setfenv(1, _ENV)
--+ Raycast wrapper with proper flowingliquid intersections
function raycast(_pos1, _pos2, objects, liquids)
local raycast = minetest.raycast(_pos1, _pos2, objects, liquids)

@ -1,6 +1,10 @@
-- Localize globals
local VoxelArea, assert, error, io, ipairs, math, minetest, modlib, next, pairs, setmetatable, table, vector = VoxelArea, assert, error, io, ipairs, math, minetest, modlib, next, pairs, setmetatable, table, vector
-- Set environment
local _ENV = ...
setfenv(1, _ENV)
schematic = {}
local metatable = {__index = schematic}

@ -1,6 +1,10 @@
-- Localize globals
local minetest, modlib, pairs, table = minetest, modlib, pairs, table
-- Set environment
local _ENV = ...
setfenv(1, _ENV)
players = {}
registered_on_wielditem_changes = {function(...)

17
mod.lua

@ -1,6 +1,18 @@
-- Localize globals
local Settings, _G, assert, dofile, error, getmetatable, ipairs, loadfile, loadstring, minetest, modlib, pairs, rawget, rawset, setfenv, setmetatable, tonumber, type = Settings, _G, assert, dofile, error, getmetatable, ipairs, loadfile, loadstring, minetest, modlib, pairs, rawget, rawset, setfenv, setmetatable, tonumber, type
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
function loadfile_exports(filename)
local env = setmetatable({}, {__index = _G})
local file = assert(loadfile(filename))
setfenv(file, env)
file()
return env
end
-- get resource + dofile
function include(modname, file)
if not file then
@ -131,4 +143,7 @@ function configuration(modname)
return schema:load({}, {error_message = true})
end
return conf
end
end
-- Export environment
return _ENV

@ -1,5 +1,9 @@
-- Localize globals
local assert, error, io, ipairs, loadfile, math, minetest, modlib, pairs, setfenv, setmetatable, type = assert, error, io, ipairs, loadfile, math, minetest, modlib, pairs, setfenv, setmetatable, type
local assert, error, io, ipairs, loadfile, math, minetest, modlib, pairs, setfenv, setmetatable, table, type = assert, error, io, ipairs, loadfile, math, minetest, modlib, pairs, setfenv, setmetatable, table, type
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
lua_log_file = {}
local files = {}
@ -161,4 +165,7 @@ function lua_log_file:rewrite()
end
self:_rewrite()
self:open()
end
end
-- Export environment
return _ENV

@ -1,6 +1,10 @@
-- Localize globals
local ipairs, minetest, modlib = ipairs, minetest, modlib
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
forbidden_names = {}
function register_forbidden_name(name) forbidden_names[name] = true end
@ -78,4 +82,7 @@ function datatable(table, default)
end)
minetest.register_on_leaveplayer(function(player) table[player:get_player_name()] = nil end)
return table
end
end
-- Export environment
return _ENV

@ -1,6 +1,10 @@
-- Localize globals
local math, modlib, pairs, unpack, vector = math, modlib, pairs, unpack, vector
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
-- TODO OOP, extend vector
function from_euler_rotation(rotation)
@ -129,4 +133,7 @@ function to_euler_rotation_irrlicht(self)
end
return vector.apply(_calc(), math.deg)
end
end
-- Export environment
return _ENV

@ -1,10 +1,11 @@
-- Localize globals
local assert, getfenv, modlib, pairs, rawget, setmetatable, table = assert, getfenv, modlib, pairs, rawget, setmetatable, table
local assert, modlib, pairs, setmetatable, table = assert, modlib, pairs, setmetatable, table
local class = getfenv(1)
local metatable = {__index = function(_self, key)
return rawget(class, key)
end}
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
local metatable = {__index = _ENV}
comparator = modlib.table.default_comparator
@ -314,4 +315,7 @@ function get_by_rank(self, rank)
tree = right
end
until not tree
end
end
-- Export environment
return _ENV

@ -1,10 +1,11 @@
-- Localize globals
local assert, error, getfenv, ipairs, math, minetest, modlib, pairs, rawget, setmetatable, table, tonumber, tostring, type = assert, error, getfenv, ipairs, math, minetest, modlib, pairs, rawget, setmetatable, table, tonumber, tostring, type
local assert, error, ipairs, math, minetest, modlib, pairs, setmetatable, table, tonumber, tostring, type = assert, error, ipairs, math, minetest, modlib, pairs, setmetatable, table, tonumber, tostring, type
local class = getfenv(1)
local metatable = {__index = function(_self, key)
return rawget(class, key)
end}
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
local metatable = {__index = _ENV}
function new(def)
-- TODO type inference, sanity checking etc.
@ -298,4 +299,7 @@ function load(self, override, params)
end
if self.func then self.func(override) end
return override
end
end
-- Export environment
return _ENV

@ -1,6 +1,10 @@
-- Localize globals
local assert, error, ipairs, math, next, pairs, rawget, rawset, setmetatable, string, table, type = assert, error, ipairs, math, next, pairs, rawget, rawset, setmetatable, string, table, type
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
-- Table helpers
function map_index(table, func)
@ -589,4 +593,7 @@ function repetition(value, count)
table[index] = value
end
return table
end
end
-- Export environment
return _ENV

@ -1,6 +1,10 @@
-- Localize globals
local math, modlib, pairs, setmetatable, string, table = math, modlib, pairs, setmetatable, string, table
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
function upper_first(text) return text:sub(1, 1):upper() .. text:sub(2) end
function lower_first(text) return text:sub(1, 1):lower() .. text:sub(2) end
@ -166,4 +170,7 @@ function handle_ifdefs(code, vars)
end
table.insert(finalcode, string.sub(code, after_endif + 2))
return table.concat(finalcode, "")
end
end
-- Export environment
return _ENV

@ -1,10 +1,11 @@
-- Localize globals
local getfenv, math, next, pairs, rawget, setmetatable, string, table, unpack = getfenv, math, next, pairs, rawget, setmetatable, string, table, unpack
local math, next, pairs, setmetatable, string, table, unpack = math, next, pairs, setmetatable, string, table, unpack
local class = getfenv(1)
local metatable = {__index = function(_self, key)
return rawget(class, key)
end}
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
local metatable = {__index = _ENV}
function new(table) return setmetatable(table or {}, metatable) end
@ -124,4 +125,7 @@ function find_longest(self, query, query_offset)
end
end
return last_leaf, leaf_pos
end
end
-- Export environment
return _ENV

@ -1,8 +1,11 @@
-- Localize globals
local assert, getfenv, math, modlib, pairs, rawget, rawset, setmetatable, unpack, vector = assert, getfenv, math, modlib, pairs, rawget, rawset, setmetatable, unpack, vector
local assert, math, modlib, pairs, rawset, setmetatable, unpack, vector = assert, math, modlib, pairs, rawset, setmetatable, unpack, vector
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
local mt_vector = vector
local class = getfenv(1)
index_aliases = {
x = 1,
@ -19,9 +22,10 @@ metatable = {
if index ~= nil then
return table[index]
end
return rawget(class, key)
return _ENV[key]
end,
__newindex = function(table, key, value)
-- TODO
local index = letters[key]
if index ~= nil then
return rawset(table, index, value)
@ -225,4 +229,7 @@ function triangle_normal(triangle)
local point_1, point_2, point_3 = unpack(triangle)
local edge_1, edge_2 = subtract(point_2, point_1), subtract(point_3, point_1)
return normalize(cross3(edge_1, edge_2))
end
end
-- Export environment
return _ENV