|
| 1 | +package bwapi; |
| 2 | + |
| 3 | +import com.sun.jna.Native; |
| 4 | +import com.sun.jna.Platform; |
| 5 | + |
| 6 | +import java.io.ByteArrayOutputStream; |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.InputStream; |
| 9 | +import java.net.URISyntaxException; |
| 10 | +import java.nio.ByteBuffer; |
| 11 | +import java.nio.channels.SeekableByteChannel; |
| 12 | +import java.nio.file.Files; |
| 13 | +import java.nio.file.Path; |
| 14 | +import java.nio.file.Paths; |
| 15 | +import java.util.Arrays; |
| 16 | + |
| 17 | +import static java.nio.file.LinkOption.NOFOLLOW_LINKS; |
| 18 | +import static java.nio.file.StandardOpenOption.READ; |
| 19 | + |
| 20 | +/** |
| 21 | + * Loads a matching Windows JNA DLL beside the bot's JAR before connecting to BWAPI. |
| 22 | + * Shipping the DLL separately avoids extracting it to a temporary file on each launch. |
| 23 | + * The file must match the JAR's embedded library for the running JVM's architecture. |
| 24 | + * |
| 25 | + * <p>Missing, stale, or unusable DLLs leave JNA's ordinary loading path available. |
| 26 | + * Other platforms also use ordinary JNA loading. Explicit JNA loading settings |
| 27 | + * and an already initialized JNA take precedence. |
| 28 | + */ |
| 29 | +final class JnaNativeLoader { |
| 30 | + private static final String BOOT_PATH = "jna.boot.library.path"; |
| 31 | + private static final String BOOT_NAME = "jna.boot.library.name"; |
| 32 | + |
| 33 | + private JnaNativeLoader() { |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Call before constructing a platform connector or otherwise initializing JNA. |
| 38 | + */ |
| 39 | + static synchronized void initialize() { |
| 40 | + String path; |
| 41 | + String bootName; |
| 42 | + try { |
| 43 | + if (!Platform.isWindows() || isConfigured()) { |
| 44 | + return; |
| 45 | + } |
| 46 | + byte[] binary = readEmbeddedLibrary(); |
| 47 | + if (binary == null) { |
| 48 | + return; |
| 49 | + } |
| 50 | + Path library = bundledLibrary(binary); |
| 51 | + if (library == null || isConfigured()) { |
| 52 | + return; |
| 53 | + } |
| 54 | + path = library.getParent().toAbsolutePath().toString(); |
| 55 | + bootName = "jnidispatch-" + Platform.RESOURCE_PREFIX; |
| 56 | + } catch (IOException | RuntimeException unavailable) { |
| 57 | + // An optional optimization must not prevent JNA's ordinary loading path. |
| 58 | + return; |
| 59 | + } |
| 60 | + loadPreparedLibrary(path, bootName); |
| 61 | + } |
| 62 | + |
| 63 | + private static byte[] readEmbeddedLibrary() throws IOException { |
| 64 | + String resource = "/com/sun/jna/" + Platform.RESOURCE_PREFIX + "/jnidispatch.dll"; |
| 65 | + // A class literal does not initialize Native; use its loader's exact binary. |
| 66 | + try (InputStream input = Native.class.getResourceAsStream(resource); |
| 67 | + ByteArrayOutputStream output = new ByteArrayOutputStream()) { |
| 68 | + if (input == null) { |
| 69 | + return null; |
| 70 | + } |
| 71 | + byte[] buffer = new byte[32768]; |
| 72 | + int size; |
| 73 | + while ((size = input.read(buffer)) != -1) { |
| 74 | + output.write(buffer, 0, size); |
| 75 | + } |
| 76 | + return output.toByteArray(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private static void loadPreparedLibrary(String path, String bootName) { |
| 81 | + try { |
| 82 | + try { |
| 83 | + System.setProperty(BOOT_PATH, path); |
| 84 | + System.setProperty(BOOT_NAME, bootName); |
| 85 | + } catch (RuntimeException unavailable) { |
| 86 | + return; |
| 87 | + } |
| 88 | + // JNA itself falls back to ordinary extraction if this binary cannot be loaded. |
| 89 | + Class.forName(Native.class.getName(), true, Native.class.getClassLoader()); |
| 90 | + } catch (ClassNotFoundException impossible) { |
| 91 | + throw new IllegalStateException("Cannot initialize JNA: its class loader could not find " |
| 92 | + + "com.sun.jna.Native, which is required to load the native library. Ensure JNA is included " |
| 93 | + + "in the bot's runtime classpath and is visible to its class loader.", impossible); |
| 94 | + } finally { |
| 95 | + // Do not leave a process-wide path pointing at this class loader's JNA version. |
| 96 | + if (path.equals(System.getProperty(BOOT_PATH))) { |
| 97 | + System.clearProperty(BOOT_PATH); |
| 98 | + } |
| 99 | + if (bootName.equals(System.getProperty(BOOT_NAME))) { |
| 100 | + System.clearProperty(BOOT_NAME); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private static boolean isConfigured() { |
| 106 | + if (Boolean.getBoolean("jna.loaded")) { |
| 107 | + return true; |
| 108 | + } |
| 109 | + for (String setting : new String[] {BOOT_PATH, BOOT_NAME, |
| 110 | + "jna.noclasspath", "jna.nosys", "jna.prefix", "jnidispatch.preserve"}) { |
| 111 | + if (System.getProperty(setting) != null) { |
| 112 | + return true; |
| 113 | + } |
| 114 | + } |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + private static Path bundledLibrary(byte[] binary) { |
| 119 | + try { |
| 120 | + // Resolve beside the JAR (or wrapped EXE), never relative to a launcher's working directory. |
| 121 | + Path artifact = Paths.get(JnaNativeLoader.class.getProtectionDomain().getCodeSource().getLocation().toURI()); |
| 122 | + Path library = artifact.resolveSibling("jnidispatch-" + Platform.RESOURCE_PREFIX + ".dll"); |
| 123 | + // A replaced JAR must not accidentally load a DLL left over from another JNA version. |
| 124 | + return matches(library, binary) ? library : null; |
| 125 | + } catch (IOException | URISyntaxException | RuntimeException unavailable) { |
| 126 | + return null; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private static boolean matches(Path file, byte[] expected) throws IOException { |
| 131 | + if (!Files.exists(file, NOFOLLOW_LINKS)) { |
| 132 | + return false; |
| 133 | + } |
| 134 | + try (SeekableByteChannel channel = Files.newByteChannel(file, READ, NOFOLLOW_LINKS)) { |
| 135 | + if (channel.size() != expected.length) { |
| 136 | + return false; |
| 137 | + } |
| 138 | + ByteBuffer bytes = ByteBuffer.allocate(expected.length); |
| 139 | + while (bytes.hasRemaining()) { |
| 140 | + if (channel.read(bytes) < 0) { |
| 141 | + return false; |
| 142 | + } |
| 143 | + } |
| 144 | + return Arrays.equals(expected, bytes.array()); |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments