diff --git a/imgui-binding/build.gradle b/imgui-binding/build.gradle index 0f3fd544..32677f8f 100644 --- a/imgui-binding/build.gradle +++ b/imgui-binding/build.gradle @@ -34,3 +34,19 @@ jar { attributes 'Automatic-Module-Name': 'imgui.binding' } } + +tasks.register('makePropertyResource') { + def directory = file "${buildDir}/resources/main/imgui/" + directory.mkdirs() + def propertyFile = file "${directory}/imgui-java.properties" + def props = new Properties() + if (propertyFile.exists()) { + propertyFile.withReader { props.load(it) } + } + props.setProperty("version", project.version) + propertyFile.withWriter { props.store(it, "imgui-java") } +} + +processResources { + dependsOn makePropertyResource +} diff --git a/imgui-binding/src/main/java/imgui/ImGui.java b/imgui-binding/src/main/java/imgui/ImGui.java index ed1349d9..606c3dba 100644 --- a/imgui-binding/src/main/java/imgui/ImGui.java +++ b/imgui-binding/src/main/java/imgui/ImGui.java @@ -18,16 +18,18 @@ import java.io.InputStream; import java.io.UncheckedIOException; import java.lang.ref.WeakReference; +import java.nio.file.AccessDeniedException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; +import java.util.Properties; public class ImGui { private static final String LIB_PATH_PROP = "imgui.library.path"; private static final String LIB_NAME_PROP = "imgui.library.name"; private static final String LIB_NAME_DEFAULT = System.getProperty("os.arch").contains("64") ? "imgui-java64" : "imgui-java"; - private static final String LIB_TMP_DIR_PREFIX = "imgui-java-natives_" + System.currentTimeMillis(); + private static final String LIB_TMP_DIR_PREFIX = "imgui-java-natives"; private static final ImGuiContext IMGUI_CONTEXT; private static final ImGuiIO IMGUI_IO; @@ -49,14 +51,19 @@ public class ImGui { final String libName = System.getProperty(LIB_NAME_PROP, LIB_NAME_DEFAULT); final String fullLibName = resolveFullLibName(); - final String extractedLibAbsPath = tryLoadFromClasspath(fullLibName); - if (libPath != null) { - System.load(Paths.get(libPath).resolve(fullLibName).toFile().getAbsolutePath()); - } else if (extractedLibAbsPath != null) { - System.load(extractedLibAbsPath); + System.load(Paths.get(libPath).resolve(fullLibName).toAbsolutePath().toString()); } else { - System.loadLibrary(libName); + try { + System.loadLibrary(libName); + } catch (Exception | Error e) { + final String extractedLibAbsPath = tryLoadFromClasspath(fullLibName); + if (extractedLibAbsPath != null) { + System.load(extractedLibAbsPath); + } else { + throw e; + } + } } IMGUI_CONTEXT = new ImGuiContext(0); @@ -115,19 +122,43 @@ private static String tryLoadFromClasspath(final String fullLibName) { return null; } - final Path tmpDir = Paths.get(System.getProperty("java.io.tmpdir")).resolve(LIB_TMP_DIR_PREFIX); - tmpDir.toFile().mkdirs(); + String version = getVersionString(); + if (version == null) { + version = "unknown"; + } + final Path tmpDir = Paths.get(System.getProperty("java.io.tmpdir")).resolve(LIB_TMP_DIR_PREFIX).resolve(version); + if (!Files.exists(tmpDir)) { + Files.createDirectories(tmpDir); + } final Path libBin = tmpDir.resolve(fullLibName); - Files.copy(is, libBin, StandardCopyOption.REPLACE_EXISTING); - libBin.toFile().deleteOnExit(); + try { + Files.copy(is, libBin, StandardCopyOption.REPLACE_EXISTING); + } catch (AccessDeniedException e) { + if (!Files.exists(libBin)) { + throw e; + } + } - return libBin.toFile().getAbsolutePath(); + return libBin.toAbsolutePath().toString(); } catch (IOException e) { throw new UncheckedIOException(e); } } + private static String getVersionString() { + final Properties properties = new Properties(); + try (InputStream is = ImGui.class.getResourceAsStream("/imgui/imgui-java.properties")) { + if (is != null) { + properties.load(is); + return properties.get("version").toString(); + } + } catch (IOException e) { + throw new UncheckedIOException(e); + } + return null; + } + /** * For internal usage. * Method is used to initiate static instantiation (loading of the native libraries etc.).