mirror of
https://github.com/GTNewHorizons/lwjgl3ify.git
synced 2026-11-21 20:52:17 +00:00
Copy ExtensibleEnumTransformer from modern forge
This commit is contained in:
1 parent
712a9ef7aa
commit
084e176e7c
3 files changed
+509
No files matched your search
@@ -0,0 +1,282 @@
|
||||
package net.minecraftforge.common.util;
|
||||
|
||||
import cpw.mods.fml.common.FMLLog;
|
||||
import java.lang.reflect.*;
|
||||
import java.util.*;
|
||||
import net.minecraft.block.BlockPressurePlate.Sensitivity;
|
||||
import net.minecraft.block.material.Material;
|
||||
import net.minecraft.enchantment.EnumEnchantmentType;
|
||||
import net.minecraft.entity.Entity.EnumEntitySize;
|
||||
import net.minecraft.entity.EnumCreatureAttribute;
|
||||
import net.minecraft.entity.EnumCreatureType;
|
||||
import net.minecraft.entity.item.EntityPainting.EnumArt;
|
||||
import net.minecraft.entity.player.EntityPlayer.EnumStatus;
|
||||
import net.minecraft.item.EnumAction;
|
||||
import net.minecraft.item.EnumRarity;
|
||||
import net.minecraft.item.Item.ToolMaterial;
|
||||
import net.minecraft.item.ItemArmor.ArmorMaterial;
|
||||
import net.minecraft.util.EnumChatFormatting;
|
||||
import net.minecraft.util.MovingObjectPosition.MovingObjectType;
|
||||
import net.minecraft.world.EnumSkyBlock;
|
||||
import net.minecraft.world.gen.structure.StructureStrongholdPieces.Stronghold.Door;
|
||||
import net.minecraftforge.classloading.FMLForgePlugin;
|
||||
import sun.misc.Unsafe;
|
||||
|
||||
public class EnumHelper {
|
||||
private static Object reflectionFactory = null;
|
||||
private static Method newConstructorAccessor = null;
|
||||
private static Method newInstance = null;
|
||||
private static Method newFieldAccessor = null;
|
||||
private static Method fieldAccessorSet = null;
|
||||
private static boolean isSetup = false;
|
||||
|
||||
// Some enums are decompiled with extra arguments, so lets check for that
|
||||
@SuppressWarnings("rawtypes")
|
||||
private static Class[][] commonTypes = {
|
||||
{EnumAction.class},
|
||||
{ArmorMaterial.class, int.class, int[].class, int.class},
|
||||
{EnumArt.class, String.class, int.class, int.class, int.class, int.class},
|
||||
{EnumCreatureAttribute.class},
|
||||
{EnumCreatureType.class, Class.class, int.class, Material.class, boolean.class, boolean.class},
|
||||
{Door.class},
|
||||
{EnumEnchantmentType.class},
|
||||
{EnumEntitySize.class},
|
||||
{Sensitivity.class},
|
||||
{MovingObjectType.class},
|
||||
{EnumSkyBlock.class, int.class},
|
||||
{EnumStatus.class},
|
||||
{ToolMaterial.class, int.class, int.class, float.class, float.class, int.class},
|
||||
{EnumRarity.class, EnumChatFormatting.class, String.class}
|
||||
};
|
||||
|
||||
public static EnumAction addAction(String name) {
|
||||
return addEnum(EnumAction.class, name);
|
||||
}
|
||||
|
||||
public static ArmorMaterial addArmorMaterial(
|
||||
String name, int durability, int[] reductionAmounts, int enchantability) {
|
||||
return addEnum(ArmorMaterial.class, name, durability, reductionAmounts, enchantability);
|
||||
}
|
||||
|
||||
public static EnumArt addArt(String name, String tile, int sizeX, int sizeY, int offsetX, int offsetY) {
|
||||
return addEnum(EnumArt.class, name, tile, sizeX, sizeY, offsetX, offsetY);
|
||||
}
|
||||
|
||||
public static EnumCreatureAttribute addCreatureAttribute(String name) {
|
||||
return addEnum(EnumCreatureAttribute.class, name);
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static EnumCreatureType addCreatureType(
|
||||
String name, Class typeClass, int maxNumber, Material material, boolean peaceful, boolean animal) {
|
||||
return addEnum(EnumCreatureType.class, name, typeClass, maxNumber, material, peaceful, animal);
|
||||
}
|
||||
|
||||
public static Door addDoor(String name) {
|
||||
return addEnum(Door.class, name);
|
||||
}
|
||||
|
||||
public static EnumEnchantmentType addEnchantmentType(String name) {
|
||||
return addEnum(EnumEnchantmentType.class, name);
|
||||
}
|
||||
|
||||
public static EnumEntitySize addEntitySize(String name) {
|
||||
return addEnum(EnumEntitySize.class, name);
|
||||
}
|
||||
|
||||
public static Sensitivity addSensitivity(String name) {
|
||||
return addEnum(Sensitivity.class, name);
|
||||
}
|
||||
|
||||
public static MovingObjectType addMovingObjectType(String name) {
|
||||
return addEnum(MovingObjectType.class, name);
|
||||
}
|
||||
|
||||
public static EnumSkyBlock addSkyBlock(String name, int lightValue) {
|
||||
return addEnum(EnumSkyBlock.class, name, lightValue);
|
||||
}
|
||||
|
||||
public static EnumStatus addStatus(String name) {
|
||||
return addEnum(EnumStatus.class, name);
|
||||
}
|
||||
|
||||
public static ToolMaterial addToolMaterial(
|
||||
String name, int harvestLevel, int maxUses, float efficiency, float damage, int enchantability) {
|
||||
return addEnum(ToolMaterial.class, name, harvestLevel, maxUses, efficiency, damage, enchantability);
|
||||
}
|
||||
|
||||
public static EnumRarity addRarity(String name, EnumChatFormatting color, String displayName) {
|
||||
return addEnum(EnumRarity.class, name, color, displayName);
|
||||
}
|
||||
|
||||
private static Unsafe unsafe;
|
||||
|
||||
private static void setup() {
|
||||
if (isSetup) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
if (unsafe == null) {
|
||||
final Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
|
||||
unsafeField.setAccessible(true);
|
||||
unsafe = (Unsafe) unsafeField.get(null);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
isSetup = true;
|
||||
}
|
||||
|
||||
/*
|
||||
* Everything below this is found at the site below, and updated to be able to compile in Eclipse/Java 1.6+
|
||||
* Also modified for use in decompiled code.
|
||||
* Found at: http://niceideas.ch/roller2/badtrash/entry/java_create_enum_instances_dynamically
|
||||
*/
|
||||
private static Object getConstructorAccessor(Class<?> enumClass, Class<?>[] additionalParameterTypes)
|
||||
throws Exception {
|
||||
Class<?>[] parameterTypes = new Class[additionalParameterTypes.length + 2];
|
||||
parameterTypes[0] = String.class;
|
||||
parameterTypes[1] = int.class;
|
||||
System.arraycopy(additionalParameterTypes, 0, parameterTypes, 2, additionalParameterTypes.length);
|
||||
return newConstructorAccessor.invoke(reflectionFactory, enumClass.getDeclaredConstructor(parameterTypes));
|
||||
}
|
||||
|
||||
private static <T extends Enum<?>> T makeEnum(
|
||||
Class<T> enumClass, String value, int ordinal, Class<?>[] additionalTypes, Object[] additionalValues)
|
||||
throws Exception {
|
||||
Object[] parms = new Object[additionalValues.length + 2];
|
||||
parms[0] = value;
|
||||
parms[1] = Integer.valueOf(ordinal);
|
||||
System.arraycopy(additionalValues, 0, parms, 2, additionalValues.length);
|
||||
return enumClass.cast(
|
||||
newInstance.invoke(getConstructorAccessor(enumClass, additionalTypes), new Object[] {parms}));
|
||||
}
|
||||
|
||||
public static void setFailsafeFieldValue(Field field, Object target, Object value) throws Exception {
|
||||
field.setAccessible(true);
|
||||
Field modifiersField = Field.class.getDeclaredField("modifiers");
|
||||
modifiersField.setAccessible(true);
|
||||
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
|
||||
Object fieldAccessor = newFieldAccessor.invoke(reflectionFactory, field, false);
|
||||
fieldAccessorSet.invoke(fieldAccessor, target, value);
|
||||
}
|
||||
|
||||
private static void blankField(Class<?> enumClass, String fieldName) throws Exception {
|
||||
for (Field field : Class.class.getDeclaredFields()) {
|
||||
if (field.getName().contains(fieldName)) {
|
||||
field.setAccessible(true);
|
||||
setFailsafeFieldValue(field, enumClass, null);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void cleanEnumCache(Class<?> enumClass) throws Exception {
|
||||
blankField(enumClass, "enumConstantDirectory");
|
||||
blankField(enumClass, "enumConstants");
|
||||
}
|
||||
|
||||
public static <T extends Enum<?>> T addEnum(Class<T> enumType, String enumName, Object... paramValues) {
|
||||
setup();
|
||||
return addEnum(commonTypes, enumType, enumName, paramValues);
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static <T extends Enum<?>> T addEnum(
|
||||
Class[][] map, Class<T> enumType, String enumName, Object... paramValues) {
|
||||
for (Class[] lookup : map) {
|
||||
if (lookup[0] == enumType) {
|
||||
Class<?>[] paramTypes = new Class<?>[lookup.length - 1];
|
||||
if (paramTypes.length > 0) {
|
||||
System.arraycopy(lookup, 1, paramTypes, 0, paramTypes.length);
|
||||
}
|
||||
return addEnum(enumType, enumName, paramTypes, paramValues);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T extends Enum<?>> T addEnum(
|
||||
Class<T> enumType, String enumName, Class<?>[] paramTypes, Object[] paramValues) {
|
||||
if (!isSetup) {
|
||||
setup();
|
||||
}
|
||||
|
||||
Field valuesField = null;
|
||||
Field[] fields = enumType.getDeclaredFields();
|
||||
|
||||
for (Field field : fields) {
|
||||
String name = field.getName();
|
||||
if (name.equals("$VALUES")
|
||||
|| name.equals(
|
||||
"ENUM$VALUES")) // Added 'ENUM$VALUES' because Eclipse's internal compiler doesn't follow
|
||||
// standards
|
||||
{
|
||||
valuesField = field;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int flags = (FMLForgePlugin.RUNTIME_DEOBF ? Modifier.PUBLIC : Modifier.PRIVATE)
|
||||
| Modifier.STATIC
|
||||
| Modifier.FINAL
|
||||
| 0x1000 /*SYNTHETIC*/;
|
||||
if (valuesField == null) {
|
||||
String valueType = String.format("[L%s;", enumType.getName().replace('.', '/'));
|
||||
|
||||
for (Field field : fields) {
|
||||
if ((field.getModifiers() & flags) == flags
|
||||
&& field.getType()
|
||||
.getName()
|
||||
.replace('.', '/')
|
||||
.equals(valueType)) // Apparently some JVMs return .'s and some don't..
|
||||
{
|
||||
valuesField = field;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (valuesField == null) {
|
||||
FMLLog.severe("Could not find $VALUES field for enum: %s", enumType.getName());
|
||||
FMLLog.severe("Runtime Deobf: %s", FMLForgePlugin.RUNTIME_DEOBF);
|
||||
FMLLog.severe(
|
||||
"Flags: %s",
|
||||
String.format("%16s", Integer.toBinaryString(flags)).replace(' ', '0'));
|
||||
FMLLog.severe("Fields:");
|
||||
for (Field field : fields) {
|
||||
String mods = String.format("%16s", Integer.toBinaryString(field.getModifiers()))
|
||||
.replace(' ', '0');
|
||||
FMLLog.severe(
|
||||
" %s %s: %s",
|
||||
mods, field.getName(), field.getType().getName());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
valuesField.setAccessible(true);
|
||||
|
||||
try {
|
||||
T[] previousValues = (T[]) valuesField.get(enumType);
|
||||
List<T> values = new ArrayList<T>(Arrays.asList(previousValues));
|
||||
T newValue = (T) makeEnum(enumType, enumName, values.size(), paramTypes, paramValues);
|
||||
values.add(newValue);
|
||||
setFailsafeFieldValue(valuesField, null, values.toArray((T[]) Array.newInstance(enumType, 0)));
|
||||
cleanEnumCache(enumType);
|
||||
|
||||
return newValue;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
static {
|
||||
if (!isSetup) {
|
||||
setup();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
/*
|
||||
* Copyright (c) Forge Development LLC and contributors
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
package me.eigenraven.lwjgl3ify.core;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.objectweb.asm.Label;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import org.objectweb.asm.Type;
|
||||
import org.objectweb.asm.commons.InstructionAdapter;
|
||||
import org.objectweb.asm.tree.ClassNode;
|
||||
import org.objectweb.asm.tree.FieldNode;
|
||||
import org.objectweb.asm.tree.MethodNode;
|
||||
|
||||
public class ExtensibleEnumTransformerHelper {
|
||||
private final Logger LOGGER = Lwjgl3ifyCoremod.LOGGER;
|
||||
private final Type STRING = Type.getType(String.class);
|
||||
private final Type ENUM = Type.getType(Enum.class);
|
||||
private final Type MARKER_IFACE = Type.getType("Lnet/minecraftforge/common/IExtensibleEnum;");
|
||||
private final Type ARRAY_UTILS = Type.getType("Lorg/apache/commons/lang3/ArrayUtils;"); //Don't directly reference this to prevent class loading.
|
||||
private final String ADD_DESC = Type.getMethodDescriptor(Type.getType(Object[].class), Type.getType(Object[].class), Type.getType(Object.class));
|
||||
private final Type UNSAFE_HACKS = Type.getType("Lnet/minecraftforge/fml/unsafe/UnsafeHacks;"); //Again, not direct reference to prevent class loading.
|
||||
private final String CLEAN_DESC = Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(Class.class));
|
||||
private final String NAME_DESC = Type.getMethodDescriptor(STRING);
|
||||
private final String EQUALS_DESC = Type.getMethodDescriptor(Type.BOOLEAN_TYPE, STRING);
|
||||
|
||||
/**
|
||||
* @return Were changes made?
|
||||
*/
|
||||
public boolean processClassWithFlags(final ClassNode classNode, final Type classType)
|
||||
{
|
||||
if ((classNode.access & Opcodes.ACC_ENUM) == 0)
|
||||
return false;
|
||||
|
||||
Type array = Type.getType("[" + classType.getDescriptor());
|
||||
final int flags = Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL | Opcodes.ACC_SYNTHETIC;
|
||||
|
||||
FieldNode values = classNode.fields.stream().filter(f -> f.desc.contentEquals(array.getDescriptor()) && ((f.access & flags) == flags)).findFirst().orElse(null);
|
||||
|
||||
if (!classNode.interfaces.contains(MARKER_IFACE.getInternalName())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
//Static methods named "create" with first argument as a string
|
||||
List<MethodNode> candidates = classNode.methods.stream()
|
||||
.filter(m -> ((m.access & Opcodes.ACC_STATIC) != 0) && m.name.equals("create"))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
if (candidates.isEmpty()) {
|
||||
throw new IllegalStateException("IExtensibleEnum has no candidate factory methods: " + classType.getClassName());
|
||||
}
|
||||
|
||||
candidates.forEach(mtd ->
|
||||
{
|
||||
Type[] args = Type.getArgumentTypes(mtd.desc);
|
||||
if (args.length == 0 || !args[0].equals(STRING)) {
|
||||
if (LOGGER.isErrorEnabled())
|
||||
{
|
||||
String sb = "Enum has create method without String as first parameter:\n" +
|
||||
" Enum: " + classType.getDescriptor() + "\n" +
|
||||
" Target: " + mtd.name + mtd.desc + "\n";
|
||||
LOGGER.error(sb);
|
||||
}
|
||||
throw new IllegalStateException("Enum has create method without String as first parameter: " + mtd.name + mtd.desc);
|
||||
}
|
||||
|
||||
Type ret = Type.getReturnType(mtd.desc);
|
||||
if (!ret.equals(classType)) {
|
||||
if (LOGGER.isErrorEnabled())
|
||||
{
|
||||
String sb = "Enum has create method with incorrect return type:\n" +
|
||||
" Enum: " + classType.getDescriptor() + "\n" +
|
||||
" Target: " + mtd.name + mtd.desc + "\n" +
|
||||
" Found: " + ret.getClassName() + ", Expected: " + classType.getClassName();
|
||||
LOGGER.error(sb);
|
||||
}
|
||||
throw new IllegalStateException("Enum has create method with incorrect return type: " + mtd.name + mtd.desc);
|
||||
}
|
||||
|
||||
Type[] ctrArgs = new Type[args.length + 1];
|
||||
ctrArgs[0] = STRING;
|
||||
ctrArgs[1] = Type.INT_TYPE;
|
||||
for (int x = 1; x < args.length; x++)
|
||||
ctrArgs[1 + x] = args[x];
|
||||
|
||||
String desc = Type.getMethodDescriptor(Type.VOID_TYPE, ctrArgs);
|
||||
|
||||
MethodNode ctr = classNode.methods.stream().filter(m -> m.name.equals("<init>") && m.desc.equals(desc)).findFirst().orElse(null);
|
||||
if (ctr == null)
|
||||
{
|
||||
if (LOGGER.isErrorEnabled())
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("Enum has create method with no matching constructor:\n");
|
||||
sb.append(" Enum: ").append(classType.getDescriptor()).append("\n");
|
||||
sb.append(" Candidate: ").append(mtd.desc).append("\n");
|
||||
sb.append(" Target: ").append(desc).append("\n");
|
||||
classNode.methods.stream().filter(m -> m.name.equals("<init>")).forEach(m -> sb.append(" : ").append(m.desc).append("\n"));
|
||||
LOGGER.error(sb.toString());
|
||||
}
|
||||
throw new IllegalStateException("Enum has create method with no matching constructor: " + desc);
|
||||
}
|
||||
|
||||
if (values == null)
|
||||
{
|
||||
if (LOGGER.isErrorEnabled())
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("Enum has create method but we could not find $VALUES. Found:\n");
|
||||
classNode.fields.stream().filter(f -> (f.access & Opcodes.ACC_STATIC) != 0).
|
||||
forEach(m -> sb.append(" ").append(m.name).append(" ").append(m.desc).append("\n"));
|
||||
LOGGER.error(sb.toString());
|
||||
}
|
||||
throw new IllegalStateException("Enum has create method but we could not find $VALUES");
|
||||
}
|
||||
|
||||
values.access &= values.access & ~Opcodes.ACC_FINAL; //Strip the final so JITer doesn't inline things.
|
||||
|
||||
mtd.access |= Opcodes.ACC_SYNCHRONIZED;
|
||||
mtd.instructions.clear();
|
||||
mtd.localVariables.clear();
|
||||
if (mtd.tryCatchBlocks != null)
|
||||
{
|
||||
mtd.tryCatchBlocks.clear();
|
||||
}
|
||||
if (mtd.visibleLocalVariableAnnotations != null)
|
||||
{
|
||||
mtd.visibleLocalVariableAnnotations.clear();
|
||||
}
|
||||
if (mtd.invisibleLocalVariableAnnotations != null)
|
||||
{
|
||||
mtd.invisibleLocalVariableAnnotations.clear();
|
||||
}
|
||||
InstructionAdapter ins = new InstructionAdapter(mtd);
|
||||
|
||||
int vars = 0;
|
||||
for (Type arg : args)
|
||||
vars += arg.getSize();
|
||||
|
||||
{
|
||||
vars += 1; //int x
|
||||
Label for_start = new Label();
|
||||
Label for_condition = new Label();
|
||||
Label for_inc = new Label();
|
||||
|
||||
ins.iconst(0);
|
||||
ins.store(vars, Type.INT_TYPE);
|
||||
ins.goTo(for_condition);
|
||||
//if (!VALUES[x].name().equalsIgnoreCase(name)) goto for_inc
|
||||
ins.mark(for_start);
|
||||
ins.getstatic(classType.getInternalName(), values.name, values.desc);
|
||||
ins.load(vars, Type.INT_TYPE);
|
||||
ins.aload(array);
|
||||
ins.invokevirtual(ENUM.getInternalName(), "name", NAME_DESC, false);
|
||||
ins.load(0, STRING);
|
||||
ins.invokevirtual(STRING.getInternalName(), "equalsIgnoreCase", EQUALS_DESC, false);
|
||||
ins.ifeq(for_inc);
|
||||
//return VALUES[x];
|
||||
ins.getstatic(classType.getInternalName(), values.name, values.desc);
|
||||
ins.load(vars, Type.INT_TYPE);
|
||||
ins.aload(array);
|
||||
ins.areturn(classType);
|
||||
//x++
|
||||
ins.mark(for_inc);
|
||||
ins.iinc(vars, 1);
|
||||
//if (x < VALUES.length) goto for_start
|
||||
ins.mark(for_condition);
|
||||
ins.load(vars, Type.INT_TYPE);
|
||||
ins.getstatic(classType.getInternalName(), values.name, values.desc);
|
||||
ins.arraylength();
|
||||
ins.ificmplt(for_start);
|
||||
}
|
||||
|
||||
{
|
||||
vars += 1; //enum ret;
|
||||
//ret = new ThisType(name, VALUES.length, args..)
|
||||
ins.anew(classType);
|
||||
ins.dup();
|
||||
ins.load(0, STRING);
|
||||
ins.getstatic(classType.getInternalName(), values.name, values.desc);
|
||||
ins.arraylength();
|
||||
int idx = 1;
|
||||
for (int x = 1; x < args.length; x++)
|
||||
{
|
||||
ins.load(idx, args[x]);
|
||||
idx += args[x].getSize();
|
||||
}
|
||||
ins.invokespecial(classType.getInternalName(), "<init>", desc, false);
|
||||
ins.store(vars, classType);
|
||||
// VALUES = ArrayUtils.add(VALUES, ret)
|
||||
ins.getstatic(classType.getInternalName(), values.name, values.desc);
|
||||
ins.load(vars, classType);
|
||||
ins.invokestatic(ARRAY_UTILS.getInternalName(), "add", ADD_DESC, false);
|
||||
ins.checkcast(array);
|
||||
ins.putstatic(classType.getInternalName(), values.name, values.desc);
|
||||
//EnumHelper.cleanEnumCache(ThisType.class)
|
||||
ins.visitLdcInsn(classType);
|
||||
ins.invokestatic(UNSAFE_HACKS.getInternalName(), "cleanEnumCache", CLEAN_DESC, false);
|
||||
//init ret
|
||||
ins.load(vars, classType);
|
||||
ins.invokeinterface(MARKER_IFACE.getInternalName(), "init", "()V");
|
||||
//return ret
|
||||
ins.load(vars, classType);
|
||||
ins.areturn(classType);
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -4,11 +4,14 @@ import net.minecraft.launchwrapper.IClassTransformer;
|
||||
import org.objectweb.asm.ClassReader;
|
||||
import org.objectweb.asm.ClassWriter;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import org.objectweb.asm.Type;
|
||||
import org.objectweb.asm.tree.AnnotationNode;
|
||||
import org.objectweb.asm.tree.ClassNode;
|
||||
import org.objectweb.asm.tree.FieldNode;
|
||||
|
||||
public class UnfinalizeObjectHoldersTransformer implements IClassTransformer {
|
||||
final ExtensibleEnumTransformerHelper enumTransformer = new ExtensibleEnumTransformerHelper();
|
||||
|
||||
@Override
|
||||
public byte[] transform(String name, String transformedName, byte[] basicClass) {
|
||||
if (basicClass == null) {
|
||||
@@ -18,6 +21,7 @@ public class UnfinalizeObjectHoldersTransformer implements IClassTransformer {
|
||||
final ClassReader reader = new ClassReader(basicClass);
|
||||
final ClassNode node = new ClassNode();
|
||||
reader.accept(node, ClassReader.EXPAND_FRAMES);
|
||||
final Type classType = Type.getType("L" + transformedName.replace('.', '/') + ";");
|
||||
|
||||
boolean transformClass = false;
|
||||
boolean workDone = false;
|
||||
@@ -64,6 +68,16 @@ public class UnfinalizeObjectHoldersTransformer implements IClassTransformer {
|
||||
}
|
||||
if (workDone) {
|
||||
Lwjgl3ifyCoremod.LOGGER.info("Unfinalized {} Holder fields in {}", fieldsModified, transformedName);
|
||||
}
|
||||
|
||||
final boolean enumsTransformed = enumTransformer.processClassWithFlags(node, classType);
|
||||
|
||||
if (enumsTransformed) {
|
||||
workDone = true;
|
||||
Lwjgl3ifyCoremod.LOGGER.info("Dynamicized enum {}", transformedName);
|
||||
}
|
||||
|
||||
if (workDone) {
|
||||
final ClassWriter writer = new ClassWriter(0);
|
||||
node.accept(writer);
|
||||
return writer.toByteArray();
|
||||
|
||||
Reference in new issue
Block a user