mirror of
https://codeberg.org/AntumLuanti/mod-cleaner.git
synced 2025-03-15 04:41:22 +00:00
use single json file for cleaning all types
This commit is contained in:
parent
37ca2b7dab
commit
efc5ef2660
7 changed files with 147 additions and 62 deletions
1
TODO.txt
1
TODO.txt
|
@ -2,4 +2,3 @@
|
||||||
TODO:
|
TODO:
|
||||||
- add localization support
|
- add localization support
|
||||||
- add support for unregistering ores via world path file
|
- add support for unregistering ores via world path file
|
||||||
- use single json file for cleaning all types
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ v1.2
|
||||||
- find_unknown_nodes
|
- find_unknown_nodes
|
||||||
- added setting for enabling "unsafe" methods & commands
|
- added setting for enabling "unsafe" methods & commands
|
||||||
- added support for unregistering ores
|
- added support for unregistering ores
|
||||||
|
- all types are loaded from <world_path>/cleaner.json file
|
||||||
|
|
||||||
v1.1
|
v1.1
|
||||||
----
|
----
|
||||||
|
|
45
entities.lua
45
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
|
-- 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_path = core.get_worldpath() .. "/clean_entities.json"
|
||||||
local e_file = io.open(e_path, "r")
|
local e_file = io.open(e_path, "r")
|
||||||
|
|
||||||
if e_file then
|
if e_file then
|
||||||
|
cleaner.log("action", "found deprecated clean_entities.json, updating")
|
||||||
|
|
||||||
local data_in = core.parse_json(e_file:read("*a"))
|
local data_in = core.parse_json(e_file:read("*a"))
|
||||||
e_file:close()
|
e_file:close()
|
||||||
if data_in then
|
if data_in and data_in.remove then
|
||||||
e_list = data_in
|
for _, r in ipairs(data_in.remove) do
|
||||||
|
table.insert(entities_data.remove, r)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- don't read deprecated file again
|
||||||
|
os.rename(e_path, e_path .. ".old")
|
||||||
end
|
end
|
||||||
|
|
||||||
-- backward compat
|
|
||||||
local e_path_old = core.get_worldpath() .. "/clean_entities.txt"
|
local e_path_old = core.get_worldpath() .. "/clean_entities.txt"
|
||||||
e_file = io.open(e_path_old, "r")
|
e_file = io.open(e_path_old, "r")
|
||||||
|
|
||||||
|
@ -25,7 +35,7 @@ if e_file then
|
||||||
for _, e in ipairs(data_in) 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(entities_data.remove, e)
|
||||||
end
|
end
|
||||||
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
|
os.rename(e_path_old, e_path_old .. ".bak") -- don't read deprecated file again
|
||||||
end
|
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
|
-- update json file with any changes
|
||||||
e_file = io.open(e_path, "w")
|
aux.update_world_data("entities", entities_data)
|
||||||
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
|
|
||||||
|
|
||||||
|
core.register_on_mods_loaded(function()
|
||||||
for _, e in ipairs(e_list.remove) do
|
for _, e in ipairs(entities_data.remove) do
|
||||||
cleaner.log("debug", "Cleaning entity: " .. e)
|
cleaner.log("action", "registering entity for removal: " .. e)
|
||||||
cleaner.register_entity_removal(e)
|
cleaner.register_entity_removal(e)
|
||||||
end
|
end
|
||||||
|
end)
|
||||||
|
|
5
init.lua
5
init.lua
|
@ -30,6 +30,11 @@ function cleaner.log(lvl, msg)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local aux = dofile(cleaner.modpath .. "/misc_functions.lua")
|
||||||
|
|
||||||
|
-- initialize world file
|
||||||
|
aux.update_world_data()
|
||||||
|
|
||||||
|
|
||||||
local scripts = {
|
local scripts = {
|
||||||
"settings",
|
"settings",
|
||||||
|
|
37
items.lua
37
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_path = core.get_worldpath() .. "/clean_items.json"
|
||||||
local i_file = io.open(i_path, "r")
|
local i_file = io.open(i_path, "r")
|
||||||
|
|
||||||
if i_file then
|
if i_file then
|
||||||
|
cleaner.log("action", "found deprecated clean_items.json, updating")
|
||||||
|
|
||||||
local data_in = core.parse_json(i_file:read("*a"))
|
local data_in = core.parse_json(i_file:read("*a"))
|
||||||
i_file:close()
|
i_file:close()
|
||||||
if data_in then
|
if data_in and data_in.replace then
|
||||||
i_list = data_in
|
for k, v in pairs(data_in.replace) do
|
||||||
|
if not items_data.replace[k] then
|
||||||
|
items_data.replace[k] = v
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- don't read deprecated file again
|
||||||
|
os.rename(i_path, i_path .. ".old")
|
||||||
end
|
end
|
||||||
|
|
||||||
-- update json file with any changes
|
-- END: backward compat
|
||||||
i_file = io.open(i_path, "w")
|
|
||||||
if i_file then
|
|
||||||
local data_out = core.write_json(i_list, true)
|
|
||||||
|
|
||||||
data_out = data_out:gsub("\"replace\" : null", "\"replace\" : {}")
|
|
||||||
|
|
||||||
i_file:write(data_out)
|
aux.update_world_data("items", items_data)
|
||||||
i_file:close()
|
|
||||||
end
|
|
||||||
|
|
||||||
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)
|
cleaner.register_item_replacement(i_old, i_new)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- register actions for after server startup
|
-- register actions for after server startup
|
||||||
core.register_on_mods_loaded(function()
|
core.register_on_mods_loaded(function()
|
||||||
for i_old, i_new in pairs(cleaner.get_replace_items()) do
|
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
|
if not core.registered_items[i_old] then
|
||||||
cleaner.log("info", "\"" .. i_old .. "\" not registered, not unregistering")
|
cleaner.log("info", "\"" .. i_old .. "\" not registered, not unregistering")
|
||||||
|
|
|
@ -19,7 +19,56 @@ local function clean_duplicates(t)
|
||||||
return t
|
return t
|
||||||
end
|
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 {
|
return {
|
||||||
clean_duplicates = clean_duplicates,
|
clean_duplicates = clean_duplicates,
|
||||||
|
get_world_data = get_world_data,
|
||||||
|
update_world_data = update_world_data,
|
||||||
}
|
}
|
||||||
|
|
71
nodes.lua
71
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
|
-- 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_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
|
||||||
|
cleaner.log("action", "found deprecated clean_nodes.json, updating")
|
||||||
|
|
||||||
local data_in = core.parse_json(n_file:read("*a"))
|
local data_in = core.parse_json(n_file:read("*a"))
|
||||||
n_file:close()
|
n_file:close()
|
||||||
if data_in then
|
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
|
end
|
||||||
|
|
||||||
|
-- don't read deprecated file again
|
||||||
|
os.rename(n_path, n_path .. ".old")
|
||||||
end
|
end
|
||||||
|
|
||||||
-- backward compat
|
|
||||||
local n_path_old = core.get_worldpath() .. "/clean_nodes.txt"
|
local n_path_old = core.get_worldpath() .. "/clean_nodes.txt"
|
||||||
n_file = io.open(n_path_old, "r")
|
n_file = io.open(n_path_old, "r")
|
||||||
|
|
||||||
|
@ -25,33 +45,21 @@ if n_file then
|
||||||
for _, e in ipairs(data_in) 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(n_list.remove, e)
|
table.insert(nodes_data.remove, e)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
n_file:close()
|
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
|
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
|
-- update json file with any changes
|
||||||
n_file = io.open(n_path, "w")
|
aux.update_world_data("nodes", nodes_data)
|
||||||
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
|
|
||||||
|
|
||||||
core.register_lbm({
|
core.register_lbm({
|
||||||
name = "cleaner:remove_nodes",
|
name = "cleaner:remove_nodes",
|
||||||
|
@ -62,11 +70,6 @@ core.register_lbm({
|
||||||
end,
|
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({
|
core.register_lbm({
|
||||||
name = "cleaner:replace_nodes",
|
name = "cleaner:replace_nodes",
|
||||||
nodenames = {"group:to_replace"},
|
nodenames = {"group:to_replace"},
|
||||||
|
@ -83,3 +86,15 @@ core.register_lbm({
|
||||||
end
|
end
|
||||||
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)
|
||||||
|
|
Loading…
Add table
Reference in a new issue