diff --git a/README.md b/README.md index 75147f7..d2240ae 100644 --- a/README.md +++ b/README.md @@ -6,10 +6,12 @@ Java 8+ JNI wrapper to the [starship battery](https://crates.io/crates/starship- Usage ----- -The library can be installed from the `releases` repository on [maven.lostluma.net](https://maven.lostluma.net/#/releases/net/lostluma/battery). +The library can be installed from the `releases` repository on [maven.lostluma.net](https://maven.lostluma.net/#/releases/net/lostluma/battery) in two variants: -The dynamic library is currently available for Linux, MacOS, and Windows on both aarch64 and amd64 platforms. -When first being used the version for your system will be downloaded, validated, and cached for subsequent uses. +- `default`: Downloads, validates, and caches the dynamic library on demand. Saves bandwidth and disk space. +- `bundled`: Contains the dynamic library for all platforms. Recommended for situations in which first startup may be offline. + +The dynamic library is currently available for Linux, MacOS, and Windows on both aarch64 and amd64 platforms. Misc ---- diff --git a/build.gradle.kts b/build.gradle.kts index 314e80a..f1b8784 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -43,6 +43,18 @@ tasks.withType { } } +/** + * Builds the bundled Jar. + * + * Put all variants of the library into a `library/` directory first! + */ +tasks.register("buildBundled") { + archiveClassifier = "bundled" + + from(files("library/")) + from(sourceSets.main.get().output) +} + publishing { publications { create("maven") { @@ -51,6 +63,7 @@ publishing { version = version from(components["java"]) + artifact(tasks.named("buildBundled").get()) } } diff --git a/src/main/java/net/lostluma/battery/impl/util/NativeUtil.java b/src/main/java/net/lostluma/battery/impl/util/NativeUtil.java index bfaf6d6..6667787 100644 --- a/src/main/java/net/lostluma/battery/impl/util/NativeUtil.java +++ b/src/main/java/net/lostluma/battery/impl/util/NativeUtil.java @@ -9,6 +9,7 @@ import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.StandardCopyOption; import java.util.Properties; @ApiStatus.Internal @@ -71,12 +72,12 @@ private static void load0() throws IOException, LibraryLoadError{ } if (!isLibraryValid(path, hash)) { - if (!download) { - throw new LibraryLoadError("Native library could not be validated."); - } - Files.createDirectories(path.getParent()); - HttpUtil.download(new URL(BASE_URL + name), path); + boolean copied = copyFileFromJar(name, path); + + if (!copied && download) { + HttpUtil.download(new URL(BASE_URL + name), path); + } if (!isLibraryValid(path, hash)) { throw new LibraryLoadError("Native library could not be validated."); @@ -116,4 +117,19 @@ private static String getName() { private static boolean isLibraryValid(Path path, String hash) throws IOException { return Files.exists(path) && hash.equals(CryptoUtil.sha512(path)); } + + private static boolean copyFileFromJar(String name, Path destination) throws IOException { + // "+" is not valid inside ZIP files + // Gradle replaces them during build + name = "/" + name.replace("+", "."); + + try (InputStream stream = NativeUtil.class.getResourceAsStream(name)) { + if (stream != null) { + Files.copy(stream, destination, StandardCopyOption.REPLACE_EXISTING); + return true; + } + } + + return false; + } }