skinlist.lua 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. local skins_dir_list = minetest.get_dir_list(skins.modpath.."/textures")
  2. for _, fn in pairs(skins_dir_list) do
  3. local nameparts = string.gsub(fn, "[.]", "_"):split("_")
  4. local name, sort_id, assignment, is_preview
  5. if nameparts[1] == "character" then
  6. if tonumber(nameparts[2]) == nil then --default skin character.png
  7. sort_id = 5000
  8. name = "character"
  9. is_preview = (nameparts[2] == "preview")
  10. else
  11. sort_id = tonumber(nameparts[2])+5000
  12. name = "character_"..nameparts[2]
  13. is_preview = (nameparts[3] == "preview")
  14. end
  15. elseif nameparts[1] == "player" then
  16. assignment = "player:"..nameparts[2]
  17. name = "player_"..nameparts[2]
  18. if tonumber(nameparts[3]) then
  19. sort_id = tonumber(nameparts[3])
  20. is_preview = (nameparts[4] == "preview")
  21. name = name.."_"..nameparts[3]
  22. else
  23. sort_id = 1
  24. is_preview = (nameparts[3] == "preview")
  25. end
  26. end
  27. if name then
  28. local skin_obj = skins.get(name) or skins.new(name)
  29. if is_preview then
  30. skin_obj:set_preview(fn)
  31. else
  32. skin_obj:set_texture(fn)
  33. skin_obj:set_meta("_sort_id", sort_id)
  34. if assignment then
  35. skin_obj:set_meta("assignment", assignment)
  36. end
  37. local file = io.open(skins.modpath.."/meta/"..name..".txt", "r")
  38. if file then
  39. local data = string.split(file:read("*all"), "\n", 3)
  40. file:close()
  41. skin_obj:set_meta("name", data[1])
  42. skin_obj:set_meta("author", data[2])
  43. skin_obj:set_meta("license", data[3])
  44. else
  45. skin_obj:set_meta("name", name)
  46. end
  47. end
  48. end
  49. end
  50. -- get skinlist. If assignment given ("mod:wardrobe" or "player:bell07") select skins matches the assignment. select_unassigned selects the skins without any assignment too
  51. function skins.get_skinlist(assignment, select_unassigned)
  52. local skinslist = {}
  53. for _, skin in pairs(skins.meta) do
  54. if not assignment or
  55. assignment == skin:get_meta("assignment") or
  56. (select_unassigned and skin:get_meta("assignment") == nil) then
  57. table.insert(skinslist, skin)
  58. end
  59. end
  60. table.sort(skinslist, function(a,b) return a:get_meta("_sort_id") < b:get_meta("_sort_id") end)
  61. return skinslist
  62. end