Skip to content

Commit a610a24

Browse files
authored
Merge pull request #26 from Jannyboy11/fix/paper-1.21.1
Fix/paper 1.21.1
2 parents cb62fd3 + 87c6195 commit a610a24

File tree

7 files changed

+105
-11
lines changed

7 files changed

+105
-11
lines changed

.github/workflows/compile-test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ jobs:
2121
git config --global user.name "Jannyboy11"
2222
git clone https://github.com/PaperMC/Paper
2323
cd Paper
24+
git checkout ver/1.20.4
2425
./gradlew applyPatches
2526
./gradlew createReobfBundlerJar
2627
./gradlew build

ScalaLoader-Common/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<properties>
1515
<bukkitVersion>1.20.4-R0.1-SNAPSHOT</bukkitVersion>
16-
<asmVersion>9.6</asmVersion>
16+
<asmVersion>9.7</asmVersion>
1717
<bstatsVersion>3.0.0</bstatsVersion>
1818
<junitVersion>5.7.1</junitVersion>
1919
<mavenResolverVersion>1.6.2</mavenResolverVersion>

ScalaLoader-Common/src/main/java/xyz/janboerman/scalaloader/compat/Platform.java

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@
1010
import java.lang.invoke.MethodHandle;
1111
import java.lang.invoke.MethodHandles;
1212
import java.lang.invoke.MethodType;
13+
import java.util.Collections;
14+
import java.util.Set;
1315

