* NativeLibraryLoader: Add method to extract a platform's natives

experimental
shadowislord 10 years ago
parent 5c738c6a8a
commit 56a187f77a
  1. 101
      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.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -278,6 +279,106 @@ public final class NativeLibraryLoader {
}
}
}
public static void extractNativeLibraries(Platform platform, File targetDir) throws IOException {
for (Map.Entry<NativeLibrary.Key, NativeLibrary> lib : nativeLibraryMap.entrySet()) {
if (lib.getValue().getPlatform() == platform) {
if (!targetDir.exists()) {
targetDir.mkdirs();
}
extractNativeLibrary(platform, lib.getValue().getName(), targetDir);
}
}
}
private static String mapLibraryName_emulated(String name, Platform platform) {
switch (platform) {
case MacOSX32:
case MacOSX64:
return name + ".dylib";
case Windows32:
case Windows64:
return name + ".dll";
default:
return name + ".so";
}
}
public static void extractNativeLibrary(Platform platform, String name, File extractionDir) throws IOException {
NativeLibrary library = nativeLibraryMap.get(new NativeLibrary.Key(name, platform));
if (library == null) {
return;
}
String pathInJar = library.getPathInNativesJar();
if (pathInJar == null) {
return;
}
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;
}
String loadedAsFileName;
if (library.isJNI()) {
String fileNameInJarWithoutExtension
= fileNameInJar.substring(0, fileNameInJar.lastIndexOf("."));
if (platform.is64Bit() && !fileNameInJarWithoutExtension.endsWith("64")) {
fileNameInJarWithoutExtension += "64";
}
String systemJniExtension;
String dummyLib = mapLibraryName_emulated("", platform);
if (dummyLib.contains(".")) {
systemJniExtension = dummyLib.substring(dummyLib.lastIndexOf("."));
} else {
systemJniExtension = "";
}
loadedAsFileName = fileNameInJarWithoutExtension + systemJniExtension;
} else {
loadedAsFileName = fileNameInJar;
}
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream();
File targetFile = new File(extractionDir, loadedAsFileName);
OutputStream out = null;
try {
out = new FileOutputStream(targetFile);
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ex) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException ex) {
}
}
}
}
/**
* First extracts the native library and then loads it.

Loading…
Cancel
Save