1818import java .io .InputStream ;
1919import java .io .UncheckedIOException ;
2020import java .lang .ref .WeakReference ;
21+ import java .nio .file .AccessDeniedException ;
2122import java .nio .file .Files ;
2223import java .nio .file .Path ;
2324import java .nio .file .Paths ;
2425import java .nio .file .StandardCopyOption ;
26+ import java .util .Properties ;
2527
2628public 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