From dac0cc10c3fdde89986783b83b197e7342b53e16 Mon Sep 17 00:00:00 2001 From: Graham Date: Sat, 12 Oct 2019 16:21:14 +0100 Subject: [PATCH] Update loadLibrary() calls This commit makes two changes: - Renames libjaggl.jnilib to libjaggl.dylib, to reflect the more modern convention on OS X. - Removes the distinction between jaggl.dll and jaggl64.dll. The loadLibrary() code only attempts to use jaggl.dll now, with the correct architecture being selected by the loader. --- .../java/dev/openrs2/bundler/Bundler.java | 4 +- .../transform/LoadLibraryTransformer.java | 62 +++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 bundler/src/main/java/dev/openrs2/bundler/transform/LoadLibraryTransformer.java diff --git a/bundler/src/main/java/dev/openrs2/bundler/Bundler.java b/bundler/src/main/java/dev/openrs2/bundler/Bundler.java index 98ded22e11..6464a05e09 100644 --- a/bundler/src/main/java/dev/openrs2/bundler/Bundler.java +++ b/bundler/src/main/java/dev/openrs2/bundler/Bundler.java @@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList; import dev.openrs2.asm.transform.Transformer; import dev.openrs2.bundler.transform.CachePathTransformer; import dev.openrs2.bundler.transform.HostCheckTransformer; +import dev.openrs2.bundler.transform.LoadLibraryTransformer; import dev.openrs2.bundler.transform.MacResizeTransformer; import dev.openrs2.bundler.transform.RightClickTransformer; @@ -12,6 +13,7 @@ public final class Bundler { new CachePathTransformer(), new HostCheckTransformer(), new MacResizeTransformer(), - new RightClickTransformer() + new RightClickTransformer(), + new LoadLibraryTransformer() ); } diff --git a/bundler/src/main/java/dev/openrs2/bundler/transform/LoadLibraryTransformer.java b/bundler/src/main/java/dev/openrs2/bundler/transform/LoadLibraryTransformer.java new file mode 100644 index 0000000000..4644c727c0 --- /dev/null +++ b/bundler/src/main/java/dev/openrs2/bundler/transform/LoadLibraryTransformer.java @@ -0,0 +1,62 @@ +package dev.openrs2.bundler.transform; + +import dev.openrs2.asm.InsnMatcher; +import dev.openrs2.asm.classpath.ClassPath; +import dev.openrs2.asm.classpath.Library; +import dev.openrs2.asm.transform.Transformer; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.tree.ClassNode; +import org.objectweb.asm.tree.LdcInsnNode; +import org.objectweb.asm.tree.MethodNode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public final class LoadLibraryTransformer extends Transformer { + private static final Logger logger = LoggerFactory.getLogger(LoadLibraryTransformer.class); + + private static final InsnMatcher AMD64_CHECK_MATCHER = InsnMatcher.compile("GETSTATIC LDC INVOKEVIRTUAL IFNE GETSTATIC LDC INVOKEVIRTUAL IFNE"); + + private int jnilibs, amd64Checks; + + @Override + protected void preTransform(ClassPath classPath) { + jnilibs = 0; + amd64Checks = 0; + } + + @Override + protected boolean transformCode(ClassPath classPath, Library library, ClassNode clazz, MethodNode method) { + var foundJnilib = false; + + for (var insn : method.instructions) { + if (insn.getOpcode() == Opcodes.LDC) { + var ldc = (LdcInsnNode) insn; + if (ldc.cst.equals("libjaggl.jnilib")) { + ldc.cst = "libjaggl.dylib"; + foundJnilib = true; + jnilibs++; + break; + } + } + } + + if (!foundJnilib) { + return false; + } + + AMD64_CHECK_MATCHER.match(method).forEach(match -> { + var ldc = (LdcInsnNode) match.get(1); + if (ldc.cst.equals("amd64")) { + match.forEach(method.instructions::remove); + amd64Checks++; + } + }); + + return false; + } + + @Override + protected void postTransform(ClassPath classPath) { + logger.info("Replaced {} jnilibs with dylibs and removed {} amd64 jagmisc checks", jnilibs, amd64Checks); + } +}