Last active
February 16, 2018 00:43
-
-
Save Jire/91555f320799cf6ee604a8407b9bceb1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ps.eden.client; | |
import java.io.*; | |
/** | |
* @author Jire | |
*/ | |
public final class NativeLibraryLoader { | |
private static final String[] NATIVE_LIBRARIES = { | |
"nativewindow_", "nativewindow_awt", "newt", "gluegen-rt", "jogl_desktop", "jogl_mobile" | |
}; | |
public static void loadNativeLibraries() { | |
/* Determine native library information */ | |
String folder = "windows"; | |
String prefix = ""; | |
String suffix = ".dll"; | |
try { | |
final String operSys = System.getProperty("os.name").toLowerCase(); | |
if (operSys.contains("mac")) { | |
folder = "macosx-universal"; | |
prefix = "lib"; | |
suffix = ".jnilib"; | |
NATIVE_LIBRARIES[0] += "macosx"; | |
} else { | |
if (operSys.contains("nix") || operSys.contains("nux") | |
|| operSys.contains("aix") || operSys.contains("sunos")) { | |
folder = operSys.contains("sunos") ? "solaris" : "linux"; | |
prefix = "lib"; | |
suffix = ".so"; | |
NATIVE_LIBRARIES[0] += "x11"; | |
} else NATIVE_LIBRARIES[0] += "win32"; | |
folder += "32".equals(System.getProperty("sun.arch.data.model")) ? "-i586" : "-amd64"; | |
} | |
} catch (Throwable t) { | |
t.printStackTrace(); | |
} | |
for (String lib : NATIVE_LIBRARIES) { | |
try { | |
loadNativeLibrary("natives/" + folder + "/", prefix + lib + suffix); | |
} catch (Throwable t) { | |
t.printStackTrace(); | |
} | |
} | |
} | |
public static void loadNativeLibrary(String folder, String file) throws IOException { | |
final File _folder = new File(Configurations.getCachePath() + File.separator + folder); | |
if (!_folder.exists()) _folder.mkdirs(); | |
final File _file = new File(_folder, file); | |
String pathPrefix = ""; | |
InputStream in = ClassLoader.getSystemClassLoader().getResourceAsStream(folder + file); | |
try { | |
final FileOutputStream fos = new FileOutputStream(_file); | |
try { | |
final byte[] buffer = new byte[1024]; | |
int read = -1; | |
while ((read = in.read(buffer)) != -1) fos.write(buffer, 0, read); | |
} finally { | |
fos.close(); | |
} | |
} finally { | |
in.close(); | |
} | |
System.load(pathPrefix + _file.getAbsolutePath()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment