Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API] Fix native loading by stopping creating tmpDir every time #197

Merged
merged 9 commits into from
Sep 18, 2023
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/"
SpaiR marked this conversation as resolved.
Show resolved Hide resolved
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
kusaanko marked this conversation as resolved.
Show resolved Hide resolved
}
38 changes: 32 additions & 6 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 Down Expand Up @@ -115,19 +117,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