Skip to content

Commit

Permalink
Add ExpectPlatform Classes
Browse files Browse the repository at this point in the history
  • Loading branch information
KP2048 committed Mar 1, 2022
1 parent 1ea77ad commit 6916af5
Showing 1 changed file with 38 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,12 @@
import dev.architectury.transformer.transformers.base.ClassEditTransformer;
import dev.architectury.transformer.transformers.base.edit.TransformerContext;
import dev.architectury.transformer.util.Logger;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.*;
import org.objectweb.asm.tree.*;

import java.util.ArrayList;
import java.util.Objects;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

import static dev.architectury.transformer.transformers.RemapInjectables.getUniqueIdentifier;

Expand Down Expand Up @@ -85,6 +83,20 @@ private byte[] buildPlatformMethodClass(String className) {
@Override
public ClassNode doEdit(String name, ClassNode node) {
if (!RemapInjectables.isInjectInjectables()) return node;
for (MethodNode method : node.methods) {
for (ListIterator<AbstractInsnNode> iterator = method.instructions.iterator(); iterator.hasNext(); ) {
AbstractInsnNode instruction = iterator.next();
if (instruction instanceof TypeInsnNode) {
TypeInsnNode typeInsn = (TypeInsnNode) instruction;
if (typeInsn.getOpcode() == Opcodes.NEW) {
String className = typeInsn.desc;
if (getClassNode(className).visibleAnnotations.stream().anyMatch(it -> Objects.equals(it.desc, RemapInjectables.EXPECT_PLATFORM) || Objects.equals(it.desc, RemapInjectables.EXPECT_PLATFORM_LEGACY) || Objects.equals(it.desc, RemapInjectables.EXPECT_PLATFORM_LEGACY2))) {
iterator.set(new TypeInsnNode(Opcodes.NEW, getPlatformClass(className)));
}
}
}
}
}
for (MethodNode method : node.methods) {
String platformMethodsClass = null;

Expand Down Expand Up @@ -123,10 +135,28 @@ public ClassNode doEdit(String name, ClassNode node) {

return node;
}

private static String getPlatformClass(String lookupClass) {

private static ClassNode getClassNode(String name) {
ClassNode node = new ClassNode();
try {
ClassReader reader = new ClassReader(Objects.requireNonNull(Class.forName(name.replace('/', '.')).getResourceAsStream(name + ".class")));
reader.accept(node, 0);
return node;
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}

throw new RuntimeException("Couldn't create ClassNode for class " + name);
}

private static String getPlatformName() {
String platform = System.getProperty(BuiltinProperties.PLATFORM_NAME);
Preconditions.checkNotNull(platform, BuiltinProperties.PLATFORM_NAME + " is not present!");
return platform;
}

private static String getPlatformClass(String lookupClass) {
String platform = getPlatformName();
String lookupType = lookupClass.replace("$", "") + "Impl";

return lookupType.substring(0, lookupType.lastIndexOf('/')) + "/" + platform + "/" +
Expand Down

0 comments on commit 6916af5

Please sign in to comment.