From 94e38087e6d98ccab95d92d3b23faa9e708d2f31 Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Sat, 29 Feb 2020 12:55:02 +0100 Subject: [PATCH] rolling-7 --- conf.lua | 2 +- init.lua | 27 ++++++++++++++++----------- mod.lua | 15 +++++++++------ table.lua | 6 +++--- text.lua | 8 ++++---- 5 files changed, 33 insertions(+), 25 deletions(-) diff --git a/conf.lua b/conf.lua index 37fd418..3f606a7 100644 --- a/conf.lua +++ b/conf.lua @@ -17,7 +17,7 @@ function load_or_create(filename, replacement_file, constraints) return load(filename, constraints) end function import(modname,constraints) - return load_or_create(get_path(modname)..".json",get_resource(modname, "default_config.json"),constraints) + return load_or_create(get_path(modname)..".json", modlib.mod.get_resource(modname, "default_config.json"),constraints) end function check_constraints(value, constraints) local t=type(value) diff --git a/init.lua b/init.lua index 68c296a..c004474 100644 --- a/init.lua +++ b/init.lua @@ -33,6 +33,11 @@ if _VERSION then end end +-- get modpath wrapper +local function get_resource(modname, resource) + return minetest.get_modpath(modname) .. "/" .. resource +end + local function loadfile_exports(filename) local env = setmetatable({}, {__index = _G, __call = _G}) local file = assert(loadfile(filename)) @@ -45,17 +50,17 @@ end local components = { mod = {}, - class = {class = "global"}, - conf = {conf = "global"}, - data = {data = "global"}, - file = {file_ext = "global"}, - log = {log = "global"}, - minetest = {mt_ext = "global"}, - number = {number_ext = "global"}, - player = {player_ext = "global"}, - table = {table_ext = "global"}, - text = {string_ext = "global", string = "local"}, - threading = {threading_ext = "global"} + class = {}, + conf = {}, + data = {}, + file = {}, + log = {}, + minetest = {}, + number = {}, + player = {}, + table = {}, + text = {string = "local"}, + threading = {} } modlib = {} diff --git a/mod.lua b/mod.lua index d227a37..9f40cdb 100644 --- a/mod.lua +++ b/mod.lua @@ -9,7 +9,7 @@ function include(modname, file) end -- loadfile with table env -function include_class(classname, filename) +function include_namespace(classname, filename) _G[classname] = setmetatable(_G[classname] or {}, {__index = _G, __call = _G}) local class = assert(loadfile(filename)) setfenv(class, _G[classname]) @@ -18,15 +18,18 @@ function include_class(classname, filename) end -- runs main.lua in table env -function include_mod(modname) - include_class(modname, get_resource(modname, "main.lua")) +-- formerly include_mod +function init(modname) + include_namespace(modname, get_resource(modname, "main.lua")) end -function extend_mod(modname, filename) - include_class(modname, get_resource(modname, filename .. ".lua")) +-- formerly extend_mod +function extend(modname, filename) + include_namespace(modname, get_resource(modname, filename .. ".lua")) end -function extend_mod_string(modname, string) +-- formerly extend_mod_string +function extend_string(modname, string) _G[modname] = setmetatable(_G[modname] or {}, {__index = _G, __call = _G}) local string = assert(loadstring(string)) setfenv(string, _G[modname]) diff --git a/table.lua b/table.lua index 3eea176..e370e4c 100644 --- a/table.lua +++ b/table.lua @@ -150,7 +150,7 @@ function unique(table) for val in ipairs(table) do lookup[val] = true end - return table_ext.keys(lookup) + return keys(lookup) end function rpairs(t) @@ -185,11 +185,11 @@ function best_value(table, is_better_fnc) end function min(table) - return table_ext.best_value(table, function(v, m) return v < m end) + return best_value(table, function(v, m) return v < m end) end function max(table) - return table_ext.best_value(table, function(v, m) return v > m end) + return best_value(table, function(v, m) return v > m end) end function binary_search(list, value) diff --git a/text.lua b/text.lua index e9de0e4..601b52c 100644 --- a/text.lua +++ b/text.lua @@ -37,7 +37,7 @@ function trim_begin(str, to_remove) end function split(str, delim, limit) - if not limit then return string_ext.split_without_limit(str, delim) end + if not limit then return split_without_limit(str, delim) end local parts = {} local occurences = 1 local last_index = 1 @@ -72,8 +72,8 @@ letter_a = string.byte("A") letter_f = string.byte("F") function is_hexadecimal(byte) - return (byte >= zero and byte <= string_ext.nine) or - (byte >= string_ext.letter_a and byte <= string_ext.letter_f) + return (byte >= zero and byte <= nine) or + (byte >= letter_a and byte <= letter_f) end magic_chars = { @@ -81,7 +81,7 @@ magic_chars = { } function escape_magic_chars(text) - for _, magic_char in ipairs(string_ext.magic_chars) do + for _, magic_char in ipairs(magic_chars) do text = string.gsub(text, "%" .. magic_char, "%%" .. magic_char) end return text