From 57b815f91ffd9bdd165c741350c6099f6b08685b Mon Sep 17 00:00:00 2001
From: Alexander Weber <web.alexander@web.de>
Date: Sun, 18 Jun 2017 23:15:58 +0200
Subject: [PATCH] changes needed for character_creator support

---
 api.lua                    |  9 +--------
 init.lua                   |  7 ++++++-
 sfinv_page.lua             | 19 ++++++++++---------
 skin_meta_api.lua          | 12 ++++++++++++
 skinlist.lua               | 27 +++++++++------------------
 unified_inventory_page.lua | 12 ++++++------
 6 files changed, 44 insertions(+), 42 deletions(-)

diff --git a/api.lua b/api.lua
index b77c7c4..492024f 100644
--- a/api.lua
+++ b/api.lua
@@ -26,12 +26,5 @@ end
 -- update visuals
 skins.update_player_skin = function(player)
 	local skin = skins.get_player_skin(player)
-	player:set_properties({
-		textures = {skin:get_texture()},
-	})
+	skin:set_skin(player)
 end
-
--- Update skin on join
-minetest.register_on_joinplayer(function(player)
-	skins.update_player_skin(player)
-end)
diff --git a/init.lua b/init.lua
index b9abc7a..105a68a 100644
--- a/init.lua
+++ b/init.lua
@@ -26,10 +26,15 @@ end
 if minetest.global_exists("armor") then
 	armor.get_player_skin = function(self, name)
 		local skin = skins.get_player_skin(minetest.get_player_by_name(name))
-		return skin:get_meta("_key") --3d_armor adds a ".png" but it should be compatible in most cases
+		return skin:get_texture()
 	end
 	armor.get_preview = function(self, name)
 		local skin = skins.get_player_skin(minetest.get_player_by_name(name))
 		return skin:get_preview()
 	end
 end
+
+-- Update skin on join
+minetest.register_on_joinplayer(function(player)
+	skins.update_player_skin(player)
+end)
diff --git a/sfinv_page.lua b/sfinv_page.lua
index c462efe..4e429d1 100644
--- a/sfinv_page.lua
+++ b/sfinv_page.lua
@@ -9,15 +9,16 @@ end
 local function get_formspec(player, context)
 	local name = player:get_player_name()
 	local skin = skins.get_player_skin(player)
-
-	-- overview page
-	local formspec = "image[0,.75;1,2;"..skin:get_preview().."]"
-		.."label[6,.5;"..S("Raw texture")..":]"
-		.."image[6,1;2,1;"..skin:get_texture().."]"
-
+	local texture = skin:get_texture()
 	local m_name = skin:get_meta_string("name")
 	local m_author = skin:get_meta_string("author")
 	local m_license = skin:get_meta_string("license")
+	-- overview page
+	local formspec = "image[0,.75;1,2;"..skin:get_preview().."]"
+	if texture then
+		formspec = formspec.."label[6,.5;"..S("Raw texture")..":]"
+		.."image[6,1;2,1;"..skin:get_texture().."]"
+	end
 	if m_name ~= "" then
 		formspec = formspec.."label[2,.5;"..S("Name")..": "..minetest.formspec_escape(m_name).."]"
 	end
@@ -62,10 +63,10 @@ local function get_formspec(player, context)
 		page_next = 1
 	end
 	local page_list = ""
-	dropdown_values = {}
+	context.dropdown_values = {}
 	for pg=1, context.total_pages do
 		local pagename = S("Page").." "..pg.."/"..context.total_pages
-		dropdown_values[pagename] = pg
+		context.dropdown_values[pagename] = pg
 		if pg > 1 then page_list = page_list.."," end
 		page_list = page_list..pagename
 	end
