Fix Java 8 issues with LWJGL native libs on OSX?

This commit is contained in:
Petr Mrázek 2014-11-01 10:39:32 +01:00
parent 9e8a74cc89
commit 095640ed01
2 changed files with 61 additions and 4 deletions

View File

@ -17,11 +17,15 @@
package org.multimc; package org.multimc;
import java.io.*; import java.io.*;
import java.io.File;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.Arrays; import java.util.Arrays;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.List; import java.util.List;
@ -206,14 +210,30 @@ public class Utils
} }
/** /**
* Unzip zip file 'source' into the folder 'targetFolder' * Replace a 'target' string 'suffix' with 'replacement'
*/
public static String replaceSuffix (String target, String suffix, String replacement)
{
if (!target.endsWith(suffix))
{
return target;
}
String prefix = target.substring(0, target.length() - suffix.length());
return prefix + replacement;
}
/**
* Unzip zip file with natives 'source' into the folder 'targetFolder'
*
* Contains a hack for OSX. Yay.
* @param source * @param source
* @param targetFolder * @param targetFolder
* @throws IOException * @throws IOException
*/ */
public static void unzip(File source, File targetFolder) throws IOException public static void unzipNatives(File source, File targetFolder) throws IOException
{ {
ZipFile zip = new ZipFile(source); ZipFile zip = new ZipFile(source);
Set <String> toProcess = new HashSet<String>();
try try
{ {
Enumeration entries = zip.entries(); Enumeration entries = zip.entries();
@ -222,7 +242,8 @@ public class Utils
{ {
ZipEntry entry = (ZipEntry) entries.nextElement(); ZipEntry entry = (ZipEntry) entries.nextElement();
File targetFile = new File(targetFolder, entry.getName()); String entryName = entry.getName();
File targetFile = new File(targetFolder, entryName);
if (targetFile.getParentFile() != null) if (targetFile.getParentFile() != null)
{ {
targetFile.getParentFile().mkdirs(); targetFile.getParentFile().mkdirs();
@ -232,11 +253,47 @@ public class Utils
continue; continue;
copyStream(zip.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(targetFile))); copyStream(zip.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(targetFile)));
toProcess.add(entryName);
} }
} finally } finally
{ {
zip.close(); zip.close();
} }
for (String entryName : toProcess)
{
// check if we need a symlink
String suffixFrom = null;
String suffixTo = null;
if(entryName.endsWith(".dylib"))
{
suffixFrom = ".dylib";
suffixTo = ".jnilib";
}
else if(entryName.endsWith(".jnilib"))
{
suffixFrom = ".jnilib";
suffixTo = ".dylib";
}
else
{
continue;
}
String linkName = replaceSuffix(entryName, suffixFrom, suffixTo);
File targetFile = new File(targetFolder, entryName);
File symlinkFile = new File(targetFolder, linkName);
// if the link file exists already for whatever reason, do not create symlinks
if(symlinkFile.exists())
{
continue;
}
// create a symlink. This means we always have .jnilib and .dylib variants of the same libs.
Path linkLink = symlinkFile.toPath();
Path linkTarget = targetFile.toPath();
Files.createSymbolicLink(linkLink, linkTarget);
}
} }
} }

View File

@ -334,7 +334,7 @@ public class OneSixLauncher implements Launcher
String cleanlib = extlib.replace("${arch}", is_64 ? "64" : "32"); String cleanlib = extlib.replace("${arch}", is_64 ? "64" : "32");
File cleanlibf = new File(cleanlib); File cleanlibf = new File(cleanlib);
Utils.log("Extracting " + cleanlibf.getName()); Utils.log("Extracting " + cleanlibf.getName());
Utils.unzip(cleanlibf, new File(natives)); Utils.unzipNatives(cleanlibf, new File(natives));
} catch (IOException e) } catch (IOException e)
{ {
System.err.println("Failed to extract native library:"); System.err.println("Failed to extract native library:");