mtinfo/common.lua

62 lines
1.3 KiB
Lua
Raw Normal View History

2020-01-06 13:31:18 +01:00
2020-01-10 08:10:30 +01:00
function mtinfo.map_list(target, list, keys, filter)
filter = filter or function()
-- show all
return true
end
2020-01-06 13:31:18 +01:00
for name, def in pairs(list) do
2020-01-10 08:10:30 +01:00
if filter(def) then
local item = {}
for _, key in ipairs(keys) do
item[key] = def[key]
end
target[name] = item
2020-01-06 13:31:18 +01:00
end
end
end
function mtinfo.copyfile(src, target)
local infile = io.open(src, "r")
local instr = infile:read("*a")
infile:close()
2020-01-10 07:58:48 +01:00
if not instr then
return
end
2020-01-06 14:29:14 +01:00
local outfile, err = io.open(target, "w")
2020-01-06 13:31:18 +01:00
if not outfile then
2020-01-06 14:29:14 +01:00
error("File " .. target .. " could not be opened for writing! " .. err or "")
2020-01-06 13:31:18 +01:00
end
outfile:write(instr)
outfile:close()
end
2020-01-06 14:29:14 +01:00
function mtinfo.copyrecursive(src, target)
minetest.mkdir(target)
local file_list = minetest.get_dir_list(src, false)
for _, filename in pairs(file_list) do
mtinfo.copyfile(src .. "/" .. filename, target .. "/" .. filename)
end
local dir_list = minetest.get_dir_list(src, true)
for _, filename in pairs(dir_list) do
minetest.mkdir(target .. "/" .. filename)
mtinfo.copyrecursive(src .. "/" .. filename, target .. "/" .. filename)
end
end
function mtinfo.export_json(fname, data, varname)
2020-01-06 13:31:18 +01:00
local f = io.open(fname, "w")
local data_string, err = minetest.write_json(data)
if err then
error(err)
end
f:write(varname .. "=")
2020-01-06 13:31:18 +01:00
f:write(data_string)
f:write(";")
2020-01-06 13:31:18 +01:00
io.close(f)
end