From d6f741a8b663ad979483d6890c748580dda1c14c Mon Sep 17 00:00:00 2001 From: Puqns67 Date: Mon, 10 Jun 2024 20:30:39 +0800 Subject: [PATCH] perf: improve code to optimize memory usage and speed --- .../mixin/accessor/HttpTextureAccessor.java | 2 +- .../mixin/patch/HttpTextureMixin.java | 25 +++++++++++-------- .../mixin/patch/SkinManagerMixin.java | 25 +++++++++++++++---- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/main/java/icu/puqns67/skintypefix/mixin/accessor/HttpTextureAccessor.java b/src/main/java/icu/puqns67/skintypefix/mixin/accessor/HttpTextureAccessor.java index 0cc1d65..ba44b5a 100644 --- a/src/main/java/icu/puqns67/skintypefix/mixin/accessor/HttpTextureAccessor.java +++ b/src/main/java/icu/puqns67/skintypefix/mixin/accessor/HttpTextureAccessor.java @@ -3,7 +3,7 @@ package icu.puqns67.skintypefix.mixin.accessor; import com.mojang.blaze3d.platform.NativeImage; public interface HttpTextureAccessor { - void skinTypeFix$joinLoader(); + void skinTypeFix$joinFuture(); NativeImage skinTypeFix$getImage(); } diff --git a/src/main/java/icu/puqns67/skintypefix/mixin/patch/HttpTextureMixin.java b/src/main/java/icu/puqns67/skintypefix/mixin/patch/HttpTextureMixin.java index 96b9778..97347e7 100644 --- a/src/main/java/icu/puqns67/skintypefix/mixin/patch/HttpTextureMixin.java +++ b/src/main/java/icu/puqns67/skintypefix/mixin/patch/HttpTextureMixin.java @@ -1,14 +1,13 @@ package icu.puqns67.skintypefix.mixin.patch; import com.mojang.blaze3d.platform.NativeImage; -import com.mojang.blaze3d.platform.TextureUtil; -import icu.puqns67.skintypefix.SkinTypeFix; import icu.puqns67.skintypefix.mixin.accessor.HttpTextureAccessor; import net.minecraft.client.renderer.texture.HttpTexture; import net.minecraft.client.renderer.texture.SimpleTexture; import net.minecraft.resources.ResourceLocation; import net.neoforged.api.distmarker.Dist; import net.neoforged.api.distmarker.OnlyIn; +import org.slf4j.Logger; import org.spongepowered.asm.mixin.*; import javax.annotation.Nullable; @@ -18,6 +17,9 @@ import java.util.concurrent.CompletableFuture; @OnlyIn(Dist.CLIENT) @Mixin(HttpTexture.class) public abstract class HttpTextureMixin extends SimpleTexture implements HttpTextureAccessor { + @Shadow + @Final + private static Logger LOGGER; @Unique @Nullable protected NativeImage skinTypeFix$image = null; @@ -37,7 +39,7 @@ public abstract class HttpTextureMixin extends SimpleTexture implements HttpText protected abstract NativeImage processLegacySkin(NativeImage image); @Unique - public void skinTypeFix$joinLoader() { + public void skinTypeFix$joinFuture() { if (this.future != null) { this.future.join(); } @@ -56,18 +58,19 @@ public abstract class HttpTextureMixin extends SimpleTexture implements HttpText @Overwrite private NativeImage load(InputStream stream) { try { - var buffer = TextureUtil.readResource(stream); - buffer.rewind(); - this.skinTypeFix$image = NativeImage.read(buffer); - buffer.rewind(); - var result = NativeImage.read(buffer); + var result = NativeImage.read(stream); if (this.processLegacySkin) { - this.skinTypeFix$image = this.processLegacySkin(this.skinTypeFix$image); result = this.processLegacySkin(result); + + // If this.processLegacySkin is true, the image is the player's skin, so a backup needs to be created for check + if (result != null) { + this.skinTypeFix$image = new NativeImage(64, 64, true); + this.skinTypeFix$image.copyFrom(result); + } } return result; - } catch (Exception exception) { - SkinTypeFix.LOGGER.warn("Error while loading the skin texture", exception); + } catch (Exception e) { + LOGGER.warn("Error while loading the skin texture", e); } return null; } diff --git a/src/main/java/icu/puqns67/skintypefix/mixin/patch/SkinManagerMixin.java b/src/main/java/icu/puqns67/skintypefix/mixin/patch/SkinManagerMixin.java index 52c49ce..53f89ea 100644 --- a/src/main/java/icu/puqns67/skintypefix/mixin/patch/SkinManagerMixin.java +++ b/src/main/java/icu/puqns67/skintypefix/mixin/patch/SkinManagerMixin.java @@ -4,28 +4,45 @@ import com.llamalad7.mixinextras.sugar.Local; import com.mojang.authlib.SignatureState; import com.mojang.authlib.minecraft.MinecraftProfileTexture; import com.mojang.authlib.minecraft.MinecraftProfileTextures; +import com.mojang.authlib.minecraft.MinecraftSessionService; import icu.puqns67.skintypefix.Config; import icu.puqns67.skintypefix.SkinTypeFix; import icu.puqns67.skintypefix.mixin.accessor.HttpTextureAccessor; import icu.puqns67.skintypefix.util.Utils; import icu.puqns67.skintypefix.util.image.Places; -import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.texture.TextureManager; import net.minecraft.client.resources.PlayerSkin; import net.minecraft.client.resources.SkinManager; import net.minecraft.resources.ResourceLocation; import net.neoforged.api.distmarker.Dist; import net.neoforged.api.distmarker.OnlyIn; +import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Mutable; +import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import java.nio.file.Path; import java.util.UUID; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executor; @OnlyIn(Dist.CLIENT) @Mixin(SkinManager.class) public class SkinManagerMixin { + @Mutable + @Final + @Unique + private TextureManager skinTypeFix$textureManager; + + @Inject(method = "", at = @At("TAIL")) + public void onInit(TextureManager textureManager, Path path, MinecraftSessionService minecraftSessionService, Executor executor, CallbackInfo ci) { + this.skinTypeFix$textureManager = textureManager; + } + @Inject(method = "registerTextures", at = @At("TAIL"), cancellable = true) private void onRegisterTextures( UUID uuid, @@ -49,14 +66,12 @@ public class SkinManagerMixin { return; } - var textureManager = Minecraft.getInstance().getTextureManager(); - CompletableFuture modelFuture = skinFuture.thenApply(v -> { // Get texture from TextureManager - var skinTexture = (HttpTextureAccessor) textureManager.getTexture(skinFuture.join()); + var skinTexture = (HttpTextureAccessor) skinTypeFix$textureManager.getTexture(skinFuture.join()); // Wait skin loading if it needed fetch from web - skinTexture.skinTypeFix$joinLoader(); + skinTexture.skinTypeFix$joinFuture(); // Get image from PlayerSkinTexture var skinImage = skinTexture.skinTypeFix$getImage();