generated from your-land/yl_template
55 lines
1.7 KiB
Lua
55 lines
1.7 KiB
Lua
-- The functions and variables in this file are only for use in the mod itself.
|
|
-- Those that do real work should be local and wrapped in public functions
|
|
local function log(text)
|
|
local logmessage = yl_template.t("log_prefix", yl_template.modname, text)
|
|
if yl_template.settings.debug then minetest.log("action", logmessage) end
|
|
return logmessage
|
|
end
|
|
|
|
function yl_template.log(text) return log(text) end
|
|
|
|
local function get_savepath()
|
|
local savepath = yl_template.worldpath .. yl_template.settings.save_path
|
|
log(yl_template.t("log_prefix", dump(savepath)))
|
|
return savepath
|
|
end
|
|
|
|
local function get_filepath(filename)
|
|
local path_to_file = get_savepath() .. DIR_DELIM .. filename .. ".json"
|
|
log(yl_template.t("get_filepath", dump(filename), dump(path_to_file)))
|
|
return path_to_file
|
|
end
|
|
|
|
local function save_json(filename, content)
|
|
if type(filename) ~= "string" or type(content) ~= "table" then
|
|
return false
|
|
end
|
|
local savepath = get_filepath(filename)
|
|
local savecontent = minetest.write_json(content)
|
|
return minetest.safe_file_write(savepath, savecontent)
|
|
end
|
|
|
|
local function load_json(path)
|
|
local file = io.open(path, "r")
|
|
if not file then
|
|
return false, yl_template.t("error_cannot_open_file", dump(path))
|
|
end
|
|
|
|
local content = file:read("*all")
|
|
file:close()
|
|
|
|
if not content then
|
|
return false, yl_template.t("error_cannot_read_file", dump(path))
|
|
end
|
|
|
|
return true, minetest.parse_json(content)
|
|
end
|
|
|
|
-- Public functions wrap the private ones, so they can be exchanged easily
|
|
|
|
function yl_template.load(filename, ...) return load_json(filename, ...) end
|
|
|
|
function yl_template.save(filename, content, ...)
|
|
return save_json(filename, content, ...)
|
|
end
|