* NativeLibraryLoader.registerNativeLibrary("mystuff", Platform.Windows32, "native/windows/mystuff.dll");
* NativeLibraryLoader.registerNativeLibrary("mystuff", Platform.Windows64, "native/windows/mystuff64.dll");
* NativeLibraryLoader.registerNativeLibrary("mystuff", Platform.Linux32, "native/linux/libmystuff.so");
* NativeLibraryLoader.registerNativeLibrary("mystuff", Platform.Linux64, "native/linux/libmystuff64.so");
* NativeLibraryLoader.registerNativeLibrary("mystuff", Platform.MacOSX32, "native/macosx/libmystuff.jnilib");
* NativeLibraryLoader.registerNativeLibrary("mystuff", Platform.MacOSX64, "native/macosx/libmystuff.jnilib");
*
*
* NativeLibraryLoader.loadNativeLibrary("mystuff", true);
*
* It will load the right library automatically based on the platform.
*
* @author Kirill Vainer
*/
public final class NativeLibraryLoader {
private static final Logger logger = Logger.getLogger(NativeLibraryLoader.class.getName());
private static final byte[] buf = new byte[1024 * 100];
private static File extractionFolderOverride = null;
private static File extractionFolder = null;
private static final HashMapnull
to restore default
* functionality.
*
* @param path Path where to extract native libraries.
*/
public static void setCustomExtractionFolder(String path) {
extractionFolderOverride = new File(path).getAbsoluteFile();
}
/**
* Returns the folder where native libraries will be extracted.
* This is automatically determined at run-time based on the
* following criteria:natives_<hash>
where <hash>
* is computed automatically as the XOR of the classpath hash code
* and the last modified date of this class.
*
* @return Path where natives will be extracted to.
*/
public static File getExtractionFolder() {
if (extractionFolderOverride != null) {
return extractionFolderOverride;
}
if (extractionFolder == null) {
File workingFolder = new File("").getAbsoluteFile();
if (!workingFolder.canWrite()) {
setExtractionDirToStorageDir();
} else {
try {
File file = new File(workingFolder + File.separator + ".jmetestwrite");
file.createNewFile();
file.delete();
extractionFolder = workingFolder;
} catch (Exception e) {
setExtractionDirToStorageDir();
}
}
}
return extractionFolder;
}
private static void setExtractionDirToStorageDir() {
logger.log(Level.WARNING, "Working directory is not writable. Using home directory instead.");
extractionFolder = new File(JmeSystem.getStorageFolder(),
"natives_" + Integer.toHexString(computeNativesHash()));
if (!extractionFolder.exists()) {
extractionFolder.mkdir();
}
}
private static int computeNativesHash() {
URLConnection conn = null;
try {
String classpath = System.getProperty("java.class.path");
URL url = Thread.currentThread().getContextClassLoader().getResource("com/jme3/system/NativeLibraryLoder.class");
StringBuilder sb = new StringBuilder(url.toString());
if (sb.indexOf("jar:") == 0) {
sb.delete(0, 4);
sb.delete(sb.indexOf("!"), sb.length());
sb.delete(sb.lastIndexOf("/") + 1, sb.length());
}
try {
url = new URL(sb.toString());
} catch (MalformedURLException ex) {
throw new UnsupportedOperationException(ex);
}
conn = url.openConnection();
int hash = classpath.hashCode() ^ (int) conn.getLastModified();
return hash;
} catch (IOException ex) {
throw new UnsupportedOperationException(ex);
} finally {
if (conn != null) {
try {
conn.getInputStream().close();
conn.getOutputStream().close();
} catch (IOException ex) { }
}
}
}
public static File[] getJarsWithNatives() {
HashSet
* E.g.
*