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

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", {
@ -639,48 +661,58 @@ minetest.register_chatcommand("unused_textures", {
end end
local texture_path = modpath .. "/textures" local texture_path = modpath .. "/textures"
local textures = {} local textures = {}
local function list_files_in_directory(directory) local function list_files_in_directory(directory)
local files = {} return minetest.get_dir_list(directory, false)
local p = ie.io.popen('ls "' .. directory .. '"')
for filename in p:lines() do
table.insert(files, filename)
end
p:close()
return files
end end
local texture_files = list_files_in_directory(texture_path) local texture_files = list_files_in_directory(texture_path)
for _, filename in ipairs(texture_files) do for _, filename in ipairs(texture_files) do
if filename:match("%.png$") then if filename:match("%.png$") then
textures[filename] = true textures[filename] = true
end end
end end
local function mark_texture_used(texture) local function mark_texture_used(texture)
for sub_texture in texture:gmatch("[^%^]+") do for sub_texture in texture:gmatch("[^%^]+") do
sub_texture = sub_texture:gsub("%s+", "")-- Remove any whitespace sub_texture = sub_texture:gsub("%s+", "") -- Remove any whitespace
if textures[sub_texture] then if textures[sub_texture] then
textures[sub_texture] = nil textures[sub_texture] = nil
end end
end end
end end
local function strip_comments(lua_code)
lua_code = lua_code:gsub("%-%-%[%[.-%]%]", "") -- Remove block comments
lua_code = lua_code:gsub("%-%-.-\n", "\n") -- Remove line comments
return lua_code
end
local function parse_lua_file(file_path) local function parse_lua_file(file_path)
local file = ie.io.open(file_path, "r") local file = io.open(file_path, "r")
if not file then if not file then
return return
end end
local lua_code = file:read("*all") local lua_code = file:read("*all")
file:close() file:close()
for str in lua_code:gmatch('"([^"]+%.png)"') do lua_code = strip_comments(lua_code)
for str in lua_code:gmatch('[%a%d_.]+%.png') do
mark_texture_used(str) mark_texture_used(str)
end end
end end
local function scan_lua_files(directory)
local function scan_files(directory)
for _, filename in ipairs(list_files_in_directory(directory)) do for _, filename in ipairs(list_files_in_directory(directory)) do
local filepath = directory .. "/" .. filename
if filename:match("%.lua$") then if filename:match("%.lua$") then
parse_lua_file(directory .. "/" .. filename) parse_lua_file(filepath)
end end
end end
end end
scan_lua_files(modpath)
scan_files(modpath)
-- Scan nodes
for name, def in pairs(minetest.registered_nodes) do for name, def in pairs(minetest.registered_nodes) do
if type(def.tiles) == "table" then if type(def.tiles) == "table" then
for _, texture in ipairs(def.tiles) do for _, texture in ipairs(def.tiles) do
@ -701,6 +733,8 @@ minetest.register_chatcommand("unused_textures", {
mark_texture_used(def.overlay_tiles) mark_texture_used(def.overlay_tiles)
end end
end end
-- Scan items
for name, def in pairs(minetest.registered_items) do for name, def in pairs(minetest.registered_items) do
if type(def.inventory_image) == "string" then if type(def.inventory_image) == "string" then
mark_texture_used(def.inventory_image) mark_texture_used(def.inventory_image)
@ -718,6 +752,8 @@ minetest.register_chatcommand("unused_textures", {
mark_texture_used(def.tiles) mark_texture_used(def.tiles)
end end
end end
-- Scan entities
for name, def in pairs(minetest.registered_entities) do for name, def in pairs(minetest.registered_entities) do
if type(def.textures) == "table" then if type(def.textures) == "table" then
for _, texture in ipairs(def.textures) do for _, texture in ipairs(def.textures) do
@ -729,16 +765,39 @@ minetest.register_chatcommand("unused_textures", {
mark_texture_used(def.textures) mark_texture_used(def.textures)
end end
end end
-- Scan particles
for _, def in pairs(minetest.registered_particles or {}) do for _, def in pairs(minetest.registered_particles or {}) do
if type(def.texture) == "string" then if type(def.texture) == "string" then
mark_texture_used(def.texture) 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
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 for _, def in pairs(minetest.registered_particlespawners or {}) do
if type(def.texture) == "string" then if type(def.texture) == "string" then
mark_texture_used(def.texture) 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
end
elseif type(def.texpool) == "string" then
mark_texture_used(def.texpool)
end
end
local unused_textures = {} local unused_textures = {}
for texture, _ in pairs(textures) do for texture, _ in pairs(textures) do
table.insert(unused_textures, texture) table.insert(unused_textures, texture)