mirror of
https://codeberg.org/AntumLuanti/mod-cleaner.git
synced 2025-03-15 12:51:22 +00:00
Convert to json format for clean_entities file
This commit is contained in:
parent
00ab41d6de
commit
871981ddd7
1 changed files with 49 additions and 17 deletions
66
entities.lua
66
entities.lua
|
@ -1,35 +1,67 @@
|
|||
|
||||
local old_entities = {}
|
||||
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
|
||||
|
||||
-- Populate entities list from file in world path
|
||||
local e_list = nil
|
||||
local e_path = core.get_worldpath() .. "/clean_entities.txt"
|
||||
local e_list = {remove={}}
|
||||
local e_path = core.get_worldpath() .. "/clean_entities.json"
|
||||
local e_file = io.open(e_path, "r")
|
||||
|
||||
if e_file then
|
||||
e_list = e_file:read("*a")
|
||||
local data_in = core.parse_json(e_file:read("*a"))
|
||||
e_file:close()
|
||||
else
|
||||
-- Create empty file
|
||||
e_file = io.open(e_path, "w")
|
||||
if e_file then
|
||||
e_file:close()
|
||||
if data_in then
|
||||
for _, e in ipairs(data_in.remove) do
|
||||
table.insert(e_list.remove, e)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if e_list then
|
||||
cleaner.log("debug", "Loading entities to clean from file ...")
|
||||
-- backward compat
|
||||
local e_path_old = core.get_worldpath() .. "/clean_entities.txt"
|
||||
e_file = io.open(e_path_old, "r")
|
||||
|
||||
e_list = string.split(e_list, "\n")
|
||||
for _, entity_name in ipairs(e_list) do
|
||||
table.insert(old_entities, entity_name)
|
||||
if e_file then
|
||||
cleaner.log("action", "found deprecated clean_entities.txt, converting to json")
|
||||
|
||||
local data = string.split(e_file:read("*a"), "\n")
|
||||
for _, e in ipairs(data) do
|
||||
e = e:trim()
|
||||
if e ~= "" and e:sub(1, 1) ~= "#" then
|
||||
table.insert(e_list.remove, e)
|
||||
end
|
||||
end
|
||||
|
||||
e_file:close()
|
||||
os.rename(e_path_old, e_path_old .. ".bak") -- don't read deprecated file again
|
||||
end
|
||||
|
||||
for _, entity_name in ipairs(old_entities) do
|
||||
cleaner.log("debug", "Cleaning entity: " .. entity_name)
|
||||
e_list.remove = clean_duplicates(e_list.remove)
|
||||
|
||||
core.register_entity(":" .. entity_name, {
|
||||
-- update json file with any changes
|
||||
e_file = io.open(e_path, "w")
|
||||
if e_file then
|
||||
local data = core.write_json(e_list, true):gsub("\"remove\" : null", "\"remove\" : []")
|
||||
e_file:write(data)
|
||||
e_file:close()
|
||||
end
|
||||
|
||||
|
||||
for _, e in ipairs(e_list.remove) do
|
||||
cleaner.log("debug", "Cleaning entity: " .. e)
|
||||
|
||||
core.register_entity(":" .. e, {
|
||||
on_activate = function(self, staticdata)
|
||||
self.object:remove()
|
||||
end,
|
||||
|
|
Loading…
Add table
Reference in a new issue