Skip to content

Commit c46830a

Browse files
authored
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
1 parent f9a152d commit c46830a

2 files changed

Lines changed: 59 additions & 12 deletions

File tree

imgui-binding/build.gradle

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,19 @@ jar {
3434
attributes 'Automatic-Module-Name': 'imgui.binding'
3535
}
3636
}
37+
38+
tasks.register('makePropertyResource') {
39+
def directory = file "${buildDir}/resources/main/imgui/"
40+
directory.mkdirs()
41+
def propertyFile = file "${directory}/imgui-java.properties"
42+
def props = new Properties()
43+
if (propertyFile.exists()) {
44+
propertyFile.withReader { props.load(it) }
45+
}
46+
props.setProperty("version", project.version)
47+
propertyFile.withWriter { props.store(it, "imgui-java") }
48+
}
49+
50+
processResources {
51+
dependsOn makePropertyResource
52+
}

imgui-binding/src/main/java/imgui/ImGui.java

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,18 @@
1818
import java.io.InputStream;
1919
import java.io.UncheckedIOException;
2020
import java.lang.ref.WeakReference;
21+
import java.nio.file.AccessDeniedException;
2122
import java.nio.file.Files;
2223
import java.nio.file.Path;
2324
import java.nio.file.Paths;
2425
import java.nio.file.StandardCopyOption;
26+
import java.util.Properties;
2527

2628
public class ImGui {
2729
private static final String LIB_PATH_PROP = "imgui.library.path";
2830
private static final String LIB_NAME_PROP = "imgui.library.name";
2931
private static final String LIB_NAME_DEFAULT = System.getProperty("os.arch").contains("64") ? "imgui-java64" : "imgui-java";
30-
private static final String LIB_TMP_DIR_PREFIX = "imgui-java-natives_" + System.currentTimeMillis();
32+
private static final String LIB_TMP_DIR_PREFIX = "imgui-java-natives";
3133

3234
private static final ImGuiContext IMGUI_CONTEXT;
3335
private static final ImGuiIO IMGUI_IO;
@@ -49,14 +51,19 @@ public class ImGui {
4951
final String libName = System.getProperty(LIB_NAME_PROP, LIB_NAME_DEFAULT);
5052
final String fullLibName = resolveFullLibName();
5153

52-
final String extractedLibAbsPath = tryLoadFromClasspath(fullLibName);
53-
5454
if (libPath != null) {
55-
System.load(Paths.get(libPath).resolve(fullLibName).toFile().getAbsolutePath());
56-
} else if (extractedLibAbsPath != null) {
57-
System.load(extractedLibAbsPath);
55+
System.load(Paths.get(libPath).resolve(fullLibName).toAbsolutePath().toString());
5856
} else {
59-
System.loadLibrary(libName);
57+
try {
58+
System.loadLibrary(libName);
59+
} catch (Exception | Error e) {
60+
final String extractedLibAbsPath = tryLoadFromClasspath(fullLibName);
61+
if (extractedLibAbsPath != null) {
62+
System.load(extractedLibAbsPath);
63+
} else {
64+
throw e;
65+
}
66+
}
6067
}
6168

6269
IMGUI_CONTEXT = new ImGuiContext(0);
@@ -115,19 +122,43 @@ private static String tryLoadFromClasspath(final String fullLibName) {
115122
return null;
116123
}
117124

118-
final Path tmpDir = Paths.get(System.getProperty("java.io.tmpdir")).resolve(LIB_TMP_DIR_PREFIX);
119-
tmpDir.toFile().mkdirs();
125+
String version = getVersionString();
126+
if (version == null) {
127+
version = "unknown";
128+
}
129+
final Path tmpDir = Paths.get(System.getProperty("java.io.tmpdir")).resolve(LIB_TMP_DIR_PREFIX).resolve(version);
130+
if (!Files.exists(tmpDir)) {
131+
Files.createDirectories(tmpDir);
132+
}
120133

121134
final Path libBin = tmpDir.resolve(fullLibName);
122-
Files.copy(is, libBin, StandardCopyOption.REPLACE_EXISTING);
123-
libBin.toFile().deleteOnExit();
135+
try {
136+
Files.copy(is, libBin, StandardCopyOption.REPLACE_EXISTING);
137+
} catch (AccessDeniedException e) {
138+
if (!Files.exists(libBin)) {
139+
throw e;
140+
}
141+
}
124142

125-
return libBin.toFile().getAbsolutePath();
143+
return libBin.toAbsolutePath().toString();
126144
} catch (IOException e) {
127145
throw new UncheckedIOException(e);
128146
}
129147
}
130148

149+
private static String getVersionString() {
150+
final Properties properties = new Properties();
151+
try (InputStream is = ImGui.class.getResourceAsStream("/imgui/imgui-java.properties")) {
152+
if (is != null) {
153+
properties.load(is);
154+
return properties.get("version").toString();
155+
}
156+
} catch (IOException e) {
157+
throw new UncheckedIOException(e);
158+
}
159+
return null;
160+
}
161+
131162
/**
132163
* For internal usage.
133164
* Method is used to initiate static instantiation (loading of the native libraries etc.).

0 commit comments

Comments
 (0)