perf: improve code to optimize memory usage and speed

This commit is contained in:
Puqns67 2024-06-10 20:30:39 +08:00
parent 0b02ba67eb
commit d6f741a8b6
Signed by: Puqns67
GPG Key ID: 9669DF042554F536
3 changed files with 35 additions and 17 deletions

View File

@ -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();
}

View File

@ -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;
}

View File

@ -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 = "<init>", 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<PlayerSkin.Model> 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();