2021-05-28 08:23:51 +02:00
|
|
|
|
2021-05-28 09:24:10 +02:00
|
|
|
--- World Data Manager API
|
2021-05-28 08:37:37 +02:00
|
|
|
--
|
2021-08-29 04:47:00 +02:00
|
|
|
-- @topic api
|
2021-05-28 08:37:37 +02:00
|
|
|
|
|
|
|
|
2021-05-28 08:23:51 +02:00
|
|
|
-- store formatted world path
|
|
|
|
local world_path = core.get_worldpath():gsub("\\", "/")
|
|
|
|
|
|
|
|
--- Retrieves directory path where file is located.
|
|
|
|
--
|
|
|
|
-- @local
|
2021-05-28 08:37:37 +02:00
|
|
|
-- @function get_dir
|
2021-05-28 08:23:51 +02:00
|
|
|
-- @tparam string fpath Full path to file.
|
|
|
|
-- @treturn string Full path to directory.
|
|
|
|
local function get_dir(fpath)
|
|
|
|
-- format to make working with easier
|
|
|
|
fpath = fpath:gsub("\\", "/")
|
|
|
|
local idx = fpath:find("/[^/]*$")
|
|
|
|
|
|
|
|
-- use world directory by default
|
|
|
|
if not idx or idx == 0 then
|
|
|
|
return world_path
|
|
|
|
end
|
|
|
|
|
|
|
|
return fpath:sub(1, idx-1)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--- Reads config file from world directory.
|
|
|
|
--
|
2021-05-28 09:24:10 +02:00
|
|
|
-- @function wdata.read
|
2021-08-29 04:22:20 +02:00
|
|
|
-- @tparam string fname Base filename with optional directory structure (e.g. "my\_mod/my\_config")
|
2021-05-28 08:23:51 +02:00
|
|
|
-- @treturn table Table with contents read from json file or `nil`.
|
2021-05-28 09:24:10 +02:00
|
|
|
function wdata.read(fname)
|
2021-05-28 08:23:51 +02:00
|
|
|
local fpath = world_path .. "/" .. fname .. ".json"
|
|
|
|
|
|
|
|
-- check if file exists
|
|
|
|
local fopen = io.open(fpath, "r")
|
|
|
|
if not fopen then
|
2021-05-28 11:27:35 +02:00
|
|
|
wdata.log("warning", "file not found: " .. fpath)
|
2021-05-28 08:23:51 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local table_data = core.parse_json(fopen:read("*a"))
|
|
|
|
io.close(fopen)
|
|
|
|
|
|
|
|
if not table_data then
|
2021-05-28 09:24:10 +02:00
|
|
|
wdata.log("error", "cannot read json data from file: " .. fpath)
|
2021-05-28 08:23:51 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
return table_data
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-08-29 03:51:13 +02:00
|
|
|
--- Flags definition.
|
|
|
|
--
|
|
|
|
-- @table FlagsDef
|
|
|
|
-- @tfield[opt] bool styled Outputs in a human-readable format if this is set (default: `true`).
|
2021-08-29 03:52:01 +02:00
|
|
|
-- @tfield[opt] bool null_to_table "null" values will be converted to tables in output (default: `false`).
|
2021-08-29 03:51:13 +02:00
|
|
|
|
|
|
|
|
2021-05-28 08:23:51 +02:00
|
|
|
--- Writes to config file in world directory.
|
|
|
|
--
|
2021-05-28 09:24:10 +02:00
|
|
|
-- @function wdata.write
|
2021-08-29 04:22:20 +02:00
|
|
|
-- @tparam string fname Base filename with optional directory structure (e.g. "my\_mod/my\_config").
|
2021-05-28 08:23:51 +02:00
|
|
|
-- @tparam table data Table data to be written to config file.
|
2021-08-29 03:51:13 +02:00
|
|
|
-- @tparam[opt] FlagsDef flags
|
2021-05-28 08:23:51 +02:00
|
|
|
-- @treturn bool `true` if succeeded, `false` if not.
|
2021-08-29 03:51:13 +02:00
|
|
|
function wdata.write(fname, data, flags)
|
|
|
|
-- backward compat
|
|
|
|
if type(flags) == "boolean" then
|
|
|
|
wdata.log("warning", "wdata.write: \"styled\" parameter deprecated, use \"flags\"")
|
|
|
|
flags = {styled=flags}
|
|
|
|
end
|
|
|
|
|
|
|
|
if type(flags) ~= "table" then
|
|
|
|
flags = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
flags.styled = flags.styled ~= false
|
2021-05-28 08:23:51 +02:00
|
|
|
|
2021-08-29 03:51:13 +02:00
|
|
|
local json_data = core.write_json(data, flags.styled)
|
2021-05-28 08:23:51 +02:00
|
|
|
if not json_data then
|
2021-05-28 09:24:10 +02:00
|
|
|
wdata.log("error", "cannot convert data to json format")
|
2021-05-28 08:23:51 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
local fpath = world_path .. "/" .. fname .. ".json"
|
|
|
|
|
|
|
|
-- create directory tree if necessary
|
|
|
|
local dirname = get_dir(fpath)
|
|
|
|
if dirname ~= world_path then
|
|
|
|
if not core.mkdir(dirname) then
|
2021-05-28 09:24:10 +02:00
|
|
|
wdata.log("error", "cannot create directory: " .. dirname)
|
2021-05-28 08:23:51 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-29 03:52:01 +02:00
|
|
|
if flags.null_to_table then
|
|
|
|
if flags.styled then
|
|
|
|
json_data = json_data:gsub(": null([,\n])", ": {}%1")
|
|
|
|
else
|
|
|
|
json_data = json_data:gsub(":null([,%]}])", ":{}%1")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-28 08:23:51 +02:00
|
|
|
return core.safe_file_write(fpath, json_data)
|
|
|
|
end
|