perf: improve code to optimize memory usage and speed
This commit is contained in:
parent
22180abd04
commit
65d530f703
@ -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();
|
||||
}
|
||||
|
@ -1,8 +1,6 @@
|
||||
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.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
@ -10,6 +8,7 @@ import net.minecraft.client.renderer.texture.HttpTexture;
|
||||
import net.minecraft.client.renderer.texture.SimpleTexture;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
import org.spongepowered.asm.mixin.*;
|
||||
|
||||
import java.io.InputStream;
|
||||
@ -18,6 +17,9 @@ import java.util.concurrent.CompletableFuture;
|
||||
@Environment(EnvType.CLIENT)
|
||||
@Mixin(HttpTexture.class)
|
||||
public abstract class HttpTextureMixin extends SimpleTexture implements HttpTextureAccessor {
|
||||
@Shadow
|
||||
@Final
|
||||
private static Logger LOGGER;
|
||||
@Unique
|
||||
@Nullable
|
||||
protected NativeImage 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.image = NativeImage.read(buffer);
|
||||
buffer.rewind();
|
||||
var result = NativeImage.read(buffer);
|
||||
var result = NativeImage.read(stream);
|
||||
if (this.processLegacySkin) {
|
||||
this.image = this.processLegacySkin(this.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.image = new NativeImage(64, 64, true);
|
||||
this.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;
|
||||
}
|
||||
|
@ -4,27 +4,44 @@ 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.SkinTypeFix;
|
||||
import icu.puqns67.skintypefix.mixin.accessor.HttpTextureAccessor;
|
||||
import icu.puqns67.skintypefix.util.Utils;
|
||||
import icu.puqns67.skintypefix.util.image.Places;
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
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 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;
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
@Mixin(SkinManager.class)
|
||||
public class SkinManagerMixin {
|
||||
@Mutable
|
||||
@Final
|
||||
@Unique
|
||||
private TextureManager textureManager;
|
||||
|
||||
@Inject(method = "<init>", at = @At("TAIL"))
|
||||
public void onInit(TextureManager textureManager, Path path, MinecraftSessionService minecraftSessionService, Executor executor, CallbackInfo ci) {
|
||||
this.textureManager = textureManager;
|
||||
}
|
||||
|
||||
@Inject(method = "registerTextures", at = @At("TAIL"), cancellable = true)
|
||||
private void onRegisterTextures(
|
||||
UUID uuid,
|
||||
@ -48,14 +65,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());
|
||||
|
||||
// Wait skin loading if it needed fetch from web
|
||||
skinTexture.skinTypeFix$joinLoader();
|
||||
skinTexture.skinTypeFix$joinFuture();
|
||||
|
||||
// Get image from PlayerSkinTexture
|
||||
var skinImage = skinTexture.skinTypeFix$getImage();
|
||||
|
Loading…
x
Reference in New Issue
Block a user