diff --git a/src/main/java/net/lostluma/battery/api/util/LibraryUtil.java b/src/main/java/net/lostluma/battery/api/util/LibraryUtil.java new file mode 100644 index 0000000..ef3b065 --- /dev/null +++ b/src/main/java/net/lostluma/battery/api/util/LibraryUtil.java @@ -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); + } +} diff --git a/src/main/java/net/lostluma/battery/impl/Constants.java b/src/main/java/net/lostluma/battery/impl/Constants.java index 9ecdde0..3eac886 100644 --- a/src/main/java/net/lostluma/battery/impl/Constants.java +++ b/src/main/java/net/lostluma/battery/impl/Constants.java @@ -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"); diff --git a/src/main/java/net/lostluma/battery/impl/ManagerImpl.java b/src/main/java/net/lostluma/battery/impl/ManagerImpl.java index b13147a..d5f5c48 100644 --- a/src/main/java/net/lostluma/battery/impl/ManagerImpl.java +++ b/src/main/java/net/lostluma/battery/impl/ManagerImpl.java @@ -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; @@ -27,7 +27,7 @@ public final class ManagerImpl implements Manager { private final Map, Long> references; public ManagerImpl() throws IOException, LibraryLoadError { - LibraryUtil.load(); + NativeUtil.load(); this.ptr = create(); this.active = true; diff --git a/src/main/java/net/lostluma/battery/impl/util/LibraryUtil.java b/src/main/java/net/lostluma/battery/impl/util/NativeUtil.java similarity index 80% rename from src/main/java/net/lostluma/battery/impl/util/LibraryUtil.java rename to src/main/java/net/lostluma/battery/impl/util/NativeUtil.java index 268ca0c..272fc39 100644 --- a/src/main/java/net/lostluma/battery/impl/util/LibraryUtil.java +++ b/src/main/java/net/lostluma/battery/impl/util/NativeUtil.java @@ -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 + "/"; @@ -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."); } @@ -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); }