Skip to content

Commit

Permalink
fix: commodore class remap on latest spigot versions
Browse files Browse the repository at this point in the history
  • Loading branch information
derklaro committed Oct 21, 2024
1 parent 392b8da commit e973da2
Showing 1 changed file with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import java.lang.classfile.ClassTransform;
import java.lang.classfile.CodeBuilder;
import java.lang.classfile.CodeElement;
import java.lang.classfile.CodeModel;
import java.lang.classfile.CodeTransform;
import java.lang.reflect.AccessFlag;
import java.util.ArrayDeque;
import java.util.Deque;
import lombok.NonNull;
Expand Down Expand Up @@ -94,6 +96,15 @@ public void accept(@NonNull CodeBuilder builder, @NonNull CodeElement element) {
*/
@Override
public void atEnd(@NonNull CodeBuilder builder) {
// get if the method we're transforming is static or not, this changed in 1.21.1
// if the method is non-static we need to load a different slot to get the raw bytecode
// argument that was supplied to the method
// see https://hub.spigotmc.org/stash/projects/SPIGOT/repos/craftbukkit/commits/0a7bd6c81a33cfaaa2f4d2456c6b237792f38fe6
var methodModel = builder.original()
.flatMap(CodeModel::parent)
.orElseThrow(() -> new IllegalStateException("original method not preset on remap"));
var transformMethodIsStatic = methodModel.flags().has(AccessFlag.STATIC);

// inserts a try block using the captured instructions that are in the original method
// inserts a no-op catch block & a return instruction after the catch block to return the raw input data
// this is needed as sometimes there are labels generated after the last return instruction in the
Expand All @@ -106,7 +117,8 @@ public void atEnd(@NonNull CodeBuilder builder) {
this.methodElements::forEach,
catchBuilder -> catchBuilder.catchingAll(_ -> {
}))
.aload(0).areturn();
.aload(transformMethodIsStatic ? 0 : 1)
.areturn();
}
}
}

0 comments on commit e973da2

Please sign in to comment.