Skip to content

Commit

Permalink
Allow using custom library builds
Browse files Browse the repository at this point in the history
  • Loading branch information
LostLuma committed Oct 27, 2024
1 parent f8b7a1d commit 4358746
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
Battery
=======
# Battery

Java 8+ JNI wrapper to the [starship battery](https://crates.io/crates/starship-battery) crate.

Usage
-----
## Usage

The library can be installed from the `releases` repository on [maven.lostluma.net](https://maven.lostluma.net/#/releases/net/lostluma/battery) in two variants:

- `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.
- `bundled`: Contains the dynamic library for all platforms. Recommended if first application startup may be offline.

The dynamic library is comes prebuilt for the following platforms:

Expand All @@ -19,7 +17,9 @@ The dynamic library is comes prebuilt for the following platforms:
| MacOS | yes | yes | |
| Windows | yes | yes | |

Misc
----
Running on other platforms is also possible, however some manual setup is required:
First build the project with Cargo, then set the `battery.natives.path` system property to the file path.

## Misc

This is my first time using both Rust and JNI :)
1 change: 1 addition & 0 deletions src/main/java/net/lostluma/battery/impl/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public final class Constants {
public static final String NATIVES_VERSION = "1.2.0";

public static final Path DEFAULT_CACHE_DIR = getDefaultCacheDir();
public static final String NATIVES_PATH_PROPERTY = "battery.natives.path";

private static String getUserHome() {
return System.getProperty("user.home");
Expand Down
24 changes: 19 additions & 5 deletions src/main/java/net/lostluma/battery/impl/util/NativeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Properties;

Expand Down Expand Up @@ -44,6 +45,23 @@ public static void setAllowDownloads(boolean value) {
}

private static void load0() throws IOException, LibraryLoadError{
Path path;
String custom = System.getProperty(Constants.NATIVES_PATH_PROPERTY);

if (custom == null) {
path = getPath();
} else {
path = Paths.get(custom);
}

try {
System.load(path.toAbsolutePath().toString());
} catch (UnsatisfiedLinkError e) {
throw new LibraryLoadError(e);
}
}

private static Path getPath() throws IOException, LibraryLoadError {
Properties properties = new Properties();

try (InputStream stream = NativeUtil.class.getResourceAsStream(METADATA)) {
Expand Down Expand Up @@ -84,11 +102,7 @@ private static void load0() throws IOException, LibraryLoadError{
}
}

try {
System.load(path.toAbsolutePath().toString());
} catch (UnsatisfiedLinkError e) {
throw new LibraryLoadError(e);
}
return path;
}

private static String getArch() {
Expand Down

0 comments on commit 4358746

Please sign in to comment.