@@ -106,7 +107,7 @@ sfinv.register_page("skins:overview", {
 			end
 		end
 		if fields.skins_selpg then
-			context.skins_page = tonumber(dropdown_values[fields.skins_selpg])
+			context.skins_page = tonumber(context.dropdown_values[fields.skins_selpg])
 			sfinv.set_player_inventory_formspec(player)
 			return
 		end
diff --git a/skin_meta_api.lua b/skin_meta_api.lua
index 367dee1..f01671c 100644
--- a/skin_meta_api.lua
+++ b/skin_meta_api.lua
@@ -54,3 +54,15 @@ end
 function skin_class:get_preview()
 	return self._preview or "player.png"
 end
+
+function skin_class:set_skin(player)
+	player:set_properties({
+		visual_size = {
+			x = 1,
+			y = 1
+		}
+	})
+	player:set_properties({
+		textures = {self:get_texture()},
+	})
+end
diff --git a/skinlist.lua b/skinlist.lua
index 328e518..e1be623 100644
--- a/skinlist.lua
+++ b/skinlist.lua
@@ -1,7 +1,6 @@
 
 local skins_dir_list = minetest.get_dir_list(skins.modpath.."/textures")
-local unsorted_skinslist = {}
-local sorted_skinslist
+
 for _, fn in pairs(skins_dir_list) do
 	local nameparts = string.gsub(fn, "[.]", "_"):split("_")
 
@@ -43,28 +42,20 @@ for _, fn in pairs(skins_dir_list) do
 			else
 				skin_obj:set_meta("name", name)
 			end
-			table.insert(unsorted_skinslist, skin_obj)
 		end
 	end
 end
 
 -- get skinlist. If assignment given ("mod:wardrobe" or "player:bell07") select skins matches the assignment. select_unassigned selects the skins without any assignment too
 function skins.get_skinlist(assignment, select_unassigned)
-	-- sort on demand
-	if not sorted_skinslist then
-		table.sort(unsorted_skinslist, function(a,b) return a:get_meta("_sort_id") < b:get_meta("_sort_id") end)
-		sorted_skinslist = unsorted_skinslist
-	end
-	if not assignment then
-		return sorted_skinslist
-	else
-		local ret = {}
-		for _, skin in ipairs(sorted_skinslist) do
-			if assignment == skin:get_meta("assignment") or
-					(select_unassigned and skin:get_meta("assignment") == nil) then
-				table.insert(ret, skin)
-			end
+	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)
 		end
-		return ret
 	end
+	table.sort(skinslist, function(a,b) return a:get_meta("_sort_id") < b:get_meta("_sort_id") end)
+	return skinslist
 end
diff --git a/unified_inventory_page.lua b/unified_inventory_page.lua
index 1892263..0bbb854 100644
--- a/unified_inventory_page.lua
+++ b/unified_inventory_page.lua
@@ -12,15 +12,15 @@ unified_inventory.register_page("skins", {
 	get_formspec = function(player)
 		local name = player:get_player_name()
 		local skin = skins.get_player_skin(player)
-		local formspec = ("background[0.06,0.99;7.92,7.52;ui_misc_form.png]"
-			.."image[0,.75;1,2;"..skin:get_preview().."]"
-			.."label[6,.5;"..S("Raw texture")..":]"
-			.."image[6,1;2,1;"..skin:get_texture().."]")
-
-
+		local texture = skin:get_texture()
 		local m_name = skin:get_meta_string("name")
 		local m_author = skin:get_meta_string("author")
 		local m_license = skin:get_meta_string("license")
+		local formspec = "background[0.06,0.99;7.92,7.52;ui_misc_form.png]".."image[0,.75;1,2;"..skin:get_preview().."]"
+		if texture then
+			formspec=formspec.."label[6,.5;"..S("Raw texture")..":]"
+			.."image[6,1;2,1;"..texture.."]"
+		end
 		if m_name ~= "" then
 			formspec = formspec.."label[2,.5;"..S("Name")..": "..minetest.formspec_escape(m_name).."]"
 		end