1416
/**
1517
* This class is NOT part of the public API!
1618
*/
1719
public class Platform {
1820

21+
private static final String FAKE_PLUGIN_NAME = "Fake";
22+
1923
protected Platform() {
2024
}
2125

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

6973
public static class CraftBukkitPlatform extends Platform {
7074

75+
private static final Class<?> API_VERSION_CLASS;
76+
static {
77+
Class<?> apiVersionClass;
78+
try {
79+
apiVersionClass = Class.forName("org.bukkit.craftbukkit.util.ApiVersion");
80+
} catch (ClassNotFoundException e) {
81+
apiVersionClass = null;
82+
}
83+
API_VERSION_CLASS = apiVersionClass;
84+
}
85+
7186
private CraftBukkitPlatform() {}
7287

7388
private MethodHandle commodoreConvert = null;
7489
private boolean attempted = false;
7590

91+
public <ScalaPluginClassLoader extends ClassLoader & IScalaPluginClassLoader> byte[] transformNative(Server craftServer, byte[] classBytes, ScalaPluginClassLoader pluginClassLoader) throws Throwable {
92+
MethodHandles.Lookup lookup = MethodHandles.lookup();
93+
if (commodoreConvert == null) {
94+
attempted = true;
95+
try {
96+
// public static byte[] convert(byte[] b, final String pluginName, final ApiVersion pluginVersion, final Set<String> activeCompatibilities)
97+
Class<?> commodoreClass = Class.forName(getPackageName(craftServer.getClass()) + ".util.Commodore");
98+
String methodName = "convert";
99+
MethodType methodType = MethodType.methodType(byte[].class,
100+
new Class<?>[] { byte[].class, String.class, API_VERSION_CLASS, Set.class });
101+
commodoreConvert = lookup.findStatic(commodoreClass, methodName, methodType);
102+
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException ignored) {
103+
//impossible
104+
}
105+
}
106+
107+
if (commodoreConvert != null) {
108+
String pluginName = getPluginName(pluginClassLoader);
109+
try {
110+
MethodHandle getOrCreateVersion = lookup.findStatic(API_VERSION_CLASS, "getOrCreateVersion", MethodType.methodType(API_VERSION_CLASS, String.class));
111+
Object apiVersion = getOrCreateVersion.invoke(pluginClassLoader.getApiVersion().getVersionString());
112+
113+
Set activeCompatibilities = Collections.emptySet();
114+
try {
115+
MethodHandle compatibilitiesGetter = lookup.findGetter(craftServer.getClass(), "activeCompatibilities", Set.class);
116+
activeCompatibilities = (Set) compatibilitiesGetter.invoke(craftServer);
117+
} catch (Exception couldNotDetermineActiveCompatibilities) {
118+
}
119+
120+
classBytes = (byte[]) commodoreConvert.invoke(classBytes, pluginName, apiVersion, activeCompatibilities);
121+
} catch (NoSuchMethodException | IllegalAccessException ignored) {
122+
}
123+
}
124+
125+
return classBytes;
126+
}
127+
76128
public byte[] transformNative(Server craftServer, byte[] classBytes, boolean modern) throws Throwable {
77129
if (!attempted) {
78130
attempted = true;
79131
MethodHandles.Lookup lookup = MethodHandles.lookup();
80132
try {
133+
// public static byte[] convert(byte[] b, boolean isModern)
81134
Class<?> commodoreClass = Class.forName(getPackageName(craftServer.getClass()) + ".util.Commodore");
82135
String methodName = "convert";
83-
MethodType methodType = MethodType.methodType(byte[].class, new Class<?>[]{byte[].class, boolean.class});
136+
MethodType methodType = MethodType.methodType(byte[].class,
137+
new Class<?>[] { byte[].class, boolean.class });
84138
commodoreConvert = lookup.findStatic(commodoreClass, methodName, methodType);
85139
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException ignored) {
86140
//running on craftbukkit 1.12.2 or earlier
@@ -96,9 +150,21 @@ public byte[] transformNative(Server craftServer, byte[] classBytes, boolean mod
96150

97151
@Override
98152
public <ScalaPluginClassLoader extends ClassLoader & IScalaPluginClassLoader> byte[] transform(String jarEntryPath, byte[] classBytes, ScalaPluginClassLoader pluginClassLoader) throws Throwable {
99-
return transformNative(pluginClassLoader.getServer(), classBytes, pluginClassLoader.getApiVersion() != ApiVersion.LEGACY);
153+
if (API_VERSION_CLASS != null) {
154+
return transformNative(pluginClassLoader.getServer(), classBytes, pluginClassLoader);
155+
} else {
156+
return transformNative(pluginClassLoader.getServer(), classBytes, pluginClassLoader.getApiVersion() != ApiVersion.LEGACY);
157+
}
100158
}
101159

160+
private static String getPluginName(IScalaPluginClassLoader classLoader) {
161+
IScalaPlugin plugin = classLoader.getPlugin();
162+
if (plugin == null) {
163+
return FAKE_PLUGIN_NAME;
164+
} else {
165+
return plugin.getName();
166+
}
167+
}
102168
}
103169

104170
public static class GlowstonePlatform extends Platform {

ScalaLoader-Common/src/main/java/xyz/janboerman/scalaloader/configurationserializable/runtime/types/ScalaCollection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ private static <ScalaPluginClassLoader extends ClassLoader & IScalaPluginClassLo
435435
for (int k = 1; k <= N; k++) {
436436
// just call iterator.next() unsafely because we know how many elements there are!
437437

438-
// java.util.Object elementK = iterator.next();
438+
// java.util.Object elementK = RuntimeConversions.deserialize(iterator.next());
439439
methodVisitor.visitVarInsn(ALOAD, iteratorIndex); operandStack.push(Type.getType(java.util.Iterator.class));
440440
methodVisitor.visitMethodInsn(INVOKEINTERFACE, "java/util/Iterator", "next", "()Ljava/lang/Object;", true); operandStack.replaceTop(Type.getType(Object.class));
441441
genParameterType(methodVisitor, elementType, operandStack);

ScalaLoader-Common/src/main/java/xyz/janboerman/scalaloader/plugin/description/ScalaVersion.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public enum ScalaVersion {
3030
v2_12_15("2.12.15"),
3131
v2_12_16("2.12.16"),
3232
v2_12_17("2.12.17"),
33+
v2_12_18("2.12.18"),
34+
v2_12_19("2.12.19"),
35+
v2_12_20("2.12.20"),
3336

3437
//2.13.x
3538
v2_13_0("2.13.0"),
@@ -46,6 +49,7 @@ public enum ScalaVersion {
4649
v2_13_11("2.13.11"),
4750
v2_13_12("2.13.12"),
4851
v2_13_13("2.13.13"),
52+
v2_13_14("2.13.14"),
4953

5054
//3.0.x
5155
v3_0_0("3.0.0"),
@@ -70,7 +74,13 @@ public enum ScalaVersion {
7074
v3_3_3("3.3.3"),
7175

7276
//3.4.0
73-
v3_4_0("3.4.0");
77+
v3_4_0("3.4.0"),
78+
v3_4_1("3.4.1"),
79+
v3_4_2("3.4.2"),
80+
v3_4_3("3.4.3"),
81+
82+
//3.5.0
83+
v3_5_0("3.5.0");
7484

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

ScalaLoader-Paper/src/main/java/xyz/janboerman/scalaloader/paper/plugin/PaperHacks.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import org.bukkit.Bukkit;
55
import org.bukkit.plugin.PluginManager;
66

7-
public class PaperHacks {
7+
public final class PaperHacks {
88

99
private PaperHacks() {}
1010

ScalaLoader-Paper/src/main/java/xyz/janboerman/scalaloader/paper/plugin/ScalaPluginClassLoader.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import java.io.IOException;
55
import java.io.InputStream;
66
import java.io.PrintWriter;
7+
import java.lang.invoke.MethodHandle;
8+
import java.lang.invoke.MethodHandles;
9+
import java.lang.invoke.MethodType;
710
import java.lang.reflect.Constructor;
811
import java.lang.reflect.Field;
912
import java.lang.reflect.InvocationTargetException;
@@ -21,12 +24,11 @@
2124
import java.util.jar.JarEntry;
2225
import java.util.logging.Logger;
2326
import java.util.jar.JarFile;
24-
import java.util.stream.Collectors;
2527

28+
import io.papermc.paper.plugin.configuration.PluginMeta;
2629
import io.papermc.paper.plugin.entrypoint.classloader.ClassloaderBytecodeModifier;
2730
import io.papermc.paper.plugin.entrypoint.classloader.PaperPluginClassLoader;
2831

29-
import io.papermc.paper.plugin.manager.PaperPluginManagerImpl;
3032
import org.bukkit.Server;
3133
import org.bukkit.command.Command;
3234
import org.bukkit.command.PluginCommand;
@@ -44,6 +46,8 @@
4446
import org.objectweb.asm.util.Printer;
4547
import org.objectweb.asm.util.Textifier;
4648
import org.objectweb.asm.util.TraceClassVisitor;
49+
50+
import io.papermc.paper.plugin.entrypoint.classloader.PaperSimplePluginClassLoader;
4751
import xyz.janboerman.scalaloader.DebugSettings;
4852
import xyz.janboerman.scalaloader.bytecode.TransformerRegistry;
4953
import xyz.janboerman.scalaloader.compat.Compat;
@@ -127,7 +131,20 @@ public File getPluginJarFile() {
127131

128132
@Override
129133
public ScalaPluginMeta getConfiguration() {
130-
return (ScalaPluginMeta) super.getConfiguration();
134+
return (ScalaPluginMeta) _getConfiguration();
135+
}
136+
137+
private PluginMeta _getConfiguration() {
138+
//Paper changed PaperSimplePluginClassLoader from PaperPluginMeta to PluginMeta:
139+
//https://github.com/PaperMC/Paper/pull/10758/files#diff-1b48bde36fde990048ba454fb0bcc4e2b92a441c8387c350f1a8f0f09dbc8f8eR1127
140+
141+
try {
142+
Field field = PaperSimplePluginClassLoader.class.getDeclaredField("configuration");
143+
field.setAccessible(true);
144+
return (PluginMeta) field.get(this);
145+
} catch (Throwable e) {
146+
throw new RuntimeException("Could not get scala plugin configuration?", e);
147+
}
131148
}
132149

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

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

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

0 commit comments

Comments
 (0)