From 855131d1aa10831ece3d95f5528484e4ecb9aa9f Mon Sep 17 00:00:00 2001 From: ZekerZhayard Date: Sun, 20 Dec 2020 01:02:07 +0800 Subject: [PATCH] Fix DefaultFMLPlatform can't be loaded when other FML platform being used. (#132) --- .../customskinloader/forge/ForgeTweaker.java | 4 ++ .../forge/TransformerManager.java | 23 +++++------ .../forge/loader/LaunchWrapper.java | 4 +- .../forge/platform/DefaultFMLPlatform.java | 11 +++-- .../forge/platform/IFMLPlatform.java | 41 ++++++++++++------- 5 files changed, 51 insertions(+), 32 deletions(-) diff --git a/Forge/source/customskinloader/forge/ForgeTweaker.java b/Forge/source/customskinloader/forge/ForgeTweaker.java index ffa868b..f76569e 100644 --- a/Forge/source/customskinloader/forge/ForgeTweaker.java +++ b/Forge/source/customskinloader/forge/ForgeTweaker.java @@ -5,11 +5,14 @@ import java.lang.reflect.Field; import java.util.List; import java.util.Set; +import customskinloader.Logger; import net.minecraft.launchwrapper.ITweaker; import net.minecraft.launchwrapper.Launch; import net.minecraft.launchwrapper.LaunchClassLoader; public class ForgeTweaker implements ITweaker { + public static Logger logger = new Logger(new File("./CustomSkinLoader/ForgePlugin.log")); + private final static String FML_PLATFORM_INITIALIZER = "customskinloader.forge.platform.IFMLPlatform$FMLPlatformInitializer"; @SuppressWarnings("unchecked") @@ -21,6 +24,7 @@ public class ForgeTweaker implements ITweaker { String exclusionName = this.getClass().getName().substring(0, this.getClass().getName().lastIndexOf(".")); ((Set) field.get(Launch.classLoader)).remove(exclusionName); + Launch.classLoader.addClassLoaderExclusion(Logger.class.getName()); Launch.classLoader.addTransformerExclusion(exclusionName); Launch.classLoader.loadClass(FML_PLATFORM_INITIALIZER).getMethod("initFMLPlatform").invoke(null); } diff --git a/Forge/source/customskinloader/forge/TransformerManager.java b/Forge/source/customskinloader/forge/TransformerManager.java index 03b2d8a..8d3b2f8 100644 --- a/Forge/source/customskinloader/forge/TransformerManager.java +++ b/Forge/source/customskinloader/forge/TransformerManager.java @@ -1,6 +1,5 @@ package customskinloader.forge; -import java.io.File; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -8,12 +7,10 @@ import java.lang.annotation.Target; import java.util.HashMap; import java.util.Map; -import customskinloader.Logger; import org.objectweb.asm.tree.ClassNode; import org.objectweb.asm.tree.MethodNode; public class TransformerManager { - public static Logger logger = new Logger(new File("./CustomSkinLoader/ForgePlugin.log")); @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @@ -56,9 +53,9 @@ public class TransformerManager { } private TransformTarget getTransformTarget(Class cl) { - logger.info("[CSL DEBUG] REGISTERING TRANSFORMER %s", cl.getName()); + ForgeTweaker.logger.info("[CSL DEBUG] REGISTERING TRANSFORMER %s", cl.getName()); if (!cl.isAnnotationPresent(TransformTarget.class)) { - logger.info("[CSL DEBUG] ERROR occurs while parsing Annotation"); + ForgeTweaker.logger.info("[CSL DEBUG] ERROR occurs while parsing Annotation"); return null; } return cl.getAnnotation(TransformTarget.class); @@ -67,7 +64,7 @@ public class TransformerManager { private void addClassTransformer(String className, IClassTransformer transformer) { if (!classMap.containsKey(className)) { classMap.put(className, transformer); - logger.info("[CSL DEBUG] REGISTERING CLASS %s", className); + ForgeTweaker.logger.info("[CSL DEBUG] REGISTERING CLASS %s", className); } } @@ -76,7 +73,7 @@ public class TransformerManager { map.put(className, new HashMap()); for (String methodName : target.methodNames()) { map.get(className).put(methodName + target.desc(), transformer); - logger.info("[CSL DEBUG] REGISTERING METHOD %s::%s", className, methodName + target.desc()); + ForgeTweaker.logger.info("[CSL DEBUG] REGISTERING METHOD %s::%s", className, methodName + target.desc()); } } @@ -85,10 +82,10 @@ public class TransformerManager { if (transformer != null) { try { classNode = transformer.transform(classNode); - logger.info("[CSL DEBUG] Successfully transformed class %s", className); + ForgeTweaker.logger.info("[CSL DEBUG] Successfully transformed class %s", className); } catch (Exception e) { - logger.warning("[CSL DEBUG] An error happened when transforming class %s", className); - logger.warning(e); + ForgeTweaker.logger.warning("[CSL DEBUG] An error happened when transforming class %s", className); + ForgeTweaker.logger.warning(e); } } return classNode; @@ -100,10 +97,10 @@ public class TransformerManager { if (transMap != null && transMap.containsKey(methodTarget)) { try { methodNode = transMap.get(methodTarget).transform(classNode, methodNode); - logger.info("[CSL DEBUG] Successfully transformed method %s in class %s", methodName, className); + ForgeTweaker.logger.info("[CSL DEBUG] Successfully transformed method %s in class %s", methodName, className); } catch (Exception e) { - logger.warning("[CSL DEBUG] An error happened when transforming method %s in class %s", methodTarget, className); - logger.warning(e); + ForgeTweaker.logger.warning("[CSL DEBUG] An error happened when transforming method %s in class %s", methodTarget, className); + ForgeTweaker.logger.warning(e); } } return methodNode; diff --git a/Forge/source/customskinloader/forge/loader/LaunchWrapper.java b/Forge/source/customskinloader/forge/loader/LaunchWrapper.java index 6987c15..702af41 100644 --- a/Forge/source/customskinloader/forge/loader/LaunchWrapper.java +++ b/Forge/source/customskinloader/forge/loader/LaunchWrapper.java @@ -54,8 +54,8 @@ public class LaunchWrapper implements IClassTransformer { cn = transformerManager.transform(cn, className); ArrayList methods = new ArrayList<>(); for (MethodNode mn : cn.methods) { - String mappedMethodName = IFMLPlatform.FMLPlatformInitializer.platform.getRemapper().mapMethodName(cn.name, mn.name, mn.desc); - String mappedMethodDesc = IFMLPlatform.FMLPlatformInitializer.platform.getRemapper().mapMethodDesc(mn.desc); + String mappedMethodName = IFMLPlatform.FMLPlatformInitializer.getPlatform().getRemapper().mapMethodName(cn.name, mn.name, mn.desc); + String mappedMethodDesc = IFMLPlatform.FMLPlatformInitializer.getPlatform().getRemapper().mapMethodDesc(mn.desc); methods.add(transformerManager.transform(cn, mn, className, mappedMethodName, mappedMethodDesc)); } cn.methods.clear(); diff --git a/Forge/source/customskinloader/forge/platform/DefaultFMLPlatform.java b/Forge/source/customskinloader/forge/platform/DefaultFMLPlatform.java index 133f691..4659b0d 100644 --- a/Forge/source/customskinloader/forge/platform/DefaultFMLPlatform.java +++ b/Forge/source/customskinloader/forge/platform/DefaultFMLPlatform.java @@ -6,6 +6,7 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.util.List; import java.util.Set; +import java.util.function.Supplier; import com.google.common.base.Strings; import customskinloader.forge.ForgePlugin; @@ -71,7 +72,13 @@ public class DefaultFMLPlatform implements IFMLPlatform { @Override public Remapper getRemapper() { - return FMLDeobfuscatingRemapper.INSTANCE; + // DO NOT replace with lambda or replace call with method body. + return new Supplier() { + @Override + public Remapper get() { + return FMLDeobfuscatingRemapper.INSTANCE; + } + }.get(); } protected Class getFMLLoadingPluginClass() { @@ -83,7 +90,6 @@ public class DefaultFMLPlatform implements IFMLPlatform { } protected String getNameValue(Annotation annotationIn) { - assert annotationIn instanceof IFMLLoadingPlugin.Name; return ((IFMLLoadingPlugin.Name) annotationIn).value(); } @@ -92,7 +98,6 @@ public class DefaultFMLPlatform implements IFMLPlatform { } protected int getSortingIndexValue(Annotation annotationIn) { - assert annotationIn instanceof IFMLLoadingPlugin.SortingIndex; return ((IFMLLoadingPlugin.SortingIndex) annotationIn).value(); } } diff --git a/Forge/source/customskinloader/forge/platform/IFMLPlatform.java b/Forge/source/customskinloader/forge/platform/IFMLPlatform.java index 583e316..3f9a42a 100644 --- a/Forge/source/customskinloader/forge/platform/IFMLPlatform.java +++ b/Forge/source/customskinloader/forge/platform/IFMLPlatform.java @@ -3,12 +3,14 @@ package customskinloader.forge.platform; import java.io.File; import java.net.URL; import java.security.CodeSource; +import java.util.ArrayList; import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.ServiceLoader; import java.util.Set; -import customskinloader.forge.TransformerManager; +import customskinloader.forge.ForgeTweaker; import net.minecraft.launchwrapper.ITweaker; import net.minecraft.launchwrapper.Launch; import org.objectweb.asm.commons.Remapper; @@ -27,19 +29,21 @@ public interface IFMLPlatform { } class FMLPlatformInitializer { - public static IFMLPlatform platform; - - private final static ServiceLoader platformsLoader = ServiceLoader.load(IFMLPlatform.class); - private final static Set platforms = new HashSet<>(); - - static { - for (IFMLPlatform platform : platformsLoader) { - platforms.add(platform); - } - } + private static IFMLPlatform platform; @SuppressWarnings("unchecked") public static void initFMLPlatform() throws Exception { + Set platforms = new HashSet<>(); + List throwables = new ArrayList<>(); + + for (Iterator iterator = ServiceLoader.load(IFMLPlatform.class).iterator(); iterator.hasNext(); ) { + try { + platforms.add(iterator.next()); + } catch (Throwable t) { + throwables.add(t); + } + } + for (IFMLPlatform platform0 : platforms) { Set otherPlatforms = new HashSet<>(platforms); otherPlatforms.remove(platform0); @@ -52,7 +56,12 @@ public interface IFMLPlatform { } } if (platform == null) { - throw new RuntimeException("No platform!"); + for (int i = 0, len = throwables.size(); i < len; i++) { + Throwable throwable = throwables.get(i); + ForgeTweaker.logger.warning("Platform - %s :", i); + ForgeTweaker.logger.warning(throwable); + } + throw new RuntimeException("No available platform!"); } // CustomSkinLoader is a client side mod @@ -73,10 +82,14 @@ public interface IFMLPlatform { platform.addLoadPlugins(tweaker); ((List) Launch.blackboard.get("Tweaks")).add(tweaker); } else { - TransformerManager.logger.warning("No CodeSource, if this is not a development environment we might run into problems!"); - TransformerManager.logger.warning(FMLPlatformInitializer.class.getProtectionDomain().toString()); + ForgeTweaker.logger.warning("No CodeSource, if this is not a development environment we might run into problems!"); + ForgeTweaker.logger.warning(FMLPlatformInitializer.class.getProtectionDomain().toString()); } } + + public static IFMLPlatform getPlatform() { + return platform; + } } Result init(Set otherPlatforms);