Replace exportdb with configdb

- Open up room for potential for more config
  options (which properties to export, additional
  indirectly-depended media, etc).
- Restructure API to use items as well.  For now,
  I am assuming there are no legacy applications
  that need to be supported so the old methods
  are immediately removed; may be re-added
  if compat turns out to be needed.
This commit is contained in:
Aaron Suen 2022-11-27 09:36:52 -05:00
parent b179ef0b9c
commit 79324d4aa0
5 changed files with 85 additions and 48 deletions

12
api.lua
View File

@ -10,19 +10,19 @@ local modname = minetest.get_current_modname()
local api = rawget(_G, modname) or {}
rawset(_G, modname, api)
local exportdb, savedb = include("exportdb")
local configdb, savedb = include("configdb")
local exportall = include("exportall")
function api.get_all()
function api.item_get_all()
local t = {}
for k in pairs(exportdb) do t[k] = true end
for k in pairs(configdb.items) do t[k] = true end
return t
end
function api.get(k) return exportdb[k] end
function api.item_get(k) return configdb.items[k] end
function api.set(k, v)
exportdb[k] = v and true or nil
function api.item_set(k, v)
configdb.items[k] = v and true or nil
end
function api.export()

View File

@ -6,7 +6,7 @@ local string_format, string_gsub, string_match
-- LUALOCALS > ---------------------------------------------------------
local include = ...
local exportdb, savedb = include("exportdb")
local configdb, savedb = include("configdb")
local exportall = include("exportall")
local modname = minetest.get_current_modname()
@ -27,7 +27,7 @@ minetest.register_chatcommand(modname .. "_clear", {
description = "Clear all saved exports",
privs = {server = true},
func = function()
for k in pairs(exportdb) do exportdb[k] = nil end
for k in pairs(configdb.items) do configdb.items[k] = nil end
return save_export_report()
end
})
@ -37,8 +37,8 @@ local function ripinv(inv)
for _, list in pairs(inv:get_lists()) do
for _, stack in pairs(list) do
local n = stack:get_name()
if n and n ~= "" and not exportdb[n] then
exportdb[n] = true
if n and n ~= "" and not configdb.items[n] then
configdb.items[n] = true
dirty = true
end
end
@ -63,7 +63,7 @@ local function patternfunc(setto)
local ok, err = pcall(function()
for k in pairs(minetest.registered_items) do
if string_match(k, param) then
exportdb[k] = setto
configdb.items[k] = setto
end
end
end)
@ -97,8 +97,8 @@ local function ripradius(pos, radius)
local dirty
for k in pairs(foundids) do
local n = minetest.get_name_from_content_id(k)
if n and n ~= "" and not exportdb[n] then
exportdb[n] = true
if n and n ~= "" and not configdb.items[n] then
configdb.items[n] = true
dirty = true
end
end

70
configdb.lua Normal file
View File

@ -0,0 +1,70 @@
-- LUALOCALS < ---------------------------------------------------------
local error, io, minetest, pairs, rawset, setmetatable, string
= error, io, minetest, pairs, rawset, setmetatable, string
local io_open, string_format
= io.open, string.format
-- LUALOCALS > ---------------------------------------------------------
local include = ...
local getdir = include("fileops")
local modname = minetest.get_current_modname()
local mydir = getdir(minetest.get_worldpath() .. "/" .. modname)
local dbpath = mydir .. "/" .. modname .. ".json"
------------------------------------------------------------------------
local function read_config()
local fh = io_open(dbpath, "r")
if not fh then return end
local s = fh:read("*all")
fh:close()
return minetest.parse_json(s)
end
local function read_legacy_json()
local fh = io_open(mydir .. "/export.json", "r")
if not fh then return end
local s = fh:read("*all")
fh:close()
s = minetest.parse_json(s)
return s and {items = s}
end
local function read_legacy_storage()
local modstore = minetest.get_mod_storage()
local s = modstore:get_string("export")
s = s and s ~= "" and minetest.deserialize(s) or {}
return s and {items = s}
end
------------------------------------------------------------------------
local configdb = read_config() or read_legacy_json() or read_legacy_storage()
local function savedb()
minetest.safe_file_write(dbpath, minetest.write_json(configdb, true))
end
------------------------------------------------------------------------
local allkeys = {
items = true
}
setmetatable(configdb, {
__newindex = function(t, k, v)
if not allkeys[k] then
error(string_format("disallowed configdb key %q", k))
end
return rawset(t, k, v)
end
})
for k in pairs(allkeys) do
if not configdb[k] then
configdb[k] = {}
end
end
------------------------------------------------------------------------
return configdb, savedb

View File

@ -8,7 +8,7 @@ local io_open, math_ceil, os_remove, string_gsub, string_sub
local include = ...
local dumptable, sortedpairs = include("dumptable")
local getdir, ripmedia = include("fileops")
local exportdb = include("exportdb")
local configdb = include("configdb")
local modname = minetest.get_current_modname()
@ -71,7 +71,7 @@ local function exportall()
local filtered = {}
for k, v in pairs(minetest.registered_items) do
if not blocked[k] and exportdb[k] then
if not blocked[k] and configdb.items[k] then
local t = {}
for k2, v2 in pairs(nodekeys) do
if type(v2) == "function" then

View File

@ -1,33 +0,0 @@
-- LUALOCALS < ---------------------------------------------------------
local io, minetest
= io, minetest
local io_open
= io.open
-- LUALOCALS > ---------------------------------------------------------
local include = ...
local getdir = include("fileops")
local modname = minetest.get_current_modname()
local dbpath = getdir(minetest.get_worldpath()
.. "/" .. modname) .. "/export.json"
local exportdb
do
local fh = io_open(dbpath, "r")
if fh then
local s = fh:read("*all")
fh:close()
exportdb = s and minetest.parse_json(s) or {}
else
local modstore = minetest.get_mod_storage()
local s = modstore:get_string("export")
exportdb = s and s ~= "" and minetest.deserialize(s) or {}
end
end
local function savedb()
minetest.safe_file_write(dbpath, minetest.write_json(exportdb, true))
end
return exportdb, savedb