/* * Copyright (c) 2009-2012 jMonkeyEngine * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * * Neither the name of 'jMonkeyEngine' nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.jme3.system; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; 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; /** * Utility class to register, extract, and load native libraries. *
* Register your own libraries via the * {@link #registerNativeLibrary(java.lang.String, com.jme3.system.Platform, java.lang.String, boolean) } * method, for each platform. * You can then extract this library (depending on platform), by * using {@link #loadNativeLibrary(java.lang.String, boolean) }. *
* Example:
*
 * 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");
 * 
*
* This will register the library. Load it via:
*
 * 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 HashMap nativeLibraryMap = new HashMap(); /** * Register a new known library. * * This simply registers a known library, the actual extraction and loading * is performed by calling {@link #loadNativeLibrary(java.lang.String, boolean) }. * * @param name The name / ID of the library (not OS or architecture specific). * @param platform The platform for which the in-natives-jar path has * been specified for. * @param path The path inside the natives-jar or classpath * corresponding to this library. Must be compatible with the platform * argument. * @param isJNI True if this is a JNI library, false if this is a regular * native (C/C++) library. */ public static void registerNativeLibrary(String name, Platform platform, String path, boolean isJNI) { nativeLibraryMap.put(new NativeLibrary.Key(name, platform), new NativeLibrary(name, platform, path, isJNI)); } /** * Register a new known JNI library. * * This simply registers a known library, the actual extraction and loading * is performed by calling {@link #loadNativeLibrary(java.lang.String, boolean) }. * * This method should be called several times for each library name, * each time specifying a different platform + path combination. * * @param name The name / ID of the library (not OS or architecture specific). * @param platform The platform for which the in-natives-jar path has * been specified for. * @param path The path inside the natives-jar or classpath * corresponding to this library. Must be compatible with the platform * argument. */ public static void registerNativeLibrary(String name, Platform platform, String path) { registerNativeLibrary(name, platform, path, true); } static { // LWJGL registerNativeLibrary("lwjgl", Platform.Windows32, "native/windows/lwjgl.dll"); registerNativeLibrary("lwjgl", Platform.Windows64, "native/windows/lwjgl64.dll"); registerNativeLibrary("lwjgl", Platform.Linux32, "native/linux/liblwjgl.so"); registerNativeLibrary("lwjgl", Platform.Linux64, "native/linux/liblwjgl64.so"); registerNativeLibrary("lwjgl", Platform.MacOSX32, "native/macosx/liblwjgl.dylib"); registerNativeLibrary("lwjgl", Platform.MacOSX64, "native/macosx/liblwjgl.dylib"); // OpenAL registerNativeLibrary("openal", Platform.Windows32, "native/windows/OpenAL32.dll", false); registerNativeLibrary("openal", Platform.Windows64, "native/windows/OpenAL64.dll", false); registerNativeLibrary("openal", Platform.Linux32, "native/linux/libopenal.so", false); registerNativeLibrary("openal", Platform.Linux64, "native/linux/libopenal64.so", false); registerNativeLibrary("openal", Platform.MacOSX32, "native/macosx/openal.dylib", false); registerNativeLibrary("openal", Platform.MacOSX64, "native/macosx/openal.dylib", false); // BulletJme registerNativeLibrary("bulletjme", Platform.Windows32, "native/windows/x86/bulletjme.dll"); registerNativeLibrary("bulletjme", Platform.Windows64, "native/windows/x86_64/bulletjme.dll"); registerNativeLibrary("bulletjme", Platform.Linux32, "native/linux/x86/libbulletjme.so"); registerNativeLibrary("bulletjme", Platform.Linux64, "native/linux/x86_64/libbulletjme.so"); registerNativeLibrary("bulletjme", Platform.MacOSX32, "native/osx/x86/libbulletjme.dylib"); registerNativeLibrary("bulletjme", Platform.MacOSX64, "native/osx/x86_64/libbulletjme.dylib"); // JInput registerNativeLibrary("jinput", Platform.Windows32, "native/windows/jinput-raw.dll"); registerNativeLibrary("jinput", Platform.Windows64, "native/windows/jinput-raw_64.dll"); registerNativeLibrary("jinput", Platform.Linux32, "native/windows/libjinput-linux.so"); registerNativeLibrary("jinput", Platform.Linux64, "native/windows/libjinput-linux64.so"); registerNativeLibrary("jinput", Platform.MacOSX32, "native/macosx/libjinput-osx.jnilib"); registerNativeLibrary("jinput", Platform.MacOSX64, "native/macosx/libjinput-osx.jnilib"); // JInput Auxiliary (only required on Windows) registerNativeLibrary("jinput-dx8", Platform.Windows32, "native/windows/jinput-dx8.dll"); registerNativeLibrary("jinput-dx8", Platform.Windows64, "native/windows/jinput-dx8_64.dll"); registerNativeLibrary("jinput-dx8", Platform.Linux32, null); registerNativeLibrary("jinput-dx8", Platform.Linux64, null); registerNativeLibrary("jinput-dx8", Platform.MacOSX32, null); registerNativeLibrary("jinput-dx8", Platform.MacOSX64, null); } private NativeLibraryLoader() { } /** * Determine if native bullet is on the classpath. * * Currently the context extracts the native bullet libraries, so * this method is needed to determine if it is needed. * Ideally, native bullet should be responsible for its own natives. * * @return True native bullet is on the classpath, false otherwise. */ public static boolean isUsingNativeBullet() { try { Class clazz = Class.forName("com.jme3.bullet.util.NativeMeshUtil"); return clazz != null; } catch (ClassNotFoundException ex) { return false; } } /** * Specify a custom location where native libraries should * be extracted to. Ensure this is a unique path not used * by other applications to extract their libraries. * Set to null 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:
*