Make GTNH work with Java 11:

- Add a javax.xml.bind -> jakarta.xml.bind remapper and shadow in the library
 - Update commons-lang3 to fix a loading crash
 - Fix UnsafeHacks on static fields
 - Add more enums to add extensibility to
 - Workaround java 8 miscompilation of lambda fields in interfaces
This commit is contained in:
Raven Szewczyk committed 2023-01-21 11:51:20 +00:00
1 parent 67eb934178
commit 3ff3d0a2cc
10 files changed
+154 -31

No files matched your search

@@ -47,23 +47,47 @@ public class UnsafeHacks {
@SuppressWarnings("unchecked")
public static <T> T getField(Field field, Object object) {
final long l = UNSAFE.objectFieldOffset(field);
return (T) UNSAFE.getObject(object, l);
if (object == null) {
long offset = UNSAFE.staticFieldOffset(field);
Object base = UNSAFE.staticFieldBase(field);
return (T) UNSAFE.getObject(base, offset);
} else {
long offset = UNSAFE.objectFieldOffset(field);
return (T) UNSAFE.getObject(object, offset);
}
}
public static void setField(Field data, Object object, Object value) {
long offset = UNSAFE.objectFieldOffset(data);
UNSAFE.putObject(object, offset, value);
if (object == null) {
long offset = UNSAFE.staticFieldOffset(data);
Object base = UNSAFE.staticFieldBase(data);
UNSAFE.putObject(base, offset, value);
} else {
long offset = UNSAFE.objectFieldOffset(data);
UNSAFE.putObject(object, offset, value);
}
}
public static int getIntField(Field f, Object obj) {
long offset = UNSAFE.objectFieldOffset(f);
return UNSAFE.getInt(obj, offset);
public static int getIntField(Field field, Object object) {
if (object == null) {
long offset = UNSAFE.staticFieldOffset(field);
Object base = UNSAFE.staticFieldBase(field);
return UNSAFE.getInt(base, offset);
} else {
long offset = UNSAFE.objectFieldOffset(field);
return UNSAFE.getInt(object, offset);
}
}
public static void setIntField(Field data, Object object, int value) {
long offset = UNSAFE.objectFieldOffset(data);
UNSAFE.putInt(object, offset, value);
if (object == null) {
long offset = UNSAFE.staticFieldOffset(data);
Object base = UNSAFE.staticFieldBase(data);
UNSAFE.putInt(base, offset, value);
} else {
long offset = UNSAFE.objectFieldOffset(data);
UNSAFE.putInt(object, offset, value);
}
}
// Make sure we don't crash if any future versions change field names
@@ -32,9 +32,15 @@ public class Config {
"vswe.stevesfactory.components.ComponentType",
"vswe.stevesfactory.components.ConnectionSet",
"vswe.stevesfactory.components.ConnectionOption",
"ic2.core.init.InternalName",
"gregtech.api.enums.Element",
"gregtech.api.enums.OrePrefixes",
"net.minecraft.client.audio.MusicTicker$MusicType",
"org.bukkit.Material",
"buildcraft.api.transport.IPipeTile.PipeType",
"thaumcraft.common.entities.golems.EnumGolemType",
};
// TODO: Make this an actual config file :P
public static List<String> EXTENSIBLE_ENUMS = Arrays.asList(DEFAULT_EXTENSIBLE_ENUMS);
}
@@ -0,0 +1,67 @@
package me.eigenraven.lwjgl3ify.core;
import java.util.concurrent.atomic.AtomicBoolean;
import org.objectweb.asm.Handle;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.InvokeDynamicInsnNode;
import org.objectweb.asm.tree.MethodNode;
/**
* Fixes a compilation bug of the java 8 compiler leading to the following exception at runtime:
* cpw.mods.fml.common.LoaderException: java.lang.IncompatibleClassChangeError: Inconsistent constant pool data
* in classfile for class com/gtnewhorizon/structurelib/alignment/IAlignmentLimits. Method
* 'boolean lambda$static$0(net.minecraftforge.common.util.ForgeDirection,
* com.gtnewhorizon.structurelib.alignment.enumerable.Rotation, com.gtnewhorizon.structurelib.alignment.enumerable.Flip)'
* at index 77 is CONSTANT_MethodRef and should be CONSTANT_InterfaceMethodRef
*/
public class FixConstantPoolInterfaceMethodRefHelper {
public boolean transform(ClassNode node) {
if ((node.access & Opcodes.ACC_INTERFACE) == 0) {
return false;
}
boolean changesMade = false;
final String internalClassName = node.name;
if (node.methods != null) {
for (MethodNode method : node.methods) {
if (method.instructions != null) {
for (AbstractInsnNode insn : method.instructions) {
changesMade |= validateInstruction(internalClassName, insn);
}
}
}
}
return changesMade;
}
private boolean validateInstruction(String internalClassName, AbstractInsnNode rawInsn) {
AtomicBoolean changed = new AtomicBoolean(false);
switch (rawInsn.getType()) {
case AbstractInsnNode.INVOKE_DYNAMIC_INSN:
final InvokeDynamicInsnNode insn = (InvokeDynamicInsnNode) rawInsn;
insn.bsm = fixHandle(internalClassName, insn.bsm, changed);
if (insn.bsmArgs != null) {
for (int i = 0; i < insn.bsmArgs.length; i++) {
final Object arg = insn.bsmArgs[i];
if (arg instanceof Handle) {
insn.bsmArgs[i] = fixHandle(internalClassName, (Handle) arg, changed);
}
}
}
break;
default:
// no-op
break;
}
return changed.get();
}
private Handle fixHandle(String internalClassName, Handle handle, AtomicBoolean changed) {
if (!handle.isInterface() && handle.getOwner().equals(internalClassName)) {
changed.set(true);
return new Handle(handle.getTag(), handle.getOwner(), handle.getName(), handle.getDesc(), true);
}
return handle;
}
}
@@ -55,26 +55,29 @@ public class LwjglRedirectTransformer extends Remapper implements IClassTransfor
return writer.toByteArray();
}
final String[] fromPrefixes = new String[] {
"org/lwjgl/", "paulscode/sound/libraries/", "javax/xml/bind/",
};
final String[] toPrefixes = new String[] {
"org/lwjglx/", "me/eigenraven/lwjgl3ify/paulscode/sound/libraries/", "jakarta/xml/bind/",
};
@Override
public String map(String typeName) {
if (typeName == null) {
return null;
}
calls++;
final String OLD_PREFIX_GL = "org/lwjgl/";
final String NEW_PREFIX_GL = "org/lwjglx/";
final String OLD_PREFIX_PCS = "paulscode/sound/libraries/";
final String NEW_PREFIX_PCS = "me/eigenraven/lwjgl3ify/paulscode/sound/libraries/";
if (typeName.startsWith(OLD_PREFIX_GL)) {
remaps++;
final String newName = NEW_PREFIX_GL + typeName.substring(OLD_PREFIX_GL.length());
return newName;
} else if (typeName.startsWith(OLD_PREFIX_PCS)) {
remaps++;
return NEW_PREFIX_PCS + typeName.substring(OLD_PREFIX_PCS.length());
} else {
return typeName;
for (int pfx = 0; pfx < fromPrefixes.length; pfx++) {
if (typeName.startsWith(fromPrefixes[pfx])) {
remaps++;
final String newName = toPrefixes[pfx] + typeName.substring(fromPrefixes[pfx].length());
return newName;
}
}
return typeName;
}
private class Lwjgl3AwareException extends RuntimeException {}
@@ -11,7 +11,10 @@ import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldNode;
public class UnfinalizeObjectHoldersTransformer implements IClassTransformer {
// Keep ClassNode-operating transformers together for efficiency (don't read/write the class multiple times)
final ExtensibleEnumTransformerHelper enumTransformer = new ExtensibleEnumTransformerHelper();
final FixConstantPoolInterfaceMethodRefHelper cpiMethodRefTransformer =
new FixConstantPoolInterfaceMethodRefHelper();
@Override
public byte[] transform(String name, String transformedName, byte[] basicClass) {
@@ -88,6 +91,14 @@ public class UnfinalizeObjectHoldersTransformer implements IClassTransformer {
Lwjgl3ifyCoremod.LOGGER.info("Dynamicized enum {}={}", name, transformedName);
}
final boolean ifaceMethodRefsTransformed = cpiMethodRefTransformer.transform(node);
if (ifaceMethodRefsTransformed) {
workDone = true;
Lwjgl3ifyCoremod.LOGGER.warn(
"Fixed missing CONSTANT_InterfaceMethodRef miscompilation in {}={}", name, transformedName);
}
if (workDone) {
final ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
node.accept(writer);