From ec42236133d4e6a1621feea8683063cd67e97da3 Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Fri, 15 Jan 2016 15:16:05 +0000 Subject: [PATCH] Abstract blackboard access and update/abstract FML reflection, fixes #92 --- gradle.properties | 2 +- .../spongepowered/asm/launch/Blackboard.java | 78 +++++++++++++++++++ .../asm/launch/MixinBootstrap.java | 11 ++- .../asm/launch/MixinLaunchAgentAbstract.java | 2 +- .../asm/launch/MixinLaunchAgentFML.java | 52 ++++++++++--- .../asm/launch/MixinTweakContainer.java | 3 +- .../asm/mixin/MixinEnvironment.java | 21 ++--- 7 files changed, 134 insertions(+), 35 deletions(-) create mode 100644 src/main/java/org/spongepowered/asm/launch/Blackboard.java diff --git a/gradle.properties b/gradle.properties index 756d88e0..ebfaec96 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,5 +4,5 @@ packaging=jar description=Mixin url=https://www.spongepowered.org organization=SpongePowered -buildVersion=0.4.11 +buildVersion=0.4.12 buildType=SNAPSHOT diff --git a/src/main/java/org/spongepowered/asm/launch/Blackboard.java b/src/main/java/org/spongepowered/asm/launch/Blackboard.java new file mode 100644 index 00000000..bc6ea837 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/launch/Blackboard.java @@ -0,0 +1,78 @@ +/* + * This file is part of Mixin, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.spongepowered.asm.launch; + +import net.minecraft.launchwrapper.Launch; + +/** + * Abstractions for working with {@link Launch#blackboard} + */ +public final class Blackboard { + + /** + * Blackboard keys + */ + public static final class Keys { + + public static final String TWEAKCLASSES = "TweakClasses"; + public static final String TWEAKS = "Tweaks"; + + public static final String INIT = "mixin.initialised"; + public static final String AGENTS = "mixin.agents"; + public static final String CONFIGS = "mixin.configs"; + public static final String TRANSFORMER = "mixin.transformer"; + + public static final String FML_LOAD_CORE_MOD = "mixin.launch.fml.loadcoremodmethod"; + public static final String FML_GET_REPARSEABLE_COREMODS = "mixin.launch.fml.reparseablecoremodsmethod"; + public static final String FML_CORE_MOD_MANAGER = "mixin.launch.fml.coremodmanagerclass"; + public static final String FML_GET_IGNORED_MODS = "mixin.launch.fml.ignoredmodsmethod"; + + private Keys() {} + + } + + private Blackboard() {} + + @SuppressWarnings("unchecked") + public static T get(String key) { + return (T)Launch.blackboard.get(key); + } + + public static void put(String key, Object value) { + Launch.blackboard.put(key, value); + } + + @SuppressWarnings("unchecked") + public static T get(String key, T defaultValue) { + Object value = Launch.blackboard.get(key); + return value != null ? (T)value : defaultValue; + } + + public static String getString(String key, String defaultValue) { + Object value = Launch.blackboard.get(key); + return value != null ? value.toString() : defaultValue; + } + +} diff --git a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java index 08eb8c0a..66339f75 100644 --- a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java +++ b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java @@ -60,13 +60,13 @@ public abstract class MixinBootstrap { /** * Subsystem version */ - public static final String VERSION = "0.4.11"; + public static final String VERSION = "0.4.12"; /** * Blackboard key where the subsystem version will be stored to indicate * successful bootstrap */ - public static final String INIT_KEY = "mixin.initialised"; +// public static final String INIT_KEY = Blackboard.Keys.INIT; // Consts private static final String LAUNCH_PACKAGE = "org.spongepowered.asm.launch."; @@ -119,7 +119,7 @@ public abstract class MixinBootstrap { * Phase 1 of mixin initialisation */ static boolean preInit() { - Object registeredVersion = Launch.blackboard.get(MixinBootstrap.INIT_KEY); + Object registeredVersion = Blackboard.get(Blackboard.Keys.INIT); if (registeredVersion != null) { if (!registeredVersion.equals(MixinBootstrap.VERSION)) { throw new MixinInitialisationError("Mixin subsystem version " + registeredVersion @@ -129,7 +129,7 @@ public abstract class MixinBootstrap { return false; } - Launch.blackboard.put(MixinBootstrap.INIT_KEY, MixinBootstrap.VERSION); + Blackboard.put(Blackboard.Keys.INIT, MixinBootstrap.VERSION); if (!MixinBootstrap.initialised) { MixinBootstrap.initialised = true; @@ -161,8 +161,7 @@ public abstract class MixinBootstrap { MixinBootstrap.logger.warn("MixinBootstrap.register() called during a tweak constructor. Expect CoModificationException in 5.. 4.."); } - @SuppressWarnings("unchecked") - List tweakClasses = (List)Launch.blackboard.get("TweakClasses"); + List tweakClasses = Blackboard.>get(Blackboard.Keys.TWEAKCLASSES); if (tweakClasses != null) { tweakClasses.add(MixinEnvironment.class.getName() + "$EnvironmentStateTweaker"); } diff --git a/src/main/java/org/spongepowered/asm/launch/MixinLaunchAgentAbstract.java b/src/main/java/org/spongepowered/asm/launch/MixinLaunchAgentAbstract.java index 22182c30..4ba3b701 100644 --- a/src/main/java/org/spongepowered/asm/launch/MixinLaunchAgentAbstract.java +++ b/src/main/java/org/spongepowered/asm/launch/MixinLaunchAgentAbstract.java @@ -38,7 +38,7 @@ public abstract class MixinLaunchAgentAbstract implements IMixinLaunchAgent { /** * Logger */ - protected final Logger logger = LogManager.getLogger("mixin"); + protected static final Logger logger = LogManager.getLogger("mixin"); /** * URI to the container diff --git a/src/main/java/org/spongepowered/asm/launch/MixinLaunchAgentFML.java b/src/main/java/org/spongepowered/asm/launch/MixinLaunchAgentFML.java index e2b32bdb..316cf195 100644 --- a/src/main/java/org/spongepowered/asm/launch/MixinLaunchAgentFML.java +++ b/src/main/java/org/spongepowered/asm/launch/MixinLaunchAgentFML.java @@ -28,8 +28,10 @@ import java.io.File; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URI; +import java.util.Collections; import java.util.List; +import org.apache.logging.log4j.Level; import org.spongepowered.asm.mixin.MixinEnvironment; import org.spongepowered.asm.mixin.extensibility.IRemapper; @@ -46,6 +48,13 @@ import net.minecraft.launchwrapper.LaunchClassLoader; * performs no further processing of containers if they contain a tweaker!

