Convert to json format for clean_nodes file

This commit is contained in:
Jordan Irwin 2021-05-18 20:11:38 -07:00
parent 871981ddd7
commit d69e47ca2a
3 changed files with 64 additions and 38 deletions

View file

@ -1,19 +1,7 @@
local function clean_duplicates(t) local misc = dofile(cleaner.modpath .. "/misc_functions.lua")
local tmp = {}
for _, v in ipairs(t) do
tmp[v] = true
end
t = {} -- populate entities list from file in world path
for k in pairs(tmp) do
table.insert(t, k)
end
return t
end
-- Populate entities list from file in world path
local e_list = {remove={}} local e_list = {remove={}}
local e_path = core.get_worldpath() .. "/clean_entities.json" local e_path = core.get_worldpath() .. "/clean_entities.json"
local e_file = io.open(e_path, "r") local e_file = io.open(e_path, "r")
@ -35,8 +23,8 @@ e_file = io.open(e_path_old, "r")
if e_file then if e_file then
cleaner.log("action", "found deprecated clean_entities.txt, converting to json") cleaner.log("action", "found deprecated clean_entities.txt, converting to json")
local data = string.split(e_file:read("*a"), "\n") local data_in = string.split(e_file:read("*a"), "\n")
for _, e in ipairs(data) do for _, e in ipairs(data_in) do
e = e:trim() e = e:trim()
if e ~= "" and e:sub(1, 1) ~= "#" then if e ~= "" and e:sub(1, 1) ~= "#" then
table.insert(e_list.remove, e) table.insert(e_list.remove, e)
@ -47,13 +35,13 @@ if e_file then
os.rename(e_path_old, e_path_old .. ".bak") -- don't read deprecated file again os.rename(e_path_old, e_path_old .. ".bak") -- don't read deprecated file again
end end
e_list.remove = clean_duplicates(e_list.remove) e_list.remove = misc.clean_duplicates(e_list.remove)
-- update json file with any changes -- update json file with any changes
e_file = io.open(e_path, "w") e_file = io.open(e_path, "w")
if e_file then if e_file then
local data = core.write_json(e_list, true):gsub("\"remove\" : null", "\"remove\" : []") local data_out = core.write_json(e_list, true):gsub("\"remove\" : null", "\"remove\" : []")
e_file:write(data) e_file:write(data_out)
e_file:close() e_file:close()
end end

19
misc_functions.lua Normal file
View file

@ -0,0 +1,19 @@
local function clean_duplicates(t)
local tmp = {}
for _, v in ipairs(t) do
tmp[v] = true
end
t = {}
for k in pairs(tmp) do
table.insert(t, k)
end
return t
end
return {
clean_duplicates = clean_duplicates,
}

View file

@ -1,35 +1,54 @@
local old_nodes = {} local misc = dofile(cleaner.modpath .. "/misc_functions.lua")
-- Populate nodes list from file in world path -- populate nodes list from file in world path
local n_list = nil local n_list = {remove={}}
local n_path = core.get_worldpath() .. "/clean_nodes.txt" local n_path = core.get_worldpath() .. "/clean_nodes.json"
local n_file = io.open(n_path, "r") local n_file = io.open(n_path, "r")
if n_file then if n_file then
n_list = n_file:read("*a") local data_in = core.parse_json(n_file:read("*a"))
n_file:close() n_file:close()
else if data_in then
-- Create empty file for _, n in ipairs(data_in.remove) do
n_file = io.open(n_path, "w") table.insert(n_list.remove, n)
if n_file then end
n_file:close()
end end
end end
if n_list then -- backward compat
cleaner.log("debug", "Loading nodes to clean from file ...") local n_path_old = core.get_worldpath() .. "/clean_nodes.txt"
n_file = io.open(n_path_old, "r")
n_list = string.split(n_list, "\n") if n_file then
for _, node_name in ipairs(n_list) do cleaner.log("action", "found deprecated clean_nodes.txt, converting to json")
table.insert(old_nodes, node_name)
local data_in = string.split(n_file:read("*a"), "\n")
for _, e in ipairs(data_in) do
e = e:trim()
if e ~= "" and e:sub(1, 1) ~= "#" then
table.insert(n_list.remove, e)
end
end end
n_file:close()
os.rename(n_path_old, n_path_old .. ".bak") -- don't read deprecated file again
end end
for _, node_name in ipairs(old_nodes) do n_list.remove = misc.clean_duplicates(n_list.remove)
cleaner.log("debug", "Cleaning node: " .. node_name)
core.register_node(":" .. node_name, { -- update json file with any changes
n_file = io.open(n_path, "w")
if n_file then
local data_out = core.write_json(n_list, true):gsub("\"remove\" : null", "\"remove\" : []")
n_file:write(data_out)
n_file:close()
end
for _, n in ipairs(n_list.remove) do
cleaner.log("debug", "Cleaning node: " .. n)
core.register_node(":" .. n, {
groups = {old=1}, groups = {old=1},
}) })
end end
@ -39,6 +58,6 @@ core.register_abm({
interval = 1, interval = 1,
chance = 1, chance = 1,
action = function(pos, node) action = function(pos, node)
core.remove_node(pos) core.remove_node(pos)
end, end,
}) })