Skip to content

Commit c259aaa

Browse files
committed
Add an ability to load binaries from classpath
1 parent f1e5464 commit c259aaa

File tree

5 files changed

+118
-21
lines changed

5 files changed

+118
-21
lines changed

imgui-binding-natives/build.gradle

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
plugins {
2+
id 'java'
3+
id 'com.jfrog.bintray' version '1.8.4'
4+
id 'maven-publish'
5+
}
6+
7+
task sourcesJar(type: Jar, dependsOn: classes) {
8+
archiveClassifier.set('sources')
9+
from sourceSets.main.allSource
10+
}
11+
12+
def packageName = 'natives-linux'
13+
def libName = 'libimgui-java64.so'
14+
15+
if (System.getProperty('deployWin32')) {
16+
packageName = 'natives-windows-x86'
17+
libName = 'imgui-java.dll'
18+
} else if (System.getProperty('deployWin64')) {
19+
packageName = 'natives-windows'
20+
libName = 'imgui-java64.dll'
21+
} else if (System.getProperty('deployLinux32')) {
22+
packageName = 'natives-linux-x86'
23+
libName = 'libimgui-java.so'
24+
} else if (System.getProperty('deployLinux64')) {
25+
packageName = 'natives-linux'
26+
libName = 'libimgui-java64.so'
27+
}
28+
29+
jar {
30+
from('../bin') {
31+
include "$libName" // it's ok
32+
into 'io/imgui/java/native-bin/'
33+
}
34+
}
35+
36+
publishing {
37+
publications {
38+
MyPublication(MavenPublication) {
39+
from components.java
40+
groupId 'io.imgui.java'
41+
artifactId packageName
42+
artifact sourcesJar
43+
version property('version')
44+
}
45+
}
46+
}
47+
48+
bintray {
49+
user = System.getenv('BINTRAY_USER')
50+
key = System.getenv('BINTRAY_API_KEY')
51+
publications = ['MyPublication']
52+
override = true
53+
pkg {
54+
repo = 'io.imgui.java'
55+
name = packageName
56+
}
57+
}

imgui-binding/build.gradle

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,5 @@ bintray {
5252
pkg {
5353
repo = 'io.imgui.java'
5454
name = 'binding'
55-
userOrg = user
56-
desc = 'A handcrafted/generated Java binding for the Dear-ImGui'
57-
licenses = ['Apache-2.0']
58-
websiteUrl = 'https://github.com/SpaiR/imgui-java'
59-
issueTrackerUrl = 'https://github.com/SpaiR/imgui-java/issues'
60-
vcsUrl = 'https://github.com/SpaiR/imgui-java.git'
61-
labels = ['dear-imgui', 'java', 'jni']
62-
githubRepo = 'SpaiR/imgui-java'
63-
publicDownloadNumbers = true
6455
}
6556
}

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

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,20 @@
22

33
import imgui.enums.ImGuiInputTextFlags;
44

5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.io.UncheckedIOException;
58
import java.nio.ByteBuffer;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
import java.nio.file.Paths;
12+
import java.nio.file.StandardCopyOption;
613

