Move dir_delim to file module, add file.concat_path

This commit is contained in:
Lars Mueller 2021-11-06 18:30:57 +01:00
parent 277c076651
commit c02a83aee8
2 changed files with 12 additions and 5 deletions

@ -1,3 +1,4 @@
local dir_delim = ...
-- Localize globals -- Localize globals
local io, minetest, modlib, string = io, minetest, modlib, string local io, minetest, modlib, string = io, minetest, modlib, string
@ -5,10 +6,12 @@ local io, minetest, modlib, string = io, minetest, modlib, string
local _ENV = {} local _ENV = {}
setfenv(1, _ENV) setfenv(1, _ENV)
_ENV.dir_delim = dir_delim
function get_name(filepath) function get_name(filepath)
assert(#modlib.dir_delim == 1) return filepath:match(dir_delim .. "(.-)$") or filepath
return filepath:match(modlib.dir_delim .. "?(.-)$")
end end
function split_extension(filename) function split_extension(filename)
return filename:match"^(.*)%.(.*)$" return filename:match"^(.*)%.(.*)$"
end end

@ -81,13 +81,19 @@ if not minetest then
parent_dir = init_path and init_path:match"^.[/\\]" or "" parent_dir = init_path and init_path:match"^.[/\\]" or ""
end end
local dir_delim = rawget(_G, "DIR_DELIM") or (package and package.config and assert(package.config:match("^(.-)[\r\n]"))) or "/"
local function concat_path(path)
return table.concat(path, dir_delim)
end
-- only used if Minetest is available -- only used if Minetest is available
local function get_resource(modname, resource, ...) local function get_resource(modname, resource, ...)
if not resource then if not resource then
resource = modname resource = modname
modname = minetest.get_current_modname() modname = minetest.get_current_modname()
end end
return table.concat({minetest.get_modpath(modname), resource, ...}, modlib.dir_delim) return concat_path{minetest.get_modpath(modname), resource, ...}
end end
local rawget, rawset = rawget, rawset local rawget, rawset = rawget, rawset
@ -95,8 +101,6 @@ modlib = setmetatable({
-- TODO bump on release -- TODO bump on release
version = 73, version = 73,
modname = minetest and minetest.get_current_modname(), modname = minetest and minetest.get_current_modname(),
-- TODO
dir_delim = rawget(_G, "DIR_DELIM") or "/",
_RG = setmetatable({}, { _RG = setmetatable({}, {
__index = function(_, index) __index = function(_, index)
return rawget(_G, index) return rawget(_G, index)