Skip to content

Commit

Permalink
Merge pull request #26 from Jannyboy11/fix/paper-1.21.1
Browse files Browse the repository at this point in the history
Fix/paper 1.21.1
  • Loading branch information
Jannyboy11 authored Sep 10, 2024
2 parents cb62fd3 + 87c6195 commit a610a24
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 11 deletions.
1 change: 1 addition & 0 deletions .github/workflows/compile-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
git config --global user.name "Jannyboy11"
git clone https://github.com/PaperMC/Paper
cd Paper
git checkout ver/1.20.4
./gradlew applyPatches
./gradlew createReobfBundlerJar
./gradlew build
Expand Down
2 changes: 1 addition & 1 deletion ScalaLoader-Common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<properties>
<bukkitVersion>1.20.4-R0.1-SNAPSHOT</bukkitVersion>
<asmVersion>9.6</asmVersion>
<asmVersion>9.7</asmVersion>
<bstatsVersion>3.0.0</bstatsVersion>
<junitVersion>5.7.1</junitVersion>
<mavenResolverVersion>1.6.2</mavenResolverVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.Collections;
import java.util.Set;

/**
* This class is NOT part of the public API!
*/
public class Platform {

private static final String FAKE_PLUGIN_NAME = "Fake";

protected Platform() {
}

Expand All @@ -31,7 +35,7 @@ public <ScalaPluginClassLoader extends ClassLoader & IScalaPluginClassLoader> by
try {
Server server = currentPluginClassLoader.getServer();
UnsafeValues unsafeValues = server.getUnsafe();
String fakeDescription = "name: Fake" + System.lineSeparator() +
String fakeDescription = "name: " + FAKE_PLUGIN_NAME + System.lineSeparator() +
"version: 1.0" + System.lineSeparator() +
"main: xyz.janboerman.scalaloader.FakePlugin" + System.lineSeparator();
ApiVersion apiVersion = currentPluginClassLoader.getApiVersion();
Expand Down Expand Up @@ -68,19 +72,69 @@ public static Platform detect(Server server) {

public static class CraftBukkitPlatform extends Platform {

private static final Class<?> API_VERSION_CLASS;
static {
Class<?> apiVersionClass;
try {
apiVersionClass = Class.forName("org.bukkit.craftbukkit.util.ApiVersion");
} catch (ClassNotFoundException e) {
apiVersionClass = null;
}
API_VERSION_CLASS = apiVersionClass;
}

private CraftBukkitPlatform() {}

private MethodHandle commodoreConvert = null;
private boolean attempted = false;

public <ScalaPluginClassLoader extends ClassLoader & IScalaPluginClassLoader> byte[] transformNative(Server craftServer, byte[] classBytes, ScalaPluginClassLoader pluginClassLoader) throws Throwable {
MethodHandles.Lookup lookup = MethodHandles.lookup();
if (commodoreConvert == null) {
attempted = true;
try {
// public static byte[] convert(byte[] b, final String pluginName, final ApiVersion pluginVersion, final Set<String> activeCompatibilities)
Class<?> commodoreClass = Class.forName(getPackageName(craftServer.getClass()) + ".util.Commodore");
String methodName = "convert";
MethodType methodType = MethodType.methodType(byte[].class,
new Class<?>[] { byte[].class, String.class, API_VERSION_CLASS, Set.class });
commodoreConvert = lookup.findStatic(commodoreClass, methodName, methodType);
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException ignored) {
//impossible
}
}

if (commodoreConvert != null) {
String pluginName = getPluginName(pluginClassLoader);
try {
MethodHandle getOrCreateVersion = lookup.findStatic(API_VERSION_CLASS, "getOrCreateVersion", MethodType.methodType(API_VERSION_CLASS, String.class));
Object apiVersion = getOrCreateVersion.invoke(pluginClassLoader.getApiVersion().getVersionString());

Set activeCompatibilities = Collections.emptySet();
try {
MethodHandle compatibilitiesGetter = lookup.findGetter(craftServer.getClass(), "activeCompatibilities", Set.class);
activeCompatibilities = (Set) compatibilitiesGetter.invoke(craftServer);
} catch (Exception couldNotDetermineActiveCompatibilities) {
}

classBytes = (byte[]) commodoreConvert.invoke(classBytes, pluginName, apiVersion, activeCompatibilities);
} catch (NoSuchMethodException | IllegalAccessException ignored) {
}
}

return classBytes;
}

public byte[] transformNative(Server craftServer, byte[] classBytes, boolean modern) throws Throwable {
if (!attempted) {
attempted = true;
MethodHandles.Lookup lookup = MethodHandles.lookup();
try {
// public static byte[] convert(byte[] b, boolean isModern)
Class<?> commodoreClass = Class.forName(getPackageName(craftServer.getClass()) + ".util.Commodore");
String methodName = "convert";
MethodType methodType = MethodType.methodType(byte[].class, new Class<?>[]{byte[].class, boolean.class});
MethodType methodType = MethodType.methodType(byte[].class,
new Class<?>[] { byte[].class, boolean.class });
commodoreConvert = lookup.findStatic(commodoreClass, methodName, methodType);
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException ignored) {
//running on craftbukkit 1.12.2 or earlier
Expand All @@ -96,9 +150,21 @@ public byte[] transformNative(Server craftServer, byte[] classBytes, boolean mod

@Override
public <ScalaPluginClassLoader extends ClassLoader & IScalaPluginClassLoader> byte[] transform(String jarEntryPath, byte[] classBytes, ScalaPluginClassLoader pluginClassLoader) throws Throwable {
return transformNative(pluginClassLoader.getServer(), classBytes, pluginClassLoader.getApiVersion() != ApiVersion.LEGACY);
if (API_VERSION_CLASS != null) {
return transformNative(pluginClassLoader.getServer(), classBytes, pluginClassLoader);
} else {
return transformNative(pluginClassLoader.getServer(), classBytes, pluginClassLoader.getApiVersion() != ApiVersion.LEGACY);
}
}

private static String getPluginName(IScalaPluginClassLoader classLoader) {
IScalaPlugin plugin = classLoader.getPlugin();
if (plugin == null) {
return FAKE_PLUGIN_NAME;
} else {
return plugin.getName();
}
}
}

public static class GlowstonePlatform extends Platform {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ private static <ScalaPluginClassLoader extends ClassLoader & IScalaPluginClassLo
for (int k = 1; k <= N; k++) {
// just call iterator.next() unsafely because we know how many elements there are!

// java.util.Object elementK = iterator.next();
// java.util.Object elementK = RuntimeConversions.deserialize(iterator.next());
methodVisitor.visitVarInsn(ALOAD, iteratorIndex); operandStack.push(Type.getType(java.util.Iterator.class));
methodVisitor.visitMethodInsn(INVOKEINTERFACE, "java/util/Iterator", "next", "()Ljava/lang/Object;", true); operandStack.replaceTop(Type.getType(Object.class));
genParameterType(methodVisitor, elementType, operandStack);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public enum ScalaVersion {
v2_12_15("2.12.15"),
v2_12_16("2.12.16"),
v2_12_17("2.12.17"),
v2_12_18("2.12.18"),
v2_12_19("2.12.19"),
v2_12_20("2.12.20"),

//2.13.x
v2_13_0("2.13.0"),
Expand All @@ -46,6 +49,7 @@ public enum ScalaVersion {
v2_13_11("2.13.11"),
v2_13_12("2.13.12"),
v2_13_13("2.13.13"),
v2_13_14("2.13.14"),

//3.0.x
v3_0_0("3.0.0"),
Expand All @@ -70,7 +74,13 @@ public enum ScalaVersion {
v3_3_3("3.3.3"),

//3.4.0
v3_4_0("3.4.0");
v3_4_0("3.4.0"),
v3_4_1("3.4.1"),
v3_4_2("3.4.2"),
v3_4_3("3.4.3"),

//3.5.0
v3_5_0("3.5.0");

//TODO include hashes of the jars! so that the loader can verify the integrity of the jars!

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.bukkit.Bukkit;
import org.bukkit.plugin.PluginManager;

public class PaperHacks {
public final class PaperHacks {

private PaperHacks() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
Expand All @@ -21,12 +24,11 @@
import java.util.jar.JarEntry;
import java.util.logging.Logger;
import java.util.jar.JarFile;
import java.util.stream.Collectors;

import io.papermc.paper.plugin.configuration.PluginMeta;
import io.papermc.paper.plugin.entrypoint.classloader.ClassloaderBytecodeModifier;
import io.papermc.paper.plugin.entrypoint.classloader.PaperPluginClassLoader;

import io.papermc.paper.plugin.manager.PaperPluginManagerImpl;
import org.bukkit.Server;
import org.bukkit.command.Command;
import org.bukkit.command.PluginCommand;
Expand All @@ -44,6 +46,8 @@
import org.objectweb.asm.util.Printer;
import org.objectweb.asm.util.Textifier;
import org.objectweb.asm.util.TraceClassVisitor;

import io.papermc.paper.plugin.entrypoint.classloader.PaperSimplePluginClassLoader;
import xyz.janboerman.scalaloader.DebugSettings;
import xyz.janboerman.scalaloader.bytecode.TransformerRegistry;
import xyz.janboerman.scalaloader.compat.Compat;
Expand Down Expand Up @@ -127,7 +131,20 @@ public File getPluginJarFile() {

@Override
public ScalaPluginMeta getConfiguration() {
return (ScalaPluginMeta) super.getConfiguration();
return (ScalaPluginMeta) _getConfiguration();
}

private PluginMeta _getConfiguration() {
//Paper changed PaperSimplePluginClassLoader from PaperPluginMeta to PluginMeta:
//https://github.com/PaperMC/Paper/pull/10758/files#diff-1b48bde36fde990048ba454fb0bcc4e2b92a441c8387c350f1a8f0f09dbc8f8eR1127

try {
Field field = PaperSimplePluginClassLoader.class.getDeclaredField("configuration");
field.setAccessible(true);
return (PluginMeta) field.get(this);
} catch (Throwable e) {
throw new RuntimeException("Could not get scala plugin configuration?", e);
}
}

@Override
Expand Down Expand Up @@ -236,7 +253,7 @@ private void debugClass(String className, byte[] bytecode) {

private byte[] transformBytecode(String className, byte[] byteCode) {
//Paper-supported bytecode transformer via ServiceLoader api!
byteCode = ClassloaderBytecodeModifier.bytecodeModifier().modify(configuration, byteCode);
byteCode = ClassloaderBytecodeModifier.bytecodeModifier().modify(getConfiguration(), byteCode);

//ScalaLoader transformations (everything except main class)
byteCode = ClassLoaderUtils.transform(className, byteCode, this, transformerRegistry, this, ScalaLoader.getInstance().getLogger());
Expand Down

0 comments on commit a610a24

Please sign in to comment.