yl_survey/internal.lua
2024-09-12 05:35:28 +02:00

154 lines
4.4 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_survey.t("log_prefix", yl_survey.modname, text)
if yl_survey.settings.debug then minetest.log("action", logmessage) end
return logmessage
end
function yl_survey.log(text) return log(text) end
-- Helpers
local function filename2uuid(filename) return filename:sub(1, -6) end
local function is_visible(filename) return (string.sub(filename, 1, 1) ~= ".") end
local function is_json(filename) return (filename:match("%.json$")) end
local function ends_with(str, suffix) return str:sub(-suffix:len()) == suffix end
local function split(str)
local parts = {}
for part in str:gmatch("[^,%s]+") do table.insert(parts, part) end
return parts
end
local function priv_exists(priv)
return (minetest.registered_privileges[priv] ~= nil) or false
end
function yl_survey.priv_exists(priv) return priv_exists(priv) end
-- Hash name
local function hash_name(name)
if ((type(name) ~= "string") or (name = "")) then
return nil
end
return minetest.sha256(name, false)
end
function yl_survey.hash_name(name) return hash_name(name) end
-- Load and save
local function get_savepath()
local savepath = yl_survey.worldpath .. yl_survey.settings.save_path
log(savepath or "")
return savepath
end
local function get_filepath(filename)
local path_to_file = get_savepath() .. DIR_DELIM .. filename .. ".json"
log(yl_survey.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_survey.t("error_cannot_open_file", dump(path))
end
local content = file:read("*all")
file:close()
if not content then
return false, yl_survey.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_survey.load(filename, ...) return load_json(filename, ...) end
function yl_survey.save(filename, content, ...)
return save_json(filename, content, ...)
end
-- Load all data
--
local function load_all_data()
-- Get all json files from savepath
-- Excluding invisible
-- Excluding non-json files
local save_path = get_savepath()
local files = minetest.get_dir_list(save_path, false) or {}
local data = {}
local total = 0
local good = 0
local bad = 0
for key, filename in ipairs(files) do
if is_visible(filename) and is_json(filename) then
total = total + 1
local UUID = filename2uuid(filename)
local filepath = get_filepath(UUID)
local success, content = load_json(filepath)
if success and (content.id == UUID) then
good = good + 1
data[UUID] = content
else
bad = bad + 1
end
end
end
yl_survey.data = data
local message = "[MOD] %s : bad = %s, good = %s, total = %s"
local formatted = string.format(message, yl_survey.information.name, tostring(bad), tostring(good), tostring(total))
if bad == 0 then
minetest.log("action", formatted)
return true, good, bad
else
minetest.log("warning", formatted)
return false, good, bad
end
end
function yl_survey.load_all_data() return load_all_data() end
-- Check privs
--
local function check_privs(settings)
for key, value in pairs(settings) do
if ends_with(key, "_privs") then
local parts = split(value)
for _, part in ipairs(parts) do
local message = "[MOD] %s : configured priv %s does not exist."
local formatted = string.format(message, yl_survey.information.name, dump(part))
assert(priv_exists(part), formatted)
end
end
end
log("PASS priv check")
end
function yl_survey.check_privs(settings) return check_privs(settings) end