Skip to content

Commit

Permalink
Allow customizing the native library installation
Browse files Browse the repository at this point in the history
  • Loading branch information
LostLuma committed Jun 20, 2024
1 parent 2235b04 commit f89eda1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
29 changes: 29 additions & 0 deletions src/main/java/net/lostluma/battery/api/util/LibraryUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.lostluma.battery.api.util;

import net.lostluma.battery.impl.util.NativeUtil;

import java.nio.file.Path;

/**
* Optional utilities to customize library installation.
*/
public class LibraryUtil {
/**
* Customize the directory for storing the dynamic library.
* By default, this is a subfolder in the user's cache directory.
*
* @param path the new cache directory
*/
public static void setCacheDir(Path path) {
NativeUtil.setCacheDir(path);
}

/**
* Customize whether the dynamic library may be automatically downloaded.
*
* @param value whether to allow automatic library downloading. Defaults to true.
*/
public static void setAllowDownloads(boolean value) {
NativeUtil.setAllowDownloads(value);
}
}
2 changes: 1 addition & 1 deletion src/main/java/net/lostluma/battery/impl/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public final class Constants {
public static final String VERSION = "1.0.0";
public static final String NATIVES = "1.0.0";

public static Path CACHE_DIR = getCacheDir();
public static final Path CACHE_DIR = getCacheDir();

private static String getUserHome() {
return System.getProperty("user.home");
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/lostluma/battery/impl/ManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import net.lostluma.battery.api.Battery;
import net.lostluma.battery.api.Manager;
import net.lostluma.battery.api.exception.LibraryLoadError;
import net.lostluma.battery.impl.util.LibraryUtil;
import net.lostluma.battery.impl.util.NativeUtil;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

Expand All @@ -27,7 +27,7 @@ public final class ManagerImpl implements Manager {
private final Map<PhantomReference<?>, Long> references;

public ManagerImpl() throws IOException, LibraryLoadError {
LibraryUtil.load();
NativeUtil.load();

this.ptr = create();
this.active = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
import java.util.Properties;

@ApiStatus.Internal
public class LibraryUtil {
public class NativeUtil {
private static Path cacheDir = null;
private static boolean download = true;

private static boolean isLoaded = false;
private static final String BASE_URL = "https://files.lostluma.net/battery-jni/" + Constants.NATIVES + "/";

Expand All @@ -29,10 +32,18 @@ public static void load() throws LibraryLoadError {
}
}

public static void setCacheDir(Path path) {
cacheDir = path;
}

public static void setAllowDownloads(boolean value) {
download = value;
}

private static void load0() throws IOException, LibraryLoadError{
Properties properties = new Properties();

try (InputStream stream = LibraryUtil.class.getResourceAsStream("/natives.properties")) {
try (InputStream stream = NativeUtil.class.getResourceAsStream("/natives.properties")) {
if (stream == null) {
throw new LibraryLoadError("Failed to read native library info.");
}
Expand All @@ -49,9 +60,15 @@ private static void load0() throws IOException, LibraryLoadError{
String name = properties.getProperty(base + ".name");
String hash = properties.getProperty(base + ".hash");

Path path = Constants.CACHE_DIR.resolve(name);
Path path;

if (!isLibraryValid(path, hash)) {
if (cacheDir != null) {
path = cacheDir.resolve(name);
} else {
path = Constants.CACHE_DIR.resolve(name);
}

if (download && !isLibraryValid(path, hash)) {
Files.createDirectories(Constants.CACHE_DIR);
HttpUtil.download(new URL(BASE_URL + name), path);
}
Expand Down

0 comments on commit f89eda1

Please sign in to comment.