From 0bdc2d04b4bef9eb19e01d22720f19240a240241 Mon Sep 17 00:00:00 2001 From: cpw Date: Thu, 25 Jul 2019 23:00:05 -0400 Subject: [PATCH] Added a coremod, shush, don't tell Lex. Seriously, this is how to do targeted changes to specific classes. In this case, adding a fieldtomethod redirect for EffectInstance.potion to allow substitution. Signed-off-by: cpw --- build.gradle | 2 +- .../common/asm/FieldRedirectTransformer.java | 126 ------------------ .../common/asm/PotionEffectTransformer.java | 29 ---- ...odlauncher.serviceapi.ILaunchPluginService | 3 +- src/main/resources/META-INF/coremods.json | 3 + .../META-INF/fieldtomethodtransformers.js | 15 +++ 6 files changed, 20 insertions(+), 158 deletions(-) delete mode 100644 src/fmllauncher/java/net/minecraftforge/common/asm/FieldRedirectTransformer.java delete mode 100644 src/fmllauncher/java/net/minecraftforge/common/asm/PotionEffectTransformer.java create mode 100644 src/main/resources/META-INF/coremods.json create mode 100644 src/main/resources/META-INF/fieldtomethodtransformers.js diff --git a/build.gradle b/build.gradle index 22cce5548..449daf3b2 100644 --- a/build.gradle +++ b/build.gradle @@ -372,7 +372,7 @@ project(':forge') { installer 'net.minecraftforge:accesstransformers:0.16.+:shadowed' installer 'net.minecraftforge:eventbus:0.10.+:service' installer 'net.minecraftforge:forgespi:0.13.+' - installer 'net.minecraftforge:coremods:0.6.+' + installer 'net.minecraftforge:coremods:0.7.+' installer 'net.minecraftforge:unsafe:0.2.+' installer 'com.electronwill.night-config:core:3.6.0' installer 'com.electronwill.night-config:toml:3.6.0' diff --git a/src/fmllauncher/java/net/minecraftforge/common/asm/FieldRedirectTransformer.java b/src/fmllauncher/java/net/minecraftforge/common/asm/FieldRedirectTransformer.java deleted file mode 100644 index 96ef63359..000000000 --- a/src/fmllauncher/java/net/minecraftforge/common/asm/FieldRedirectTransformer.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Minecraft Forge - * Copyright (c) 2016-2019. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation version 2.1 - * of the License. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -package net.minecraftforge.common.asm; - -import java.util.EnumSet; -import java.util.ListIterator; - -import org.objectweb.asm.Opcodes; -import org.objectweb.asm.Type; -import org.objectweb.asm.tree.AbstractInsnNode; -import org.objectweb.asm.tree.ClassNode; -import org.objectweb.asm.tree.FieldInsnNode; -import org.objectweb.asm.tree.FieldNode; -import org.objectweb.asm.tree.MethodInsnNode; -import org.objectweb.asm.tree.MethodNode; - -import cpw.mods.modlauncher.serviceapi.ILaunchPluginService; - -@Deprecated // TODO as this only targets a single class, it can be moved to a JS coremod -public abstract class FieldRedirectTransformer implements ILaunchPluginService -{ - private final String clsName; - private final String TYPE; - private final String DESC; - private final String bypass; - - protected FieldRedirectTransformer(String cls, String type, String bypass) - { - this.clsName = cls; - this.TYPE = type; - this.DESC = "()" + type; - this.bypass = bypass; - } - - private static final EnumSet YAY = EnumSet.of(Phase.AFTER); - private static final EnumSet NAY = EnumSet.noneOf(Phase.class); - - @Override - public EnumSet handlesClass(Type classType, boolean isEmpty) - { - return classType.getClassName().equals(clsName) && !isEmpty ? YAY : NAY; - } - - @Override - public boolean processClass(Phase phase, ClassNode classNode, Type classType) - { - FieldNode fieldRef = null; - for (FieldNode f : classNode.fields) - { - if (this.TYPE.equals(f.desc) && fieldRef == null) - { - fieldRef = f; - } - else if (this.TYPE.equals(f.desc)) - { - throw new RuntimeException("Error processing " + clsName + " - found a duplicate holder field"); - } - } - if (fieldRef == null) - { - throw new RuntimeException("Error processing " + clsName + " - no holder field declared (is the code somehow obfuscated?)"); - } - - MethodNode getMethod = null; - for (MethodNode m: classNode.methods) - { - if (m.name.equals(this.bypass)) continue; - if (this.DESC.equals(m.desc) && getMethod == null) - { - getMethod = m; - } - else if (this.DESC.equals(m.desc)) - { - throw new RuntimeException("Error processing " + clsName + " - duplicate get method found"); - } - } - if (getMethod == null) - { - throw new RuntimeException("Error processing " + clsName + " - no get method found (is the code somehow obfuscated?)"); - } - - for (MethodNode m: classNode.methods) - { - if (m.name.equals(this.bypass)) continue; - for (ListIterator it = m.instructions.iterator(); it.hasNext(); ) - { - AbstractInsnNode insnNode = it.next(); - if (insnNode.getType() == AbstractInsnNode.FIELD_INSN) - { - FieldInsnNode fi = (FieldInsnNode)insnNode; - if (fieldRef.name.equals(fi.name) && fi.getOpcode() == Opcodes.GETFIELD) - { - it.remove(); - MethodInsnNode replace = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classNode.name, getMethod.name, getMethod.desc, false); - it.add(replace); - } - } - } - } - - return true; - } - - @Override - public String name() - { - return "field_redirect_" + clsName + "/" + TYPE; - } -} \ No newline at end of file diff --git a/src/fmllauncher/java/net/minecraftforge/common/asm/PotionEffectTransformer.java b/src/fmllauncher/java/net/minecraftforge/common/asm/PotionEffectTransformer.java deleted file mode 100644 index 342e5ef74..000000000 --- a/src/fmllauncher/java/net/minecraftforge/common/asm/PotionEffectTransformer.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Minecraft Forge - * Copyright (c) 2016-2019. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation version 2.1 - * of the License. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -package net.minecraftforge.common.asm; - -@Deprecated -public class PotionEffectTransformer extends FieldRedirectTransformer -{ - public PotionEffectTransformer() - { - super("net.minecraft.potion.PotionEffect", "Lnet/minecraft/potion/Potion;", "getPotionRaw"); - } -} diff --git a/src/fmllauncher/resources/META-INF/services/cpw.mods.modlauncher.serviceapi.ILaunchPluginService b/src/fmllauncher/resources/META-INF/services/cpw.mods.modlauncher.serviceapi.ILaunchPluginService index 7bfa2bf82..377197229 100644 --- a/src/fmllauncher/resources/META-INF/services/cpw.mods.modlauncher.serviceapi.ILaunchPluginService +++ b/src/fmllauncher/resources/META-INF/services/cpw.mods.modlauncher.serviceapi.ILaunchPluginService @@ -1,5 +1,4 @@ net.minecraftforge.fml.loading.RuntimeDistCleaner net.minecraftforge.common.asm.RuntimeEnumExtender net.minecraftforge.common.asm.ObjectHolderDefinalize -net.minecraftforge.common.asm.CapabilityInjectDefinalize -net.minecraftforge.common.asm.PotionEffectTransformer +net.minecraftforge.common.asm.CapabilityInjectDefinalize \ No newline at end of file diff --git a/src/main/resources/META-INF/coremods.json b/src/main/resources/META-INF/coremods.json new file mode 100644 index 000000000..9a57f5f70 --- /dev/null +++ b/src/main/resources/META-INF/coremods.json @@ -0,0 +1,3 @@ +{ + "fieldtomethodtransformers": "META-INF/fieldtomethodtransformers.js" +} diff --git a/src/main/resources/META-INF/fieldtomethodtransformers.js b/src/main/resources/META-INF/fieldtomethodtransformers.js new file mode 100644 index 000000000..219df7063 --- /dev/null +++ b/src/main/resources/META-INF/fieldtomethodtransformers.js @@ -0,0 +1,15 @@ +function initializeCoreMod() { + return { + 'potion': { + 'target': { + 'type': 'CLASS', + 'name': 'net.minecraft.potion.EffectInstance' + }, + 'transformer': function(classNode) { + var asmapi=Java.type('net.minecraftforge.coremod.api.ASMAPI') + asmapi.redirectFieldToMethod(classNode, 'potion', 'getPotionRaw') + return classNode; + } + } + } +}