fix(api): native loading by stopping creating tmpDir every time (#197)

* [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
This commit is contained in:
Kusaanko authored and GitHub committed 2023-09-18 09:19:01 +03:00
1 parent f9a152d49f
commit c46830a3b7
2 files changed
+59 -12

No files matched your search

+16
View File
@@ -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
}
+43 -12
View File
@@ -18,16 +18,18 @@ import java.io.IOException;
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 @@ public class ImGui {
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.).