From 2831a8b86969e976f640e28d66ec8f3eb1838579 Mon Sep 17 00:00:00 2001 From: Kusaanko <39370373+kusaanko@users.noreply.github.com> Date: Tue, 29 Aug 2023 17:04:16 +0900 Subject: [PATCH 1/9] [API] Stop creating tmpDir every time --- imgui-binding/src/main/java/imgui/ImGui.java | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/imgui-binding/src/main/java/imgui/ImGui.java b/imgui-binding/src/main/java/imgui/ImGui.java index ed1349d9..d889d6a9 100644 --- a/imgui-binding/src/main/java/imgui/ImGui.java +++ b/imgui-binding/src/main/java/imgui/ImGui.java @@ -18,6 +18,7 @@ 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; @@ -27,7 +28,7 @@ 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; @@ -115,14 +116,21 @@ 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(); + final Path tmpDir = Paths.get(System.getProperty("java.io.tmpdir")).resolve(LIB_TMP_DIR_PREFIX).resolve(ImGui.class.getPackage().getImplementationVersion()); + 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); } From 46f35d898d917a7c6e9c458d7b6f26dd1e46d336 Mon Sep 17 00:00:00 2001 From: Kusaanko <39370373+kusaanko@users.noreply.github.com> Date: Tue, 29 Aug 2023 17:18:29 +0900 Subject: [PATCH 2/9] [API] Add a whitespace after catch --- imgui-binding/src/main/java/imgui/ImGui.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/imgui-binding/src/main/java/imgui/ImGui.java b/imgui-binding/src/main/java/imgui/ImGui.java index d889d6a9..d803df39 100644 --- a/imgui-binding/src/main/java/imgui/ImGui.java +++ b/imgui-binding/src/main/java/imgui/ImGui.java @@ -124,7 +124,7 @@ private static String tryLoadFromClasspath(final String fullLibName) { final Path libBin = tmpDir.resolve(fullLibName); try { Files.copy(is, libBin, StandardCopyOption.REPLACE_EXISTING); - } catch(AccessDeniedException e) { + } catch (AccessDeniedException e) { if (!Files.exists(libBin)) { throw e; } From 6c9bcfc0acc72c916db828ada9fdd2d6e83a5ee4 Mon Sep 17 00:00:00 2001 From: Kusaanko <39370373+kusaanko@users.noreply.github.com> Date: Tue, 29 Aug 2023 18:32:24 +0900 Subject: [PATCH 3/9] [API] Fix getting imgui-java version --- imgui-binding/.gitignore | 1 + imgui-binding/build.gradle | 17 +++++++++++++++++ imgui-binding/src/main/java/imgui/ImGui.java | 20 +++++++++++++++++++- 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 imgui-binding/.gitignore diff --git a/imgui-binding/.gitignore b/imgui-binding/.gitignore new file mode 100644 index 00000000..bc3db54e --- /dev/null +++ b/imgui-binding/.gitignore @@ -0,0 +1 @@ +src/main/resources/META-INF/imgui-java.properties \ No newline at end of file diff --git a/imgui-binding/build.gradle b/imgui-binding/build.gradle index 0f3fd544..4b22cf5e 100644 --- a/imgui-binding/build.gradle +++ b/imgui-binding/build.gradle @@ -1,4 +1,5 @@ import tool.generator.GenerateLibs +import java.nio.file.Files plugins { id 'java' @@ -34,3 +35,19 @@ jar { attributes 'Automatic-Module-Name': 'imgui.binding' } } + +tasks.register('makePropertyResource') { + def directory = file "src/main/resources/META-INF" + Files.createDirectories(directory.toPath()) + def propertyFile = file "${project.projectDir}/src/main/resources/META-INF/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 d803df39..29fec46e 100644 --- a/imgui-binding/src/main/java/imgui/ImGui.java +++ b/imgui-binding/src/main/java/imgui/ImGui.java @@ -23,6 +23,7 @@ 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"; @@ -116,7 +117,11 @@ private static String tryLoadFromClasspath(final String fullLibName) { return null; } - final Path tmpDir = Paths.get(System.getProperty("java.io.tmpdir")).resolve(LIB_TMP_DIR_PREFIX).resolve(ImGui.class.getPackage().getImplementationVersion()); + 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); } @@ -136,6 +141,19 @@ private static String tryLoadFromClasspath(final String fullLibName) { } } + private static String getVersionString() { + Properties properties = new Properties(); + try (InputStream is = ImGui.class.getResourceAsStream("/META-INF/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.). From 842500a5ed6794d3151a410574e5bc81882b93e1 Mon Sep 17 00:00:00 2001 From: Kusaanko <39370373+kusaanko@users.noreply.github.com> Date: Tue, 29 Aug 2023 18:37:33 +0900 Subject: [PATCH 4/9] [API] Add final to properties variable --- imgui-binding/src/main/java/imgui/ImGui.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/imgui-binding/src/main/java/imgui/ImGui.java b/imgui-binding/src/main/java/imgui/ImGui.java index 29fec46e..0506e051 100644 --- a/imgui-binding/src/main/java/imgui/ImGui.java +++ b/imgui-binding/src/main/java/imgui/ImGui.java @@ -142,7 +142,7 @@ private static String tryLoadFromClasspath(final String fullLibName) { } private static String getVersionString() { - Properties properties = new Properties(); + final Properties properties = new Properties(); try (InputStream is = ImGui.class.getResourceAsStream("/META-INF/imgui-java.properties")) { if (is != null) { properties.load(is); From 5f5dea445b2fed9749b7640f9301b740a4405535 Mon Sep 17 00:00:00 2001 From: Kusaanko <39370373+kusaanko@users.noreply.github.com> Date: Tue, 29 Aug 2023 22:48:35 +0900 Subject: [PATCH 5/9] [API] Make it brief to generate properties --- imgui-binding/.gitignore | 1 - imgui-binding/build.gradle | 7 +++---- 2 files changed, 3 insertions(+), 5 deletions(-) delete mode 100644 imgui-binding/.gitignore diff --git a/imgui-binding/.gitignore b/imgui-binding/.gitignore deleted file mode 100644 index bc3db54e..00000000 --- a/imgui-binding/.gitignore +++ /dev/null @@ -1 +0,0 @@ -src/main/resources/META-INF/imgui-java.properties \ No newline at end of file diff --git a/imgui-binding/build.gradle b/imgui-binding/build.gradle index 4b22cf5e..32677f8f 100644 --- a/imgui-binding/build.gradle +++ b/imgui-binding/build.gradle @@ -1,5 +1,4 @@ import tool.generator.GenerateLibs -import java.nio.file.Files plugins { id 'java' @@ -37,9 +36,9 @@ jar { } tasks.register('makePropertyResource') { - def directory = file "src/main/resources/META-INF" - Files.createDirectories(directory.toPath()) - def propertyFile = file "${project.projectDir}/src/main/resources/META-INF/imgui-java.properties" + 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) } From 7e49c3e3feee0fcb71243c3ba22b54849d93ae49 Mon Sep 17 00:00:00 2001 From: Kusaanko <39370373+kusaanko@users.noreply.github.com> Date: Tue, 29 Aug 2023 23:03:27 +0900 Subject: [PATCH 6/9] [API] Fix the path of imgui-java.properties --- imgui-binding/src/main/java/imgui/ImGui.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/imgui-binding/src/main/java/imgui/ImGui.java b/imgui-binding/src/main/java/imgui/ImGui.java index 0506e051..5917ad1f 100644 --- a/imgui-binding/src/main/java/imgui/ImGui.java +++ b/imgui-binding/src/main/java/imgui/ImGui.java @@ -143,7 +143,7 @@ private static String tryLoadFromClasspath(final String fullLibName) { private static String getVersionString() { final Properties properties = new Properties(); - try (InputStream is = ImGui.class.getResourceAsStream("/META-INF/imgui-java.properties")) { + try (InputStream is = ImGui.class.getResourceAsStream("/imgui/imgui-java.properties")) { if (is != null) { properties.load(is); return properties.get("version").toString(); From 60a4dd0847cea9ae40b88e7ae4ec136ac478a83a Mon Sep 17 00:00:00 2001 From: Kusaanko <39370373+kusaanko@users.noreply.github.com> Date: Fri, 1 Sep 2023 17:51:21 +0900 Subject: [PATCH 7/9] [API] Change the order of loading native library 1.imgui.library.path 2.java.library.path 3.extract from resources --- imgui-binding/src/main/java/imgui/ImGui.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/imgui-binding/src/main/java/imgui/ImGui.java b/imgui-binding/src/main/java/imgui/ImGui.java index 5917ad1f..cf6cbb95 100644 --- a/imgui-binding/src/main/java/imgui/ImGui.java +++ b/imgui-binding/src/main/java/imgui/ImGui.java @@ -55,10 +55,18 @@ public class ImGui { if (libPath != null) { System.load(Paths.get(libPath).resolve(fullLibName).toFile().getAbsolutePath()); - } else if (extractedLibAbsPath != null) { - System.load(extractedLibAbsPath); } else { - System.loadLibrary(libName); + try { + System.loadLibrary(libName); + } catch (Exception | Error e) { + if (extractedLibAbsPath != null) { + System.out.println("Extract"); + System.load(extractedLibAbsPath); + e.printStackTrace(); + } else { + throw e; + } + } } IMGUI_CONTEXT = new ImGuiContext(0); From 59ecb4a4c80c7a6c590fb67d65b84d5558b263d6 Mon Sep 17 00:00:00 2001 From: Kusaanko <39370373+kusaanko@users.noreply.github.com> Date: Thu, 7 Sep 2023 19:16:19 +0900 Subject: [PATCH 8/9] [API] Remove debugging codes --- imgui-binding/src/main/java/imgui/ImGui.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/imgui-binding/src/main/java/imgui/ImGui.java b/imgui-binding/src/main/java/imgui/ImGui.java index cf6cbb95..45bec9cc 100644 --- a/imgui-binding/src/main/java/imgui/ImGui.java +++ b/imgui-binding/src/main/java/imgui/ImGui.java @@ -60,9 +60,7 @@ public class ImGui { System.loadLibrary(libName); } catch (Exception | Error e) { if (extractedLibAbsPath != null) { - System.out.println("Extract"); System.load(extractedLibAbsPath); - e.printStackTrace(); } else { throw e; } From b267e9b5d4ba63e7ff69ff1dfa5f205e89ce6336 Mon Sep 17 00:00:00 2001 From: Kusaanko <39370373+kusaanko@users.noreply.github.com> Date: Thu, 7 Sep 2023 19:22:12 +0900 Subject: [PATCH 9/9] [API] Move extraction code before it is loaded --- imgui-binding/src/main/java/imgui/ImGui.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/imgui-binding/src/main/java/imgui/ImGui.java b/imgui-binding/src/main/java/imgui/ImGui.java index 45bec9cc..606c3dba 100644 --- a/imgui-binding/src/main/java/imgui/ImGui.java +++ b/imgui-binding/src/main/java/imgui/ImGui.java @@ -51,14 +51,13 @@ 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()); + System.load(Paths.get(libPath).resolve(fullLibName).toAbsolutePath().toString()); } else { try { System.loadLibrary(libName); } catch (Exception | Error e) { + final String extractedLibAbsPath = tryLoadFromClasspath(fullLibName); if (extractedLibAbsPath != null) { System.load(extractedLibAbsPath); } else {