*/ public class MixinLaunchAgentFML extends MixinLaunchAgentAbstract { + + private static final String LOAD_CORE_MOD_METHOD = "loadCoreMod"; + private static final String GET_REPARSEABLE_COREMODS_METHOD = "getReparseableCoremods"; + private static final String CORE_MOD_MANAGER_CLASS = "net.minecraftforge.fml.relauncher.CoreModManager"; + private static final String GET_IGNORED_MODS_METHOD = "getIgnoredMods"; + + private static final String FML_REMAPPER_ADAPTER_CLASS = "org.spongepowered.asm.bridge.RemapperAdapterFML"; private static final String MFATT_FORCELOADASMOD = "ForceLoadAsMod"; private static final String MFATT_FMLCOREPLUGIN = "FMLCorePlugin"; @@ -110,12 +119,9 @@ public class MixinLaunchAgentFML extends MixinLaunchAgentAbstract { */ private void loadAsMod() { try { - Method mdGetLoadedCoremods = this.clCoreModManager.getDeclaredMethod("getLoadedCoremods"); - @SuppressWarnings("unchecked") - List loadedCoremods = (List)mdGetLoadedCoremods.invoke(null); - loadedCoremods.remove(this.fileName); + MixinLaunchAgentFML.getIgnoredMods(this.clCoreModManager).remove(this.fileName); } catch (Exception ex) { - ex.printStackTrace(); + MixinLaunchAgentAbstract.logger.catching(ex); } if (this.attributes.get(MixinLaunchAgentFML.MFATT_COREMODCONTAINSMOD) != null) { @@ -129,7 +135,8 @@ public class MixinLaunchAgentFML extends MixinLaunchAgentAbstract { */ private void addReparseableJar() { try { - Method mdGetReparsedCoremods = this.clCoreModManager.getDeclaredMethod("getReparseableCoremods"); + Method mdGetReparsedCoremods = this.clCoreModManager.getDeclaredMethod(Blackboard.getString( + Blackboard.Keys.FML_GET_REPARSEABLE_COREMODS, MixinLaunchAgentFML.GET_REPARSEABLE_COREMODS_METHOD)); @SuppressWarnings("unchecked") List reparsedCoremods = (List)mdGetReparsedCoremods.invoke(null); if (!reparsedCoremods.contains(this.fileName)) { @@ -146,7 +153,8 @@ public class MixinLaunchAgentFML extends MixinLaunchAgentAbstract { return null; } - Method mdLoadCoreMod = this.clCoreModManager.getDeclaredMethod("loadCoreMod", LaunchClassLoader.class, String.class, File.class); + Method mdLoadCoreMod = this.clCoreModManager.getDeclaredMethod(Blackboard.getString( + Blackboard.Keys.FML_LOAD_CORE_MOD, MixinLaunchAgentFML.LOAD_CORE_MOD_METHOD), LaunchClassLoader.class, String.class, File.class); mdLoadCoreMod.setAccessible(true); ITweaker wrapper = (ITweaker)mdLoadCoreMod.invoke(null, Launch.classLoader, coreModName, this.container); if (wrapper == null) { @@ -176,12 +184,12 @@ public class MixinLaunchAgentFML extends MixinLaunchAgentAbstract { private void injectRemapper() { try { - Class clFmlRemapperAdapter = Class.forName("org.spongepowered.asm.bridge.RemapperAdapterFML", true, Launch.classLoader); + Class clFmlRemapperAdapter = Class.forName(MixinLaunchAgentFML.FML_REMAPPER_ADAPTER_CLASS, true, Launch.classLoader); Method mdCreate = clFmlRemapperAdapter.getDeclaredMethod("create"); IRemapper remapper = (IRemapper)mdCreate.invoke(null); MixinEnvironment.getDefaultEnvironment().getRemappers().add(remapper); } catch (Exception ex) { - this.logger.debug("Failed instancing remapper adapter for FML, things will probably go horribly for notch-obf'd mods!"); + MixinLaunchAgentAbstract.logger.debug("Failed instancing FML remapper adapter, things will probably go horribly for notch-obf'd mods!"); } } @@ -216,9 +224,8 @@ public class MixinLaunchAgentFML extends MixinLaunchAgentAbstract { * * @return true if FML was already injected */ - @SuppressWarnings("unchecked") protected final boolean isFMLInjected() { - for (String tweaker : (List)Launch.blackboard.get("TweakClasses")) { + for (String tweaker : Blackboard.>get(Blackboard.Keys.TWEAKCLASSES)) { if (tweaker.endsWith("FMLDeobfTweaker")) { return true; } @@ -232,10 +239,31 @@ public class MixinLaunchAgentFML extends MixinLaunchAgentAbstract { */ private static Class getCoreModManagerClass() throws ClassNotFoundException { try { - return Class.forName("net.minecraftforge.fml.relauncher.CoreModManager"); + return Class.forName(Blackboard.getString( + Blackboard.Keys.FML_CORE_MOD_MANAGER, MixinLaunchAgentFML.CORE_MOD_MANAGER_CLASS)); } catch (ClassNotFoundException ex) { return Class.forName("cpw.mods.fml.relauncher.CoreModManager"); } } + @SuppressWarnings("unchecked") + private static List getIgnoredMods(Class clCoreModManager) throws IllegalAccessException, InvocationTargetException { + Method mdGetIgnoredMods = null; + + try { + mdGetIgnoredMods = clCoreModManager.getDeclaredMethod(Blackboard.getString( + Blackboard.Keys.FML_GET_IGNORED_MODS, MixinLaunchAgentFML.GET_IGNORED_MODS_METHOD)); + } catch (NoSuchMethodException ex1) { + try { + // Legacy name + mdGetIgnoredMods = clCoreModManager.getDeclaredMethod("getLoadedCoremods"); + } catch (NoSuchMethodException ex2) { + MixinLaunchAgentAbstract.logger.catching(Level.DEBUG, ex2); + return Collections.emptyList(); + } + } + + return (List)mdGetIgnoredMods.invoke(null); + } + } diff --git a/src/main/java/org/spongepowered/asm/launch/MixinTweakContainer.java b/src/main/java/org/spongepowered/asm/launch/MixinTweakContainer.java index 0bb58198..137b69d9 100644 --- a/src/main/java/org/spongepowered/asm/launch/MixinTweakContainer.java +++ b/src/main/java/org/spongepowered/asm/launch/MixinTweakContainer.java @@ -32,7 +32,6 @@ import java.util.List; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import net.minecraft.launchwrapper.Launch; import net.minecraft.launchwrapper.LaunchClassLoader; /** @@ -43,7 +42,7 @@ public class MixinTweakContainer { public static final List agentClasses = new ArrayList(); static { - Launch.blackboard.put("mixin.agents", MixinTweakContainer.agentClasses); + Blackboard.put(Blackboard.Keys.AGENTS, MixinTweakContainer.agentClasses); MixinTweakContainer.agentClasses.add("org.spongepowered.asm.launch.MixinLaunchAgentFML"); MixinTweakContainer.agentClasses.add("org.spongepowered.asm.launch.MixinLaunchAgentDefault"); } diff --git a/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java b/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java index 44a8dc8b..b6a3e0b1 100644 --- a/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java +++ b/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java @@ -46,6 +46,7 @@ import org.apache.logging.log4j.core.Layout; import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.appender.AbstractAppender; import org.apache.logging.log4j.core.helpers.Booleans; +import org.spongepowered.asm.launch.Blackboard; import org.spongepowered.asm.launch.MixinBootstrap; import org.spongepowered.asm.lib.Opcodes; import org.spongepowered.asm.mixin.extensibility.IEnvironmentTokenProvider; @@ -191,11 +192,10 @@ public class MixinEnvironment implements ITokenProvider { protected abstract boolean detect(); - @SuppressWarnings("unchecked") protected final String getSideName() { // Using this method first prevents us from accidentally loading FML classes // too early when using the tweaker in dev - for (ITweaker tweaker : (List)Launch.blackboard.get("Tweaks")) { + for (ITweaker tweaker : Blackboard.>get(Blackboard.Keys.TWEAKS)) { if (tweaker.getClass().getName().endsWith(".common.launcher.FMLServerTweaker")) { return "SERVER"; } else if (tweaker.getClass().getName().endsWith(".common.launcher.FMLTweaker")) { @@ -569,10 +569,6 @@ public class MixinEnvironment implements ITokenProvider { "cpw.mods.fml.common.asm.transformers.TerminalTransformer" ); - // Blackboard keys - private static final String CONFIGS_KEY = "mixin.configs"; - private static final String TRANSFORMER_KEY = "mixin.transformer"; - /** * Currently active environment */ @@ -664,10 +660,10 @@ public class MixinEnvironment implements ITokenProvider { MixinEnvironment(Phase phase) { this.phase = phase; - this.configsKey = MixinEnvironment.CONFIGS_KEY + "." + this.phase.name.toLowerCase(); + this.configsKey = Blackboard.Keys.CONFIGS + "." + this.phase.name.toLowerCase(); // Sanity check - Object version = Launch.blackboard.get(MixinBootstrap.INIT_KEY); + Object version = this.getVersion(); if (version == null || !MixinBootstrap.VERSION.equals(version)) { throw new RuntimeException("Environment conflict, mismatched versions or you didn't call MixinBootstrap.init()"); } @@ -734,8 +730,7 @@ public class MixinEnvironment implements ITokenProvider { * @return list of registered mixin configs */ public List getMixinConfigs() { - @SuppressWarnings("unchecked") - List mixinConfigs = (List) Launch.blackboard.get(this.configsKey); + List mixinConfigs = Blackboard.>get(this.configsKey); if (mixinConfigs == null) { mixinConfigs = new ArrayList(); Launch.blackboard.put(this.configsKey, mixinConfigs); @@ -844,7 +839,7 @@ public class MixinEnvironment implements ITokenProvider { * @return active mixin transformer instance */ public Object getActiveTransformer() { - return Launch.blackboard.get(MixinEnvironment.TRANSFORMER_KEY); + return Blackboard.get(Blackboard.Keys.TRANSFORMER); } /** @@ -854,7 +849,7 @@ public class MixinEnvironment implements ITokenProvider { */ public void setActiveTransformer(IClassTransformer transformer) { if (transformer != null) { - Launch.blackboard.put(MixinEnvironment.TRANSFORMER_KEY, transformer); + Blackboard.put(Blackboard.Keys.TRANSFORMER, transformer); } } @@ -895,7 +890,7 @@ public class MixinEnvironment implements ITokenProvider { * @return current version */ public String getVersion() { - return (String)Launch.blackboard.get(MixinBootstrap.INIT_KEY); + return Blackboard.get(Blackboard.Keys.INIT); } /**