modlib/minetest/media.lua

62 lines
2.0 KiB
Lua
Raw Normal View History

2021-08-29 11:41:10 +02:00
local minetest, modlib, pairs, ipairs
= minetest, modlib, pairs, ipairs
-- TODO support for server texture packs (and possibly client TPs in singleplayer?)
local media_foldernames = {"textures", "sounds", "media", "models", "locale"}
local media_extensions = modlib.table.set{
-- Textures
"png", "jpg", "bmp", "tga", "pcx", "ppm", "psd", "wal", "rgb";
-- Sounds
"ogg";
-- Models
"x", "b3d", "md2", "obj";
-- Translations
"tr";
}
local function collect_media(modname)
local media = {}
2023-03-04 15:00:09 +01:00
local function traverse(folderpath)
-- Traverse files (collect media)
local filenames = minetest.get_dir_list(folderpath, false)
2021-08-29 11:41:10 +02:00
for _, filename in pairs(filenames) do
local _, ext = modlib.file.get_extension(filename)
2021-08-29 11:41:10 +02:00
if media_extensions[ext] then
2023-03-04 15:00:09 +01:00
media[filename] = modlib.file.concat_path{folderpath, filename}
2021-08-29 11:41:10 +02:00
end
end
2023-03-04 15:00:09 +01:00
-- Traverse subfolders
local foldernames = minetest.get_dir_list(folderpath, true)
for _, foldername in pairs(foldernames) do
if not foldername:match"^[_%.]" then -- ignore hidden subfolders / subfolders starting with `_`
traverse(modlib.file.concat_path{folderpath, foldername})
2021-08-29 11:41:10 +02:00
end
end
end
2023-03-04 15:00:09 +01:00
for _, foldername in ipairs(media_foldernames) do -- order matters!
2021-08-29 11:41:10 +02:00
traverse(modlib.mod.get_resource(modname, foldername))
end
return media
end
-- TODO clean this up eventually
2021-08-29 11:41:10 +02:00
local paths = {}
local mods = {}
local overridden_paths = {}
2023-03-04 15:00:33 +01:00
local overridden_mods = {}
2021-08-29 11:41:10 +02:00
for _, mod in ipairs(modlib.minetest.get_mod_load_order()) do
local mod_media = collect_media(mod.name)
for medianame, path in pairs(mod_media) do
if paths[medianame] then
overridden_paths[medianame] = overridden_paths[medianame] or {}
table.insert(overridden_paths[medianame], paths[medianame])
2023-03-04 15:00:33 +01:00
overridden_mods[medianame] = overridden_mods[medianame] or {}
table.insert(overridden_mods[medianame], mods[medianame])
end
2021-08-29 11:41:10 +02:00
paths[medianame] = path
mods[medianame] = mod.name
2021-08-29 11:41:10 +02:00
end
end
2023-03-04 15:00:33 +01:00
return {paths = paths, mods = mods, overridden_paths = overridden_paths, overridden_mods = overridden_mods}