Write a better native library loader so DLLs are no longer extracted to the current directory on Windows.

This commit is contained in:
Cameron Gutman
2013-12-28 16:56:38 -05:00
parent fcbc80c05c
commit 844060a4ee
4 changed files with 139 additions and 61 deletions

View File

@@ -1,14 +1,12 @@
package com.limelight;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import com.limelight.binding.LibraryHelper;
import com.limelight.binding.PlatformBinding;
import com.limelight.gui.MainFrame;
import com.limelight.gui.StreamFrame;
@@ -34,53 +32,6 @@ public class Limelight implements NvConnectionListener {
this.host = host;
}
private static void extractNativeLibrary(String libraryName, String targetDirectory) throws IOException {
InputStream resource = new Object().getClass().getResourceAsStream("/binlib/"+libraryName);
if (resource == null) {
throw new FileNotFoundException("Unable to find native library in JAR: "+libraryName);
}
File destination = new File(targetDirectory+File.separatorChar+libraryName);
// this will only delete it if it exists, and then create a new file
destination.delete();
destination.createNewFile();
//this is the janky java 6 way to copy a file
FileOutputStream fos = null;
try {
fos = new FileOutputStream(destination);
int read;
byte[] readBuffer = new byte[16384];
while ((read = resource.read(readBuffer)) != -1) {
fos.write(readBuffer, 0, read);
}
} finally {
if (fos != null) {
fos.close();
}
}
}
private static void prepareNativeLibraries() throws IOException {
if (!System.getProperty("os.name").contains("Windows")) {
// Nothing to do for platforms other than Windows
return;
}
// We need to extract nv_avc_dec's runtime dependencies manually
// because the current JRE extracts them with different file names
// so they don't load properly.
String nativeLibDir = ".";
extractNativeLibrary("avfilter-3.dll", nativeLibDir);
extractNativeLibrary("avformat-55.dll", nativeLibDir);
extractNativeLibrary("avutil-52.dll", nativeLibDir);
extractNativeLibrary("postproc-52.dll", nativeLibDir);
extractNativeLibrary("pthreadVC2.dll", nativeLibDir);
extractNativeLibrary("swresample-0.dll", nativeLibDir);
extractNativeLibrary("swscale-2.dll", nativeLibDir);
extractNativeLibrary("avcodec-55.dll", nativeLibDir);
}
private void startUp() {
streamFrame = new StreamFrame();
@@ -150,7 +101,7 @@ public class Limelight implements NvConnectionListener {
}
try {
prepareNativeLibraries();
LibraryHelper.prepareNativeLibraries();
} catch (IOException e) {
// This is expected to fail when not in a JAR
}