Natives.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright (c) 2009-2010 jMonkeyEngine
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. package com.jme3.system;
  33. import java.io.*;
  34. import java.net.MalformedURLException;
  35. import java.net.URL;
  36. import java.net.URLConnection;
  37. import java.util.logging.Level;
  38. import java.util.logging.Logger;
  39. /**
  40. * Helper class for extracting the natives (dll, so) from the jars.
  41. * This class should only be used internally.
  42. */
  43. public final class Natives {
  44. private static final Logger logger = Logger.getLogger(Natives.class.getName());
  45. private static final byte[] buf = new byte[1024];
  46. private static File extractionDirOverride = null;
  47. private static File extractionDir = null;
  48. public static void setExtractionDir(String name) {
  49. extractionDirOverride = new File(name).getAbsoluteFile();
  50. }
  51. public static File getExtractionDir() {
  52. if (extractionDirOverride != null) {
  53. return extractionDirOverride;
  54. }
  55. if (extractionDir == null) {
  56. File workingFolder = new File("").getAbsoluteFile();
  57. if (!workingFolder.canWrite()) {
  58. setStorageExtractionDir();
  59. } else {
  60. try {
  61. File file = new File(workingFolder.getAbsolutePath() + File.separator + ".jmetestwrite");
  62. file.createNewFile();
  63. file.delete();
  64. extractionDir = workingFolder;
  65. } catch (Exception e) {
  66. setStorageExtractionDir();
  67. }
  68. }
  69. }
  70. return extractionDir;
  71. }
  72. private static void setStorageExtractionDir() {
  73. logger.log(Level.WARNING, "Working directory is not writable. Using home directory instead.");
  74. extractionDir = new File(JmeSystem.getStorageFolder(),
  75. "natives_" + Integer.toHexString(computeNativesHash()));
  76. if (!extractionDir.exists()) {
  77. extractionDir.mkdir();
  78. }
  79. }
  80. private static int computeNativesHash() {
  81. try {
  82. String classpath = System.getProperty("java.class.path");
  83. URL url = Thread.currentThread().getContextClassLoader().getResource("com/jme3/system/Natives.class");
  84. StringBuilder sb = new StringBuilder(url.toString());
  85. if (sb.indexOf("jar:") == 0) {
  86. sb.delete(0, 4);
  87. sb.delete(sb.indexOf("!"), sb.length());
  88. sb.delete(sb.lastIndexOf("/") + 1, sb.length());
  89. }
  90. try {
  91. url = new URL(sb.toString());
  92. } catch (MalformedURLException ex) {
  93. throw new UnsupportedOperationException(ex);
  94. }
  95. URLConnection conn = url.openConnection();
  96. int hash = classpath.hashCode() ^ (int) conn.getLastModified();
  97. return hash;
  98. } catch (IOException ex) {
  99. throw new UnsupportedOperationException(ex);
  100. }
  101. }
  102. public static void extractNativeLib(String sysName, String name) throws IOException {
  103. extractNativeLib(sysName, name, false, true);
  104. }
  105. public static void extractNativeLib(String sysName, String name, boolean load) throws IOException {
  106. extractNativeLib(sysName, name, load, true);
  107. }
  108. public static void extractNativeLib(String sysName, String name, boolean load, boolean warning) throws IOException {
  109. String fullname;
  110. String path;
  111. //XXX: hack to allow specifying the extension via supplying an extension in the name (e.g. "blah.dylib")
  112. // this is needed on osx where the openal.dylib always needs to be extracted as dylib
  113. // and never as jnilib, even if that is the platform JNI lib suffix (OpenAL is no JNI library)
  114. if(!name.contains(".")){
  115. // automatic name mapping
  116. fullname = System.mapLibraryName(name);
  117. path = "native/" + sysName + "/" + fullname;
  118. //XXX: Hack to extract jnilib to dylib on OSX Java 1.7+
  119. // This assumes all jni libs for osx are stored as "jnilib" in the jar file.
  120. // It will be extracted with the name required for the platform.
  121. // At a later stage this should probably inverted so that dylib is the default name.
  122. if(sysName.equals("macosx")){
  123. path = path.replaceAll("dylib","jnilib");
  124. }
  125. } else{
  126. fullname = name;
  127. path = "native/" + sysName + "/" + fullname;
  128. }
  129. URL url = Thread.currentThread().getContextClassLoader().getResource(path);
  130. if (url == null) {
  131. if (!warning) {
  132. logger.log(Level.WARNING, "Cannot locate native library: {0}/{1}",
  133. new String[]{sysName, fullname});
  134. }
  135. return;
  136. }
  137. URLConnection conn = url.openConnection();
  138. InputStream in = conn.getInputStream();
  139. File targetFile = new File(getExtractionDir(), fullname);
  140. OutputStream out = null;
  141. try {
  142. if (targetFile.exists()) {
  143. // OK, compare last modified date of this file to
  144. // file in jar
  145. long targetLastModified = targetFile.lastModified();
  146. long sourceLastModified = conn.getLastModified();
  147. // Allow ~1 second range for OSes that only support low precision
  148. if (targetLastModified + 1000 > sourceLastModified) {
  149. logger.log(Level.FINE, "Not copying library {0}. Latest already extracted.", fullname);
  150. return;
  151. }
  152. }
  153. out = new FileOutputStream(targetFile);
  154. int len;
  155. while ((len = in.read(buf)) > 0) {
  156. out.write(buf, 0, len);
  157. }
  158. in.close();
  159. in = null;
  160. out.close();
  161. out = null;
  162. // NOTE: On OSes that support "Date Created" property,
  163. // this will cause the last modified date to be lower than
  164. // date created which makes no sense
  165. targetFile.setLastModified(conn.getLastModified());
  166. } catch (FileNotFoundException ex) {
  167. if (ex.getMessage().contains("used by another process")) {
  168. return;
  169. }
  170. throw ex;
  171. } finally {
  172. if (load) {
  173. System.load(targetFile.getAbsolutePath());
  174. }
  175. if(in != null){
  176. in.close();
  177. }
  178. if(out != null){
  179. out.close();
  180. }
  181. }
  182. logger.log(Level.FINE, "Copied {0} to {1}", new Object[]{fullname, targetFile});
  183. }
  184. protected static boolean isUsingNativeBullet() {
  185. try {
  186. Class clazz = Class.forName("com.jme3.bullet.util.NativeMeshUtil");
  187. return clazz != null;
  188. } catch (ClassNotFoundException ex) {
  189. return false;
  190. }
  191. }
  192. public static void extractNativeLibs(Platform platform, AppSettings settings) throws IOException {
  193. String renderer = settings.getRenderer();
  194. String audioRenderer = settings.getAudioRenderer();
  195. boolean needLWJGL = false;
  196. boolean needOAL = false;
  197. boolean needJInput = false;
  198. boolean needNativeBullet = isUsingNativeBullet();
  199. if (renderer != null) {
  200. if (renderer.startsWith("LWJGL")) {
  201. needLWJGL = true;
  202. }
  203. }
  204. if (audioRenderer != null) {
  205. if (audioRenderer.equals("LWJGL")) {
  206. needLWJGL = true;
  207. needOAL = true;
  208. }
  209. }
  210. needJInput = settings.useJoysticks();
  211. String libraryPath = getExtractionDir().toString();
  212. if (needLWJGL) {
  213. logger.log(Level.INFO, "Extraction Directory: {0}", getExtractionDir().toString());
  214. // LWJGL supports this feature where
  215. // it can load libraries from this path.
  216. System.setProperty("org.lwjgl.librarypath", libraryPath);
  217. }
  218. if (needJInput) {
  219. // AND Luckily enough JInput supports the same feature.
  220. System.setProperty("net.java.games.input.librarypath", libraryPath);
  221. }
  222. switch (platform) {
  223. case Windows64:
  224. if (needLWJGL) {
  225. extractNativeLib("windows", "lwjgl64");
  226. }
  227. if (needOAL) {
  228. extractNativeLib("windows", "OpenAL64");
  229. }
  230. if (needJInput) {
  231. extractNativeLib("windows", "jinput-dx8_64");
  232. extractNativeLib("windows", "jinput-raw_64");
  233. }
  234. if (needNativeBullet) {
  235. extractNativeLib("windows", "bulletjme64", true, false);
  236. }
  237. break;
  238. case Windows32:
  239. if (needLWJGL) {
  240. extractNativeLib("windows", "lwjgl");
  241. }
  242. if (needOAL) {
  243. extractNativeLib("windows", "OpenAL32");
  244. }
  245. if (needJInput) {
  246. extractNativeLib("windows", "jinput-dx8");
  247. extractNativeLib("windows", "jinput-raw");
  248. }
  249. if (needNativeBullet) {
  250. extractNativeLib("windows", "bulletjme", true, false);
  251. }
  252. break;
  253. case Linux64:
  254. if (needLWJGL) {
  255. extractNativeLib("linux", "lwjgl64");
  256. }
  257. if (needJInput) {
  258. extractNativeLib("linux", "jinput-linux64");
  259. }
  260. if (needOAL) {
  261. extractNativeLib("linux", "openal64");
  262. }
  263. if (needNativeBullet) {
  264. extractNativeLib("linux", "bulletjme64", true, false);
  265. }
  266. break;
  267. case Linux32:
  268. if (needLWJGL) {
  269. extractNativeLib("linux", "lwjgl");
  270. }
  271. if (needJInput) {
  272. extractNativeLib("linux", "jinput-linux");
  273. }
  274. if (needOAL) {
  275. extractNativeLib("linux", "openal");
  276. }
  277. if (needNativeBullet) {
  278. extractNativeLib("linux", "bulletjme", true, false);
  279. }
  280. break;
  281. case MacOSX_PPC32:
  282. case MacOSX32:
  283. case MacOSX_PPC64:
  284. case MacOSX64:
  285. if (needLWJGL) {
  286. extractNativeLib("macosx", "lwjgl");
  287. }
  288. if (needOAL){
  289. extractNativeLib("macosx", "openal.dylib");
  290. }
  291. if (needJInput) {
  292. extractNativeLib("macosx", "jinput-osx");
  293. }
  294. if (needNativeBullet) {
  295. extractNativeLib("macosx", "bulletjme", true, false);
  296. }
  297. break;
  298. }
  299. }
  300. }