diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/system/base/texture/skin/SkinManagerTest.kt b/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/system/base/texture/skin/SkinManagerTest.kt new file mode 100644 index 000000000..e81e2f330 --- /dev/null +++ b/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/system/base/texture/skin/SkinManagerTest.kt @@ -0,0 +1,47 @@ +/* + * Minosoft + * Copyright (C) 2020-2023 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.gui.rendering.system.base.texture.skin + +import de.bixilon.kotlinglm.vec2.Vec2i +import de.bixilon.minosoft.gui.rendering.system.base.texture.data.TextureData +import de.bixilon.minosoft.gui.rendering.textures.TextureUtil.readTexture +import de.bixilon.minosoft.test.IT +import org.testng.Assert.assertEquals +import org.testng.annotations.Test + +@Test(groups = ["rendering", "textures"]) +class SkinManagerTest { + val skin = IT.OBJENESIS.newInstance(SkinManager::class.java) + val readSkin = SkinManager::class.java.getDeclaredMethod("readSkin", ByteArray::class.java).apply { isAccessible = true } + + + private fun ByteArray.readSkin(): TextureData { + return readSkin.invoke(skin, this) as TextureData + } + + fun `automatically detect and fix legacy skin`() { + val old = SkinManager::class.java.getResourceAsStream("/skins/7af7c07d1ded61b1d3312685b32e4568ffdda762ec8d808895cc329a93d606e0.png").readAllBytes().readSkin() + val expected = SkinManager::class.java.getResourceAsStream("/skins/7af7c07d1ded61b1d3312685b32e4568ffdda762ec8d808895cc329a93d606e0_fixed.png")!!.readTexture() + + assertEquals(old.size, Vec2i(64, 64)) // fixed size + assertEquals(expected.size, Vec2i(64, 64)) + + old.buffer.rewind() + expected.buffer.rewind() + + // TextureUtil.dump(File("/home/moritz/test.png"), old.size, old.buffer, true, false) + assertEquals(old.buffer, expected.buffer) + } + +} diff --git a/src/integration-test/resources/skins/7af7c07d1ded61b1d3312685b32e4568ffdda762ec8d808895cc329a93d606e0.png b/src/integration-test/resources/skins/7af7c07d1ded61b1d3312685b32e4568ffdda762ec8d808895cc329a93d606e0.png new file mode 100644 index 000000000..cdff90f7a Binary files /dev/null and b/src/integration-test/resources/skins/7af7c07d1ded61b1d3312685b32e4568ffdda762ec8d808895cc329a93d606e0.png differ diff --git a/src/integration-test/resources/skins/7af7c07d1ded61b1d3312685b32e4568ffdda762ec8d808895cc329a93d606e0_fixed.png b/src/integration-test/resources/skins/7af7c07d1ded61b1d3312685b32e4568ffdda762ec8d808895cc329a93d606e0_fixed.png new file mode 100644 index 000000000..c1fee48fe Binary files /dev/null and b/src/integration-test/resources/skins/7af7c07d1ded61b1d3312685b32e4568ffdda762ec8d808895cc329a93d606e0_fixed.png differ diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/atlas/textures/AtlasTexture.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/atlas/textures/AtlasTexture.kt index 5b9aaea1b..41d814d89 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/atlas/textures/AtlasTexture.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/atlas/textures/AtlasTexture.kt @@ -22,6 +22,7 @@ import de.bixilon.minosoft.gui.rendering.system.base.texture.array.TextureArrayP import de.bixilon.minosoft.gui.rendering.system.base.texture.data.TextureData import de.bixilon.minosoft.gui.rendering.system.base.texture.texture.Texture import de.bixilon.minosoft.gui.rendering.system.base.texture.texture.TextureRenderData +import de.bixilon.minosoft.gui.rendering.textures.TextureUtil import de.bixilon.minosoft.gui.rendering.textures.properties.ImageProperties class AtlasTexture( @@ -42,18 +43,7 @@ class AtlasTexture( fun request(size: Vec2i): Vec2i? = null fun put(offset: Vec2i, source: TextureData, start: Vec2i, size: Vec2i): CodeTexturePart { - for (y in 0 until size.y) { - for (x in 0 until size.x) { - val sourceOffset = ((start.y + y) * source.size.x + (start.x + x)) * 4 - val destinationOffset = ((offset.y + y) * this.size.x + (offset.x + x)) * 4 - - data.buffer.put(destinationOffset + 0, source.buffer.get(sourceOffset + 0)) - data.buffer.put(destinationOffset + 1, source.buffer.get(sourceOffset + 1)) - data.buffer.put(destinationOffset + 2, source.buffer.get(sourceOffset + 2)) - data.buffer.put(destinationOffset + 3, source.buffer.get(sourceOffset + 3)) - } - } - + TextureUtil.copy(start, source, offset, this.data, size) return CodeTexturePart(this, pixel * offset, pixel * (offset + size), size) } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/texture/skin/SkinManager.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/texture/skin/SkinManager.kt index 4a90a296b..c3bf9a093 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/texture/skin/SkinManager.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/texture/skin/SkinManager.kt @@ -13,6 +13,7 @@ package de.bixilon.minosoft.gui.rendering.system.base.texture.skin +import de.bixilon.kotlinglm.vec2.Vec2i import de.bixilon.kutil.exception.ExceptionUtil.catchAll import de.bixilon.minosoft.assets.AssetsManager import de.bixilon.minosoft.config.profile.profiles.account.AccountProfileManager @@ -21,7 +22,11 @@ import de.bixilon.minosoft.data.entities.entities.player.PlayerEntity import de.bixilon.minosoft.data.entities.entities.player.local.LocalPlayerEntity import de.bixilon.minosoft.data.entities.entities.player.properties.PlayerProperties import de.bixilon.minosoft.gui.rendering.system.base.texture.TextureManager +import de.bixilon.minosoft.gui.rendering.system.base.texture.data.TextureData import de.bixilon.minosoft.gui.rendering.system.base.texture.skin.vanilla.DefaultSkinProvider +import de.bixilon.minosoft.gui.rendering.textures.TextureUtil +import de.bixilon.minosoft.gui.rendering.textures.TextureUtil.readTexture +import java.io.ByteArrayInputStream import java.util.* class SkinManager(private val textureManager: TextureManager) { @@ -51,7 +56,7 @@ class SkinManager(private val textureManager: TextureManager) { private fun getSkin(uuid: UUID, properties: PlayerProperties?, async: Boolean = true): PlayerSkin? { val texture = properties?.textures?.skin ?: return default[uuid] - return PlayerSkin(textureManager.dynamicTextures.pushRaw(texture.getHash(), async) { texture.read() }, default[uuid]?.texture, texture.metadata.model) + return PlayerSkin(textureManager.dynamicTextures.push(texture.getHash(), async) { texture.read().readSkin() }, default[uuid]?.texture, texture.metadata.model) } fun getSkin(player: PlayerEntity, properties: PlayerProperties? = null, fetch: Boolean = true, async: Boolean = true): PlayerSkin? { @@ -67,4 +72,20 @@ class SkinManager(private val textureManager: TextureManager) { return getSkin(uuid, properties ?: if (fetch) catchAll { PlayerProperties.fetch(uuid) } else null, async) } + + private fun ByteArray.readSkin(): TextureData { + val data = ByteArrayInputStream(this).readTexture() + if (data.size.y != 32) return data + + val next = TextureData(Vec2i(64)) + data.buffer.rewind() + next.buffer.put(data.buffer) + + TextureUtil.copy(Vec2i(0, 16), next, Vec2i(16, 48), next, Vec2i(16, 16)) // leg [0, 16][16,16] to left leg [16, 48] + TextureUtil.copy(Vec2i(40, 16), next, Vec2i(32, 48), next, Vec2i(16, 16)) // arm [40, 16] to left arm [32, 48] + + // TODO: flip + + return next + } } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/textures/TextureUtil.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/textures/TextureUtil.kt index 027bd1f30..e68b9c52c 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/textures/TextureUtil.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/textures/TextureUtil.kt @@ -123,4 +123,18 @@ object TextureUtil { ImageIO.write(bufferedImage, "png", file) } + + fun copy(sourceOffset: Vec2i, source: TextureData, targetOffset: Vec2i, target: TextureData, size: Vec2i) { + for (y in 0 until size.y) { + for (x in 0 until size.x) { + val sofs = ((sourceOffset.y + y) * source.size.x + (sourceOffset.x + x)) * 4 + val dofs = ((targetOffset.y + y) * target.size.x + (targetOffset.x + x)) * 4 + + target.buffer.put(dofs + 0, source.buffer.get(sofs + 0)) + target.buffer.put(dofs + 1, source.buffer.get(sofs + 1)) + target.buffer.put(dofs + 2, source.buffer.get(sofs + 2)) + target.buffer.put(dofs + 3, source.buffer.get(sofs + 3)) + } + } + } }