From e19b9d860fb6b94963dcca70dbb0fc225d3f375e Mon Sep 17 00:00:00 2001 From: ZekerZhayard Date: Wed, 11 Dec 2019 18:53:57 +0800 Subject: [PATCH] Support Fabric for Minecraft 1.15.x (#75) * Support Fabric for Minecraft 1.15.x * Remove unused imports --- .../customskinloader/fake/FakeSkinBuffer.java | 4 ++ .../fake/FakeSkinManager.java | 16 ++--- .../customskinloader/utils/MinecraftUtil.java | 38 ++++++++++-- .../client/renderer/IImageBuffer.java | 13 ++++ Fabric/build.gradle | 4 +- Fabric/resource/fabric.mod.json | 8 +-- Fabric/resource/mixins.customskinloader.json | 2 + .../fabric/MixinConfigPlugin.java | 61 +++++++++++++++++++ .../fabric/mixin/MixinFakeSkinManager.java | 11 ++++ .../fabric/mixin/MixinSkinManager.java | 8 ++- Forge/build.gradle | 2 +- 11 files changed, 144 insertions(+), 23 deletions(-) create mode 100644 Common/source/net/minecraft/client/renderer/IImageBuffer.java create mode 100644 Fabric/source/customskinloader/fabric/MixinConfigPlugin.java create mode 100644 Fabric/source/customskinloader/fabric/mixin/MixinFakeSkinManager.java diff --git a/Common/source/customskinloader/fake/FakeSkinBuffer.java b/Common/source/customskinloader/fake/FakeSkinBuffer.java index 15cecd3..62e65d0 100644 --- a/Common/source/customskinloader/fake/FakeSkinBuffer.java +++ b/Common/source/customskinloader/fake/FakeSkinBuffer.java @@ -145,6 +145,10 @@ public class FakeSkinBuffer implements net.minecraft.client.renderer.IImageBuffe //A callback when skin loaded, nothing to do } + public void run() { + this.skinAvailable(); + } + private static int getARGB(int a, int r, int g, int b) { return (a << 24) | (r << 16) | (g << 8) | b; } diff --git a/Common/source/customskinloader/fake/FakeSkinManager.java b/Common/source/customskinloader/fake/FakeSkinManager.java index 0aa0b02..38a844e 100644 --- a/Common/source/customskinloader/fake/FakeSkinManager.java +++ b/Common/source/customskinloader/fake/FakeSkinManager.java @@ -13,15 +13,13 @@ import com.mojang.authlib.GameProfile; import com.mojang.authlib.minecraft.MinecraftProfileTexture; import com.mojang.authlib.minecraft.MinecraftProfileTexture.Type; import com.mojang.authlib.minecraft.MinecraftSessionService; - import customskinloader.CustomSkinLoader; import customskinloader.utils.HttpTextureUtil; import customskinloader.utils.HttpTextureUtil.HttpTextureInfo; - +import customskinloader.utils.MinecraftUtil; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.IImageBuffer; import net.minecraft.client.renderer.ThreadDownloadImageData; -import net.minecraft.client.renderer.texture.ITextureObject; import net.minecraft.client.renderer.texture.TextureManager; import net.minecraft.client.resources.DefaultPlayerSkin; import net.minecraft.client.resources.SkinManager.SkinAvailableCallback; @@ -47,13 +45,11 @@ public class FakeSkinManager { HttpTextureInfo info = HttpTextureUtil.toHttpTextureInfo(profileTexture.getUrl()); final ResourceLocation resourcelocation = new ResourceLocation("skins/" + info.hash); - ITextureObject itextureobject = this.textureManager.getTexture(resourcelocation); - if (itextureobject != null)//Have already loaded + if (this.textureManager.getTexture(resourcelocation) != null)//Have already loaded makeCallback(skinAvailableCallback, textureType, resourcelocation, profileTexture); else { - ThreadDownloadImageData threaddownloadimagedata = new ThreadDownloadImageData(info.cacheFile, info.url, DefaultPlayerSkin.getDefaultSkinLegacy(), - new BaseBuffer(skinAvailableCallback, textureType, resourcelocation, profileTexture)); + ThreadDownloadImageData threaddownloadimagedata = MinecraftUtil.createThreadDownloadImageData(info.cacheFile, info.url, DefaultPlayerSkin.getDefaultSkinLegacy(), new BaseBuffer(skinAvailableCallback, textureType, resourcelocation, profileTexture), textureType); if (skinAvailableCallback instanceof FakeClientPlayer.LegacyBuffer)//Cache for client player FakeClientPlayer.textureCache.put(resourcelocation, threaddownloadimagedata); this.textureManager.loadTexture(resourcelocation, threaddownloadimagedata); @@ -62,7 +58,7 @@ public class FakeSkinManager { } public void loadProfileTextures(final GameProfile profile, final SkinAvailableCallback skinAvailableCallback, final boolean requireSecure) { - THREAD_POOL.submit(new Runnable() { + THREAD_POOL.execute(new Runnable() { public void run() { final Map map = Maps.newHashMap(); map.putAll(customskinloader.CustomSkinLoader.loadProfile(profile)); @@ -128,5 +124,9 @@ public class FakeSkinManager { FakeSkinManager.makeCallback(callback, type, location, texture); } + + public void run() { + this.skinAvailable(); + } } } diff --git a/Common/source/customskinloader/utils/MinecraftUtil.java b/Common/source/customskinloader/utils/MinecraftUtil.java index e1ef2bf..4b8be39 100644 --- a/Common/source/customskinloader/utils/MinecraftUtil.java +++ b/Common/source/customskinloader/utils/MinecraftUtil.java @@ -1,20 +1,22 @@ package customskinloader.utils; import java.io.File; +import java.lang.reflect.Constructor; import java.net.URL; import java.net.URLDecoder; import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; -import net.minecraft.client.resources.SkinManager; -import org.apache.commons.lang3.StringUtils; - import com.mojang.authlib.GameProfile; -import com.mojang.authlib.minecraft.MinecraftSessionService; - +import com.mojang.authlib.minecraft.MinecraftProfileTexture; import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.IImageBuffer; +import net.minecraft.client.renderer.ThreadDownloadImageData; import net.minecraft.client.renderer.texture.TextureManager; +import net.minecraft.client.resources.SkinManager; +import net.minecraft.util.ResourceLocation; +import org.apache.commons.lang3.StringUtils; /** * @author Alexander Xia @@ -33,6 +35,32 @@ public class MinecraftUtil { return Minecraft.getMinecraft().getSkinManager(); } + private static boolean is_1_15_plus = false; + private static Constructor constructor = null; + public static ThreadDownloadImageData createThreadDownloadImageData(File cacheFileIn, String imageUrlIn, ResourceLocation textureResourceLocation, IImageBuffer imageBufferIn, MinecraftProfileTexture.Type textureType) { + if (constructor == null) { + try { + constructor = ThreadDownloadImageData.class.getConstructor(File.class, String.class, ResourceLocation.class, boolean.class, Runnable.class); // For 1.15+ + is_1_15_plus = true; + } catch (NoSuchMethodException e1) { + try { + constructor = ThreadDownloadImageData.class.getConstructor(File.class, String.class, ResourceLocation.class, IImageBuffer.class); // For 1.15- + } catch (NoSuchMethodException e2) { + throw new RuntimeException(e2); + } + } + } + try { + if (is_1_15_plus) { + return constructor.newInstance(cacheFileIn, imageUrlIn, textureResourceLocation, textureType == MinecraftProfileTexture.Type.SKIN, imageBufferIn); + } else { + return constructor.newInstance(cacheFileIn, imageUrlIn, textureResourceLocation, imageBufferIn); + } + } catch (Exception e) { + throw new RuntimeException(e); + } + } + private static ArrayList minecraftVersion = new ArrayList(); private static String minecraftMainVersion = null; diff --git a/Common/source/net/minecraft/client/renderer/IImageBuffer.java b/Common/source/net/minecraft/client/renderer/IImageBuffer.java new file mode 100644 index 0000000..1115c12 --- /dev/null +++ b/Common/source/net/minecraft/client/renderer/IImageBuffer.java @@ -0,0 +1,13 @@ +package net.minecraft.client.renderer; + +import java.awt.image.BufferedImage; + +import net.minecraft.client.renderer.texture.NativeImage; + +public interface IImageBuffer extends Runnable { + BufferedImage parseUserSkin(BufferedImage image); + + NativeImage func_195786_a(NativeImage image); + + void skinAvailable(); +} diff --git a/Fabric/build.gradle b/Fabric/build.gradle index 31ce461..b11cebb 100644 --- a/Fabric/build.gradle +++ b/Fabric/build.gradle @@ -30,7 +30,7 @@ minecraft { } jar { - exclude 'net/**' + exclude 'net/minecraft/client/renderer/texture/**' } repositories { @@ -41,7 +41,7 @@ repositories { } dependencies { - implementation 'org.spongepowered:mixin:0.7.11-SNAPSHOT' + implementation 'org.spongepowered:mixin:0.8-SNAPSHOT' } mixin { diff --git a/Fabric/resource/fabric.mod.json b/Fabric/resource/fabric.mod.json index c6acbb3..a118eba 100644 --- a/Fabric/resource/fabric.mod.json +++ b/Fabric/resource/fabric.mod.json @@ -24,11 +24,7 @@ "mixins.customskinloader.json" ], - "requires": { - "fabricloader": ">=0.4.0", - "fabric": "*" - }, - "suggests": { - "flamingo": "*" + "depends": { + "fabricloader": ">=0.7.0" } } diff --git a/Fabric/resource/mixins.customskinloader.json b/Fabric/resource/mixins.customskinloader.json index 42eaea2..c1a1c47 100644 --- a/Fabric/resource/mixins.customskinloader.json +++ b/Fabric/resource/mixins.customskinloader.json @@ -2,11 +2,13 @@ "compatibilityLevel": "JAVA_8", "package": "customskinloader.fabric.mixin", "client": [ + "MixinFakeSkinManager", "MixinGuiPlayerTabOverlay", "MixinMinecraft", "MixinSkinManager" ], "refmap": "mixins.customskinloader.refmap.json", + "plugin": "customskinloader.fabric.MixinConfigPlugin", "injectors": { "defaultRequire": 1 } diff --git a/Fabric/source/customskinloader/fabric/MixinConfigPlugin.java b/Fabric/source/customskinloader/fabric/MixinConfigPlugin.java new file mode 100644 index 0000000..0057f14 --- /dev/null +++ b/Fabric/source/customskinloader/fabric/MixinConfigPlugin.java @@ -0,0 +1,61 @@ +package customskinloader.fabric; + +import java.util.List; +import java.util.Set; + +import org.objectweb.asm.tree.AbstractInsnNode; +import org.objectweb.asm.tree.ClassNode; +import org.objectweb.asm.tree.MethodInsnNode; +import org.objectweb.asm.tree.MethodNode; +import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; +import org.spongepowered.asm.mixin.extensibility.IMixinInfo; + +public class MixinConfigPlugin implements IMixinConfigPlugin { + public static boolean hasITextureObject = true; + + @Override + public void onLoad(String mixinPackage) { + + } + + @Override + public String getRefMapperConfig() { + return null; + } + + @Override + public boolean shouldApplyMixin(String targetClassName, String mixinClassName) { + return true; + } + + @Override + public void acceptTargets(Set myTargets, Set otherTargets) { + + } + + @Override + public List getMixins() { + return null; + } + + @Override + public void preApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) { + if (!hasITextureObject && targetClassName.equals("customskinloader.fake.FakeSkinManager")) { + for (MethodNode mn : targetClass.methods) { + if (mn.name.equals("loadSkin") && mn.desc.equals("(Lcom/mojang/authlib/minecraft/MinecraftProfileTexture;Lcom/mojang/authlib/minecraft/MinecraftProfileTexture$Type;Lnet/minecraft/class_1071$class_1072;)Lnet/minecraft/class_2960;")) { + for (AbstractInsnNode ain : mn.instructions.toArray()) { + if (ain instanceof MethodInsnNode) { + MethodInsnNode min = (MethodInsnNode) ain; + min.desc = min.desc.replace("Lnet/minecraft/class_1062;", "Lnet/minecraft/class_1044;"); + } + } + } + } + } + } + + @Override + public void postApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) { + + } +} diff --git a/Fabric/source/customskinloader/fabric/mixin/MixinFakeSkinManager.java b/Fabric/source/customskinloader/fabric/mixin/MixinFakeSkinManager.java new file mode 100644 index 0000000..ac0c643 --- /dev/null +++ b/Fabric/source/customskinloader/fabric/mixin/MixinFakeSkinManager.java @@ -0,0 +1,11 @@ +package customskinloader.fabric.mixin; + +import customskinloader.fake.FakeSkinManager; +import org.spongepowered.asm.mixin.Mixin; + +/** + * @see customskinloader.fabric.MixinConfigPlugin#preApply + */ +@Mixin(FakeSkinManager.class) +public abstract class MixinFakeSkinManager { +} diff --git a/Fabric/source/customskinloader/fabric/mixin/MixinSkinManager.java b/Fabric/source/customskinloader/fabric/mixin/MixinSkinManager.java index 2d6442e..1a69551 100644 --- a/Fabric/source/customskinloader/fabric/mixin/MixinSkinManager.java +++ b/Fabric/source/customskinloader/fabric/mixin/MixinSkinManager.java @@ -6,6 +6,7 @@ import java.util.Map; import com.mojang.authlib.GameProfile; import com.mojang.authlib.minecraft.MinecraftProfileTexture; import com.mojang.authlib.minecraft.MinecraftSessionService; +import customskinloader.fabric.MixinConfigPlugin; import customskinloader.fake.FakeSkinManager; import net.minecraft.client.renderer.texture.TextureManager; import net.minecraft.client.resources.SkinManager; @@ -24,7 +25,12 @@ public abstract class MixinSkinManager { method = "Lnet/minecraft/client/resources/SkinManager;(Lnet/minecraft/client/renderer/texture/TextureManager;Ljava/io/File;Lcom/mojang/authlib/minecraft/MinecraftSessionService;)V", at = @At("RETURN") ) - private void initFakeSkinManager(TextureManager textureManagerInstance, File skinCacheDirectory, MinecraftSessionService sessionService, CallbackInfo callbackInfo) { + private void inject_init(TextureManager textureManagerInstance, File skinCacheDirectory, MinecraftSessionService sessionService, CallbackInfo callbackInfo) { + try { + Class.forName("net.minecraft.class_1062", false, SkinManager.class.getClassLoader()); // net.minecraft.client.texture.Texture + } catch (ClassNotFoundException e) { + MixinConfigPlugin.hasITextureObject = false; + } this.fakeManager = new FakeSkinManager(textureManagerInstance, skinCacheDirectory, sessionService); } diff --git a/Forge/build.gradle b/Forge/build.gradle index 2821ff8..8ebd35d 100644 --- a/Forge/build.gradle +++ b/Forge/build.gradle @@ -43,7 +43,7 @@ jar { "Implementation-Timestamp" : new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") ]) } - exclude 'net/minecraft/client/renderer/**' + exclude 'net/minecraft/client/renderer/texture/**' exclude 'net/minecraftforge/**' } sourceSets {