| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- /*
- * Copyright (c) 2009-2010 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.*;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLConnection;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- * Helper class for extracting the natives (dll, so) from the jars.
- * This class should only be used internally.
- */
- public final class Natives {
- private static final Logger logger = Logger.getLogger(Natives.class.getName());
- private static final byte[] buf = new byte[1024];
- private static File extractionDirOverride = null;
- private static File extractionDir = null;
- public static void setExtractionDir(String name) {
- extractionDirOverride = new File(name).getAbsoluteFile();
- }
- public static File getExtractionDir() {
- if (extractionDirOverride != null) {
- return extractionDirOverride;
- }
- if (extractionDir == null) {
- File workingFolder = new File("").getAbsoluteFile();
- if (!workingFolder.canWrite()) {
- setStorageExtractionDir();
- } else {
- try {
- File file = new File(workingFolder.getAbsolutePath() + File.separator + ".jmetestwrite");
- file.createNewFile();
- file.delete();
- extractionDir = workingFolder;
- } catch (Exception e) {
- setStorageExtractionDir();
- }
- }
- }
- return extractionDir;
- }
- private static void setStorageExtractionDir() {
- logger.log(Level.WARNING, "Working directory is not writable. Using home directory instead.");
- extractionDir = new File(JmeSystem.getStorageFolder(),
- "natives_" + Integer.toHexString(computeNativesHash()));
- if (!extractionDir.exists()) {
- extractionDir.mkdir();
- }
- }
- private static int computeNativesHash() {
- try {
- String classpath = System.getProperty("java.class.path");
- URL url = Thread.currentThread().getContextClassLoader().getResource("com/jme3/system/Natives.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);
- }
- URLConnection conn = url.openConnection();
- int hash = classpath.hashCode() ^ (int) conn.getLastModified();
- return hash;
- } catch (IOException ex) {
- throw new UnsupportedOperationException(ex);
- }
- }
- public static void extractNativeLib(String sysName, String name) throws IOException {
- extractNativeLib(sysName, name, false, true);
- }
- public static void extractNativeLib(String sysName, String name, boolean load) throws IOException {
- extractNativeLib(sysName, name, load, true);
- }
- public static void extractNativeLib(String sysName, String name, boolean load, boolean warning) throws IOException {
- String fullname;
- String path;
- //XXX: hack to allow specifying the extension via supplying an extension in the name (e.g. "blah.dylib")
- // this is needed on osx where the openal.dylib always needs to be extracted as dylib
- // and never as jnilib, even if that is the platform JNI lib suffix (OpenAL is no JNI library)
- if(!name.contains(".")){
- // automatic name mapping
- fullname = System.mapLibraryName(name);
- path = "native/" + sysName + "/" + fullname;
- //XXX: Hack to extract jnilib to dylib on OSX Java 1.7+
- // This assumes all jni libs for osx are stored as "jnilib" in the jar file.
- // It will be extracted with the name required for the platform.
- // At a later stage this should probably inverted so that dylib is the default name.
- if(sysName.equals("macosx")){
- path = path.replaceAll("dylib","jnilib");
- }
- } else{
- fullname = name;
- path = "native/" + sysName + "/" + fullname;
- }
- URL url = Thread.currentThread().getContextClassLoader().getResource(path);
- if (url == null) {
- if (!warning) {
- logger.log(Level.WARNING, "Cannot locate native library: {0}/{1}",
- new String[]{sysName, fullname});
- }
- return;
- }
- URLConnection conn = url.openConnection();
- InputStream in = conn.getInputStream();
- File targetFile = new File(getExtractionDir(), fullname);
- OutputStream out = null;
- try {
- if (targetFile.exists()) {
- // OK, compare last modified date of this file to
- // file in jar
- long targetLastModified = targetFile.lastModified();
- long sourceLastModified = conn.getLastModified();
- // Allow ~1 second range for OSes that only support low precision
- if (targetLastModified + 1000 > sourceLastModified) {
- logger.log(Level.FINE, "Not copying library {0}. Latest already extracted.", fullname);
- return;
- }
- }
- out = new FileOutputStream(targetFile);
- int len;
- while ((len = in.read(buf)) > 0) {
- out.write(buf, 0, len);
- }
- in.close();
- in = null;
- out.close();
- out = null;
- // NOTE: On OSes that support "Date Created" property,
- // this will cause the last modified date to be lower than
- // date created which makes no sense
- targetFile.setLastModified(conn.getLastModified());
- } catch (FileNotFoundException ex) {
- if (ex.getMessage().contains("used by another process")) {
- return;
- }
- throw ex;
- } finally {
- if (load) {
- System.load(targetFile.getAbsolutePath());
- }
- if(in != null){
- in.close();
- }
- if(out != null){
- out.close();
- }
- }
- logger.log(Level.FINE, "Copied {0} to {1}", new Object[]{fullname, targetFile});
- }
- protected static boolean isUsingNativeBullet() {
- try {
- Class clazz = Class.forName("com.jme3.bullet.util.NativeMeshUtil");
- return clazz != null;
- } catch (ClassNotFoundException ex) {
- return false;
- }
- }
- public static void extractNativeLibs(Platform platform, AppSettings settings) throws IOException {
- String renderer = settings.getRenderer();
- String audioRenderer = settings.getAudioRenderer();
- boolean needLWJGL = false;
- boolean needOAL = false;
- boolean needJInput = false;
- boolean needNativeBullet = isUsingNativeBullet();
-
- if (renderer != null) {
- if (renderer.startsWith("LWJGL")) {
- needLWJGL = true;
- }
- }
- if (audioRenderer != null) {
- if (audioRenderer.equals("LWJGL")) {
- needLWJGL = true;
- needOAL = true;
- }
- }
- needJInput = settings.useJoysticks();
- String libraryPath = getExtractionDir().toString();
- if (needLWJGL) {
- logger.log(Level.INFO, "Extraction Directory: {0}", getExtractionDir().toString());
- // LWJGL supports this feature where
- // it can load libraries from this path.
- System.setProperty("org.lwjgl.librarypath", libraryPath);
- }
- if (needJInput) {
- // AND Luckily enough JInput supports the same feature.
- System.setProperty("net.java.games.input.librarypath", libraryPath);
- }
- switch (platform) {
- case Windows64:
- if (needLWJGL) {
- extractNativeLib("windows", "lwjgl64");
- }
- if (needOAL) {
- extractNativeLib("windows", "OpenAL64");
- }
- if (needJInput) {
- extractNativeLib("windows", "jinput-dx8_64");
- extractNativeLib("windows", "jinput-raw_64");
- }
- if (needNativeBullet) {
- extractNativeLib("windows", "bulletjme64", true, false);
- }
- break;
- case Windows32:
- if (needLWJGL) {
- extractNativeLib("windows", "lwjgl");
- }
- if (needOAL) {
- extractNativeLib("windows", "OpenAL32");
- }
- if (needJInput) {
- extractNativeLib("windows", "jinput-dx8");
- extractNativeLib("windows", "jinput-raw");
- }
- if (needNativeBullet) {
- extractNativeLib("windows", "bulletjme", true, false);
- }
- break;
- case Linux64:
- if (needLWJGL) {
- extractNativeLib("linux", "lwjgl64");
- }
- if (needJInput) {
- extractNativeLib("linux", "jinput-linux64");
- }
- if (needOAL) {
- extractNativeLib("linux", "openal64");
- }
- if (needNativeBullet) {
- extractNativeLib("linux", "bulletjme64", true, false);
- }
- break;
- case Linux32:
- if (needLWJGL) {
- extractNativeLib("linux", "lwjgl");
- }
- if (needJInput) {
- extractNativeLib("linux", "jinput-linux");
- }
- if (needOAL) {
- extractNativeLib("linux", "openal");
- }
- if (needNativeBullet) {
- extractNativeLib("linux", "bulletjme", true, false);
- }
- break;
- case MacOSX_PPC32:
- case MacOSX32:
- case MacOSX_PPC64:
- case MacOSX64:
- if (needLWJGL) {
- extractNativeLib("macosx", "lwjgl");
- }
- if (needOAL){
- extractNativeLib("macosx", "openal.dylib");
- }
- if (needJInput) {
- extractNativeLib("macosx", "jinput-osx");
- }
- if (needNativeBullet) {
- extractNativeLib("macosx", "bulletjme", true, false);
- }
- break;
- }
- }
- }
|