Skip to content

Commit

Permalink
Clean up bytecode transformer debug.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Boerman committed Sep 10, 2024
1 parent b7700ac commit debed00
Showing 1 changed file with 17 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,41 +70,42 @@ 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, Class<?> apiVersionClass, byte[] classBytes, ScalaPluginClassLoader pluginClassLoader) throws Throwable {
IScalaLoader.getInstance().getLogger().info("DEBUG: transform native..");
public <ScalaPluginClassLoader extends ClassLoader & IScalaPluginClassLoader> byte[] transformNative(Server craftServer, byte[] classBytes, ScalaPluginClassLoader pluginClassLoader) throws Throwable {
MethodHandles.Lookup lookup = MethodHandles.lookup();
if (commodoreConvert == null) {

IScalaLoader.getInstance().getLogger().info("DEBUG: first attempt to find Commodore#convert!");
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, apiVersionClass, Set.class });
IScalaLoader.getInstance().getLogger().info("DEBUG: looking up static convert method..");
new Class<?>[] { byte[].class, String.class, API_VERSION_CLASS, Set.class });
commodoreConvert = lookup.findStatic(commodoreClass, methodName, methodType);
IScalaLoader.getInstance().getLogger().info("DEBUG: found commodore convert!");
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException ignored) {
IScalaLoader.getInstance()
.getLogger()
.info("DEBUG: could not find Commodore#convert!!! " + ignored.getClass().getName() + ": " +
ignored.getMessage());
//impossible
}
}

if (commodoreConvert != null) {
IScalaLoader.getInstance().getLogger().info("DEBUG: converting class..");
String pluginName = pluginClassLoader.getPlugin().getName();
try {
MethodHandle getOrCreateVersion = lookup.findStatic(apiVersionClass, "getOrCreateVersion", MethodType.methodType(apiVersionClass, String.class));
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();
Expand All @@ -115,9 +116,7 @@ public <ScalaPluginClassLoader extends ClassLoader & IScalaPluginClassLoader> by
}

classBytes = (byte[]) commodoreConvert.invoke(classBytes, pluginName, apiVersion, activeCompatibilities);
IScalaLoader.getInstance().getLogger().info("DEBUG: converted class!");
} catch (NoSuchMethodException | IllegalAccessException ignored) {
IScalaLoader.getInstance().getLogger().info("DEBUG: could not invoke Commodore#convert!!! " + ignored.getClass().getName() + ": " + ignored.getMessage());
}
}

Expand Down Expand Up @@ -149,10 +148,9 @@ 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 {
try {
Class<?> apiVersionClass = Class.forName("org.bukkit.craftbukkit.util.ApiVersion");
return transformNative(pluginClassLoader.getServer(), apiVersionClass, classBytes, pluginClassLoader);
} catch (ClassNotFoundException e) {
if (API_VERSION_CLASS != null) {
return transformNative(pluginClassLoader.getServer(), classBytes, pluginClassLoader);
} else {
return transformNative(pluginClassLoader.getServer(), classBytes, pluginClassLoader.getApiVersion() != ApiVersion.LEGACY);
}
}
Expand Down

0 comments on commit debed00

Please sign in to comment.