Skip to content

Commit

Permalink
Invoke helper methods directly
Browse files Browse the repository at this point in the history
  • Loading branch information
ME1312 committed Mar 15, 2024
1 parent 7bcde5d commit 1fd28e1
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import org.objectweb.asm.Opcodes;
import vanillacord.packaging.Package;

public class HierarchyVisitor extends HierarchyScanner {
public class SourceScanner extends HierarchyScanner {
private final Package file;
private ClassData data;
private boolean hasTID;

public HierarchyVisitor(Package file) {
public SourceScanner(Package file) {
super(Opcodes.ASM9, file.types);
this.file = file;
}
Expand Down Expand Up @@ -42,23 +42,23 @@ public void visitLdcInsn(Object value) {
if (value instanceof String) {
if ("Server console handler".equals(value)) {
System.out.print("Found the dedicated server: ");
System.out.println(HierarchyVisitor.super.name);
System.out.println(SourceScanner.super.name);
file.sources.startup = data;
} else if ("Unexpected hello packet".equals(value)) {
System.out.print("Found the login listener: ");
System.out.println(HierarchyVisitor.super.name);
System.out.println(SourceScanner.super.name);
file.sources.login = data;
} else if (hasTID && "Payload may not be larger than 1048576 bytes".equals(value)) {
System.out.print("Found a login extension packet: ");
System.out.println(HierarchyVisitor.super.name);
System.out.println(SourceScanner.super.name);
if (file.sources.send == null) {
file.sources.send = data;
} else {
file.sources.receive = data;
}
} else if ("multiplayer.disconnect.incompatible".equals(value) || "multiplayer.disconnect.outdated_server".equals(value) || ((String) value).startsWith("Outdated client! Please use")) {
System.out.print("Found the handshake listener: ");
System.out.println(HierarchyVisitor.super.name);
System.out.println(SourceScanner.super.name);
file.sources.handshake = data;
}
}
Expand Down
4 changes: 2 additions & 2 deletions java/vanillacord/packaging/Bundle.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.objectweb.asm.ClassReader;
import vanillacord.Patcher;
import vanillacord.data.Digest;
import vanillacord.data.HierarchyVisitor;
import vanillacord.data.SourceScanner;

import java.io.*;
import java.net.URL;
Expand Down Expand Up @@ -63,7 +63,7 @@ List<String[]> load(ClassLoader bundle, String name, Consumer<ClassReader> actio
public ZipInputStream read(File file) throws Throwable {
URLClassLoader bundle = new URLClassLoader(new URL[]{ (this.file = file).toURI().toURL() }, null);
load(bundle, "libraries", reader -> reader.accept(new HierarchyScanner(types), ClassReader.SKIP_CODE | ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG));
List<String[]> versions = load(bundle, "versions", reader -> reader.accept(new HierarchyVisitor(this), ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG));
List<String[]> versions = load(bundle, "versions", reader -> reader.accept(new SourceScanner(this), ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG));
if (versions.size() != 1) throw new IllegalStateException("More or less than one version was distributed");
return new ZipInputStream(bundle.getResourceAsStream("META-INF/versions/" + (server = versions.get(0))[2]));
}
Expand Down
4 changes: 2 additions & 2 deletions java/vanillacord/packaging/FatJar.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package vanillacord.packaging;

import org.objectweb.asm.ClassReader;
import vanillacord.data.HierarchyVisitor;
import vanillacord.data.SourceScanner;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -17,7 +17,7 @@ public ZipInputStream read(File file) throws Throwable {
try (ZipInputStream zis = new ZipInputStream(new FileInputStream(file))) {
for (ZipEntry entry; (entry = zis.getNextEntry()) != null;) {
if (entry.getName().endsWith(".class")) {
new ClassReader(zis).accept(new HierarchyVisitor(this), ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG);
new ClassReader(zis).accept(new SourceScanner(this), ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG);
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion java/vanillacord/patch/DedicatedServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,24 @@ public MethodVisitor visitMethod(int access, String name, String descriptor, Str
@Override
public void visitMethodInsn(int opcode, String owner, String name, String descriptor, boolean isInterface) {
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
if (state == 0 && opcode == INVOKEVIRTUAL && owner.equals("java/lang/Thread") && descriptor.equals("()V")) {
if (state == 0 && opcode == INVOKEVIRTUAL && owner.equals("java/lang/Thread") && name.equals("start") && descriptor.equals("()V")) {
state = 1;
} else if (state == 3 && opcode == INVOKEINTERFACE && owner.endsWith("/Logger")) {
Label start, end, jump, handler;
mv.visitTryCatchBlock(start = new Label(), end = new Label(), handler = new Label(), "java/lang/ExceptionInInitializerError");
mv.visitLabel(start);
mv.visitFieldInsn(GETSTATIC,
"vanillacord/server/VanillaCord",
"helper",
"Lvanillacord/server/ForwardingHelper;"
);
mv.visitInsn(POP);
mv.visitLabel(end);
mv.visitJumpInsn(GOTO, jump = new Label());
mv.visitLabel(handler);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/ExceptionInInitializerError", "getCause", "()Ljava/lang/Throwable;", false);
mv.visitInsn(ATHROW);
mv.visitLabel(jump);
state = 4;
}
}
Expand Down
9 changes: 7 additions & 2 deletions java/vanillacord/patch/HandshakeListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,20 @@ public void visitMethodInsn(int opcode, String owner, String name, String descri
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
if (vanilla && opcode == INVOKEVIRTUAL && file.sources.connection.type.type.getInternalName().equals(owner)) {
mv.visitLabel(new Label());
mv.visitFieldInsn(GETSTATIC,
"vanillacord/server/VanillaCord",
"helper",
"Lvanillacord/server/ForwardingHelper;"
);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD,
connection.owner.clazz.type.getInternalName(),
connection.name,
connection.descriptor
);
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKESTATIC,
"vanillacord/server/VanillaCord",
mv.visitMethodInsn(INVOKEVIRTUAL,
"vanillacord/server/ForwardingHelper",
"parseHandshake",
"(Ljava/lang/Object;Ljava/lang/Object;)V",
false
Expand Down
78 changes: 52 additions & 26 deletions java/vanillacord/patch/LoginListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public MethodVisitor visitMethod(int access, String name, String descriptor, Str
MethodVisitor mv = super.visitMethod(access, name, descriptor, signature, exceptions);
mv.visitCode();
mv.visitLabel(new Label());
mv.visitFieldInsn(GETSTATIC,
"vanillacord/server/VanillaCord",
"helper",
"Lvanillacord/server/ForwardingHelper;"
);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD,
file.sources.connection.owner.clazz.type.getInternalName(),
Expand All @@ -68,8 +73,8 @@ public MethodVisitor visitMethod(int access, String name, String descriptor, Str
"()Ljava/lang/String;",
false
);
mv.visitMethodInsn(INVOKESTATIC,
"vanillacord/server/VanillaCord",
mv.visitMethodInsn(INVOKEVIRTUAL,
"vanillacord/server/ForwardingHelper",
"injectProfile",
"(Ljava/lang/Object;Ljava/lang/String;)Lcom/mojang/authlib/GameProfile;",
false
Expand All @@ -87,6 +92,11 @@ public void visitLabel(Label label) {
super.visitLabel(label);
if (prelabel) {
label = new Label();
mv.visitFieldInsn(GETSTATIC,
"vanillacord/server/VanillaCord",
"helper",
"Lvanillacord/server/ForwardingHelper;"
);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD,
file.sources.connection.owner.clazz.type.getInternalName(),
Expand All @@ -95,8 +105,8 @@ public void visitLabel(Label label) {
);
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKESTATIC,
"vanillacord/server/VanillaCord",
mv.visitMethodInsn(INVOKEVIRTUAL,
"vanillacord/server/ForwardingHelper",
"completeTransaction",
"(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Z",
false
Expand All @@ -112,17 +122,20 @@ public void visitLabel(Label label) {
return new MethodVisitor(ASM9, super.visitMethod(access, name, descriptor, signature, exceptions)) {
private final LinkedList<Runnable> undo = new LinkedList<Runnable>();
private final MethodVisitor mv = super.mv;
private int state = 0;
private byte state = 0;
private Label skip;

@Override
public void visitVarInsn(int opcode, int index) {
if (state == 1 && opcode == ALOAD && index == 0) {
undo.add(() -> super.visitVarInsn(opcode, index));
undo.add(() -> super.visitVarInsn(ALOAD, 0));
state = 2;
return;
} else {
undo();
}
undo();
if (state == 6 && opcode == ALOAD && index == 0) {
undo.add(() -> super.visitVarInsn(ALOAD, 0));
return;
}
super.visitVarInsn(opcode, index);
}
Expand All @@ -144,17 +157,12 @@ public void visitFieldInsn(int opcode, String owner, String name, String desc) {

} else if (state == 2 && opcode == GETFIELD && desc.equals("Lnet/minecraft/server/MinecraftServer;")) {
state = 3;
undo.add(() -> super.visitFieldInsn(opcode, owner, name, desc));
undo.add(() -> super.visitFieldInsn(GETFIELD, owner, name, "Lnet/minecraft/server/MinecraftServer;"));
return;
} else if (state == 6 && opcode == GETFIELD && desc.equals("Ljava/lang/String;") && file.sources.login.owner.clazz.type.getInternalName().equals(owner)) {
state = 7;
undo.add(() -> super.visitFieldInsn(GETFIELD, owner, name, "Ljava/lang/String;"));
return;
} else if (state == -6 && opcode == GETFIELD && desc.equals("Ljava/lang/String;") && file.sources.login.owner.clazz.type.getInternalName().equals(owner)) {
state = -7;
mv.visitFieldInsn(
GETFIELD,
file.sources.connection.owner.clazz.type.getInternalName(),
file.sources.connection.name,
file.sources.connection.descriptor
);
mv.visitVarInsn(ALOAD, 0);
} else {
undo();
}
Expand All @@ -168,14 +176,27 @@ public void visitMethodInsn(int opcode, String owner, String name, String desc,
undo.clear();
state = 4;
return;
} else if (state == -7 && opcode == INVOKESTATIC && desc.equals("(Ljava/lang/String;)Lcom/mojang/authlib/GameProfile;")) {
mv.visitMethodInsn(INVOKESTATIC,
} else if (state == 7 && opcode == INVOKESTATIC && desc.equals("(Ljava/lang/String;)Lcom/mojang/authlib/GameProfile;")) {
mv.visitFieldInsn(GETSTATIC,
"vanillacord/server/VanillaCord",
"helper",
"Lvanillacord/server/ForwardingHelper;"
);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(
GETFIELD,
file.sources.connection.owner.clazz.type.getInternalName(),
file.sources.connection.name,
file.sources.connection.descriptor
);
undo();
mv.visitMethodInsn(INVOKEVIRTUAL,
"vanillacord/server/ForwardingHelper",
"injectProfile",
"(Ljava/lang/Object;Ljava/lang/String;)Lcom/mojang/authlib/GameProfile;",
false
);
state = -8;
state = 8;
return;
} else {
undo();
Expand All @@ -186,7 +207,7 @@ public void visitMethodInsn(int opcode, String owner, String name, String desc,
@Override
public void visitJumpInsn(int opcode, Label label) {
if (state == 4 && opcode == IFEQ) {
state = -5;
state = 5;
skip = label;
} else {
undo();
Expand All @@ -196,9 +217,9 @@ public void visitJumpInsn(int opcode, Label label) {

@Override
public void visitLabel(Label label) {
if (state == -5 && skip == label) {
if (state == 5 && skip == label) {
super.mv = mv;
state = -6;
state = 6;
}
super.visitLabel(label);
}
Expand All @@ -211,15 +232,20 @@ public void visitTypeInsn(int opcode, String type) {

@Override
public void visitEnd() {
if (state >= 0) throw new IllegalStateException("Inject failed: 0x0" + state);
if (state != 8 && state != 6) throw new IllegalStateException("Inject failed: 0x0" + state);
super.visitEnd();
}

private void undo() {
if (state > 1) {
byte s;
if ((s = state) > 1 && s < 5) {
for (Runnable action : undo) action.run();
undo.clear();
state = 1;
} else if (s > 5 && s < 8) {
for (Runnable action : undo) action.run();
undo.clear();
state = 6;
}
}
};
Expand Down
9 changes: 7 additions & 2 deletions java/vanillacord/patch/LoginPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public void visitLabel(Label label) {
super.visitLabel(label);
if (prelabel) {
label = new Label();
mv.visitFieldInsn(GETSTATIC,
"vanillacord/server/VanillaCord",
"helper",
"Lvanillacord/server/ForwardingHelper;"
);
mv.visitVarInsn(ALOAD, 1);
if (!file.sources.login.owner.clazz.type.equals(args[0])) {
mv.visitTypeInsn(CHECKCAST, file.sources.login.owner.clazz.type.getInternalName());
Expand All @@ -48,8 +53,8 @@ public void visitLabel(Label label) {
file.sources.connection.descriptor
);
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESTATIC,
"vanillacord/server/VanillaCord",
mv.visitMethodInsn(INVOKEVIRTUAL,
"vanillacord/server/ForwardingHelper",
"initializeTransaction",
"(Ljava/lang/Object;Ljava/lang/Object;)Z",
false
Expand Down
22 changes: 3 additions & 19 deletions java/vanillacord/server/VanillaCord.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class VanillaCord {
writer.println();
writer.println("# Some forwarding standards require a seecret key to function.");
writer.println("# Specify that here. Repeat this line to specify multiple.");
if (seecrets.isEmpty()) {
if (seecrets.size() == 0) {
writer.println("seecret = ");
} else {
for (String s : seecrets) {
Expand All @@ -86,7 +86,7 @@ public class VanillaCord {
helper = (ForwardingHelper) MethodHandles.lookup().findConstructor(VelocityHelper.class, MethodType.methodType(void.class, LinkedList.class)).invoke(seecrets);
break;
default:
throw new IllegalArgumentException("Unknown forwarding option: " + forwarding);
throw new IllegalArgumentException("Unknown forwarding provider: " + forwarding);
}
} catch (NoClassDefFoundError e) {
throw new UnsupportedOperationException(forwarding.substring(0, 1).toUpperCase(Locale.ROOT) + forwarding.substring(1).toLowerCase(Locale.ROOT) + " forwarding is not available in this version");
Expand All @@ -96,22 +96,6 @@ public class VanillaCord {
}

private VanillaCord() {

}

public static void parseHandshake(Object connection, Object handshake) {
helper.parseHandshake(connection, handshake);
}

public static boolean initializeTransaction(Object connection, Object hello) {
return helper.initializeTransaction(connection, hello);
}

public static boolean completeTransaction(Object connection, Object login, Object response) {
return helper.completeTransaction(connection, login, response);
}

public static GameProfile injectProfile(Object connection, String username) {
return helper.injectProfile(connection, username);
// this constructor is never called
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</pluginRepositories>

<properties>
<bridge.version>24w08b</bridge.version>
<bridge.version>24w11a</bridge.version>
</properties>

<dependencies>
Expand Down

0 comments on commit 1fd28e1

Please sign in to comment.