Update init.lua and unused_textures command

This commit is contained in:
DragonWrangler1 2024-06-23 16:13:17 -05:00 committed by GitHub
parent 9b231a6539
commit 6786eb73fd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

313
init.lua
View File

@ -1,17 +1,39 @@
local S = minetest.get_translator("modding_commands") local S = minetest.get_translator("modding_commands")
local ie = minetest.request_insecure_environment()
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
local player_name = player:get_player_name() local player_name = player:get_player_name()
local message = minetest.colorize("#00FFFF", "<Modding Commands> Please make backups of any files you plan to modify with any of the commands from the modding_commands mod. This mod is still experimental, but fully functional (for the most part. Besides the commands that aren't included ingame). Please keep all backups until you verify the modified files work properly.") if minetest.settings:get_bool('backup_warning', true) then
local message = minetest.colorize("#00FFFF", "<Modding Commands> Please make backups of any files you plan to modify with any of the commands from the modding_commands mod. This mod is still experimental, but fully functional. Please keep all backups until you verify the modified files work properly. To disable this message please use the chat command backup_message. With param false.")
minetest.chat_send_player(player_name, message)
end
if not ie then if not ie then
local disclamer = minetest.colorize("#FF4000", "This mod requires an insecure environment, if you haven't done so already, please add it to secure.trusted_mods. If you don't trust it please examine the code, before you show it as trusted.") local disclamer = minetest.colorize("#FF4000", "This mod requires an insecure environment, please add it to secure.trusted_mods. If you don't trust it please examine the code.")
minetest.chat_send_player(player_name, disclamer) minetest.chat_send_player(player_name, disclamer)
end end
end) end)
local DIR_DELIM = "/" local DIR_DELIM = "/"
local ie = minetest.request_insecure_environment() minetest.register_chatcommand("backup_warning_message", {
params = "<false> <true>",
description = "Enables or disables the create backup message",
privs = {server = true},
func = function(name, param)
-- Convert param to lowercase to handle case insensitivity
param = param:lower()
if param == "false" then
minetest.settings:set("backup_warning", "false")
return true, "Backup warning message has been disabled."
elseif param == "true" then
minetest.settings:set("backup_warning", "true")
return true, "Backup warning message has been enabled."
else
return false, "Invalid parameters. Usage: /backup_warning_message false or /backup_warning_message true"
end
end
})
-- The following script checks for invalid whitespace areas. -- The following script checks for invalid whitespace areas.
minetest.register_chatcommand("check_whitespace", { minetest.register_chatcommand("check_whitespace", {
@ -625,130 +647,167 @@ minetest.register_chatcommand("list_schems", {
}) })
minetest.register_chatcommand("unused_textures", { minetest.register_chatcommand("unused_textures", {
params = "<modname>", params = "<modname>",
description = "Find unused textures in a specified mod", description = "Find unused textures in a specified mod",
privs = {server=true}, privs = {server=true},
func = function(name, param) func = function(name, param)
if param == "" then if param == "" then
return false, "You must specify a mod name." return false, "You must specify a mod name."
end end
local modname = param local modname = param
local modpath = minetest.get_modpath(modname) local modpath = minetest.get_modpath(modname)
if not modpath then if not modpath then
return false, "Mod '" .. modname .. "' not found." return false, "Mod '" .. modname .. "' not found."
end end
local texture_path = modpath .. "/textures" local texture_path = modpath .. "/textures"
local textures = {} local textures = {}
local function list_files_in_directory(directory)
local files = {} local function list_files_in_directory(directory)
local p = ie.io.popen('ls "' .. directory .. '"') return minetest.get_dir_list(directory, false)
for filename in p:lines() do end
table.insert(files, filename)
end local texture_files = list_files_in_directory(texture_path)
p:close() for _, filename in ipairs(texture_files) do
return files if filename:match("%.png$") then
end textures[filename] = true
local texture_files = list_files_in_directory(texture_path) end
for _, filename in ipairs(texture_files) do end
if filename:match("%.png$") then
textures[filename] = true local function mark_texture_used(texture)
end for sub_texture in texture:gmatch("[^%^]+") do
end sub_texture = sub_texture:gsub("%s+", "") -- Remove any whitespace
local function mark_texture_used(texture) if textures[sub_texture] then
for sub_texture in texture:gmatch("[^%^]+") do textures[sub_texture] = nil
sub_texture = sub_texture:gsub("%s+", "")-- Remove any whitespace end
if textures[sub_texture] then end
textures[sub_texture] = nil end
end
end local function strip_comments(lua_code)
end lua_code = lua_code:gsub("%-%-%[%[.-%]%]", "") -- Remove block comments
local function parse_lua_file(file_path) lua_code = lua_code:gsub("%-%-.-\n", "\n") -- Remove line comments
local file = ie.io.open(file_path, "r") return lua_code
if not file then end
return
end local function parse_lua_file(file_path)
local lua_code = file:read("*all") local file = io.open(file_path, "r")
file:close() if not file then
for str in lua_code:gmatch('"([^"]+%.png)"') do return
mark_texture_used(str) end
end local lua_code = file:read("*all")
end file:close()
local function scan_lua_files(directory) lua_code = strip_comments(lua_code)
for _, filename in ipairs(list_files_in_directory(directory)) do for str in lua_code:gmatch('[%a%d_.]+%.png') do
if filename:match("%.lua$") then mark_texture_used(str)
parse_lua_file(directory .. "/" .. filename) end
end end
end
end local function scan_files(directory)
scan_lua_files(modpath) for _, filename in ipairs(list_files_in_directory(directory)) do
for name, def in pairs(minetest.registered_nodes) do local filepath = directory .. "/" .. filename
if type(def.tiles) == "table" then if filename:match("%.lua$") then
for _, texture in ipairs(def.tiles) do parse_lua_file(filepath)
if type(texture) == "string" then end
mark_texture_used(texture) end
end end
end
elseif type(def.tiles) == "string" then scan_files(modpath)
mark_texture_used(def.tiles)
end -- Scan nodes
if type(def.overlay_tiles) == "table" then for name, def in pairs(minetest.registered_nodes) do
for _, texture in ipairs(def.overlay_tiles) do if type(def.tiles) == "table" then
if type(texture) == "string" then for _, texture in ipairs(def.tiles) do
mark_texture_used(texture) if type(texture) == "string" then
end mark_texture_used(texture)
end end
elseif type(def.overlay_tiles) == "string" then end
mark_texture_used(def.overlay_tiles) elseif type(def.tiles) == "string" then
end mark_texture_used(def.tiles)
end end
for name, def in pairs(minetest.registered_items) do if type(def.overlay_tiles) == "table" then
if type(def.inventory_image) == "string" then for _, texture in ipairs(def.overlay_tiles) do
mark_texture_used(def.inventory_image) if type(texture) == "string" then
end mark_texture_used(texture)
if type(def.wield_image) == "string" then end
mark_texture_used(def.wield_image) end
end elseif type(def.overlay_tiles) == "string" then
if type(def.tiles) == "table" then mark_texture_used(def.overlay_tiles)
for _, texture in ipairs(def.tiles) do end
if type(texture) == "string" then end
mark_texture_used(texture)
end -- Scan items
end for name, def in pairs(minetest.registered_items) do
elseif type(def.tiles) == "string" then if type(def.inventory_image) == "string" then
mark_texture_used(def.tiles) mark_texture_used(def.inventory_image)
end end
end if type(def.wield_image) == "string" then
for name, def in pairs(minetest.registered_entities) do mark_texture_used(def.wield_image)
if type(def.textures) == "table" then end
for _, texture in ipairs(def.textures) do if type(def.tiles) == "table" then
if type(texture) == "string" then for _, texture in ipairs(def.tiles) do
mark_texture_used(texture) if type(texture) == "string" then
end mark_texture_used(texture)
end end
elseif type(def.textures) == "string" then end
mark_texture_used(def.textures) elseif type(def.tiles) == "string" then
end mark_texture_used(def.tiles)
end end
for _, def in pairs(minetest.registered_particles or {}) do end
if type(def.texture) == "string" then
mark_texture_used(def.texture) -- Scan entities
end for name, def in pairs(minetest.registered_entities) do
end if type(def.textures) == "table" then
for _, def in pairs(minetest.registered_particlespawners or {}) do for _, texture in ipairs(def.textures) do
if type(def.texture) == "string" then if type(texture) == "string" then
mark_texture_used(def.texture) mark_texture_used(texture)
end end
end end
local unused_textures = {} elseif type(def.textures) == "string" then
for texture, _ in pairs(textures) do mark_texture_used(def.textures)
table.insert(unused_textures, texture) end
end end
if #unused_textures == 0 then
return true, "No unused textures found." -- Scan particles
else for _, def in pairs(minetest.registered_particles or {}) do
return true, "Unused textures:\n" .. table.concat(unused_textures, "\n") if type(def.texture) == "string" then
end mark_texture_used(def.texture)
end end
if type(def.texpool) == "table" then
for _, texture in ipairs(def.texpool) do
if type(texture) == "string" then
mark_texture_used(texture)
end
end
elseif type(def.texpool) == "string" then
mark_texture_used(def.texpool)
end
end
-- Scan particlespawners
for _, def in pairs(minetest.registered_particlespawners or {}) do
if type(def.texture) == "string" then
mark_texture_used(def.texture)
end
if type(def.texpool) == "table" then
for _, texture in ipairs(def.texpool) do
if type(texture) == "string" then
mark_texture_used(texture)
end
end
elseif type(def.texpool) == "string" then
mark_texture_used(def.texpool)
end
end
local unused_textures = {}
for texture, _ in pairs(textures) do
table.insert(unused_textures, texture)
end
if #unused_textures == 0 then
return true, "No unused textures found."
else
return true, "Unused textures:\n" .. table.concat(unused_textures, "\n")
end
end
}) })
-- Same as bulk_replace except it should work with all enabled mods -- Same as bulk_replace except it should work with all enabled mods