From 32cfca495d7004b00aa1a0d7b9a1236f6d15c046 Mon Sep 17 00:00:00 2001 From: shadowislord Date: Sun, 25 Jan 2015 19:32:33 -0500 Subject: [PATCH] * NativeLibraryLoader: Add method to get a list of jars that contain known natives --- .../com/jme3/system/NativeLibraryLoader.java | 53 ++++++++++++++++++- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/jme3-desktop/src/main/java/com/jme3/system/NativeLibraryLoader.java b/jme3-desktop/src/main/java/com/jme3/system/NativeLibraryLoader.java index 2d8788237..fed6816a7 100644 --- a/jme3-desktop/src/main/java/com/jme3/system/NativeLibraryLoader.java +++ b/jme3-desktop/src/main/java/com/jme3/system/NativeLibraryLoader.java @@ -40,6 +40,7 @@ import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -280,6 +281,17 @@ public final class NativeLibraryLoader { } } + public static File[] getJarsWithNatives() { + HashSet jarFiles = new HashSet(); + for (Map.Entry lib : nativeLibraryMap.entrySet()) { + File jarFile = getJarForNativeLibrary(lib.getValue().getPlatform(), lib.getValue().getName()); + if (jarFile != null) { + jarFiles.add(jarFile); + } + } + return jarFiles.toArray(new File[0]); + } + public static void extractNativeLibraries(Platform platform, File targetDir) throws IOException { for (Map.Entry lib : nativeLibraryMap.entrySet()) { if (lib.getValue().getPlatform() == platform) { @@ -304,7 +316,44 @@ public final class NativeLibraryLoader { } } - public static void extractNativeLibrary(Platform platform, String name, File extractionDir) throws IOException { + public static File getJarForNativeLibrary(Platform platform, String name) { + NativeLibrary library = nativeLibraryMap.get(new NativeLibrary.Key(name, platform)); + if (library == null) { + return null; + } + + String pathInJar = library.getPathInNativesJar(); + if (pathInJar == null) { + return null; + } + + String fileNameInJar; + if (pathInJar.contains("/")) { + fileNameInJar = pathInJar.substring(pathInJar.lastIndexOf("/") + 1); + } else { + fileNameInJar = pathInJar; + } + + URL url = Thread.currentThread().getContextClassLoader().getResource(pathInJar); + if (url == null) { + url = Thread.currentThread().getContextClassLoader().getResource(fileNameInJar); + } + + if (url == null) { + return null; + } + + StringBuilder sb = new StringBuilder(url.toString()); + if (sb.indexOf("jar:file:/") == 0) { + sb.delete(0, 9); + sb.delete(sb.indexOf("!"), sb.length()); + return new File(sb.toString()); + } else { + return null; // not a jar + } + } + + public static void extractNativeLibrary(Platform platform, String name, File targetDir) throws IOException { NativeLibrary library = nativeLibraryMap.get(new NativeLibrary.Key(name, platform)); if (library == null) { return; @@ -356,7 +405,7 @@ public final class NativeLibraryLoader { URLConnection conn = url.openConnection(); InputStream in = conn.getInputStream(); - File targetFile = new File(extractionDir, loadedAsFileName); + File targetFile = new File(targetDir, loadedAsFileName); OutputStream out = null; try { out = new FileOutputStream(targetFile);