Skip to content

Commit

Permalink
Refactoring so that service impls don't need to camp on transformer pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
Mumfrey committed Feb 24, 2021
1 parent 57055a1 commit e9f7d75
Show file tree
Hide file tree
Showing 13 changed files with 327 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,7 @@ public class MixinPlatformAgentFMLLegacy extends MixinPlatformAgentAbstract impl

@Override
public AcceptResult accept(MixinPlatformManager manager, IContainerHandle handle) {
try {
this.clCoreModManager = MixinPlatformAgentFMLLegacy.getCoreModManagerClass();
} catch (ClassNotFoundException ex) {
MixinPlatformAgentAbstract.logger.info("FML platform manager could not load class {}. Proceeding without FML support.",
ex.getMessage());
if (this.getCoreModManagerClass() == null) {
return AcceptResult.INVALID;
}

Expand Down Expand Up @@ -298,30 +294,6 @@ public String getPhaseProvider() {
public void prepare() {
this.initInjectionState |= MixinPlatformAgentFMLLegacy.isTweakerQueued(MixinPlatformAgentFMLLegacy.FML_TWEAKER_INJECTION);
}

/* (non-Javadoc)
* @see org.spongepowered.asm.launch.IMixinPlatformAgent
* #initPrimaryContainer()
*/
@Override
public void initPrimaryContainer() {
if (this.clCoreModManager != null) {
// MixinEnvironment.registerPhaseProvider(MixinPlatformAgentFMLLegacy.class.getName() + "$PhaseProvider");
this.injectRemapper();
}
}

private void injectRemapper() {
try {
MixinPlatformAgentAbstract.logger.debug("Creating FML remapper adapter: {}", MixinPlatformAgentFMLLegacy.FML_REMAPPER_ADAPTER_CLASS);
Class<?> clFmlRemapperAdapter = Class.forName(MixinPlatformAgentFMLLegacy.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) {
MixinPlatformAgentAbstract.logger.debug("Failed instancing FML remapper adapter, things will probably go horribly for notch-obf'd mods!");
}
}

/* (non-Javadoc)
* @see org.spongepowered.asm.launch.platform.IMixinPlatformAgent#inject()
Expand Down Expand Up @@ -357,6 +329,30 @@ protected final boolean checkForCoInitialisation() {
return !MixinPlatformAgentFMLLegacy.isTweakerQueued(MixinPlatformAgentFMLLegacy.FML_TWEAKER_DEOBF);
}

/**
* Attempt to get the FML CoreModManager, tries the post-1.8 namespace first
* and falls back to 1.7.10 if class lookup fails
*/
private Class<?> getCoreModManagerClass() {
if (this.clCoreModManager != null) {
return this.clCoreModManager;
}

try {
try {
this.clCoreModManager = Class.forName(GlobalProperties.getString(
GlobalProperties.Keys.FML_CORE_MOD_MANAGER, MixinPlatformAgentFMLLegacy.CORE_MOD_MANAGER_CLASS));
} catch (ClassNotFoundException ex) {
this.clCoreModManager = Class.forName(MixinPlatformAgentFMLLegacy.CORE_MOD_MANAGER_CLASS_LEGACY);
}
} catch (ClassNotFoundException ex) {
MixinPlatformAgentAbstract.logger.info("FML platform manager could not load class {}. Proceeding without FML support.",
ex.getMessage());
}

return this.clCoreModManager;
}

/**
* Check whether a tweaker ending with <tt>tweakName</tt> has been enqueued
* but not yet visited.
Expand All @@ -373,19 +369,6 @@ private static boolean isTweakerQueued(String tweakerName) {
return false;
}

/**
* Attempt to get the FML CoreModManager, tries the post-1.8 namespace first
* and falls back to 1.7.10 if class lookup fails
*/
private static Class<?> getCoreModManagerClass() throws ClassNotFoundException {
try {
return Class.forName(GlobalProperties.getString(
GlobalProperties.Keys.FML_CORE_MOD_MANAGER, MixinPlatformAgentFMLLegacy.CORE_MOD_MANAGER_CLASS));
} catch (ClassNotFoundException ex) {
return Class.forName(MixinPlatformAgentFMLLegacy.CORE_MOD_MANAGER_CLASS_LEGACY);
}
}

@SuppressWarnings("unchecked")
private static List<String> getIgnoredMods(Class<?> clCoreModManager) throws IllegalAccessException, InvocationTargetException {
Method mdGetIgnoredMods = null;
Expand All @@ -412,7 +395,22 @@ private static List<String> getIgnoredMods(Class<?> clCoreModManager) throws Ill
*/
@Override
public void init() {
// Nothing to do here
if (this.getCoreModManagerClass() != null) {
// MixinEnvironment.registerPhaseProvider(MixinPlatformAgentFMLLegacy.class.getName() + "$PhaseProvider");
this.injectRemapper();
}
}

private void injectRemapper() {
try {
MixinPlatformAgentAbstract.logger.debug("Creating FML remapper adapter: {}", MixinPlatformAgentFMLLegacy.FML_REMAPPER_ADAPTER_CLASS);
Class<?> clFmlRemapperAdapter = Class.forName(MixinPlatformAgentFMLLegacy.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) {
MixinPlatformAgentAbstract.logger.debug("Failed instancing FML remapper adapter, things will probably go horribly for notch-obf'd mods!");
}
}

/* (non-Javadoc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public class MixinServiceLaunchWrapper extends MixinServiceAbstract implements I
public static final Keys BLACKBOARD_KEY_TWEAKCLASSES = Keys.of("TweakClasses");
public static final Keys BLACKBOARD_KEY_TWEAKS = Keys.of("Tweaks");

private static final String MIXIN_TWEAKER_CLASS = "org.spongepowered.asm.launch.MixinTweaker";
private static final String MIXIN_TWEAKER_CLASS = MixinServiceAbstract.LAUNCH_PACKAGE + "MixinTweaker";

// Consts
private static final String STATE_TWEAKER = MixinServiceAbstract.MIXIN_PACKAGE + "EnvironmentStateTweaker";
Expand Down Expand Up @@ -618,5 +618,5 @@ private static int findInStackTrace(String className, String methodName) {

return 0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
public class MixinServiceLaunchWrapperBootstrap implements IMixinServiceBootstrap {

private static final String SERVICE_PACKAGE = "org.spongepowered.asm.service.";
private static final String LAUNCH_PACKAGE = "org.spongepowered.asm.launch.";

private static final String MIXIN_UTIL_PACKAGE = "org.spongepowered.asm.util.";
private static final String LEGACY_ASM_PACKAGE = "org.spongepowered.asm.lib.";
Expand All @@ -61,7 +62,8 @@ public void bootstrap() {

// Essential ones
Launch.classLoader.addClassLoaderExclusion(MixinServiceLaunchWrapperBootstrap.SERVICE_PACKAGE);

Launch.classLoader.addClassLoaderExclusion(MixinServiceLaunchWrapperBootstrap.LAUNCH_PACKAGE);

// Important ones
Launch.classLoader.addClassLoaderExclusion(MixinServiceLaunchWrapperBootstrap.ASM_PACKAGE);
Launch.classLoader.addClassLoaderExclusion(MixinServiceLaunchWrapperBootstrap.LEGACY_ASM_PACKAGE);
Expand Down
41 changes: 40 additions & 1 deletion src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/
package org.spongepowered.asm.launch;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
Expand All @@ -32,6 +34,9 @@
import org.spongepowered.asm.launch.platform.MixinPlatformManager;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.MixinEnvironment.Phase;
import org.spongepowered.asm.mixin.throwables.MixinError;
import org.spongepowered.asm.service.IMixinInternal;
import org.spongepowered.asm.service.IMixinService;
import org.spongepowered.asm.service.MixinService;

/**
Expand Down Expand Up @@ -63,11 +68,17 @@ public abstract class MixinBootstrap {
*/
public static final String VERSION = "0.8.3";

/**
* Transformer factory
*/
private static final String MIXIN_TRANSFORMER_FACTORY_CLASS = "org.spongepowered.asm.mixin.transformer.MixinTransformer$Factory";

/**
* Log all the things
*/
private static final Logger logger = LogManager.getLogger("mixin");


// These are Klass local, with luck this shouldn't be a problem
private static boolean initialised = false;
private static boolean initState = true;
Expand All @@ -77,7 +88,7 @@ public abstract class MixinBootstrap {
MixinService.boot();
MixinService.getService().prepare();
}

/**
* Platform manager instance
*/
Expand Down Expand Up @@ -134,6 +145,7 @@ static boolean start() {
}

MixinBootstrap.registerSubsystem(MixinBootstrap.VERSION);
MixinBootstrap.offerInternals();

if (!MixinBootstrap.initialised) {
MixinBootstrap.initialised = true;
Expand Down Expand Up @@ -207,4 +219,31 @@ private static void registerSubsystem(String version) {
GlobalProperties.put(GlobalProperties.Keys.INIT, version);
}

private static void offerInternals() {
IMixinService service = MixinService.getService();

try {
for (IMixinInternal internal : MixinBootstrap.getInternals()) {
service.offer(internal);
}
} catch (AbstractMethodError ex) {
// outdated service
ex.printStackTrace();
}
}

@SuppressWarnings("unchecked")
private static List<IMixinInternal> getInternals() throws MixinError {
List<IMixinInternal> internals = new ArrayList<IMixinInternal>();
try {
Class<IMixinInternal> clTransformerFactory = (Class<IMixinInternal>)Class.forName(MixinBootstrap.MIXIN_TRANSFORMER_FACTORY_CLASS);
Constructor<IMixinInternal> ctor = clTransformerFactory.getDeclaredConstructor();
ctor.setAccessible(true);
internals.add(ctor.newInstance());
} catch (ReflectiveOperationException ex) {
throw new MixinError(ex);
}
return internals;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ public interface IMixinTransformer {
*/
public abstract List<String> reload(String mixinClass, ClassNode classNode);

/**
* Called when the transformation reason is computing_frames. The only
* operation we care about here is adding interfaces to target classes but
* at the moment we don't have sufficient scaffolding to determine that
* without triggering re-entrance. Currently just a no-op in order to not
* cause a re-entrance crash under ModLauncher 7.0+.
*
* @param environment Current environment
* @param name Class transformed name
* @param classNode Class tree
* @return true if the class was transformed
*/
public abstract boolean computeFramesForClass(MixinEnvironment environment, String name, ClassNode classNode);

/**
* Callback from the hotswap agent and LaunchWrapper Proxy, transform class
* bytecode. This method delegates to class generation or class
Expand All @@ -67,6 +81,43 @@ public interface IMixinTransformer {
* @see ILegacyClassTransformer#transformClassBytes(String, String, byte[])
*/
public abstract byte[] transformClassBytes(String name, String transformedName, byte[] basicClass);

/**
* Apply mixins and postprocessors to the supplied class
*
* @param environment Current environment
* @param name Class transformed name
* @param classBytes Class bytecode
* @return Transformed bytecode
*/
public abstract byte[] transformClass(MixinEnvironment environment, String name, byte[] classBytes);

/**
* Apply mixins and postprocessors to the supplied class
*
* @param environment Current environment
* @param name Class transformed name
* @param classNode Class tree
* @return true if the class was transformed
*/
public abstract boolean transformClass(MixinEnvironment environment, String name, ClassNode classNode);

/**
* Generate the specified mixin-synthetic class
*
* @param environment Current environment
* @param name Class name to generate
* @return Generated bytecode or <tt>null</tt> if no class was generated
*/
public abstract byte[] generateClass(MixinEnvironment environment, String name);

/**
* @param environment Current environment
* @param name Class transformed name
* @param classNode Empty classnode to populate
* @return True if the class was generated successfully
*/
public abstract boolean generateClass(MixinEnvironment environment, String name, ClassNode classNode);

/**
* Get the transformer extensions
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* This file is part of Mixin, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* 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.mixin.transformer;

import org.spongepowered.asm.launch.MixinInitialisationError;
import org.spongepowered.asm.service.IMixinInternal;

/**
* Factory for the mixin transformer, concrete instances of this class are only
* provided to services since only the service should be able to decide when the
* mixin transformer is initialised.
*/
public interface IMixinTransformerFactory extends IMixinInternal {

/**
* Create a new transformer.
*/
public abstract IMixinTransformer createTransformer() throws MixinInitialisationError;

}
Loading

0 comments on commit e9f7d75

Please sign in to comment.