2017-06-15 13:45:42 +02:00
local skins_dir_list = minetest.get_dir_list ( skins.modpath .. " /textures " )
2017-06-18 23:15:58 +02:00
2017-06-15 13:45:42 +02:00
for _ , fn in pairs ( skins_dir_list ) do
2018-07-16 20:42:54 +02:00
local name , sort_id , assignment , is_preview , playername
2020-02-12 08:09:17 +01:00
local nameparts = string.gsub ( fn , " [.] " , " _ " ) : split ( " _ " )
2017-06-18 17:56:24 +02:00
2018-07-16 20:42:54 +02:00
-- check allowed prefix and file extension
if ( nameparts [ 1 ] == ' player ' or nameparts [ 1 ] == ' character ' ) and
nameparts [ # nameparts ] : lower ( ) == ' png ' then
-- cut filename extension
table.remove ( nameparts , # nameparts )
-- check preview suffix
if nameparts [ # nameparts ] == ' preview ' then
is_preview = true
table.remove ( nameparts , # nameparts )
2017-07-24 17:39:16 +02:00
end
2018-07-16 20:42:54 +02:00
-- Build technically skin name
name = table.concat ( nameparts , ' _ ' )
-- Handle metadata from file name
if not is_preview then
-- Get player name
if nameparts [ 1 ] == " player " then
playername = nameparts [ 2 ]
table.remove ( nameparts , 1 )
sort_id = 0
else
sort_id = 5000
end
-- Get sort index
if tonumber ( nameparts [ # nameparts ] ) then
sort_id = sort_id + nameparts [ # nameparts ]
end
2017-06-18 17:56:24 +02:00
end
2017-06-16 23:16:22 +02:00
local skin_obj = skins.get ( name ) or skins.new ( name )
2017-06-18 17:56:24 +02:00
if is_preview then
2017-06-16 23:00:36 +02:00
skin_obj : set_preview ( fn )
2017-06-15 13:45:42 +02:00
else
2017-06-18 17:56:24 +02:00
skin_obj : set_texture ( fn )
skin_obj : set_meta ( " _sort_id " , sort_id )
2018-01-07 14:50:22 +01:00
if playername then
2018-07-16 20:42:54 +02:00
skin_obj : set_meta ( " assignment " , " player: " .. playername )
2018-01-07 14:50:22 +01:00
skin_obj : set_meta ( " playername " , playername )
end
2018-04-15 01:09:38 +02:00
local file = io.open ( skins.modpath .. " /textures/ " .. fn , " r " )
skin_obj : set_meta ( " format " , skins.get_skin_format ( file ) )
file : close ( )
file = io.open ( skins.modpath .. " /meta/ " .. name .. " .txt " , " r " )
2017-06-15 13:45:42 +02:00
if file then
local data = string.split ( file : read ( " *all " ) , " \n " , 3 )
file : close ( )
2017-06-16 23:00:36 +02:00
skin_obj : set_meta ( " name " , data [ 1 ] )
skin_obj : set_meta ( " author " , data [ 2 ] )
skin_obj : set_meta ( " license " , data [ 3 ] )
2017-06-18 17:56:24 +02:00
else
2018-07-16 20:42:54 +02:00
-- remove player / character prefix if further naming given
if nameparts [ 2 ] and not tonumber ( nameparts [ 2 ] ) then
table.remove ( nameparts , 1 )
end
skin_obj : set_meta ( " name " , table.concat ( nameparts , ' ' ) )
2017-06-15 13:45:42 +02:00
end
end
2014-07-23 13:46:42 +02:00
end
2017-02-09 23:06:25 +01:00
end
2017-06-15 13:45:42 +02:00
2019-08-09 06:54:47 +02:00
local function skins_sort ( skinslist )
table.sort ( skinslist , function ( a , b )
2019-10-29 08:11:38 +01:00
local a_id = a : get_meta ( " _sort_id " ) or 10000
local b_id = b : get_meta ( " _sort_id " ) or 10000
2019-08-09 06:54:47 +02:00
if a_id ~= b_id then
return a : get_meta ( " _sort_id " ) < b : get_meta ( " _sort_id " )
else
return a : get_meta ( " name " ) < b : get_meta ( " name " )
end
end )
end
2018-01-07 14:50:22 +01:00
-- (obsolete) get skinlist. If assignment given ("mod:wardrobe" or "player:bell07") select skins matches the assignment. select_unassigned selects the skins without any assignment too
2017-06-18 17:56:24 +02:00
function skins . get_skinlist ( assignment , select_unassigned )
2018-01-07 14:50:22 +01:00
minetest.log ( " deprecated " , " skins.get_skinlist() is deprecated. Use skins.get_skinlist_for_player() instead " )
2017-06-18 23:15:58 +02:00
local skinslist = { }
for _ , skin in pairs ( skins.meta ) do
if not assignment or
assignment == skin : get_meta ( " assignment " ) or
( select_unassigned and skin : get_meta ( " assignment " ) == nil ) then
table.insert ( skinslist , skin )
2017-06-17 00:23:39 +02:00
end
end
2019-08-09 06:54:47 +02:00
skins_sort ( skinslist )
2017-06-18 23:15:58 +02:00
return skinslist
2017-06-15 16:16:57 +02:00
end
2018-01-07 14:50:22 +01:00
-- Get skinlist for player. If no player given, public skins only selected
function skins . get_skinlist_for_player ( playername )
local skinslist = { }
for _ , skin in pairs ( skins.meta ) do
2018-01-07 20:30:27 +01:00
if skin : is_applicable_for_player ( playername ) and skin : get_meta ( " in_inventory_list " ) ~= false then
2018-01-07 14:50:22 +01:00
table.insert ( skinslist , skin )
end
end
2019-08-09 06:54:47 +02:00
skins_sort ( skinslist )
2018-01-07 14:50:22 +01:00
return skinslist
end
2018-01-07 19:09:52 +01:00
-- Get skinlist selected by metadata
function skins . get_skinlist_with_meta ( key , value )
assert ( key , " key parameter for skins.get_skinlist_with_meta() missed " )
local skinslist = { }
for _ , skin in pairs ( skins.meta ) do
if skin : get_meta ( key ) == value then
table.insert ( skinslist , skin )
end
end
2019-08-09 06:54:47 +02:00
skins_sort ( skinslist )
2018-01-07 19:09:52 +01:00
return skinslist
end