diff --git a/szutil_usagesurvey/init.lua b/szutil_usagesurvey/init.lua index 632f26d..349b104 100644 --- a/szutil_usagesurvey/init.lua +++ b/szutil_usagesurvey/init.lua @@ -1,8 +1,10 @@ -- LUALOCALS < --------------------------------------------------------- -local io, ipairs, math, minetest, pairs, table, type - = io, ipairs, math, minetest, pairs, table, type -local io_open, math_floor, table_concat, table_sort - = io.open, math.floor, table.concat, table.sort +local io, ipairs, math, minetest, pairs, string, table, type + = io, ipairs, math, minetest, pairs, string, table, type +local io_open, math_floor, string_gsub, string_match, table_concat, + table_sort + = io.open, math.floor, string.gsub, string.match, table.concat, + table.sort -- LUALOCALS > --------------------------------------------------------- local modname = minetest.get_current_modname() @@ -136,13 +138,13 @@ local function deepadd(t, u) return t end +local dbnamepattern = "^blk.*%.json$" local function dbpath(id) local p = minetest.get_worldpath() .. "/" .. modname if id then id = "" .. id - if id:sub(1, 3) ~= "blk" then - id = "blk" .. id .. ".txt" + id = "blk" .. id .. ".json" end p = p .. "/" .. id end @@ -152,9 +154,9 @@ end local function dbload(id) local f = io_open(dbpath(id)) if not f then return {} end - local u = minetest.deserialize(f:read("*all")) + local s = f:read("*all") f:close() - return u + return s and minetest.parse_json(s) end local lasttime = minetest.get_us_time() / 1000000 @@ -169,7 +171,7 @@ local function dbflush(forcerpt) minetest.mkdir(dbpath()) for id, t in pairs(db) do t = deepadd(dbload(id), t) - minetest.safe_file_write(dbpath(id), minetest.serialize(t)) + minetest.safe_file_write(dbpath(id), minetest.write_json(t)) savedqty = savedqty + 1 end db = {} @@ -268,7 +270,9 @@ minetest.register_chatcommand("worlduse", { t = deepadd(t, v) end for _, v in ipairs(minetest.get_dir_list(dbpath(), false)) do - t = deepadd(t, dbload(v)) + if string_match(v, dbnamepattern) then + t = deepadd(t, dbload(v)) + end end minetest.chat_send_player(name, fmtrpt(t)) @@ -276,3 +280,50 @@ minetest.register_chatcommand("worlduse", { }) ------------------------------------------------------------------------ +-- DATABASE MIGRATION + +do + local started = minetest.get_us_time() + + local clean = 0 + local dirty = 0 + local fail = 0 + + local dirlist = minetest.get_dir_list(dbpath(), false) + local exists = {} + for _, v in ipairs(dirlist) do exists[v] = true end + for _, v in ipairs(dirlist) do + if string_match(v, "^blk.*%.txt$") then + local jv = string_gsub(v, "%.txt$", ".json") + if exists[jv] then + clean = clean + 1 + else + local f = io_open(dbpath(v)) + if not f then + fail = fail + 1 + else + local s = f:read("*all") + f:close() + local u = minetest.deserialize(s) + if not u then + fail = fail + 1 + else + minetest.safe_file_write(dbpath(jv), + minetest.write_json(u)) + dirty = dirty + 1 + end + end + end + end + end + local ms = math_floor((minetest.get_us_time() - started) / 1000) + if clean + dirty + fail == 0 then + minetest.log("action", modname .. ": db checked in " .. ms .. "ms") + else + minetest.log("action", modname .. ": db converted " .. dirty + .. ", " .. fail .. " failed, " .. clean .. " skipped in " + .. ms .. "ms") + end +end + +------------------------------------------------------------------------