mirror of
https://github.com/LegacyModdingMC/UniMix.git
synced 2026-11-21 03:22:17 +00:00
Modularise and improve tweaker bootstrap logic, updates #59
Separates the previously rather polluted "may be starting under FML" logic into a separate module introduces the concept of "launch agents" which will (going forward) contain any environment-specific launch tasks which need to be performed by the tweaker. This commit *also* fixes issues when bootstrapping mixin by adding the tweaker directly to the launch configuration in both production and development. It also effectively provides a mechanism whereby the first software agent to bootstrap mixin as a subsystem (via the tweaker) will then co-operatively load all others.
This commit is contained in:
1 parent
6ebfa5c016
commit
693e4701c0
8 files changed
+786
-131
No files matched your search
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* 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.launch;
|
||||
|
||||
import net.minecraft.launchwrapper.LaunchClassLoader;
|
||||
|
||||
/**
|
||||
* Base interface for launch agents. Launch agents are environment-specific
|
||||
* launch handlers which are used by the {@link MixinTweaker mixin tweaker} to
|
||||
* perform launch tasks required by that environment without having to fill the
|
||||
* tweaker itself with a bunch of environment-specific cruft.
|
||||
*
|
||||
* <p>Agents handle mixin environment launch tasks on a per-container basis,
|
||||
* with each container in the environment being assigned one of each available
|
||||
* type of agent to handle those tasks on behalf of the container.</p>
|
||||
*/
|
||||
interface IMixinLaunchAgent {
|
||||
|
||||
/**
|
||||
* Called during pre-initialisation, after all tweakers and tweak containers
|
||||
* have been added to the environment.
|
||||
*/
|
||||
public abstract void prepare();
|
||||
|
||||
/**
|
||||
* Called from <tt>injectIntoClassLoader</tt> in the parent tweaker
|
||||
*
|
||||
* @param classLoader
|
||||
*/
|
||||
public abstract void injectIntoClassLoader(LaunchClassLoader classLoader);
|
||||
|
||||
/**
|
||||
* Get the launch target from this container, should return null if no
|
||||
* custom target is available.
|
||||
*/
|
||||
public abstract String getLaunchTarget();
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* 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.launch;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.jar.Attributes;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.jar.Manifest;
|
||||
|
||||
/**
|
||||
* "Main" attribute cache for a URI container, mainly to avoid constantly
|
||||
* opening jar files just to read odd values out of the manifest.
|
||||
*/
|
||||
class MainAttributes {
|
||||
|
||||
private static final Map<URI, MainAttributes> instances = new HashMap<URI, MainAttributes>();
|
||||
|
||||
/**
|
||||
* Manifest from jar
|
||||
*/
|
||||
protected final Attributes attributes;
|
||||
|
||||
private MainAttributes() {
|
||||
this.attributes = new Attributes();
|
||||
}
|
||||
|
||||
private MainAttributes(File jar) {
|
||||
this.attributes = MainAttributes.getAttributes(jar);
|
||||
}
|
||||
|
||||
public final String get(String name) {
|
||||
if (this.attributes != null) {
|
||||
return this.attributes.getValue(name);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Attributes getAttributes(File jar) {
|
||||
if (jar == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
JarFile jarFile = null;
|
||||
try {
|
||||
jarFile = new JarFile(jar);
|
||||
Manifest manifest = jarFile.getManifest();
|
||||
if (manifest != null) {
|
||||
return manifest.getMainAttributes();
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
// be quiet checkstyle
|
||||
} finally {
|
||||
if (jarFile != null) {
|
||||
try {
|
||||
jarFile.close();
|
||||
} catch (IOException ex) {
|
||||
// this could be an issue later on :(
|
||||
}
|
||||
}
|
||||
}
|
||||
return new Attributes();
|
||||
}
|
||||
|
||||
public static MainAttributes of(File jar) {
|
||||
return MainAttributes.of(jar.toURI());
|
||||
}
|
||||
|
||||
public static MainAttributes of(URI uri) {
|
||||
MainAttributes attributes = MainAttributes.instances.get(uri);
|
||||
if (attributes == null) {
|
||||
attributes = new MainAttributes(new File(uri));
|
||||
MainAttributes.instances.put(uri, attributes);
|
||||
}
|
||||
return attributes;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* 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.launch;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URI;
|
||||
|
||||
|
||||
/**
|
||||
* Launch agent base class
|
||||
*/
|
||||
public abstract class MixinLaunchAgentAbstract implements IMixinLaunchAgent {
|
||||
|
||||
/**
|
||||
* URI to the container
|
||||
*/
|
||||
protected final URI uri;
|
||||
|
||||
/**
|
||||
* File containing this tweaker
|
||||
*/
|
||||
protected final File container;
|
||||
|
||||
/**
|
||||
* "Main" manifest attributes from the container
|
||||
*/
|
||||
protected final MainAttributes attributes;
|
||||
|
||||
/**
|
||||
* Create a new launch agent for the specified URI
|
||||
*
|
||||
* @param uri
|
||||
*/
|
||||
public MixinLaunchAgentAbstract(URI uri) {
|
||||
this.uri = uri;
|
||||
this.container = this.uri != null ? new File(this.uri) : null;
|
||||
this.attributes = MainAttributes.of(uri);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("LaunchAgent[%s:%s]", this.getClass().getSimpleName(), this.uri);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* 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.launch;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import net.minecraft.launchwrapper.LaunchClassLoader;
|
||||
|
||||
|
||||
/**
|
||||
* Default launch agent, handles the <tt>MixinConfigs</tt> manifest key
|
||||
*/
|
||||
public class MixinLaunchAgentDefault extends MixinLaunchAgentAbstract {
|
||||
|
||||
protected static final String MFATT_MIXINCONFIGS = "MixinConfigs";
|
||||
protected static final String MFATT_TOKENPROVIDERS = "MixinTokenProviders";
|
||||
protected static final String MFATT_MAINCLASS = "Main-Class";
|
||||
|
||||
public MixinLaunchAgentDefault(URI uri) {
|
||||
super(uri);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepare() {
|
||||
String mixinConfigs = this.attributes.get(MixinLaunchAgentDefault.MFATT_MIXINCONFIGS);
|
||||
if (mixinConfigs != null) {
|
||||
for (String config : mixinConfigs.split(",")) {
|
||||
MixinTweaker.addConfig(config.trim());
|
||||
}
|
||||
}
|
||||
|
||||
String tokenProviders = this.attributes.get(MixinLaunchAgentDefault.MFATT_TOKENPROVIDERS);
|
||||
if (tokenProviders != null) {
|
||||
for (String provider : tokenProviders.split(",")) {
|
||||
MixinTweaker.addTokenProvider(provider.trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void injectIntoClassLoader(LaunchClassLoader classLoader) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLaunchTarget() {
|
||||
return this.attributes.get(MixinLaunchAgentDefault.MFATT_MAINCLASS);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,218 @@
|
||||
/*
|
||||
* 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.launch;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.launchwrapper.ITweaker;
|
||||
import net.minecraft.launchwrapper.Launch;
|
||||
import net.minecraft.launchwrapper.LaunchClassLoader;
|
||||
|
||||
|
||||
/**
|
||||
* Launch agent for use under FML.
|
||||
*
|
||||
* <p>When FML is present we scan containers for the manifest entries which are
|
||||
* inhibited by the tweaker, in particular the <tt>FMLCorePlugin</tt> and
|
||||
* <tt>FMLCorePluginContainsFMLMod</tt> entries. This is required because FML
|
||||
* performs no further processing of containers if they contain a tweaker!</p>
|
||||
*/
|
||||
public class MixinLaunchAgentFML extends MixinLaunchAgentAbstract {
|
||||
|
||||
private static final String MFATT_FORCELOADASMOD = "ForceLoadAsMod";
|
||||
private static final String MFATT_FMLCOREPLUGIN = "FMLCorePlugin";
|
||||
private static final String MFATT_COREMODCONTAINSMOD = "FMLCorePluginContainsFMLMod";
|
||||
|
||||
/**
|
||||
* If running under FML, we will attempt to inject any coremod specified in
|
||||
* the metadata, FML's CoremodManager returns an ITweaker instance which is
|
||||
* the "handle" to the injected mod, we will need to proxy calls through to
|
||||
* the wrapper. If initialisation fails (for example if we are not running
|
||||
* under FML or if an FMLCorePlugin key is not specified in the metadata)
|
||||
* then this handle will be null.
|
||||
*/
|
||||
private final ITweaker coreModWrapper;
|
||||
|
||||
/**
|
||||
* Name of this container
|
||||
*/
|
||||
private final String fileName;
|
||||
|
||||
/**
|
||||
* Core mod manager class
|
||||
*/
|
||||
private Class<?> clCoreModManager;
|
||||
|
||||
/**
|
||||
* @param uri
|
||||
*/
|
||||
public MixinLaunchAgentFML(URI uri) {
|
||||
super(uri);
|
||||
this.fileName = this.container.getName();
|
||||
this.coreModWrapper = this.initFMLCoreMod();
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to initialise the FML coremod (if specified in the jar metadata)
|
||||
*/
|
||||
private ITweaker initFMLCoreMod() {
|
||||
try {
|
||||
this.clCoreModManager = MixinLaunchAgentFML.getCoreModManagerClass();
|
||||
|
||||
if ("true".equalsIgnoreCase(this.attributes.get(MixinLaunchAgentFML.MFATT_FORCELOADASMOD))) {
|
||||
this.loadAsMod();
|
||||
}
|
||||
|
||||
return this.injectCorePlugin();
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the "force load as mod" manifest setting is set to true, this agent
|
||||
* will attempt to forcibly inject the container as a coremod by first
|
||||
* removing the container from fml's "loaded coremods" list causing
|
||||
* fml to subsequently re-evaluate the container for coremod candidacy.
|
||||
*
|
||||
* <p>Further, if the "fml core plugin contains fml mod" flag is set in the
|
||||
* manifest, the container will be manually injected by this agent into the
|
||||
* "reparseable coremods" collection which will cause it to be crawled for
|
||||
* regular mods as well.</p>
|
||||
*/
|
||||
private void loadAsMod() {
|
||||
try {
|
||||
Method mdGetLoadedCoremods = this.clCoreModManager.getDeclaredMethod("getLoadedCoremods");
|
||||
@SuppressWarnings("unchecked")
|
||||
List<String> loadedCoremods = (List<String>)mdGetLoadedCoremods.invoke(null);
|
||||
loadedCoremods.remove(this.fileName);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
if (this.attributes.get(MixinLaunchAgentFML.MFATT_COREMODCONTAINSMOD) != null) {
|
||||
this.addReparseableJar();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by {@link #loadAsMod} if the "fml core plugin contains fml mod" is
|
||||
* set, adds this container to the "reparsable coremods" collection.
|
||||
*/
|
||||
private void addReparseableJar() {
|
||||
try {
|
||||
Method mdGetReparsedCoremods = this.clCoreModManager.getDeclaredMethod("getReparseableCoremods");
|
||||
@SuppressWarnings("unchecked")
|
||||
List<String> reparsedCoremods = (List<String>)mdGetReparsedCoremods.invoke(null);
|
||||
if (!reparsedCoremods.contains(this.fileName)) {
|
||||
reparsedCoremods.add(this.fileName);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private ITweaker injectCorePlugin() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
|
||||
String coreModName = this.attributes.get(MixinLaunchAgentFML.MFATT_FMLCOREPLUGIN);
|
||||
if (coreModName == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Method mdLoadCoreMod = this.clCoreModManager.getDeclaredMethod("loadCoreMod", LaunchClassLoader.class, String.class, File.class);
|
||||
mdLoadCoreMod.setAccessible(true);
|
||||
ITweaker wrapper = (ITweaker)mdLoadCoreMod.invoke(null, Launch.classLoader, coreModName, this.container);
|
||||
if (wrapper == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.spongepowered.asm.launch.IMixinLaunchAgent#prepare()
|
||||
*/
|
||||
@Override
|
||||
public void prepare() {
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.spongepowered.asm.launch.IMixinLaunchAgent
|
||||
* #injectIntoClassLoader(net.minecraft.launchwrapper.LaunchClassLoader)
|
||||
*/
|
||||
@Override
|
||||
public void injectIntoClassLoader(LaunchClassLoader classLoader) {
|
||||
if (this.coreModWrapper != null && !this.isFMLInjected()) {
|
||||
this.coreModWrapper.injectIntoClassLoader(classLoader);
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.spongepowered.asm.launch.IMixinLaunchAgent#getLaunchTarget()
|
||||
*/
|
||||
@Override
|
||||
public String getLaunchTarget() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a naive check which attempts to discover whether we are pre or
|
||||
* post FML's main injection. If we are <i>pre</i>, then we must <b>not</b>
|
||||
* manually call <tt>injectIntoClassLoader</tt> on the wrapper because FML
|
||||
* will add the wrapper to the tweaker list itself. This occurs when mixin
|
||||
* tweaker is loaded explicitly.
|
||||
*
|
||||
* <p>In the event that we are <i>post</i> FML's injection, then we must
|
||||
* instead call <tt>injectIntoClassLoader</tt> on the wrapper manually.</p>
|
||||
*
|
||||
* @return true if FML was already injected
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
protected final boolean isFMLInjected() {
|
||||
for (String tweaker : (List<String>)Launch.blackboard.get("TweakClasses")) {
|
||||
if (tweaker.endsWith("FMLDeobfTweaker")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
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("net.minecraftforge.fml.relauncher.CoreModManager");
|
||||
} catch (ClassNotFoundException ex) {
|
||||
return Class.forName("cpw.mods.fml.relauncher.CoreModManager");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* 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.launch;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.launchwrapper.Launch;
|
||||
import net.minecraft.launchwrapper.LaunchClassLoader;
|
||||
|
||||
|
||||
/**
|
||||
* A collection of {@link IMixinLaunchAgent launch agents)
|
||||
*/
|
||||
public class MixinTweakContainer {
|
||||
|
||||
public static final List<String> agentClasses = new ArrayList<String>();
|
||||
|
||||
static {
|
||||
Launch.blackboard.put("mixin.agents", MixinTweakContainer.agentClasses);
|
||||
MixinTweakContainer.agentClasses.add("org.spongepowered.asm.launch.MixinLaunchAgentFML");
|
||||
MixinTweakContainer.agentClasses.add("org.spongepowered.asm.launch.MixinLaunchAgentDefault");
|
||||
}
|
||||
|
||||
private final URI uri;
|
||||
|
||||
private final List<IMixinLaunchAgent> agents = new ArrayList<IMixinLaunchAgent>();
|
||||
|
||||
public MixinTweakContainer(URI uri) {
|
||||
this.uri = uri;
|
||||
|
||||
for (String agentClass : MixinTweakContainer.agentClasses) {
|
||||
try {
|
||||
@SuppressWarnings("unchecked")
|
||||
Class<IMixinLaunchAgent> clazz = (Class<IMixinLaunchAgent>)Class.forName(agentClass);
|
||||
Constructor<IMixinLaunchAgent> ctor = clazz.getDeclaredConstructor(URI.class);
|
||||
IMixinLaunchAgent agent = ctor.newInstance(uri);
|
||||
this.agents.add(agent);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public URI getURI() {
|
||||
return this.uri;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void prepare() {
|
||||
for (IMixinLaunchAgent agent : this.agents) {
|
||||
agent.prepare();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param classLoader
|
||||
*/
|
||||
public void injectIntoClassLoader(LaunchClassLoader classLoader) {
|
||||
for (IMixinLaunchAgent agent : this.agents) {
|
||||
agent.injectIntoClassLoader(classLoader);
|
||||
}
|
||||
}
|
||||
|
||||
public String getLaunchTarget() {
|
||||
for (IMixinLaunchAgent agent : this.agents) {
|
||||
String launchTarget = agent.getLaunchTarget();
|
||||
if (launchTarget != null) {
|
||||
return launchTarget;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -25,20 +25,22 @@
|
||||
package org.spongepowered.asm.launch;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.jar.Attributes;
|
||||
import java.util.jar.JarFile;
|
||||
|
||||
import org.spongepowered.asm.mixin.MixinEnvironment;
|
||||
import java.util.Map;
|
||||
|
||||
import net.minecraft.launchwrapper.ITweaker;
|
||||
import net.minecraft.launchwrapper.Launch;
|
||||
import net.minecraft.launchwrapper.LaunchClassLoader;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.spongepowered.asm.mixin.MixinEnvironment;
|
||||
import org.spongepowered.asm.mixin.MixinEnvironment.Phase;
|
||||
|
||||
|
||||
/**
|
||||
* TweakClass for running mixins in production. Being a tweaker ensures that we
|
||||
@@ -47,50 +49,55 @@ import net.minecraft.launchwrapper.LaunchClassLoader;
|
||||
*/
|
||||
public class MixinTweaker implements ITweaker {
|
||||
|
||||
private static final String MFATT_MIXINCONFIGS = "MixinConfigs";
|
||||
private static final String MFATT_FORCELOADASMOD = "ForceLoadAsMod";
|
||||
private static final String MFATT_FMLCOREPLUGIN = "FMLCorePlugin";
|
||||
private static final String MFATT_COREMODCONTAINSMOD = "FMLCorePluginContainsFMLMod";
|
||||
private static final String MFATT_MAINCLASS = "Main-Class";
|
||||
|
||||
/**
|
||||
* File containing this tweaker
|
||||
*/
|
||||
private final File container;
|
||||
private static final String MFATT_TWEAKER = "TweakClass";
|
||||
private static final String DEFAULT_MAIN_CLASS = "net.minecraft.client.main.Main";
|
||||
|
||||
/**
|
||||
* If running under FML, we will attempt to inject any coremod specified in
|
||||
* the metadata, FML's CoremodManager returns an ITweaker instance which is
|
||||
* the "handle" to the injected mod, we will need to proxy calls through to
|
||||
* the wrapper. If initialisation fails (for example if we are not running
|
||||
* under FML or if an FMLCorePlugin key is not specified in the metadata)
|
||||
* then this handle will be NULL and the tweaker will attempt to start the
|
||||
* Mixin subsystem automatically by looking for a MixinConfigs key in the
|
||||
* jar metadata, this should be a comma-separated list of mixin config JSON
|
||||
* file names.
|
||||
* Make with the logging already
|
||||
*/
|
||||
private ITweaker fmlWrapper;
|
||||
|
||||
private static final Logger logger = LogManager.getLogger("mixin");
|
||||
|
||||
/**
|
||||
* Tweak containers
|
||||
*/
|
||||
private final Map<URI, MixinTweakContainer> containers = new LinkedHashMap<URI, MixinTweakContainer>();
|
||||
|
||||
/**
|
||||
* Tracks whether {@link #acceptOptions} was called yet, if true, causes new
|
||||
* launch agents to be <tt>prepare</tt>d immediately
|
||||
*/
|
||||
private boolean prepared = false;
|
||||
|
||||
/**
|
||||
* Hello world
|
||||
*/
|
||||
public MixinTweaker() {
|
||||
MixinBootstrap.preInit();
|
||||
this.container = this.findJarFile();
|
||||
this.fmlWrapper = this.initFMLCoreMod();
|
||||
}
|
||||
|
||||
/**
|
||||
* Find and return the file containing this class
|
||||
*/
|
||||
private File findJarFile() {
|
||||
|
||||
// Add agents for the tweak container
|
||||
URI uri = null;
|
||||
try {
|
||||
uri = this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI();
|
||||
if (uri != null) {
|
||||
this.addContainer(uri);
|
||||
}
|
||||
} catch (URISyntaxException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return uri != null ? new File(uri) : null;
|
||||
}
|
||||
|
||||
public final void addContainer(URI uri) {
|
||||
if (this.containers.containsKey(uri)) {
|
||||
return;
|
||||
}
|
||||
|
||||
MixinTweaker.logger.debug("Adding mixin launch agents for container {}", uri);
|
||||
MixinTweakContainer container = new MixinTweakContainer(uri);
|
||||
this.containers.put(uri, container);
|
||||
|
||||
if (this.prepared) {
|
||||
container.prepare();
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -98,84 +105,27 @@ public class MixinTweaker implements ITweaker {
|
||||
* java.io.File, java.io.File, java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public void acceptOptions(List<String> args, File gameDir, File assetsDir, String profile) {
|
||||
|
||||
public final void acceptOptions(List<String> args, File gameDir, File assetsDir, String profile) {
|
||||
MixinBootstrap.register();
|
||||
|
||||
if (this.fmlWrapper == null) {
|
||||
String mixinConfigs = this.getManifestAttribute(MixinTweaker.MFATT_MIXINCONFIGS);
|
||||
if (mixinConfigs == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (String config : mixinConfigs.split(",")) {
|
||||
if (config.endsWith(".json")) {
|
||||
MixinEnvironment.getDefaultEnvironment().addConfiguration(config);
|
||||
}
|
||||
}
|
||||
this.prepared = true;
|
||||
for (MixinTweakContainer container : this.containers.values()) {
|
||||
container.prepare();
|
||||
}
|
||||
this.parseArgs(args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to initialise the FML coremod (if specified in the jar metadata)
|
||||
* Read and parse command-line arguments
|
||||
*
|
||||
* @param args command-line arguments
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private ITweaker initFMLCoreMod() {
|
||||
try {
|
||||
|
||||
String jarName = this.container.getName();
|
||||
Class<?> coreModManager = this.getCoreModManagerClass();
|
||||
|
||||
if ("true".equalsIgnoreCase(this.getManifestAttribute(MixinTweaker.MFATT_FORCELOADASMOD))) {
|
||||
try {
|
||||
Method mdGetLoadedCoremods = coreModManager.getDeclaredMethod("getLoadedCoremods");
|
||||
List<String> loadedCoremods = (List<String>)mdGetLoadedCoremods.invoke(null);
|
||||
loadedCoremods.remove(jarName);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
if (this.getManifestAttribute(MixinTweaker.MFATT_COREMODCONTAINSMOD) != null) {
|
||||
try {
|
||||
Method mdGetReparsedCoremods = coreModManager.getDeclaredMethod("getReparseableCoremods");
|
||||
List<String> reparsedCoremods = (List<String>)mdGetReparsedCoremods.invoke(null);
|
||||
if (!reparsedCoremods.contains(jarName)) {
|
||||
reparsedCoremods.add(jarName);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
private void parseArgs(List<String> args) {
|
||||
boolean captureNext = false;
|
||||
for (String arg : args) {
|
||||
if (captureNext) {
|
||||
MixinTweaker.addConfig(arg);
|
||||
}
|
||||
|
||||
String coreModName = this.getManifestAttribute(MixinTweaker.MFATT_FMLCOREPLUGIN);
|
||||
if (coreModName == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Method mdLoadCoreMod = coreModManager.getDeclaredMethod("loadCoreMod", LaunchClassLoader.class, String.class, File.class);
|
||||
mdLoadCoreMod.setAccessible(true);
|
||||
ITweaker wrapper = (ITweaker)mdLoadCoreMod.invoke(null, Launch.classLoader, coreModName, this.container);
|
||||
if (wrapper == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return wrapper;
|
||||
} catch (Exception ex) {
|
||||
// ex.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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() throws ClassNotFoundException {
|
||||
try {
|
||||
return Class.forName("net.minecraftforge.fml.relauncher.CoreModManager");
|
||||
} catch (ClassNotFoundException ex) {
|
||||
return Class.forName("cpw.mods.fml.relauncher.CoreModManager");
|
||||
captureNext = "--mixin".equals(arg);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,9 +134,42 @@ public class MixinTweaker implements ITweaker {
|
||||
* net.minecraft.launchwrapper.LaunchClassLoader)
|
||||
*/
|
||||
@Override
|
||||
public void injectIntoClassLoader(LaunchClassLoader classLoader) {
|
||||
if (this.fmlWrapper != null) {
|
||||
this.fmlWrapper.injectIntoClassLoader(Launch.classLoader);
|
||||
public final void injectIntoClassLoader(LaunchClassLoader classLoader) {
|
||||
this.scanClasspath();
|
||||
MixinTweaker.logger.debug("injectIntoClassLoader running with {} agents", this.containers.size());
|
||||
for (MixinTweakContainer container : this.containers.values()) {
|
||||
try {
|
||||
container.injectIntoClassLoader(classLoader);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Scan the classpath for mixin containers (containers which declare the
|
||||
* mixin tweaker in their manifest) and add agents for them
|
||||
*/
|
||||
private void scanClasspath() {
|
||||
for (URL url : Launch.classLoader.getSources()) {
|
||||
try {
|
||||
URI uri = url.toURI();
|
||||
if (this.containers.containsKey(uri)) {
|
||||
continue;
|
||||
}
|
||||
MixinTweaker.logger.debug("Scanning {} for mixin tweaker", uri);
|
||||
if (!"file".equals(uri.getScheme()) || !new File(uri).exists()) {
|
||||
continue;
|
||||
}
|
||||
MainAttributes attributes = MainAttributes.of(uri);
|
||||
String tweaker = attributes.get(MixinTweaker.MFATT_TWEAKER);
|
||||
if (MixinTweaker.class.getName().equals(tweaker)) {
|
||||
MixinTweaker.logger.debug("{} contains a mixin tweaker, adding agents", uri);
|
||||
this.addContainer(uri);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -195,8 +178,14 @@ public class MixinTweaker implements ITweaker {
|
||||
*/
|
||||
@Override
|
||||
public String getLaunchTarget() {
|
||||
String mainClass = this.getManifestAttribute(MixinTweaker.MFATT_MAINCLASS);
|
||||
return mainClass != null ? mainClass : "net.minecraft.client.main.Main";
|
||||
for (MixinTweakContainer container : this.containers.values()) {
|
||||
String mainClass = container.getLaunchTarget();
|
||||
if (mainClass != null) {
|
||||
return mainClass;
|
||||
}
|
||||
}
|
||||
|
||||
return MixinTweaker.DEFAULT_MAIN_CLASS;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -207,26 +196,51 @@ public class MixinTweaker implements ITweaker {
|
||||
return new String[]{};
|
||||
}
|
||||
|
||||
private String getManifestAttribute(String key) {
|
||||
if (this.container == null) {
|
||||
return null;
|
||||
}
|
||||
JarFile jarFile = null;
|
||||
try {
|
||||
jarFile = new JarFile(this.container);
|
||||
Attributes manifestAttributes = jarFile.getManifest().getMainAttributes();
|
||||
return manifestAttributes.getValue(key);
|
||||
} catch (IOException ex) {
|
||||
// be quiet checkstyle
|
||||
} finally {
|
||||
if (jarFile != null) {
|
||||
try {
|
||||
jarFile.close();
|
||||
} catch (IOException ex) {
|
||||
// this could be an issue later on :(
|
||||
}
|
||||
/**
|
||||
* Add a config from a jar manifest source or the command line. Supports
|
||||
* config declarations in the form <tt>filename.json</tt> or alternatively
|
||||
* <tt>filename.json@PHASE</tt> where <tt>PHASE</tt> is a
|
||||
* case-sensitive string token representing an environment phase.
|
||||
*
|
||||
* @param config config resource name, does not require a leading /
|
||||
*/
|
||||
static void addConfig(String config) {
|
||||
if (config.endsWith(".json")) {
|
||||
MixinTweaker.logger.debug("Registering mixin config: {}", config);
|
||||
MixinEnvironment.getDefaultEnvironment().addConfiguration(config);
|
||||
} else if (config.contains(".json@")) {
|
||||
int pos = config.indexOf(".json@");
|
||||
String phaseName = config.substring(pos + 6);
|
||||
config = config.substring(0, pos + 5);
|
||||
Phase phase = Phase.forName(phaseName);
|
||||
if (phase != null) {
|
||||
MixinTweaker.logger.debug("Registering mixin config: {}", config);
|
||||
MixinEnvironment.getEnvironment(phase).addConfiguration(config);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a token provider class from a jar manifest source. Supports either
|
||||
* bare class names in the form <tt>blah.package.ClassName</tt> or
|
||||
* alternatively <tt>blah.package.ClassName@PHASE</tt> where
|
||||
* <tt>PHASE</tt> is a case-sensitive string token representing an
|
||||
* environment phase name.
|
||||
*
|
||||
* @param provider provider class name, optionally suffixed with @PHASE
|
||||
*/
|
||||
static void addTokenProvider(String provider) {
|
||||
if (provider.contains("@")) {
|
||||
String[] parts = provider.split("@", 2);
|
||||
Phase phase = Phase.forName(parts[1]);
|
||||
if (phase != null) {
|
||||
MixinTweaker.logger.debug("Registering token provider class: {}", parts[0]);
|
||||
MixinEnvironment.getEnvironment(phase).registerTokenProviderClass(parts[0]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
MixinEnvironment.getDefaultEnvironment().registerTokenProviderClass(provider);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -108,6 +108,15 @@ public class MixinEnvironment implements ITokenProvider {
|
||||
public String toString() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public static Phase forName(String name) {
|
||||
for (Phase phase : Phase.phases) {
|
||||
if (phase.name.equals(name)) {
|
||||
return phase;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -149,7 +158,18 @@ 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<ITweaker>)Launch.blackboard.get("Tweaks")) {
|
||||
if (tweaker.getClass().getName().endsWith(".common.launcher.FMLServerTweaker")) {
|
||||
return "SERVER";
|
||||
} else if (tweaker.getClass().getName().endsWith(".common.launcher.FMLTweaker")) {
|
||||
return "CLIENT";
|
||||
}
|
||||
}
|
||||
|
||||
String name = this.getSideName("net.minecraftforge.fml.relauncher.FMLLaunchHandler", "side");
|
||||
if (name != null) {
|
||||
return name;
|
||||
|
||||
Reference in new issue
Block a user