-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extract dylib for Mac, Remove Zip Requirement #65
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,27 +21,27 @@ | |
package org.openbw.bwapi4j; | ||
|
||
import java.awt.image.ColorModel; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.charset.UnsupportedCharsetException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.*; | ||
import java.nio.file.StandardCopyOption; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.Consumer; | ||
import java.util.stream.Collectors; | ||
import net.lingala.zip4j.core.ZipFile; | ||
import net.lingala.zip4j.exception.ZipException; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.openbw.bwapi4j.type.UnitType; | ||
|
@@ -223,14 +223,11 @@ private void extractBridgeDependencies(final BridgeType bridgeType) { | |
&& jarFile.toString().endsWith(".jar")) { | ||
logger.debug("Extracting dependencies from: " + jarFile.toString()); | ||
|
||
final ZipFile jar = new ZipFile(jarFile.toFile()); | ||
|
||
extractFileIfNotExists( | ||
jar, resolvePlatformLibraryFilename(bridgeType.getLibraryName()), cwd.toString()); | ||
|
||
resolvePlatformLibraryFilename(bridgeType.getLibraryName()), cwd.toString()); | ||
for (final String externalLibrary : getExternalLibraryNames()) { | ||
extractFileIfNotExists( | ||
jar, resolvePlatformLibraryFilename(externalLibrary), cwd.toString()); | ||
resolvePlatformLibraryFilename(externalLibrary), cwd.toString()); | ||
} | ||
} | ||
} catch (final Exception e) { | ||
|
@@ -240,14 +237,16 @@ private void extractBridgeDependencies(final BridgeType bridgeType) { | |
} | ||
} | ||
|
||
private static void extractFileIfNotExists( | ||
final ZipFile zipFile, final String sourceFilename, final String targetDirectory) | ||
throws ZipException { | ||
final Path targetFile = Paths.get(targetDirectory, sourceFilename); | ||
if (!Files.isRegularFile(targetFile) | ||
&& !Files.isDirectory(targetFile) | ||
&& !Files.exists(targetFile)) { | ||
zipFile.extractFile(sourceFilename, targetDirectory); | ||
private static void extractFileIfNotExists(final String sourceFilename, final String targetDirectory) { | ||
final File library = new File(targetDirectory, sourceFilename); | ||
if (!library.exists()) { | ||
InputStream is = BW.class.getResourceAsStream(File.separator + sourceFilename); | ||
try { | ||
Files.copy(is, library.toPath(), StandardCopyOption.REPLACE_EXISTING); | ||
} catch (IOException e) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the justification for catching an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error can be re-thrown on cleanup, but I think I'll remove this since I'd have to test external jar first. |
||
//noinspection ResultOfMethodCallIgnored | ||
library.delete(); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -331,11 +330,6 @@ private List<String> getExternalLibraryNames() { | |
|
||
return libNames; | ||
} | ||
|
||
private static String getLibraryPathDelimiter() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are now at the mercy of the JDK/JRE deciding what the proper delimiter is. But, I am willing to try There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have there been instances where the JVM is detecting the wrong system? If so I would just consolidate the methods since the logic to determine the delimiter is duplicated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No instances that I know of nor for which I have any evidence. As I said, we can try it out no big deal and it's probably fine. I just like having more control over simple things rather than relying on a library (even the JDK) to probe the system for something simple. |
||
return isWindowsPlatform() ? ";" : ":"; | ||
} | ||
|
||
private static boolean isWindowsPlatform() { | ||
return System.getProperty("os.name").contains("Windows"); | ||
} | ||
|
@@ -394,7 +388,7 @@ private void addLibraryPath(final String path) { | |
|
||
logger.info("Adding library path: {}", path); | ||
|
||
final String libraryPathDelimiter = getLibraryPathDelimiter(); | ||
final String libraryPathDelimiter = File.pathSeparator; | ||
final String newLibraryPath = | ||
currentLibraryPath | ||
+ (!currentLibraryPath.endsWith(libraryPathDelimiter) ? libraryPathDelimiter : "") | ||
|
@@ -433,27 +427,18 @@ private static boolean systemPropertyEquals( | |
} | ||
|
||
private static String resolvePlatformLibraryFilename(String libraryName) { | ||
if (isWindowsPlatform()) { | ||
if (!libraryName.toLowerCase(Locale.US).endsWith(".dll")) { | ||
libraryName += ".dll"; | ||
} | ||
} else { | ||
if (!libraryName.startsWith("lib")) { | ||
libraryName = "lib" + libraryName; | ||
} | ||
|
||
if (!libraryName.toLowerCase(Locale.US).endsWith(".so")) { | ||
libraryName += ".so"; | ||
} | ||
switch (OSType.computeType()) { | ||
case WINDOWS: | ||
return libraryName + ".dll"; | ||
case MAC: | ||
return "lib" + libraryName + ".dylib"; | ||
default: | ||
return "lib" + libraryName + ".so"; | ||
} | ||
|
||
return libraryName; | ||
} | ||
|
||
private static boolean isPathFoundInPathVariable(final String pathVariable, final String path) { | ||
final String delim = isWindowsPlatform() ? ";" : ":"; | ||
|
||
final String[] paths = pathVariable.split(delim); | ||
final String[] paths = pathVariable.split(File.pathSeparator); | ||
|
||
for (final String directory : paths) { | ||
final Path targetDirectory; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Path
is more modern and robust thanFile
. https://docs.oracle.com/javase/tutorial/essential/io/legacy.html . Please provide justification for usingFile
overPath
in this case.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't aware of the changes but will migrate to using Path if I am able to convert to using
getResourceAsStream
.