generated from your-land/yl_template
173 lines
5.9 KiB
Lua
173 lines
5.9 KiB
Lua
-- The functions and variables in this file are only for use in the mod itself.
|
|
-- Those that do real work should be local and wrapped in public functions
|
|
local function log(text)
|
|
local logmessage = yl_skinsdb_addon.t("log_prefix",
|
|
yl_skinsdb_addon.modname, text)
|
|
if yl_skinsdb_addon.settings.debug then
|
|
minetest.log("action", logmessage)
|
|
end
|
|
return logmessage
|
|
end
|
|
|
|
function yl_skinsdb_addon.log(text) return log(text) end
|
|
|
|
local delivered = {} -- delivered[filename][receiver] = true
|
|
local requested = {} -- requested[filename] = sender
|
|
|
|
local function load_skins(name, path_textures, callback_available)
|
|
-- Load skins from the current mod directory
|
|
local path_skins = yl_skinsdb_addon.worldpath .. path_textures
|
|
local dir_list = minetest.get_dir_list(path_skins)
|
|
local imported_skins_list = {}
|
|
local success
|
|
local good, bad, total = 0, 0, 0
|
|
|
|
if (not next(dir_list)) then
|
|
log("Skins not found in " .. dump(path_skins))
|
|
return false, "Skins not found in " .. dump(path_skins)
|
|
end
|
|
|
|
for _, filename in pairs(dir_list) do
|
|
if (yl_skinsdb_addon.data[filename] == nil) then
|
|
local dam_options = {filepath = path_skins .. DIR_DELIM .. filename}
|
|
delivered[filename] = {}
|
|
requested[filename] = name
|
|
local do_callback
|
|
|
|
if (callback_available == true) then
|
|
do_callback = function(p_name)
|
|
yl_skinsdb_addon.callback_dam(p_name, path_skins, filename)
|
|
end
|
|
else
|
|
yl_skinsdb_addon.callback_dam("", path_skins, filename)
|
|
end
|
|
local success_dam = minetest.dynamic_add_media(dam_options,
|
|
do_callback)
|
|
if (success_dam == true) then
|
|
yl_skinsdb_addon.data[filename] = true
|
|
imported_skins_list[filename] = true
|
|
good = good + 1
|
|
else
|
|
imported_skins_list[filename] = false
|
|
bad = bad + 1
|
|
end
|
|
total = total + 1
|
|
end
|
|
end
|
|
|
|
local return_message = "Total: " .. tostring(total) .. ", good: " ..
|
|
tostring(good) .. ", bad: " .. tostring(bad) ..
|
|
"\n"
|
|
for k, state in pairs(imported_skins_list) do
|
|
if (state == true) then
|
|
return_message = return_message .. "Import success: " .. k .. "\n"
|
|
elseif (state == false) then
|
|
return_message = return_message .. "Import failed: " .. k .. "\n"
|
|
end
|
|
end
|
|
|
|
if (bad == 0) then
|
|
success = true
|
|
else
|
|
success = false
|
|
end
|
|
|
|
return success, return_message
|
|
end
|
|
|
|
function yl_skinsdb_addon.load_skins(name, path_textures, callback)
|
|
return load_skins(name, path_textures, callback)
|
|
end
|
|
|
|
function yl_skinsdb_addon.callback_dam(name, path_skins, filename)
|
|
delivered[filename][name] = true
|
|
|
|
local players = minetest.get_connected_players()
|
|
|
|
for _, player in ipairs(players) do
|
|
local playername = player:get_player_name()
|
|
local can_receive = (minetest.get_player_information(playername)
|
|
.protocol_version >= 40)
|
|
local not_delivered = (delivered[filename][player:get_player_name()] ~=
|
|
true)
|
|
if (can_receive and not_delivered) then return false end
|
|
end
|
|
delivered[filename] = nil
|
|
|
|
-- Now every connected player received the file.
|
|
-- Let's register the skin and tell the person who imported
|
|
|
|
local sender = requested[filename] or ""
|
|
|
|
local success_rs, message_rs = skins.register_skin(path_skins, filename)
|
|
local return_message
|
|
if (success_rs == true) then
|
|
return_message = "Added skin " .. dump(filename)
|
|
else
|
|
return_message = "Cannot add skin " .. dump(filename) .. " Reason: " ..
|
|
dump(message_rs)
|
|
end
|
|
|
|
log(return_message)
|
|
minetest.chat_send_player(sender, return_message)
|
|
|
|
requested[filename] = nil
|
|
|
|
end
|
|
|
|
-- legacy, pre 5.9.0
|
|
|
|
local function load_skins_legacy(path_textures)
|
|
-- Load skins from the current mod directory
|
|
local path_skins = yl_skinsdb_addon.worldpath .. path_textures
|
|
local dir_list = minetest.get_dir_list(path_skins)
|
|
|
|
local good, bad, total = 0, 0, 0
|
|
|
|
if (not next(dir_list)) then
|
|
log("Skins not found in " .. dump(path_skins))
|
|
return false, "Skins not found in " .. dump(path_skins)
|
|
end
|
|
|
|
for _, filename in pairs(dir_list) do
|
|
total = total + 1
|
|
local dam_options = {filepath = path_skins .. DIR_DELIM .. filename}
|
|
local empty = function() end
|
|
local success_dam = minetest.dynamic_add_media(dam_options, empty)
|
|
if (success_dam == true) then
|
|
yl_skinsdb_addon.data[filename] = true
|
|
local success_rs, message_rs =
|
|
skins.register_skin(path_skins, filename)
|
|
if (success_rs ~= true) then
|
|
bad = bad + 1
|
|
log("Legacy import failed: " .. filename .. ", reason: " ..
|
|
dump(message_rs))
|
|
else
|
|
good = good + 1
|
|
end
|
|
else
|
|
bad = bad + 1
|
|
end
|
|
end
|
|
log("skin import done. Total: " .. tostring(total) .. ", good: " ..
|
|
tostring(good) .. ", bad: " .. tostring(bad))
|
|
end
|
|
|
|
-- /legacy, pre 5.9.0
|
|
|
|
local function detect_version()
|
|
-- if (true == true) then
|
|
if (minetest.features.dynamic_add_media_startup ~= true) then
|
|
-- 5.8.0 and lower:
|
|
minetest.after(0, load_skins_legacy,
|
|
yl_skinsdb_addon.settings.path_textures)
|
|
log("legacy import")
|
|
else
|
|
-- After 5.9.0:
|
|
yl_skinsdb_addon.load_skins("", yl_skinsdb_addon.settings.path_textures)
|
|
log("import")
|
|
end
|
|
end
|
|
|
|
function yl_skinsdb_addon.detect_version() return detect_version() end
|