Make RedirectorTransformer handle block subclasses correctly (#249)

Fixes #248
This commit is contained in:
makamys authored and GitHub committed 2024-01-30 11:47:28 -08:00
1 parent 05158317e8
commit aebaab8d6f
3 files changed
+46 -13

No files matched your search

@@ -11,6 +11,7 @@ import java.util.List;
public class BlockTransformer implements IClassTransformer {
public static final String BlockClass = "net/minecraft/block/Block";
public static final String BlockPackage = BlockClass.substring(0, BlockClass.lastIndexOf('/') + 1);
private static final String BlockClassFriendly = BlockClass.replace('/', '.');
public static final List<Pair<String, String>> BlockBoundsFields = ImmutableList.of(
Pair.of("minX", "field_149759_B"),
@@ -61,6 +61,16 @@ public class ClassConstantPoolParser {
* for
*/
public boolean find(byte[] basicClass) {
return find(basicClass, false);
}
/**
* Returns true if the constant pool of the class represented by this byte array contains one of the Strings we are looking
* for.
*
* @param prefixes If true, it is enough for a constant pool entry to <i>start</i> with one of our Strings to count as a match -
* otherwise, the entire String has to match.
*/
public boolean find(byte[] basicClass, boolean prefixes) {
if (basicClass == null || basicClass.length == 0) {
return false;
}
@@ -92,9 +102,9 @@ public class ClassConstantPoolParser {
final int strLen = readUnsignedShort(index + 1, basicClass);
size = 3 + strLen;
for (byte[] bytes : BYTES_TO_SEARCH) {
if (strLen == bytes.length) {
if (prefixes ? strLen >= bytes.length : strLen == bytes.length) {
boolean found = true;
for (int j = index + 3; j < index + 3 + strLen; j++) {
for (int j = index + 3; j < index + 3 + bytes.length; j++) {
if (basicClass[j] != bytes[j - (index + 3)]) {
found = false;
break;
@@ -36,6 +36,7 @@ import java.util.stream.Collectors;
import static com.gtnewhorizons.angelica.transform.BlockTransformer.BlockBoundsFields;
import static com.gtnewhorizons.angelica.transform.BlockTransformer.BlockClass;
import static com.gtnewhorizons.angelica.transform.BlockTransformer.BlockPackage;
/**
* This transformer redirects all Tessellator.instance field accesses to go through our TessellatorManager.
@@ -63,8 +64,16 @@ public class RedirectorTransformer implements IClassTransformer {
"startGame", "func_71384_a",
"initializeTextures", "func_77474_a"
);
/** All classes in <tt>net.minecraft.block.*</tt> are the block subclasses save for these. */
private static final List<String> VanillaBlockExclusions = Arrays.asList(
"net/minecraft/block/IGrowable",
"net/minecraft/block/ITileEntityProvider",
"net/minecraft/block/BlockEventData",
"net/minecraft/block/BlockSourceImpl",
"net/minecraft/block/material/"
);
private static final ClassConstantPoolParser cstPoolParser = new ClassConstantPoolParser(GL11, GL13, GL14, OpenGlHelper, EXTBlendFunc, ARBMultiTexture, TessellatorClass, BlockClass,
private static final ClassConstantPoolParser cstPoolParser = new ClassConstantPoolParser(GL11, GL13, GL14, OpenGlHelper, EXTBlendFunc, ARBMultiTexture, TessellatorClass, BlockPackage,
Project);
private static final Map<String, Map<String, String>> methodRedirects = new HashMap<>();
private static final Map<Integer, String> glCapRedirects = new HashMap<>();
@@ -76,7 +85,7 @@ public class RedirectorTransformer implements IClassTransformer {
);
private static int remaps = 0;
private static final Set<String> blockSubclasses = Collections.newSetFromMap(new ConcurrentHashMap<>());
private static final Set<String> moddedBlockSubclasses = Collections.newSetFromMap(new ConcurrentHashMap<>());
// Block owners we *shouldn't* redirect because they shadow one of our fields
private static final Set<String> blockOwnerExclusions = Collections.newSetFromMap(new ConcurrentHashMap<>());
@@ -152,7 +161,22 @@ public class RedirectorTransformer implements IClassTransformer {
methodRedirects.put(EXTBlendFunc, RedirectMap.newMap().add("glBlendFuncSeparateEXT", "tryBlendFuncSeparate"));
methodRedirects.put(ARBMultiTexture, RedirectMap.newMap().add("glActiveTextureARB"));
methodRedirects.put(Project, RedirectMap.newMap().add("gluPerspective"));
blockSubclasses.add(BlockClass);
}
private boolean isVanillaBlockSubclass(String className) {
if(className.startsWith(BlockTransformer.BlockPackage)) {
for(String exclusion : VanillaBlockExclusions) {
if(className.startsWith(exclusion)) {
return false;
}
}
return true;
}
return false;
}
private boolean isBlockSubclass(String className) {
return isVanillaBlockSubclass(className) || moddedBlockSubclasses.contains(className);
}
@Override
@@ -167,7 +191,7 @@ public class RedirectorTransformer implements IClassTransformer {
}
}
if (!cstPoolParser.find(basicClass)) {
if (!cstPoolParser.find(basicClass, true)) {
return basicClass;
}
@@ -176,19 +200,17 @@ public class RedirectorTransformer implements IClassTransformer {
cr.accept(cn, 0);
// Track subclasses of Block
if (blockSubclasses.contains(cn.superName)) {
blockSubclasses.add(cn.name);
if (!isVanillaBlockSubclass(cn.name) && isBlockSubclass(cn.superName)) {
moddedBlockSubclasses.add(cn.name);
cstPoolParser.addString(cn.name);
}
// Check if this class shadows any fields of the parent class
if(blockSubclasses.contains(cn.name)) {
if(moddedBlockSubclasses.contains(cn.name)) {
// If a superclass shadows, then so do we, because JVM will resolve a reference on our class to that
// superclass
boolean doWeShadow;
if(cn.name.equals(BlockClass)) {
doWeShadow = false; // by definition
} else if(blockOwnerExclusions.contains(cn.superName)) {
if(blockOwnerExclusions.contains(cn.superName)) {
doWeShadow = true;
} else {
// Check if we declare any known field names
@@ -274,7 +296,7 @@ public class RedirectorTransformer implements IClassTransformer {
}
}
else if ((node.getOpcode() == Opcodes.GETFIELD || node.getOpcode() == Opcodes.PUTFIELD) && node instanceof FieldInsnNode fNode) {
if(!blockOwnerExclusions.contains(fNode.owner) && blockSubclasses.contains(fNode.owner) && AngelicaConfig.enableSodium) {
if(!blockOwnerExclusions.contains(fNode.owner) && isBlockSubclass(fNode.owner) && AngelicaConfig.enableSodium) {
Pair<String, String> fieldToRedirect = null;
for(Pair<String, String> blockPairs : BlockBoundsFields) {
if(fNode.name.equals(blockPairs.getLeft()) || fNode.name.equals(blockPairs.getRight())) {