/** Loads the libgdx native libraries if they have not already been loaded. */ static public synchronized void load () { if (nativesLoaded) return; nativesLoaded = true; if (disableNativesLoading) return; new SharedLibraryLoader().load("gdx"); } }
/** Extracts the LWJGL native libraries from the classpath and sets the "org.lwjgl.librarypath" system property. */ static public void load () { GdxNativesLoader.load(); if (GdxNativesLoader.disableNativesLoading) return; if (!load) return; SharedLibraryLoader loader = new SharedLibraryLoader(); File nativesDir = null; try { if (isWindows) { nativesDir = loader.extractFile(is64Bit ? "lwjgl64.dll" : "lwjgl.dll", null).getParentFile(); if (!LwjglApplicationConfiguration.disableAudio) loader.extractFileTo(is64Bit ? "OpenAL64.dll" : "OpenAL32.dll", nativesDir); } else if (isMac) { nativesDir = loader.extractFile("liblwjgl.dylib", null).getParentFile(); if (!LwjglApplicationConfiguration.disableAudio) loader.extractFileTo("openal.dylib", nativesDir); } else if (isLinux) { nativesDir = loader.extractFile(is64Bit ? "liblwjgl64.so" : "liblwjgl.so", null).getParentFile(); if (!LwjglApplicationConfiguration.disableAudio) loader.extractFileTo(is64Bit ? "libopenal64.so" : "libopenal.so", nativesDir); } } catch (Throwable ex) { throw new GdxRuntimeException("Unable to extract LWJGL natives.", ex); } System.setProperty("org.lwjgl.librarypath", nativesDir.getAbsolutePath()); load = false; } }
/** Returns true if the parent directories of the file can be created and the file can be written. */ private boolean canWrite (File file) { File parent = file.getParentFile(); File testFile; if (file.exists()) { if (!file.canWrite() || !canExecute(file)) return false; // Don't overwrite existing file just to check if we can write to directory. testFile = new File(parent, UUID.randomUUID().toString()); } else { parent.mkdirs(); if (!parent.isDirectory()) return false; testFile = file; } try { new FileOutputStream(testFile).close(); if (!canExecute(testFile)) return false; return true; } catch (Throwable ex) { return false; } finally { testFile.delete(); } }
/** Extracts the specified file into the temp directory if it does not already exist or the CRC does not match. If file * extraction fails and the file exists at java.library.path, that file is returned. * @param sourcePath The file to extract from the classpath or JAR. * @param dir The location where the extracted file will be written. */ public void extractFileTo (String sourcePath, File dir) throws IOException { extractFile(sourcePath, crc(readFile(sourcePath)), new File(dir, new File(sourcePath).getName())); }
/** Extracts the specified file to the specified directory if it does not already exist or the CRC does not match. If file * extraction fails and the file exists at java.library.path, that file is returned. * @param sourcePath The file to extract from the classpath or JAR. * @param dirName The name of the subdirectory where the file will be extracted. If null, the file's CRC will be used. * @return The extracted file. */ public File extractFile (String sourcePath, String dirName) throws IOException { try { String sourceCrc = crc(readFile(sourcePath)); if (dirName == null) dirName = sourceCrc; File extractedFile = getExtractedFile(dirName, new File(sourcePath).getName()); if (extractedFile == null) { extractedFile = getExtractedFile(UUID.randomUUID().toString(), new File(sourcePath).getName()); if (extractedFile == null) throw new GdxRuntimeException( "Unable to find writable path to extract file. Is the user home directory writable?"); } return extractFile(sourcePath, sourceCrc, extractedFile); } catch (RuntimeException ex) { // Fallback to file at java.library.path location, eg for applets. File file = new File(System.getProperty("java.library.path"), sourcePath); if (file.exists()) return file; throw ex; } }
/** Extracts the source file and calls System.load. Attemps to extract and load from multiple locations. Throws runtime * exception if all fail. */ private void loadFile (String sourcePath) { String sourceCrc = crc(readFile(sourcePath)); String fileName = new File(sourcePath).getName(); // Temp directory with username in path. File file = new File(System.getProperty("java.io.tmpdir") + "/libgdx" + System.getProperty("user.name") + "/" + sourceCrc, fileName); Throwable ex = loadFile(sourcePath, sourceCrc, file); if (ex == null) return; // System provided temp directory. try { file = File.createTempFile(sourceCrc, null); if (file.delete() && loadFile(sourcePath, sourceCrc, file) == null) return; } catch (Throwable ignored) { } // User home. file = new File(System.getProperty("user.home") + "/.libgdx/" + sourceCrc, fileName); if (loadFile(sourcePath, sourceCrc, file) == null) return; // Relative directory. file = new File(".temp/" + sourceCrc, fileName); if (loadFile(sourcePath, sourceCrc, file) == null) return; // Fallback to java.library.path location, eg for applets. file = new File(System.getProperty("java.library.path"), sourcePath); if (file.exists()) { System.load(file.getAbsolutePath()); return; } throw new GdxRuntimeException(ex); }
/** @return null if the file was extracted and loaded. */ private Throwable loadFile (String sourcePath, String sourceCrc, File extractedFile) { try { System.load(extractFile(sourcePath, sourceCrc, extractedFile).getAbsolutePath()); return null; } catch (Throwable ex) { return ex; } }
/** Returns a path to a file that can be written. Tries multiple locations and verifies writing succeeds. * @return null if a writable path could not be found. */ private File getExtractedFile (String dirName, String fileName) { // Temp directory with username in path. File idealFile = new File( System.getProperty("java.io.tmpdir") + "/libgdx" + System.getProperty("user.name") + "/" + dirName, fileName); if (canWrite(idealFile)) return idealFile; // System provided temp directory. try { File file = File.createTempFile(dirName, null); if (file.delete()) { file = new File(file, fileName); if (canWrite(file)) return file; } } catch (IOException ignored) { } // User home. File file = new File(System.getProperty("user.home") + "/.libgdx/" + dirName, fileName); if (canWrite(file)) return file; // Relative directory. file = new File(".temp/" + dirName, fileName); if (canWrite(file)) return file; // We are running in the OS X sandbox. if (System.getenv("APP_SANDBOX_CONTAINER_ID") != null) return idealFile; return null; }
/** Extracts the specified file to the specified directory if it does not already exist or the CRC does not match. If file * extraction fails and the file exists at java.library.path, that file is returned. * @param sourcePath The file to extract from the classpath or JAR. * @param dirName The name of the subdirectory where the file will be extracted. If null, the file's CRC will be used. * @return The extracted file. */ public File extractFile (String sourcePath, String dirName) throws IOException { try { String sourceCrc = crc(readFile(sourcePath)); if (dirName == null) dirName = sourceCrc; File extractedFile = getExtractedFile(dirName, new File(sourcePath).getName()); if (extractedFile == null) { extractedFile = getExtractedFile(UUID.randomUUID().toString(), new File(sourcePath).getName()); if (extractedFile == null) throw new GdxRuntimeException( "Unable to find writable path to extract file. Is the user home directory writable?"); } return extractFile(sourcePath, sourceCrc, extractedFile); } catch (RuntimeException ex) { // Fallback to file at java.library.path location, eg for applets. File file = new File(System.getProperty("java.library.path"), sourcePath); if (file.exists()) return file; throw ex; } }
/** Extracts the specified file into the temp directory if it does not already exist or the CRC does not match. If file * extraction fails and the file exists at java.library.path, that file is returned. * @param sourcePath The file to extract from the classpath or JAR. * @param dir The location where the extracted file will be written. */ public void extractFileTo (String sourcePath, File dir) throws IOException { extractFile(sourcePath, crc(readFile(sourcePath)), new File(dir, new File(sourcePath).getName())); }
/** Extracts the source file and calls System.load. Attemps to extract and load from multiple locations. Throws runtime * exception if all fail. */ private void loadFile (String sourcePath) { String sourceCrc = crc(readFile(sourcePath)); String fileName = new File(sourcePath).getName(); // Temp directory with username in path. File file = new File(System.getProperty("java.io.tmpdir") + "/libgdx" + System.getProperty("user.name") + "/" + sourceCrc, fileName); Throwable ex = loadFile(sourcePath, sourceCrc, file); if (ex == null) return; // System provided temp directory. try { file = File.createTempFile(sourceCrc, null); if (file.delete() && loadFile(sourcePath, sourceCrc, file) == null) return; } catch (Throwable ignored) { } // User home. file = new File(System.getProperty("user.home") + "/.libgdx/" + sourceCrc, fileName); if (loadFile(sourcePath, sourceCrc, file) == null) return; // Relative directory. file = new File(".temp/" + sourceCrc, fileName); if (loadFile(sourcePath, sourceCrc, file) == null) return; // Fallback to java.library.path location, eg for applets. file = new File(System.getProperty("java.library.path"), sourcePath); if (file.exists()) { System.load(file.getAbsolutePath()); return; } throw new GdxRuntimeException(ex); }
/** @return null if the file was extracted and loaded. */ private Throwable loadFile (String sourcePath, String sourceCrc, File extractedFile) { try { System.load(extractFile(sourcePath, sourceCrc, extractedFile).getAbsolutePath()); return null; } catch (Throwable ex) { return ex; } }
/** Returns a path to a file that can be written. Tries multiple locations and verifies writing succeeds. * @return null if a writable path could not be found. */ private File getExtractedFile (String dirName, String fileName) { // Temp directory with username in path. File idealFile = new File( System.getProperty("java.io.tmpdir") + "/libgdx" + System.getProperty("user.name") + "/" + dirName, fileName); if (canWrite(idealFile)) return idealFile; // System provided temp directory. try { File file = File.createTempFile(dirName, null); if (file.delete()) { file = new File(file, fileName); if (canWrite(file)) return file; } } catch (IOException ignored) { } // User home. File file = new File(System.getProperty("user.home") + "/.libgdx/" + dirName, fileName); if (canWrite(file)) return file; // Relative directory. file = new File(".temp/" + dirName, fileName); if (canWrite(file)) return file; // We are running in the OS X sandbox. if (System.getenv("APP_SANDBOX_CONTAINER_ID") != null) return idealFile; return null; }
/** Loads the libgdx native libraries if they have not already been loaded. */ static public synchronized void load () { if (nativesLoaded) return; nativesLoaded = true; if (disableNativesLoading) return; new SharedLibraryLoader().load("gdx"); } }
/** Extracts the LWJGL native libraries from the classpath and sets the "org.lwjgl.librarypath" system property. */ static public void load () { GdxNativesLoader.load(); if (GdxNativesLoader.disableNativesLoading) return; if (!load) return; SharedLibraryLoader loader = new SharedLibraryLoader(); File nativesDir = null; try { if (isWindows) { nativesDir = loader.extractFile(is64Bit ? "lwjgl64.dll" : "lwjgl.dll", null).getParentFile(); if (!LwjglApplicationConfiguration.disableAudio) loader.extractFileTo(is64Bit ? "OpenAL64.dll" : "OpenAL32.dll", nativesDir); } else if (isMac) { nativesDir = loader.extractFile("liblwjgl.dylib", null).getParentFile(); if (!LwjglApplicationConfiguration.disableAudio) loader.extractFileTo("openal.dylib", nativesDir); } else if (isLinux) { nativesDir = loader.extractFile(is64Bit ? "liblwjgl64.so" : "liblwjgl.so", null).getParentFile(); if (!LwjglApplicationConfiguration.disableAudio) loader.extractFileTo(is64Bit ? "libopenal64.so" : "libopenal.so", nativesDir); } } catch (Throwable ex) { throw new GdxRuntimeException("Unable to extract LWJGL natives.", ex); } System.setProperty("org.lwjgl.librarypath", nativesDir.getAbsolutePath()); load = false; } }
/** Extracts the specified file to the specified directory if it does not already exist or the CRC does not match. If file * extraction fails and the file exists at java.library.path, that file is returned. * @param sourcePath The file to extract from the classpath or JAR. * @param dirName The name of the subdirectory where the file will be extracted. If null, the file's CRC will be used. * @return The extracted file. */ public File extractFile (String sourcePath, String dirName) throws IOException { try { String sourceCrc = crc(readFile(sourcePath)); if (dirName == null) dirName = sourceCrc; File extractedFile = getExtractedFile(dirName, new File(sourcePath).getName()); if (extractedFile == null) { extractedFile = getExtractedFile(UUID.randomUUID().toString(), new File(sourcePath).getName()); if (extractedFile == null) throw new GdxRuntimeException( "Unable to find writable path to extract file. Is the user home directory writable?"); } return extractFile(sourcePath, sourceCrc, extractedFile); } catch (RuntimeException ex) { // Fallback to file at java.library.path location, eg for applets. File file = new File(System.getProperty("java.library.path"), sourcePath); if (file.exists()) return file; throw ex; } }
/** Extracts the specified file into the temp directory if it does not already exist or the CRC does not match. If file * extraction fails and the file exists at java.library.path, that file is returned. * @param sourcePath The file to extract from the classpath or JAR. * @param dir The location where the extracted file will be written. */ public void extractFileTo (String sourcePath, File dir) throws IOException { extractFile(sourcePath, crc(readFile(sourcePath)), new File(dir, new File(sourcePath).getName())); }
/** Extracts the source file and calls System.load. Attemps to extract and load from multiple locations. Throws runtime * exception if all fail. */ private void loadFile (String sourcePath) { String sourceCrc = crc(readFile(sourcePath)); String fileName = new File(sourcePath).getName(); // Temp directory with username in path. File file = new File(System.getProperty("java.io.tmpdir") + "/libgdx" + System.getProperty("user.name") + "/" + sourceCrc, fileName); Throwable ex = loadFile(sourcePath, sourceCrc, file); if (ex == null) return; // System provided temp directory. try { file = File.createTempFile(sourceCrc, null); if (file.delete() && loadFile(sourcePath, sourceCrc, file) == null) return; } catch (Throwable ignored) { } // User home. file = new File(System.getProperty("user.home") + "/.libgdx/" + sourceCrc, fileName); if (loadFile(sourcePath, sourceCrc, file) == null) return; // Relative directory. file = new File(".temp/" + sourceCrc, fileName); if (loadFile(sourcePath, sourceCrc, file) == null) return; // Fallback to java.library.path location, eg for applets. file = new File(System.getProperty("java.library.path"), sourcePath); if (file.exists()) { System.load(file.getAbsolutePath()); return; } throw new GdxRuntimeException(ex); }