53 lines
1.3 KiB
Lua
53 lines
1.3 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 debug = true
|
|
|
|
local function say(text)
|
|
if yl_template.debug then
|
|
core.log("action", "[MOD] yl_template : " .. text)
|
|
end
|
|
end
|
|
|
|
local function save_path(file)
|
|
return yl_template.worldpath .. file .. ".json"
|
|
end
|
|
|
|
local function save_json(filename, content)
|
|
if type(filename) ~= "string" or type(content) ~= "table" then
|
|
return false
|
|
end
|
|
local savepath = save_path(filename)
|
|
local savecontent = minetest.write_json(content)
|
|
return minetest.safe_file_write(savepath, savecontent)
|
|
end
|
|
|
|
|
|
local function load_json(filename) -- returns the saved dialog
|
|
local savepath = save_path(filename)
|
|
|
|
local file, err = io.open(savepath, "r")
|
|
if err then
|
|
return {}
|
|
end
|
|
io.input(file)
|
|
local savecontent = io.read()
|
|
local content = minetest.parse_json(savecontent)
|
|
io.close(file)
|
|
|
|
if type(content) ~= "table" then
|
|
content = {}
|
|
end
|
|
|
|
return 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.load(filename, content, ...)
|
|
return load_json(filename, content, ...)
|
|
end |