diff --git a/src/main/java/com/thegamecommunity/excite/modding/game/file/ResourceDecompressor.java b/src/main/java/com/thegamecommunity/excite/modding/game/file/ResourceDecompressor.java index 8533b16..87aa0cb 100644 --- a/src/main/java/com/thegamecommunity/excite/modding/game/file/ResourceDecompressor.java +++ b/src/main/java/com/thegamecommunity/excite/modding/game/file/ResourceDecompressor.java @@ -1,9 +1,10 @@ package com.thegamecommunity.excite.modding.game.file; -import java.nio.ByteBuffer; +import java.io.IOException; +import java.nio.file.Path; public interface ResourceDecompressor { - public ByteBuffer decompress(); + public Path decompress() throws IOException; } diff --git a/src/main/java/com/thegamecommunity/excite/modding/game/file/ResourceFile.java b/src/main/java/com/thegamecommunity/excite/modding/game/file/ResourceFile.java index f09c5b5..87f1b4b 100644 --- a/src/main/java/com/thegamecommunity/excite/modding/game/file/ResourceFile.java +++ b/src/main/java/com/thegamecommunity/excite/modding/game/file/ResourceFile.java @@ -1,18 +1,19 @@ package com.thegamecommunity.excite.modding.game.file; +import java.io.IOException; import java.nio.ByteBuffer; public interface ResourceFile { - public ByteBuffer getRawBytes(); + public ByteBuffer getRawBytes() throws IOException; - public ByteBuffer getResourceBytes(); + public ByteBuffer getHeaderBytes() throws IOException; - public boolean isCompressed(); + public ByteBuffer getResourceBytes() throws IOException; - public default ResourceDecompressor getDecompressor() { - return new UncompressedDecompressor(this); - }; + public boolean isCompressedArchive() throws IOException; + + public ResourceDecompressor getDecompressor(); diff --git a/src/main/java/com/thegamecommunity/excite/modding/game/file/UncompressedDecompressor.java b/src/main/java/com/thegamecommunity/excite/modding/game/file/UncompressedDecompressor.java deleted file mode 100644 index f787533..0000000 --- a/src/main/java/com/thegamecommunity/excite/modding/game/file/UncompressedDecompressor.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.thegamecommunity.excite.modding.game.file; - -import java.nio.ByteBuffer; - -public class UncompressedDecompressor implements ResourceDecompressor { - - private final ResourceFile file; - - public UncompressedDecompressor(ResourceFile file) { - this.file = file; - } - - @Override - public ByteBuffer decompress() { - return file.getResourceBytes(); - } - -} diff --git a/src/main/java/com/thegamecommunity/excite/modding/util/Math.java b/src/main/java/com/thegamecommunity/excite/modding/util/Math.java index b447a40..0edbd1b 100644 --- a/src/main/java/com/thegamecommunity/excite/modding/util/Math.java +++ b/src/main/java/com/thegamecommunity/excite/modding/util/Math.java @@ -5,8 +5,8 @@ public class Math { /** * Calculates the closest multiple of a given number (m) to another number (n) that is greater than or equal to n. * - * @param n the number to find the closest multiple of m to. Must be non-negative. - * @param m the multiple to consider. Must be non-negative. + * @param m the number to find the closest multiple of m to. Must be non-negative. + * @param n the multiple to consider. Must be non-negative. * @return the closest multiple of m to n that is greater than or equal to n * @throws ArithmeticException if either n or m is negative * @@ -21,14 +21,14 @@ public class Math { * */ public static int nearestMultiple(int n, int m) throws ArithmeticException { - if (n < 0) { + if (m < 0) { throw new ArithmeticException("number cannot be negative"); } - if(m < 0) { + if(n < 0) { throw new ArithmeticException("multiple cannot be negative"); } - return n == 0 ? 0 : n == m ? m : ((m + (n - 1)) / n) * n; + return m == 0 ? 0 : m == n ? n : ((n + (m - 1)) / m) * m; } } diff --git a/src/main/java/com/thegamecommunity/excite/modding/util/foreign/c/dependency/ForeignDependencies.java b/src/main/java/com/thegamecommunity/excite/modding/util/foreign/c/dependency/ForeignDependencies.java index 714d581..b05be6b 100644 --- a/src/main/java/com/thegamecommunity/excite/modding/util/foreign/c/dependency/ForeignDependencies.java +++ b/src/main/java/com/thegamecommunity/excite/modding/util/foreign/c/dependency/ForeignDependencies.java @@ -27,6 +27,7 @@ public enum ForeignDependencies { public static final Path cSourceDir = Path.of("./run/deps/src").toAbsolutePath(); public static final Path cCompileDir = Path.of("./run/deps/bin").toAbsolutePath(); + private static boolean downloadedAndCompiled = false; static { try { @@ -42,6 +43,7 @@ public enum ForeignDependencies { private final String url; private final String dest; private final Pattern obtainVersionRegex; + private boolean available = false; private static final Pattern versionRegex = Pattern.compile("@.*?@"); @@ -130,23 +132,42 @@ public File getDest() { return new File(versionRegex.matcher(dest).replaceAll(getVersion())).getAbsoluteFile(); } - public static void downloadAndCompileAllDeps() throws IOException, InterruptedException, LinkageError { + public boolean isAvailable() { + return available; + } + + public static void downloadAndCompileAllDeps() throws LinkageError { downloadAndCompileAllDeps(true); } - public static void downloadAndCompileAllDeps(boolean overwrite) throws IOException, InterruptedException, LinkageError { - HashMap deps = new HashMap<>(); - - for(ForeignDependencies dep : values()) { - Path f = dep.write(overwrite); - if(f != null) { - deps.put(dep, f); + public static void downloadAndCompileAllDeps(boolean overwrite) throws LinkageError { + if(!downloadedAndCompiled) { + HashMap deps = new HashMap<>(); + + for(ForeignDependencies dep : values()) { + try { + Path f = dep.write(overwrite); + if(f != null) { + deps.put(dep, f); + } + } + catch(Throwable t) { + System.err.println("Could not download " + dep + ". If it's already downloaded then the program can proceed, otherwise an error will probably occur."); + } } - } - - CLangCompiler compiler = CLangCompiler.get(); - for(Entry e : deps.entrySet()) { - compiler.compile(e.getValue(), (e.getKey().getCompiledLocation())); + + CLangCompiler compiler = CLangCompiler.get(); + for(Entry e : deps.entrySet()) { + try { + compiler.compile(e.getValue(), (e.getKey().getCompiledLocation())); + e.getKey().available = true; + } catch (Throwable ex) { + LinkageError ex2 = new LinkageError(); + ex2.initCause(ex); + throw ex2; + } + } + downloadedAndCompiled = true; } }