Skip to content

Commit

Permalink
fix(api): native loading by stopping creating tmpDir every time (#197)
Browse files Browse the repository at this point in the history
* [API] Stop creating tmpDir every time

* [API] Add a whitespace after catch

* [API] Fix getting imgui-java version

* [API] Add final to properties variable

* [API] Make it brief to generate properties

* [API] Fix the path of imgui-java.properties

* [API] Change the order of loading native library

1.imgui.library.path
2.java.library.path
3.extract from resources

* [API] Remove debugging codes

* [API] Move extraction code before it is loaded
  • Loading branch information
kusaanko authored Sep 18, 2023
1 parent f9a152d commit c46830a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 12 deletions.
16 changes: 16 additions & 0 deletions imgui-binding/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
55 changes: 43 additions & 12 deletions imgui-binding/src/main/java/imgui/ImGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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.).
Expand Down

0 comments on commit c46830a

Please sign in to comment.