714
public final class ImGui {
15+
private static final String LIB_PATH_PROP = "imgui.library.path";
816
private static final String LIB_NAME_PROP = "imgui.library.name";
917
private static final String LIB_NAME_DEFAULT = System.getProperty("os.arch").contains("64") ? "imgui-java64" : "imgui-java";
18+
private static final String LIB_TMP_DIR_PREFIX = "imgui-java-bin_" + System.getProperty("user.name", "user");
1019

1120
private static final ImDrawData DRAW_DATA = new ImDrawData(100_000, 100_000, 1000);
1221
private static final ImGuiIO IMGUI_IO = new ImGuiIO();
@@ -17,13 +26,61 @@ public final class ImGui {
1726
private static final ImDrawList IM_DRAW_LIST_FOREGROUND = new ImDrawList(ImDrawList.TYPE_FOREGROUND);
1827

1928
static {
20-
System.loadLibrary(System.getProperty(LIB_NAME_PROP, LIB_NAME_DEFAULT));
21-
initJni();
29+
final String libPath = System.getProperty(LIB_PATH_PROP);
30+
final String libName = System.getProperty(LIB_NAME_PROP, LIB_NAME_DEFAULT);
31+
final String fullLibName = resolveFullLibName();
32+
33+
final String extractedLibAbsPath = tryLoadFromClasspath(fullLibName);
34+
35+
if (extractedLibAbsPath != null) {
36+
System.load(extractedLibAbsPath);
37+
} else if (libPath != null) {
38+
System.load(Paths.get(libPath).resolve(fullLibName).toFile().getAbsolutePath());
39+
} else {
40+
System.loadLibrary(libName);
41+
}
42+
43+
nInitJni();
2244
ImDrawList.nInit();
2345
ImDrawData.nInit();
2446
nInitInputTextData();
2547
}
2648

49+
private static String resolveFullLibName() {
50+
final boolean isWin = System.getProperty("os.name").toLowerCase().contains("win");
51+
final String libPrefix;
52+
final String libSuffix;
53+
54+
if (isWin) {
55+
libPrefix = "";
56+
libSuffix = ".dll";
57+
} else { // Only linux as an alternative OS
58+
libPrefix = "lib";
59+
libSuffix = ".so";
60+
}
61+
62+
return System.getProperty(LIB_NAME_PROP, libPrefix + LIB_NAME_DEFAULT + libSuffix);
63+
}
64+
65+
// This method tries to unpack the library binary from classpath into the temp dir.
66+
private static String tryLoadFromClasspath(final String fullLibName) {
67+
try (InputStream is = ImGui.class.getClassLoader().getResourceAsStream("io/imgui/java/native-bin/" + fullLibName)) {
68+
if (is == null) {
69+
return null;
70+
}
71+
72+
final Path tmpDir = Paths.get(System.getProperty("java.io.tmpdir")).resolve(LIB_TMP_DIR_PREFIX);
73+
tmpDir.toFile().mkdirs();
74+
75+
final Path libBin = tmpDir.resolve(fullLibName);
76+
Files.copy(is, libBin, StandardCopyOption.REPLACE_EXISTING);
77+
78+
return libBin.toFile().getAbsolutePath();
79+
} catch (IOException e) {
80+
throw new UncheckedIOException(e);
81+
}
82+
}
83+
2784
private ImGui() {
2885
}
2986

@@ -34,7 +91,7 @@ private ImGui() {
3491
#include "jni_callbacks.h"
3592
*/
3693

37-
private static native void initJni(); /*
94+
private static native void nInitJni(); /*
3895
Jni::InitCommon(env);
3996
Jni::InitCallbacks(env);
4097
*/

imgui-lwjgl3/build.gradle

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,5 @@ bintray {
6363
pkg {
6464
repo = 'io.imgui.java'
6565
name = 'lwjgl3'
66-
userOrg = user
67-
desc = 'Backend renderer for imgui-java based on LWJGL3'
68-
licenses = ['Apache-2.0']
69-
websiteUrl = 'https://github.com/SpaiR/imgui-java'
70-
issueTrackerUrl = 'https://github.com/SpaiR/imgui-java/issues'
71-
vcsUrl = 'https://github.com/SpaiR/imgui-java.git'
72-
labels = ['dear-imgui', 'java', 'lwjgl3', 'opengl']
73-
githubRepo = 'SpaiR/imgui-java'
74-
publicDownloadNumbers = true
7566
}
7667
}

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
rootProject.name = 'imgui-java'
22
include 'imgui-binding'
33
include 'imgui-lwjgl3'
4+
include 'imgui-binding-natives'
45

0 commit comments

Comments
 (0)