From efc5ef2660ec26b4601ed29d31a4de9a5e6223cb Mon Sep 17 00:00:00 2001 From: Jordan Irwin Date: Mon, 12 Jul 2021 18:09:54 -0700 Subject: [PATCH] use single json file for cleaning all types --- TODO.txt | 1 - changelog.txt | 1 + entities.lua | 45 +++++++++++++++++------------ init.lua | 5 ++++ items.lua | 37 ++++++++++++++---------- misc_functions.lua | 49 ++++++++++++++++++++++++++++++++ nodes.lua | 71 ++++++++++++++++++++++++++++------------------ 7 files changed, 147 insertions(+), 62 deletions(-) diff --git a/TODO.txt b/TODO.txt index f3e344a..e315a16 100644 --- a/TODO.txt +++ b/TODO.txt @@ -2,4 +2,3 @@ TODO: - add localization support - add support for unregistering ores via world path file -- use single json file for cleaning all types diff --git a/changelog.txt b/changelog.txt index 90a5055..bec291a 100644 --- a/changelog.txt +++ b/changelog.txt @@ -10,6 +10,7 @@ v1.2 - find_unknown_nodes - added setting for enabling "unsafe" methods & commands - added support for unregistering ores +- all types are loaded from /cleaner.json file v1.1 ---- diff --git a/entities.lua b/entities.lua index b2b23af..7b9da53 100644 --- a/entities.lua +++ b/entities.lua @@ -1,20 +1,30 @@ -local misc = dofile(cleaner.modpath .. "/misc_functions.lua") +local aux = dofile(cleaner.modpath .. "/misc_functions.lua") -- populate entities list from file in world path -local e_list = {remove={}} +local entities_data = aux.get_world_data().entities + + +-- START: backward compat + local e_path = core.get_worldpath() .. "/clean_entities.json" local e_file = io.open(e_path, "r") if e_file then + cleaner.log("action", "found deprecated clean_entities.json, updating") + local data_in = core.parse_json(e_file:read("*a")) e_file:close() - if data_in then - e_list = data_in + if data_in and data_in.remove then + for _, r in ipairs(data_in.remove) do + table.insert(entities_data.remove, r) + end end + + -- don't read deprecated file again + os.rename(e_path, e_path .. ".old") end --- backward compat local e_path_old = core.get_worldpath() .. "/clean_entities.txt" e_file = io.open(e_path_old, "r") @@ -25,7 +35,7 @@ if e_file then for _, e in ipairs(data_in) do e = e:trim() if e ~= "" and e:sub(1, 1) ~= "#" then - table.insert(e_list.remove, e) + table.insert(entities_data.remove, e) end end @@ -33,18 +43,17 @@ if e_file then os.rename(e_path_old, e_path_old .. ".bak") -- don't read deprecated file again end -e_list.remove = misc.clean_duplicates(e_list.remove) +-- END: backward compat + + +entities_data.remove = aux.clean_duplicates(entities_data.remove) -- update json file with any changes -e_file = io.open(e_path, "w") -if e_file then - local data_out = core.write_json(e_list, true):gsub("\"remove\" : null", "\"remove\" : []") - e_file:write(data_out) - e_file:close() -end +aux.update_world_data("entities", entities_data) - -for _, e in ipairs(e_list.remove) do - cleaner.log("debug", "Cleaning entity: " .. e) - cleaner.register_entity_removal(e) -end +core.register_on_mods_loaded(function() + for _, e in ipairs(entities_data.remove) do + cleaner.log("action", "registering entity for removal: " .. e) + cleaner.register_entity_removal(e) + end +end) diff --git a/init.lua b/init.lua index 569da75..3a46afd 100644 --- a/init.lua +++ b/init.lua @@ -30,6 +30,11 @@ function cleaner.log(lvl, msg) end end +local aux = dofile(cleaner.modpath .. "/misc_functions.lua") + +-- initialize world file +aux.update_world_data() + local scripts = { "settings", diff --git a/items.lua b/items.lua index 5773212..85015e6 100644 --- a/items.lua +++ b/items.lua @@ -1,38 +1,45 @@ -local misc = dofile(cleaner.modpath .. "/misc_functions.lua") +local aux = dofile(cleaner.modpath .. "/misc_functions.lua") + +-- populate items list from file in world path +local items_data = aux.get_world_data().items + + +-- START: backward compat --- populate nodes list from file in world path -local i_list = {replace={}} local i_path = core.get_worldpath() .. "/clean_items.json" local i_file = io.open(i_path, "r") if i_file then + cleaner.log("action", "found deprecated clean_items.json, updating") + local data_in = core.parse_json(i_file:read("*a")) i_file:close() - if data_in then - i_list = data_in + if data_in and data_in.replace then + for k, v in pairs(data_in.replace) do + if not items_data.replace[k] then + items_data.replace[k] = v + end + end end + + -- don't read deprecated file again + os.rename(i_path, i_path .. ".old") end --- update json file with any changes -i_file = io.open(i_path, "w") -if i_file then - local data_out = core.write_json(i_list, true) +-- END: backward compat - data_out = data_out:gsub("\"replace\" : null", "\"replace\" : {}") - i_file:write(data_out) - i_file:close() -end +aux.update_world_data("items", items_data) -for i_old, i_new in pairs(i_list.replace) do +for i_old, i_new in pairs(items_data.replace) do cleaner.register_item_replacement(i_old, i_new) end -- register actions for after server startup core.register_on_mods_loaded(function() for i_old, i_new in pairs(cleaner.get_replace_items()) do - cleaner.log("action", "replacing item \"" .. i_old .. "\" with \"" .. i_new .. "\"") + cleaner.log("action", "registering item \"" .. i_old .. "\" to be replaced with \"" .. i_new .. "\"") if not core.registered_items[i_old] then cleaner.log("info", "\"" .. i_old .. "\" not registered, not unregistering") diff --git a/misc_functions.lua b/misc_functions.lua index 7278c93..4812d49 100644 --- a/misc_functions.lua +++ b/misc_functions.lua @@ -19,7 +19,56 @@ local function clean_duplicates(t) return t end +local world_file = core.get_worldpath() .. "/cleaner.json" + +local function get_world_data() + local wdata = {} + local buffer = io.open(world_file, "r") + if buffer then + wdata = core.parse_json(buffer:read("*a")) + buffer:close() + end + + local rem_types = {"entities", "nodes", "ores",} + local rep_types = {"items", "nodes",} + + for _, t in ipairs(rem_types) do + wdata[t] = wdata[t] or {} + wdata[t].remove = wdata[t].remove or {} + end + + for _, t in ipairs(rep_types) do + wdata[t] = wdata[t] or {} + wdata[t].replace = wdata[t].replace or {} + end + + return wdata +end + +local function update_world_data(t, data) + local wdata = get_world_data() + if t and data then + wdata[t].remove = data.remove + wdata[t].replace = data.replace + end + + local json_string = core.write_json(wdata, true):gsub("\"remove\" : null", "\"remove\" : []") + :gsub("\"replace\" : null", "\"replace\" : {}") + + local buffer = io.open(world_file, "w") + if buffer then + buffer:write(json_string) + buffer:close() + + return true + end + + return false +end + return { clean_duplicates = clean_duplicates, + get_world_data = get_world_data, + update_world_data = update_world_data, } diff --git a/nodes.lua b/nodes.lua index c9d72b4..4f1239d 100644 --- a/nodes.lua +++ b/nodes.lua @@ -1,20 +1,40 @@ -local misc = dofile(cleaner.modpath .. "/misc_functions.lua") +local aux = dofile(cleaner.modpath .. "/misc_functions.lua") -- populate nodes list from file in world path -local n_list = {remove={}, replace={}} +local nodes_data = aux.get_world_data().nodes + + +-- START: backward compat + local n_path = core.get_worldpath() .. "/clean_nodes.json" local n_file = io.open(n_path, "r") if n_file then + cleaner.log("action", "found deprecated clean_nodes.json, updating") + local data_in = core.parse_json(n_file:read("*a")) n_file:close() if data_in then - n_list = data_in + if data_in.remove then + for _, r in ipairs(data_in.remove) do + table.insert(nodes_data.remove, r) + end + end + + if data_in.replace then + for k, v in pairs(data_in.replace) do + if not nodes_data.replace[k] then + nodes_data.replace[k] = v + end + end + end end + + -- don't read deprecated file again + os.rename(n_path, n_path .. ".old") end --- backward compat local n_path_old = core.get_worldpath() .. "/clean_nodes.txt" n_file = io.open(n_path_old, "r") @@ -25,33 +45,21 @@ if n_file then for _, e in ipairs(data_in) do e = e:trim() if e ~= "" and e:sub(1, 1) ~= "#" then - table.insert(n_list.remove, e) + table.insert(nodes_data.remove, e) end end n_file:close() - os.rename(n_path_old, n_path_old .. ".bak") -- don't read deprecated file again + os.rename(n_path_old, n_path_old .. ".old") -- don't read deprecated file again end -n_list.remove = misc.clean_duplicates(n_list.remove) +-- END: backward compat + + +nodes_data.remove = aux.clean_duplicates(nodes_data.remove) -- 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) - - -- FIXME: how to do this with a single regex? - data_out = data_out:gsub("\"remove\" : null", "\"remove\" : []") - data_out = data_out:gsub("\"replace\" : null", "\"replace\" : {}") - - n_file:write(data_out) - n_file:close() -end - -for _, n in ipairs(n_list.remove) do - cleaner.log("debug", "Cleaning node: " .. n) - cleaner.register_node_removal(n) -end +aux.update_world_data("nodes", nodes_data) core.register_lbm({ name = "cleaner:remove_nodes", @@ -62,11 +70,6 @@ core.register_lbm({ end, }) -for n_old, n_new in pairs(n_list.replace) do - cleaner.log("debug", "Replacing node \"" .. n_old .. "\" with \"" .. n_new .. "\"") - cleaner.register_node_replacement(n_old, n_new) -end - core.register_lbm({ name = "cleaner:replace_nodes", nodenames = {"group:to_replace"}, @@ -83,3 +86,15 @@ core.register_lbm({ end end, }) + +core.register_on_mods_loaded(function() + for _, n in ipairs(nodes_data.remove) do + cleaner.log("action", "registering node for removal: " .. n) + cleaner.register_node_removal(n) + end + + for n_old, n_new in pairs(nodes_data.replace) do + cleaner.log("action", "registering node \"" .. n_old .. "\" to be replaced with \"" .. n_new .. "\"") + cleaner.register_node_replacement(n_old, n_new) + end +end)