From c218a4a76e8542ab788360163993f1249304ec51 Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Mon, 9 Oct 2017 16:06:29 +0100 Subject: [PATCH] More internal service refactoring, remove temp launchwrapper dependency --- build.gradle | 5 - .../transformer/debug/RuntimeDecompiler.java | 11 ++- .../platform/MixinPlatformAgentFML.java | 22 ++--- .../asm/service/mojang/Blackboard.java | 91 +++++++++++++++++ .../service/mojang/LaunchClassLoaderUtil.java | 66 ++----------- .../mojang/LegacyTransformerHandle.java | 4 +- .../mojang/MixinServiceLaunchWrapper.java | 99 +++++++------------ .../MixinServiceLaunchWrapperBootstrap.java | 63 ++++++++++++ ...powered.asm.service.IGlobalPropertyService | 1 + ...powered.asm.service.IMixinServiceBootstrap | 1 + ...{Blackboard.java => GlobalProperties.java} | 34 ++++--- .../asm/launch/MixinBootstrap.java | 48 +++------ .../asm/launch/platform/MixinContainer.java | 4 +- .../asm/mixin/MixinEnvironment.java | 14 +-- .../org/spongepowered/asm/mixin/Mixins.java | 11 +-- .../mixin/transformer/MixinPostProcessor.java | 4 +- .../asm/service/IGlobalPropertyService.java | 72 ++++++++++++++ .../asm/service/IMixinService.java | 48 ++------- .../asm/service/IMixinServiceBootstrap.java | 49 +++++++++ .../asm/service/MixinService.java | 41 +++++++- 20 files changed, 438 insertions(+), 250 deletions(-) create mode 100644 src/launchwrapper/java/org/spongepowered/asm/service/mojang/Blackboard.java create mode 100644 src/launchwrapper/java/org/spongepowered/asm/service/mojang/MixinServiceLaunchWrapperBootstrap.java create mode 100644 src/launchwrapper/resources/META-INF/services/org.spongepowered.asm.service.IGlobalPropertyService create mode 100644 src/launchwrapper/resources/META-INF/services/org.spongepowered.asm.service.IMixinServiceBootstrap rename src/main/java/org/spongepowered/asm/launch/{Blackboard.java => GlobalProperties.java} (78%) create mode 100644 src/main/java/org/spongepowered/asm/service/IGlobalPropertyService.java create mode 100644 src/main/java/org/spongepowered/asm/service/IMixinServiceBootstrap.java diff --git a/build.gradle b/build.gradle index 5ca5b1e8..71bc8b6a 100644 --- a/build.gradle +++ b/build.gradle @@ -158,11 +158,6 @@ dependencies { compile 'com.google.code.gson:gson:2.2.4' compile files(tasks.renamedASM.outputs.files[0]) // Establish implicit dependency on remapped ASM - // AMS - temp fix, see #210 - compile('net.minecraft:launchwrapper:1.11') { - exclude module: 'lwjgl' - } - // Tests // testCompile 'junit:junit:4.11' // testCompile 'org.hamcrest:hamcrest-library:1.3' diff --git a/src/fernflower/java/org/spongepowered/asm/mixin/transformer/debug/RuntimeDecompiler.java b/src/fernflower/java/org/spongepowered/asm/mixin/transformer/debug/RuntimeDecompiler.java index d027ab4a..2cc232fa 100644 --- a/src/fernflower/java/org/spongepowered/asm/mixin/transformer/debug/RuntimeDecompiler.java +++ b/src/fernflower/java/org/spongepowered/asm/mixin/transformer/debug/RuntimeDecompiler.java @@ -30,6 +30,7 @@ import java.util.Map; import java.util.jar.Manifest; import org.apache.commons.io.FileUtils; +import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jetbrains.java.decompiler.main.Fernflower; @@ -48,6 +49,8 @@ import com.google.common.io.Files; */ public class RuntimeDecompiler extends IFernflowerLogger implements IDecompiler, IResultSaver { + private static final Level[] SEVERITY_LEVELS = { Level.TRACE, Level.INFO, Level.WARN, Level.ERROR }; + private final Map options = ImmutableMap.builder() .put("din", "0").put("rbr", "0").put("dgs", "1").put("asc", "1") .put("den", "1").put("hdc", "1").put("ind", " ").build(); @@ -114,13 +117,18 @@ public class RuntimeDecompiler extends IFernflowerLogger implements IDecompiler, @Override public void writeMessage(String message, Severity severity) { - this.logger.info(message); + this.logger.log(RuntimeDecompiler.SEVERITY_LEVELS[severity.ordinal()], message); } @Override public void writeMessage(String message, Throwable t) { this.logger.warn("{} {}: {}", message, t.getClass().getSimpleName(), t.getMessage()); } + + @Override + public void writeMessage(String message, Severity severity, Throwable t) { + this.logger.log(RuntimeDecompiler.SEVERITY_LEVELS[severity.ordinal()], message, t); + } @Override public void copyFile(String source, String path, String entryName) { @@ -145,4 +153,5 @@ public class RuntimeDecompiler extends IFernflowerLogger implements IDecompiler, @Override public void closeArchive(String path, String archiveName) { } + } diff --git a/src/launchwrapper/java/org/spongepowered/asm/launch/platform/MixinPlatformAgentFML.java b/src/launchwrapper/java/org/spongepowered/asm/launch/platform/MixinPlatformAgentFML.java index a53aa594..744cd850 100644 --- a/src/launchwrapper/java/org/spongepowered/asm/launch/platform/MixinPlatformAgentFML.java +++ b/src/launchwrapper/java/org/spongepowered/asm/launch/platform/MixinPlatformAgentFML.java @@ -36,7 +36,7 @@ import java.util.List; import java.util.Set; import org.apache.logging.log4j.Level; -import org.spongepowered.asm.launch.Blackboard; +import org.spongepowered.asm.launch.GlobalProperties; import org.spongepowered.asm.mixin.MixinEnvironment; //import org.spongepowered.asm.mixin.environment.IPhaseProvider; //import org.spongepowered.asm.mixin.environment.PhaseDefinition; @@ -187,8 +187,8 @@ public class MixinPlatformAgentFML extends MixinPlatformAgentAbstract { */ private void addReparseableJar() { try { - Method mdGetReparsedCoremods = this.clCoreModManager.getDeclaredMethod(Blackboard.getString( - Blackboard.Keys.FML_GET_REPARSEABLE_COREMODS, MixinPlatformAgentFML.GET_REPARSEABLE_COREMODS_METHOD)); + Method mdGetReparsedCoremods = this.clCoreModManager.getDeclaredMethod(GlobalProperties.getString( + GlobalProperties.Keys.FML_GET_REPARSEABLE_COREMODS, MixinPlatformAgentFML.GET_REPARSEABLE_COREMODS_METHOD)); @SuppressWarnings("unchecked") List reparsedCoremods = (List)mdGetReparsedCoremods.invoke(null); if (!reparsedCoremods.contains(this.fileName)) { @@ -212,8 +212,8 @@ public class MixinPlatformAgentFML extends MixinPlatformAgentAbstract { } MixinPlatformAgentAbstract.logger.debug("{} has core plugin {}. Injecting it into FML for co-initialisation:", this.fileName, coreModName); - Method mdLoadCoreMod = this.clCoreModManager.getDeclaredMethod(Blackboard.getString( - Blackboard.Keys.FML_LOAD_CORE_MOD, MixinPlatformAgentFML.LOAD_CORE_MOD_METHOD), LaunchClassLoader.class, String.class, File.class); + Method mdLoadCoreMod = this.clCoreModManager.getDeclaredMethod(GlobalProperties.getString(GlobalProperties.Keys.FML_LOAD_CORE_MOD, + MixinPlatformAgentFML.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) { @@ -237,7 +237,7 @@ public class MixinPlatformAgentFML extends MixinPlatformAgentAbstract { // Was it already loaded, check the tweakers list try { - List tweakers = Blackboard.>get(Blackboard.Keys.TWEAKS); + List tweakers = GlobalProperties.>get(GlobalProperties.Keys.TWEAKS); if (tweakers == null) { return false; } @@ -347,7 +347,7 @@ public class MixinPlatformAgentFML extends MixinPlatformAgentAbstract { * @return true if a tweaker with the specified name is queued */ private static boolean isTweakerQueued(String tweakerName) { - for (String tweaker : Blackboard.>get(Blackboard.Keys.TWEAKCLASSES)) { + for (String tweaker : GlobalProperties.>get(GlobalProperties.Keys.TWEAKCLASSES)) { if (tweaker.endsWith(tweakerName)) { return true; } @@ -361,8 +361,8 @@ public class MixinPlatformAgentFML extends MixinPlatformAgentAbstract { */ private static Class getCoreModManagerClass() throws ClassNotFoundException { try { - return Class.forName(Blackboard.getString( - Blackboard.Keys.FML_CORE_MOD_MANAGER, MixinPlatformAgentFML.CORE_MOD_MANAGER_CLASS)); + return Class.forName(GlobalProperties.getString( + GlobalProperties.Keys.FML_CORE_MOD_MANAGER, MixinPlatformAgentFML.CORE_MOD_MANAGER_CLASS)); } catch (ClassNotFoundException ex) { return Class.forName(MixinPlatformAgentFML.CORE_MOD_MANAGER_CLASS_LEGACY); } @@ -373,8 +373,8 @@ public class MixinPlatformAgentFML extends MixinPlatformAgentAbstract { Method mdGetIgnoredMods = null; try { - mdGetIgnoredMods = clCoreModManager.getDeclaredMethod(Blackboard.getString( - Blackboard.Keys.FML_GET_IGNORED_MODS, MixinPlatformAgentFML.GET_IGNORED_MODS_METHOD)); + mdGetIgnoredMods = clCoreModManager.getDeclaredMethod(GlobalProperties.getString( + GlobalProperties.Keys.FML_GET_IGNORED_MODS, MixinPlatformAgentFML.GET_IGNORED_MODS_METHOD)); } catch (NoSuchMethodException ex1) { try { // Legacy name diff --git a/src/launchwrapper/java/org/spongepowered/asm/service/mojang/Blackboard.java b/src/launchwrapper/java/org/spongepowered/asm/service/mojang/Blackboard.java new file mode 100644 index 00000000..55c4e611 --- /dev/null +++ b/src/launchwrapper/java/org/spongepowered/asm/service/mojang/Blackboard.java @@ -0,0 +1,91 @@ +/* + * 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.service.mojang; + +import org.spongepowered.asm.service.IGlobalPropertyService; + +import net.minecraft.launchwrapper.Launch; + +/** + * Global property service backed by LaunchWrapper blackboard + */ +public class Blackboard implements IGlobalPropertyService { + + /** + * Get a value from the blackboard and duck-type it to the specified type + * + * @param key blackboard key + * @return value + * @param duck type + */ + @Override + @SuppressWarnings("unchecked") + public final T getProperty(String key) { + return (T)Launch.blackboard.get(key); + } + + /** + * Put the specified value onto the blackboard + * + * @param key blackboard key + * @param value new value + */ + @Override + public final void setProperty(String key, Object value) { + Launch.blackboard.put(key, value); + } + + /** + * Get the value from the blackboard but return defaultValue if the + * specified key is not set. + * + * @param key blackboard key + * @param defaultValue value to return if the key is not set or is null + * @return value from blackboard or default value + * @param duck type + */ + @Override + @SuppressWarnings("unchecked") + public final T getProperty(String key, T defaultValue) { + Object value = Launch.blackboard.get(key); + return value != null ? (T)value : defaultValue; + } + + /** + * Get a string from the blackboard, returns default value if not set or + * null. + * + * @param key blackboard key + * @param defaultValue default value to return if the specified key is not + * set or is null + * @return value from blackboard or default + */ + @Override + public final String getPropertyString(String key, String defaultValue) { + Object value = Launch.blackboard.get(key); + return value != null ? value.toString() : defaultValue; + } + +} diff --git a/src/launchwrapper/java/org/spongepowered/asm/service/mojang/LaunchClassLoaderUtil.java b/src/launchwrapper/java/org/spongepowered/asm/service/mojang/LaunchClassLoaderUtil.java index 1ae92037..188c7533 100644 --- a/src/launchwrapper/java/org/spongepowered/asm/service/mojang/LaunchClassLoaderUtil.java +++ b/src/launchwrapper/java/org/spongepowered/asm/service/mojang/LaunchClassLoaderUtil.java @@ -26,8 +26,6 @@ package org.spongepowered.asm.service.mojang; import java.lang.reflect.Field; import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; import java.util.Map; import java.util.Set; @@ -39,17 +37,12 @@ import net.minecraft.launchwrapper.LaunchClassLoader; * them to perform some validation tasks, and insert entries for mixin "classes" * into the invalid classes set. */ -public final class LaunchClassLoaderUtil { +final class LaunchClassLoaderUtil { private static final String CACHED_CLASSES_FIELD = "cachedClasses"; private static final String INVALID_CLASSES_FIELD = "invalidClasses"; private static final String CLASS_LOADER_EXCEPTIONS_FIELD = "classLoaderExceptions"; private static final String TRANSFORMER_EXCEPTIONS_FIELD = "transformerExceptions"; - - /** - * ClassLoader -> util mapping - */ - private static final Map utils = new HashMap(); /** * ClassLoader for this util @@ -57,7 +50,7 @@ public final class LaunchClassLoaderUtil { private final LaunchClassLoader classLoader; // Reflected fields - private Map> cachedClasses; + private final Map> cachedClasses; private final Set invalidClasses; private final Set classLoaderExceptions; private final Set transformerExceptions; @@ -67,7 +60,7 @@ public final class LaunchClassLoaderUtil { * * @param classLoader class loader */ - private LaunchClassLoaderUtil(LaunchClassLoader classLoader) { + LaunchClassLoaderUtil(LaunchClassLoader classLoader) { this.classLoader = classLoader; this.cachedClasses = LaunchClassLoaderUtil.>>getField(classLoader, LaunchClassLoaderUtil.CACHED_CLASSES_FIELD); this.invalidClasses = LaunchClassLoaderUtil.>getField(classLoader, LaunchClassLoaderUtil.INVALID_CLASSES_FIELD); @@ -78,34 +71,10 @@ public final class LaunchClassLoaderUtil { /** * Get the classloader */ - public LaunchClassLoader getClassLoader() { + LaunchClassLoader getClassLoader() { return this.classLoader; } - /** - * Get all loaded class names from the cache - */ - public Set getLoadedClasses() { - return this.getLoadedClasses(null); - } - - /** - * Get the names of loaded classes from the cache, filter using the supplied - * filter string - * - * @param filter filter string or null - * @return set of class names - */ - public Set getLoadedClasses(String filter) { - Set loadedClasses = new HashSet(); - for (String className : this.cachedClasses.keySet()) { - if (filter == null || className.startsWith(filter)) { - loadedClasses.add(className); - } - } - return loadedClasses; - } - /** * Get whether a class name exists in the cache (indicating it was loaded * via the inner loader @@ -113,8 +82,8 @@ public final class LaunchClassLoaderUtil { * @param name class name * @return true if the class name exists in the cache */ - public boolean isClassLoaded(String name) { - return this.cachedClasses != null && this.cachedClasses.containsKey(name); + boolean isClassLoaded(String name) { + return this.cachedClasses.containsKey(name); } /** @@ -125,7 +94,7 @@ public final class LaunchClassLoaderUtil { * @param transformedName transformed class name * @return true if either exclusion list contains either of the names */ - public boolean isClassExcluded(String name, String transformedName) { + boolean isClassExcluded(String name, String transformedName) { for (final String exception : this.getClassLoaderExceptions()) { if (transformedName.startsWith(exception) || name.startsWith(exception)) { return true; @@ -148,7 +117,7 @@ public final class LaunchClassLoaderUtil { * * @param name class name */ - public void registerInvalidClass(String name) { + void registerInvalidClass(String name) { if (this.invalidClasses != null) { this.invalidClasses.add(name); } @@ -157,7 +126,7 @@ public final class LaunchClassLoaderUtil { /** * Get the classloader exclusions from the target classloader */ - public Set getClassLoaderExceptions() { + Set getClassLoaderExceptions() { if (this.classLoaderExceptions != null) { return this.classLoaderExceptions; } @@ -167,7 +136,7 @@ public final class LaunchClassLoaderUtil { /** * Get the transformer exclusions from the target classloader */ - public Set getTransformerExceptions() { + Set getTransformerExceptions() { if (this.transformerExceptions != null) { return this.transformerExceptions; } @@ -186,19 +155,4 @@ public final class LaunchClassLoaderUtil { return null; } - /** - * Get the utility class for the supplied classloader - * - * @param classLoader classLoader to fetch utility wrapper for - * @return utility wrapper - */ - public static LaunchClassLoaderUtil forClassLoader(LaunchClassLoader classLoader) { - LaunchClassLoaderUtil util = LaunchClassLoaderUtil.utils.get(classLoader); - if (util == null) { - util = new LaunchClassLoaderUtil(classLoader); - LaunchClassLoaderUtil.utils.put(classLoader, util); - } - return util; - } - } diff --git a/src/launchwrapper/java/org/spongepowered/asm/service/mojang/LegacyTransformerHandle.java b/src/launchwrapper/java/org/spongepowered/asm/service/mojang/LegacyTransformerHandle.java index 9f926003..519ccec3 100644 --- a/src/launchwrapper/java/org/spongepowered/asm/service/mojang/LegacyTransformerHandle.java +++ b/src/launchwrapper/java/org/spongepowered/asm/service/mojang/LegacyTransformerHandle.java @@ -34,14 +34,14 @@ import net.minecraft.launchwrapper.IClassTransformer; * A handle for a legacy {@link IClassTransformer} for processing as a legacy * transformer */ -public class LegacyTransformerHandle implements ILegacyClassTransformer { +class LegacyTransformerHandle implements ILegacyClassTransformer { /** * Wrapped transformer */ private final IClassTransformer transformer; - public LegacyTransformerHandle(IClassTransformer transformer) { + LegacyTransformerHandle(IClassTransformer transformer) { this.transformer = transformer; } diff --git a/src/launchwrapper/java/org/spongepowered/asm/service/mojang/MixinServiceLaunchWrapper.java b/src/launchwrapper/java/org/spongepowered/asm/service/mojang/MixinServiceLaunchWrapper.java index 84059d37..887cbd0a 100644 --- a/src/launchwrapper/java/org/spongepowered/asm/service/mojang/MixinServiceLaunchWrapper.java +++ b/src/launchwrapper/java/org/spongepowered/asm/service/mojang/MixinServiceLaunchWrapper.java @@ -36,10 +36,11 @@ import java.util.List; import org.apache.commons.io.IOUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.spongepowered.asm.launch.Blackboard; +import org.spongepowered.asm.launch.GlobalProperties; import org.spongepowered.asm.lib.ClassReader; import org.spongepowered.asm.lib.tree.ClassNode; import org.spongepowered.asm.mixin.MixinEnvironment; +import org.spongepowered.asm.mixin.MixinEnvironment.Phase; import org.spongepowered.asm.service.ILegacyClassTransformer; import org.spongepowered.asm.service.IMixinService; import org.spongepowered.asm.service.ITransformer; @@ -62,8 +63,6 @@ public class MixinServiceLaunchWrapper implements IMixinService { // Consts private static final String LAUNCH_PACKAGE = "org.spongepowered.asm.launch."; private static final String MIXIN_PACKAGE = "org.spongepowered.asm.mixin."; - private static final String MIXIN_UTIL_PACKAGE = "org.spongepowered.asm.util."; - private static final String ASM_PACKAGE = "org.spongepowered.asm.lib."; private static final String STATE_TWEAKER = MixinServiceLaunchWrapper.MIXIN_PACKAGE + "EnvironmentStateTweaker"; private static final String TRANSFORMER_PROXY_CLASS = MixinServiceLaunchWrapper.MIXIN_PACKAGE + "transformer.Proxy"; @@ -72,7 +71,7 @@ public class MixinServiceLaunchWrapper implements IMixinService { */ private static final Logger logger = LogManager.getLogger("mixin"); - private final LaunchClassLoaderUtil classLoaderUtil = LaunchClassLoaderUtil.forClassLoader(Launch.classLoader); + private final LaunchClassLoaderUtil classLoaderUtil = new LaunchClassLoaderUtil(Launch.classLoader); private final ReEntranceLock lock = new ReEntranceLock(1); @@ -105,21 +104,31 @@ public class MixinServiceLaunchWrapper implements IMixinService { */ @Override public void prepare() { - // The important ones - Launch.classLoader.addClassLoaderExclusion(MixinServiceLaunchWrapper.ASM_PACKAGE); - Launch.classLoader.addClassLoaderExclusion(MixinServiceLaunchWrapper.MIXIN_PACKAGE); - Launch.classLoader.addClassLoaderExclusion(MixinServiceLaunchWrapper.MIXIN_UTIL_PACKAGE); - // Only needed in dev, in production this would be handled by the tweaker Launch.classLoader.addClassLoaderExclusion(MixinServiceLaunchWrapper.LAUNCH_PACKAGE); } + /* (non-Javadoc) + * @see org.spongepowered.asm.service.IMixinService#getInitialPhase() + */ + @Override + public Phase getInitialPhase() { + if (MixinServiceLaunchWrapper.findInStackTrace("net.minecraft.launchwrapper.Launch", "launch") > 132) { + return Phase.DEFAULT; + } + return Phase.PREINIT; + } + /* (non-Javadoc) * @see org.spongepowered.asm.service.IMixinService#init() */ @Override public void init() { - List tweakClasses = Blackboard.>get(Blackboard.Keys.TWEAKCLASSES); + if (MixinServiceLaunchWrapper.findInStackTrace("net.minecraft.launchwrapper.Launch", "launch") < 4) { + MixinServiceLaunchWrapper.logger.error("MixinBootstrap.doInit() called during a tweak constructor!"); + } + + List tweakClasses = GlobalProperties.>get(GlobalProperties.Keys.TWEAKCLASSES); if (tweakClasses != null) { tweakClasses.add(MixinServiceLaunchWrapper.STATE_TWEAKER); } @@ -395,7 +404,7 @@ public class MixinServiceLaunchWrapper implements IMixinService { public 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 : this.>getGlobalProperty(Blackboard.Keys.TWEAKS)) { + for (ITweaker tweaker : GlobalProperties.>get(GlobalProperties.Keys.TWEAKS)) { if (tweaker.getClass().getName().endsWith(".common.launcher.FMLServerTweaker")) { return "SERVER"; } else if (tweaker.getClass().getName().endsWith(".common.launcher.FMLTweaker")) { @@ -431,59 +440,21 @@ public class MixinServiceLaunchWrapper implements IMixinService { } } - /** - * Get a value from the blackboard and duck-type it to the specified type - * - * @param key blackboard key - * @return value - * @param duck type - */ - @Override - @SuppressWarnings("unchecked") - public final T getGlobalProperty(String key) { - return (T)Launch.blackboard.get(key); - } - - /** - * Put the specified value onto the blackboard - * - * @param key blackboard key - * @param value new value - */ - @Override - public final void setGlobalProperty(String key, Object value) { - Launch.blackboard.put(key, value); - } - - /** - * Get the value from the blackboard but return defaultValue if the - * specified key is not set. - * - * @param key blackboard key - * @param defaultValue value to return if the key is not set or is null - * @return value from blackboard or default value - * @param duck type - */ - @Override - @SuppressWarnings("unchecked") - public final T getGlobalProperty(String key, T defaultValue) { - Object value = Launch.blackboard.get(key); - return value != null ? (T)value : defaultValue; - } - - /** - * Get a string from the blackboard, returns default value if not set or - * null. - * - * @param key blackboard key - * @param defaultValue default value to return if the specified key is not - * set or is null - * @return value from blackboard or default - */ - @Override - public final String getGlobalPropertyString(String key, String defaultValue) { - Object value = Launch.blackboard.get(key); - return value != null ? value.toString() : defaultValue; + private static int findInStackTrace(String className, String methodName) { + Thread currentThread = Thread.currentThread(); + + if (!"main".equals(currentThread.getName())) { + return 0; + } + + StackTraceElement[] stackTrace = currentThread.getStackTrace(); + for (StackTraceElement s : stackTrace) { + if (className.equals(s.getClassName()) && methodName.equals(s.getMethodName())) { + return s.getLineNumber(); + } + } + + return 0; } } diff --git a/src/launchwrapper/java/org/spongepowered/asm/service/mojang/MixinServiceLaunchWrapperBootstrap.java b/src/launchwrapper/java/org/spongepowered/asm/service/mojang/MixinServiceLaunchWrapperBootstrap.java new file mode 100644 index 00000000..9ea686c0 --- /dev/null +++ b/src/launchwrapper/java/org/spongepowered/asm/service/mojang/MixinServiceLaunchWrapperBootstrap.java @@ -0,0 +1,63 @@ +/* + * 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.service.mojang; + +import org.spongepowered.asm.service.IMixinServiceBootstrap; + +import net.minecraft.launchwrapper.Launch; + +/** + * Bootstrap for LaunchWrapper service + */ +public class MixinServiceLaunchWrapperBootstrap implements IMixinServiceBootstrap { + + private static final String SERVICE_PACKAGE = "org.spongepowered.asm.service."; + + private static final String MIXIN_UTIL_PACKAGE = "org.spongepowered.asm.util."; + private static final String ASM_PACKAGE = "org.spongepowered.asm.lib."; + private static final String MIXIN_PACKAGE = "org.spongepowered.asm.mixin."; + + @Override + public String getName() { + return "LaunchWrapper"; + } + + @Override + public String getServiceClassName() { + return "org.spongepowered.asm.service.mojang.MixinServiceLaunchWrapper"; + } + + @Override + public void boostrap() { + // Essential ones + Launch.classLoader.addClassLoaderExclusion(MixinServiceLaunchWrapperBootstrap.SERVICE_PACKAGE); + + // Important ones + Launch.classLoader.addClassLoaderExclusion(MixinServiceLaunchWrapperBootstrap.ASM_PACKAGE); + Launch.classLoader.addClassLoaderExclusion(MixinServiceLaunchWrapperBootstrap.MIXIN_PACKAGE); + Launch.classLoader.addClassLoaderExclusion(MixinServiceLaunchWrapperBootstrap.MIXIN_UTIL_PACKAGE); + } + +} diff --git a/src/launchwrapper/resources/META-INF/services/org.spongepowered.asm.service.IGlobalPropertyService b/src/launchwrapper/resources/META-INF/services/org.spongepowered.asm.service.IGlobalPropertyService new file mode 100644 index 00000000..5f8632de --- /dev/null +++ b/src/launchwrapper/resources/META-INF/services/org.spongepowered.asm.service.IGlobalPropertyService @@ -0,0 +1 @@ +org.spongepowered.asm.service.mojang.Blackboard \ No newline at end of file diff --git a/src/launchwrapper/resources/META-INF/services/org.spongepowered.asm.service.IMixinServiceBootstrap b/src/launchwrapper/resources/META-INF/services/org.spongepowered.asm.service.IMixinServiceBootstrap new file mode 100644 index 00000000..cfd5d428 --- /dev/null +++ b/src/launchwrapper/resources/META-INF/services/org.spongepowered.asm.service.IMixinServiceBootstrap @@ -0,0 +1 @@ +org.spongepowered.asm.service.mojang.MixinServiceLaunchWrapperBootstrap \ No newline at end of file diff --git a/src/main/java/org/spongepowered/asm/launch/Blackboard.java b/src/main/java/org/spongepowered/asm/launch/GlobalProperties.java similarity index 78% rename from src/main/java/org/spongepowered/asm/launch/Blackboard.java rename to src/main/java/org/spongepowered/asm/launch/GlobalProperties.java index eb3860c7..b76efd4b 100644 --- a/src/main/java/org/spongepowered/asm/launch/Blackboard.java +++ b/src/main/java/org/spongepowered/asm/launch/GlobalProperties.java @@ -24,16 +24,18 @@ */ package org.spongepowered.asm.launch; -import org.spongepowered.asm.service.IMixinService; -import org.spongepowered.asm.service.MixinService; +import java.util.ServiceLoader; + +import org.spongepowered.asm.service.IGlobalPropertyService; /** - * Abstractions for working with the blackboard + * Access to underlying global property service provided by the current + * environment */ -public final class Blackboard { +public final class GlobalProperties { /** - * Blackboard keys + * Global property keys */ public static final class Keys { @@ -55,15 +57,17 @@ public final class Blackboard { } - private static IMixinService service; + private static IGlobalPropertyService service; - private Blackboard() {} + private GlobalProperties() {} - private static IMixinService getService() { - if (Blackboard.service == null) { - Blackboard.service = MixinService.getService(); + private static IGlobalPropertyService getService() { + if (GlobalProperties.service == null) { + ServiceLoader serviceLoader = + ServiceLoader.load(IGlobalPropertyService.class, GlobalProperties.class.getClassLoader()); + GlobalProperties.service = serviceLoader.iterator().next(); } - return Blackboard.service; + return GlobalProperties.service; } /** @@ -74,7 +78,7 @@ public final class Blackboard { * @return value */ public static T get(String key) { - return Blackboard.getService().getGlobalProperty(key); + return GlobalProperties.getService().getProperty(key); } /** @@ -84,7 +88,7 @@ public final class Blackboard { * @param value new value */ public static void put(String key, Object value) { - Blackboard.getService().setGlobalProperty(key, value); + GlobalProperties.getService().setProperty(key, value); } /** @@ -97,7 +101,7 @@ public final class Blackboard { * @return value from blackboard or default value */ public static T get(String key, T defaultValue) { - return Blackboard.getService().getGlobalProperty(key, defaultValue); + return GlobalProperties.getService().getProperty(key, defaultValue); } /** @@ -110,7 +114,7 @@ public final class Blackboard { * @return value from blackboard or default */ public static String getString(String key, String defaultValue) { - return Blackboard.getService().getGlobalPropertyString(key, defaultValue); + return GlobalProperties.getService().getPropertyString(key, defaultValue); } } diff --git a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java index 8dd4a092..3d8ebf1b 100644 --- a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java +++ b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java @@ -33,8 +33,6 @@ import org.spongepowered.asm.mixin.MixinEnvironment; import org.spongepowered.asm.mixin.MixinEnvironment.Phase; import org.spongepowered.asm.service.MixinService; -import net.minecraft.launchwrapper.Launch; - /** * Bootstaps the mixin subsystem. This class acts as a bridge between the mixin * subsystem and the tweaker or coremod which is boostrapping it. Without this @@ -73,10 +71,9 @@ public abstract class MixinBootstrap { private static boolean initialised = false; private static boolean initState = true; - // Static initialiser, add classloader exclusions as early as possible + // Static initialiser, run boot services as early as possible static { - // AMS - temp fix, see #210 - Launch.classLoader.addClassLoaderExclusion("org.spongepowered.asm.service."); + MixinService.boot(); MixinService.getService().prepare(); } @@ -100,12 +97,12 @@ public abstract class MixinBootstrap { */ public static MixinPlatformManager getPlatform() { if (MixinBootstrap.platform == null) { - Object globalPlatformManager = Blackboard.get(Blackboard.Keys.PLATFORM_MANAGER); + Object globalPlatformManager = GlobalProperties.get(GlobalProperties.Keys.PLATFORM_MANAGER); if (globalPlatformManager instanceof MixinPlatformManager) { MixinBootstrap.platform = (MixinPlatformManager)globalPlatformManager; } else { MixinBootstrap.platform = new MixinPlatformManager(); - Blackboard.put(Blackboard.Keys.PLATFORM_MANAGER, MixinBootstrap.platform); + GlobalProperties.put(GlobalProperties.Keys.PLATFORM_MANAGER, MixinBootstrap.platform); MixinBootstrap.platform.init(); } } @@ -145,16 +142,17 @@ public abstract class MixinBootstrap { System.setProperty("mixin.env.remapRefMap", "true"); } - if (MixinBootstrap.findInStackTrace("net.minecraft.launchwrapper.Launch", "launch") > 132) { + Phase initialPhase = MixinService.getService().getInitialPhase(); + if (initialPhase == Phase.DEFAULT) { MixinBootstrap.logger.error("Initialising mixin subsystem after game pre-init phase! Some mixins may be skipped."); - MixinEnvironment.init(Phase.DEFAULT); + MixinEnvironment.init(initialPhase); MixinBootstrap.getPlatform().prepare(null); MixinBootstrap.initState = false; } else { - MixinEnvironment.init(Phase.PREINIT); + MixinEnvironment.init(initialPhase); } - MixinBootstrap.addProxy(); + MixinService.getService().beginPhase(); } MixinBootstrap.getPlatform(); @@ -183,11 +181,6 @@ public abstract class MixinBootstrap { if (MixinBootstrap.initState) { MixinBootstrap.getPlatform().prepare(args); - - if (MixinBootstrap.findInStackTrace("net.minecraft.launchwrapper.Launch", "launch") < 4) { - MixinBootstrap.logger.warn("MixinBootstrap.doInit() called during a tweak constructor. Expect CoModificationException in 5.. 4.."); - } - MixinService.getService().init(); } } @@ -197,7 +190,7 @@ public abstract class MixinBootstrap { } private static boolean isSubsystemRegistered() { - return Blackboard.get(Blackboard.Keys.INIT) != null; + return GlobalProperties.get(GlobalProperties.Keys.INIT) != null; } private static boolean checkSubsystemVersion() { @@ -205,29 +198,12 @@ public abstract class MixinBootstrap { } private static Object getActiveSubsystemVersion() { - Object version = Blackboard.get(Blackboard.Keys.INIT); + Object version = GlobalProperties.get(GlobalProperties.Keys.INIT); return version != null ? version : ""; } private static void registerSubsystem(String version) { - Blackboard.put(Blackboard.Keys.INIT, version); - } - - private static int findInStackTrace(String className, String methodName) { - Thread currentThread = Thread.currentThread(); - - if (!"main".equals(currentThread.getName())) { - return 0; - } - - StackTraceElement[] stackTrace = currentThread.getStackTrace(); - for (StackTraceElement s : stackTrace) { - if (className.equals(s.getClassName()) && methodName.equals(s.getMethodName())) { - return s.getLineNumber(); - } - } - - return 0; + GlobalProperties.put(GlobalProperties.Keys.INIT, version); } } diff --git a/src/main/java/org/spongepowered/asm/launch/platform/MixinContainer.java b/src/main/java/org/spongepowered/asm/launch/platform/MixinContainer.java index 0a4a5131..15c2f70d 100644 --- a/src/main/java/org/spongepowered/asm/launch/platform/MixinContainer.java +++ b/src/main/java/org/spongepowered/asm/launch/platform/MixinContainer.java @@ -32,7 +32,7 @@ import java.util.List; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.spongepowered.asm.launch.Blackboard; +import org.spongepowered.asm.launch.GlobalProperties; import org.spongepowered.asm.service.MixinService; /** @@ -43,7 +43,7 @@ public class MixinContainer { private static final List agentClasses = new ArrayList(); static { - Blackboard.put(Blackboard.Keys.AGENTS, MixinContainer.agentClasses); + GlobalProperties.put(GlobalProperties.Keys.AGENTS, MixinContainer.agentClasses); for (String agent : MixinService.getService().getPlatformAgents()) { MixinContainer.agentClasses.add(agent); } diff --git a/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java b/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java index e22195c0..b2edd010 100644 --- a/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java +++ b/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java @@ -40,7 +40,7 @@ import org.apache.logging.log4j.core.Filter; import org.apache.logging.log4j.core.Layout; import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.appender.AbstractAppender; -import org.spongepowered.asm.launch.Blackboard; +import org.spongepowered.asm.launch.GlobalProperties; import org.spongepowered.asm.launch.MixinBootstrap; import org.spongepowered.asm.lib.Opcodes; import org.spongepowered.asm.mixin.extensibility.IEnvironmentTokenProvider; @@ -873,7 +873,7 @@ public final class MixinEnvironment implements ITokenProvider { MixinEnvironment(Phase phase) { this.service = MixinService.getService(); this.phase = phase; - this.configsKey = Blackboard.Keys.CONFIGS + "." + this.phase.name.toLowerCase(); + this.configsKey = GlobalProperties.Keys.CONFIGS + "." + this.phase.name.toLowerCase(); // Sanity check Object version = this.getVersion(); @@ -949,10 +949,10 @@ public final class MixinEnvironment implements ITokenProvider { */ @Deprecated public List getMixinConfigs() { - List mixinConfigs = Blackboard.>get(this.configsKey); + List mixinConfigs = GlobalProperties.>get(this.configsKey); if (mixinConfigs == null) { mixinConfigs = new ArrayList(); - this.service.setGlobalProperty(this.configsKey, mixinConfigs); + GlobalProperties.put(this.configsKey, mixinConfigs); } return mixinConfigs; } @@ -1069,7 +1069,7 @@ public final class MixinEnvironment implements ITokenProvider { * @return active mixin transformer instance */ public Object getActiveTransformer() { - return Blackboard.get(Blackboard.Keys.TRANSFORMER); + return GlobalProperties.get(GlobalProperties.Keys.TRANSFORMER); } /** @@ -1079,7 +1079,7 @@ public final class MixinEnvironment implements ITokenProvider { */ public void setActiveTransformer(ITransformer transformer) { if (transformer != null) { - Blackboard.put(Blackboard.Keys.TRANSFORMER, transformer); + GlobalProperties.put(GlobalProperties.Keys.TRANSFORMER, transformer); } } @@ -1120,7 +1120,7 @@ public final class MixinEnvironment implements ITokenProvider { * @return current version */ public String getVersion() { - return Blackboard.get(Blackboard.Keys.INIT); + return GlobalProperties.get(GlobalProperties.Keys.INIT); } /** diff --git a/src/main/java/org/spongepowered/asm/mixin/Mixins.java b/src/main/java/org/spongepowered/asm/mixin/Mixins.java index fa805a5b..9db5b87a 100644 --- a/src/main/java/org/spongepowered/asm/mixin/Mixins.java +++ b/src/main/java/org/spongepowered/asm/mixin/Mixins.java @@ -30,9 +30,8 @@ import java.util.Set; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.spongepowered.asm.launch.Blackboard; +import org.spongepowered.asm.launch.GlobalProperties; import org.spongepowered.asm.mixin.transformer.Config; -import org.spongepowered.asm.service.MixinService; /** * Entry point for registering global mixin resources. Compatibility with @@ -47,9 +46,9 @@ public final class Mixins { private static final Logger logger = LogManager.getLogger("mixin"); /** - * Blackboard key storing mixin configs which are pending + * GlobalProperties key storing mixin configs which are pending */ - private static final String CONFIGS_KEY = Blackboard.Keys.CONFIGS + ".queue"; + private static final String CONFIGS_KEY = GlobalProperties.Keys.CONFIGS + ".queue"; /** * Error handlers for environment @@ -139,10 +138,10 @@ public final class Mixins { * consumed are present in this set */ public static Set getConfigs() { - Set mixinConfigs = Blackboard.>get(Mixins.CONFIGS_KEY); + Set mixinConfigs = GlobalProperties.>get(Mixins.CONFIGS_KEY); if (mixinConfigs == null) { mixinConfigs = new LinkedHashSet(); - MixinService.getService().setGlobalProperty(Mixins.CONFIGS_KEY, mixinConfigs); + GlobalProperties.put(Mixins.CONFIGS_KEY, mixinConfigs); } return mixinConfigs; } diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPostProcessor.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPostProcessor.java index 30df42fc..ddf99b35 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPostProcessor.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPostProcessor.java @@ -138,8 +138,8 @@ class MixinPostProcessor extends TreeTransformer implements MixinConfig.IListene } /* (non-Javadoc) - * @see net.minecraft.launchwrapper.IClassTransformer - * #transform(java.lang.String, java.lang.String, byte[]) + * @see org.spongepowered.asm.service.ILegacyClassTransformer + * #transformClassBytes(java.lang.String, java.lang.String, byte[]) */ @Override public byte[] transformClassBytes(String name, String transformedName, byte[] bytes) { diff --git a/src/main/java/org/spongepowered/asm/service/IGlobalPropertyService.java b/src/main/java/org/spongepowered/asm/service/IGlobalPropertyService.java new file mode 100644 index 00000000..890cd42c --- /dev/null +++ b/src/main/java/org/spongepowered/asm/service/IGlobalPropertyService.java @@ -0,0 +1,72 @@ +/* + * 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.service; + +/** + * Global property service + */ +public interface IGlobalPropertyService { + + /** + * Get a value from the global property store (blackboard) and duck-type it + * to the specified type + * + * @param key blackboard key + * @param duck type + * @return value + */ + public abstract T getProperty(String key); + + /** + * Set the specified value in the global property store (blackboard) + * + * @param key blackboard key + * @param value new value + */ + public abstract void setProperty(String key, Object value); + + /** + * Get the value from the global property store (blackboard) but return + * defaultValue if the specified key is not set. + * + * @param key blackboard key + * @param defaultValue value to return if the key is not set or is null + * @param duck type + * @return value from blackboard or default value + */ + public abstract T getProperty(String key, T defaultValue); + + /** + * Get a string from the global property store (blackboard), returns default + * value if not set or null. + * + * @param key blackboard key + * @param defaultValue default value to return if the specified key is not + * set or is null + * @return value from blackboard or default + */ + public abstract String getPropertyString(String key, String defaultValue); + +} diff --git a/src/main/java/org/spongepowered/asm/service/IMixinService.java b/src/main/java/org/spongepowered/asm/service/IMixinService.java index 8ae7cf59..f9c6448d 100644 --- a/src/main/java/org/spongepowered/asm/service/IMixinService.java +++ b/src/main/java/org/spongepowered/asm/service/IMixinService.java @@ -30,6 +30,7 @@ import java.net.URL; import java.util.Collection; import org.spongepowered.asm.lib.tree.ClassNode; +import org.spongepowered.asm.mixin.MixinEnvironment.Phase; import org.spongepowered.asm.util.ReEntranceLock; /** @@ -56,12 +57,17 @@ public interface IMixinService { * Called at subsystem boot */ public abstract void prepare(); + + /** + * Get the initial subsystem phase + */ + public abstract Phase getInitialPhase(); /** * Called at the end of subsystem boot */ public abstract void init(); - + /** * Called whenever a new phase is started */ @@ -182,45 +188,5 @@ public interface IMixinService { * Get the detected side name for this environment */ public abstract String getSideName(); - - /** - * Get a value from the global property store (blackboard) and duck-type it - * to the specified type - * - * @param key blackboard key - * @param duck type - * @return value - */ - public abstract T getGlobalProperty(String key); - /** - * Set the specified value in the global property store (blackboard) - * - * @param key blackboard key - * @param value new value - */ - public abstract void setGlobalProperty(String key, Object value); - - /** - * Get the value from the global property store (blackboard) but return - * defaultValue if the specified key is not set. - * - * @param key blackboard key - * @param defaultValue value to return if the key is not set or is null - * @param duck type - * @return value from blackboard or default value - */ - public abstract T getGlobalProperty(String key, T defaultValue); - - /** - * Get a string from the global property store (blackboard), returns default - * value if not set or null. - * - * @param key blackboard key - * @param defaultValue default value to return if the specified key is not - * set or is null - * @return value from blackboard or default - */ - public abstract String getGlobalPropertyString(String key, String defaultValue); - } diff --git a/src/main/java/org/spongepowered/asm/service/IMixinServiceBootstrap.java b/src/main/java/org/spongepowered/asm/service/IMixinServiceBootstrap.java new file mode 100644 index 00000000..5102bd30 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/service/IMixinServiceBootstrap.java @@ -0,0 +1,49 @@ +/* + * 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.service; + +/** + * Bootstrap interface for Mixin services. Because of the nature of service + * environments, it may be necessary to provide bootstrap logic to prepare + * the classloader environment before the services themselves are enumerated. + */ +public interface IMixinServiceBootstrap { + + /** + * Get the friendly name for the underlying service + */ + public abstract String getName(); + + /** + * Get the fully qualified class name of the underlying service + */ + public abstract String getServiceClassName(); + + /** + * Run bootstrap-time preparation for the underlying service + */ + public abstract void boostrap(); + +} diff --git a/src/main/java/org/spongepowered/asm/service/MixinService.java b/src/main/java/org/spongepowered/asm/service/MixinService.java index bbf686bd..497b4593 100644 --- a/src/main/java/org/spongepowered/asm/service/MixinService.java +++ b/src/main/java/org/spongepowered/asm/service/MixinService.java @@ -24,9 +24,14 @@ */ package org.spongepowered.asm.service; +import java.util.HashSet; import java.util.Iterator; import java.util.ServiceConfigurationError; import java.util.ServiceLoader; +import java.util.Set; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; /** * Provides access to the service layer which connects the mixin transformer to @@ -35,11 +40,20 @@ import java.util.ServiceLoader; * core. This allows us to support LegacyLauncher */ public final class MixinService { + + /** + * Log all the things + */ + private static final Logger logger = LogManager.getLogger("mixin"); /** * Singleton */ private static MixinService instance; + + private ServiceLoader bootstrapServiceLoader; + + private final Set bootedServices = new HashSet(); /** * Service loader @@ -55,12 +69,25 @@ public final class MixinService { * Singleton pattern */ private MixinService() { + this.runBootServices(); + } + + private void runBootServices() { + this.bootstrapServiceLoader = ServiceLoader.load(IMixinServiceBootstrap.class, this.getClass().getClassLoader()); + for (IMixinServiceBootstrap bootService : this.bootstrapServiceLoader) { + try { + bootService.boostrap(); + this.bootedServices.add(bootService.getServiceClassName()); + } catch (Throwable th) { + MixinService.logger.catching(th); + } + } } /** * Singleton pattern, get or create the instance */ - private static MixinService getCanonicalInstance() { + private static MixinService getInstance() { if (MixinService.instance == null) { MixinService.instance = new MixinService(); } @@ -68,8 +95,15 @@ public final class MixinService { return MixinService.instance; } + /** + * Boot + */ + public static void boot() { + MixinService.getInstance(); + } + public static IMixinService getService() { - return MixinService.getCanonicalInstance().getServiceInstance(); + return MixinService.getInstance().getServiceInstance(); } private synchronized IMixinService getServiceInstance() { @@ -88,6 +122,9 @@ public final class MixinService { while (iter.hasNext()) { try { IMixinService service = iter.next(); + if (this.bootedServices.contains(service.getClass().getName())) { + MixinService.logger.debug("MixinService [{}] was successfully booted in {}", service.getName(), this.getClass().getClassLoader()); + } if (service.isValid()) { return service; }