mtinfo/common.lua

75 lines
1.7 KiB
Lua
Raw Normal View History

2020-01-06 13:31:18 +01:00
2021-03-24 18:20:36 +01:00
function mtinfo.map_list(target, list, keys, filter, enhancefn)
2020-01-10 08:10:30 +01:00
filter = filter or function()
-- show all
return true
end
2021-03-24 18:20:36 +01:00
-- no-op
enhancefn = enhancefn or function() 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
2020-12-03 14:35:40 +01:00
if def[key] and def[key] ~= 0 then
2020-12-03 14:30:13 +01:00
-- only export fields that are populated
item[key] = def[key]
2021-02-11 11:25:18 +01:00
if key == "description" or key == "short_description" then
2021-01-16 21:03:43 +01:00
-- translate content
item[key] = minetest.get_translated_string("", def[key])
end
2020-12-03 14:30:13 +01:00
end
2020-01-10 08:10:30 +01:00
end
2021-03-24 18:20:36 +01:00
enhancefn(name, item)
2020-01-10 08:10:30 +01:00
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
2021-03-21 10:59:17 +01:00
return 0
2020-01-10 07:58:48 +01:00
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()
2020-02-20 12:37:23 +01:00
return #instr
2020-01-06 13:31:18 +01:00
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)
2021-01-16 21:03:43 +01:00
if err or not f then
error("error while opening " .. fname .. " " .. (err or ""))
2020-01-06 13:31:18 +01:00
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