Add an ability to load binaries from classpath

This commit is contained in:
SpaiR committed 2020-01-19 14:54:04 +03:00
1 parent f1e5464d51
commit c259aaa8b6
5 files changed
+118 -21

No files matched your search

+57
View File
@@ -0,0 +1,57 @@
plugins {
id 'java'
id 'com.jfrog.bintray' version '1.8.4'
id 'maven-publish'
}
task sourcesJar(type: Jar, dependsOn: classes) {
archiveClassifier.set('sources')
from sourceSets.main.allSource
}
def packageName = 'natives-linux'
def libName = 'libimgui-java64.so'
if (System.getProperty('deployWin32')) {
packageName = 'natives-windows-x86'
libName = 'imgui-java.dll'
} else if (System.getProperty('deployWin64')) {
packageName = 'natives-windows'
libName = 'imgui-java64.dll'
} else if (System.getProperty('deployLinux32')) {
packageName = 'natives-linux-x86'
libName = 'libimgui-java.so'
} else if (System.getProperty('deployLinux64')) {
packageName = 'natives-linux'
libName = 'libimgui-java64.so'
}
jar {
from('../bin') {
include "$libName" // it's ok
into 'io/imgui/java/native-bin/'
}
}
publishing {
publications {
MyPublication(MavenPublication) {
from components.java
groupId 'io.imgui.java'
artifactId packageName
artifact sourcesJar
version property('version')
}
}
}
bintray {
user = System.getenv('BINTRAY_USER')
key = System.getenv('BINTRAY_API_KEY')
publications = ['MyPublication']
override = true
pkg {
repo = 'io.imgui.java'
name = packageName
}
}
-9
View File
@@ -52,14 +52,5 @@ bintray {
pkg {
repo = 'io.imgui.java'
name = 'binding'
userOrg = user
desc = 'A handcrafted/generated Java binding for the Dear-ImGui'
licenses = ['Apache-2.0']
websiteUrl = 'https://github.com/SpaiR/imgui-java'
issueTrackerUrl = 'https://github.com/SpaiR/imgui-java/issues'
vcsUrl = 'https://github.com/SpaiR/imgui-java.git'
labels = ['dear-imgui', 'java', 'jni']
githubRepo = 'SpaiR/imgui-java'
publicDownloadNumbers = true
}
}
+60 -3
View File
@@ -2,11 +2,20 @@ package imgui;
import imgui.enums.ImGuiInputTextFlags;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public final 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-bin_" + System.getProperty("user.name", "user");
private static final ImDrawData DRAW_DATA = new ImDrawData(100_000, 100_000, 1000);
private static final ImGuiIO IMGUI_IO = new ImGuiIO();
@@ -17,13 +26,61 @@ public final class ImGui {
private static final ImDrawList IM_DRAW_LIST_FOREGROUND = new ImDrawList(ImDrawList.TYPE_FOREGROUND);
static {
System.loadLibrary(System.getProperty(LIB_NAME_PROP, LIB_NAME_DEFAULT));
initJni();
final String libPath = System.getProperty(LIB_PATH_PROP);
final String libName = System.getProperty(LIB_NAME_PROP, LIB_NAME_DEFAULT);
final String fullLibName = resolveFullLibName();
final String extractedLibAbsPath = tryLoadFromClasspath(fullLibName);
if (extractedLibAbsPath != null) {
System.load(extractedLibAbsPath);
} else if (libPath != null) {
System.load(Paths.get(libPath).resolve(fullLibName).toFile().getAbsolutePath());
} else {
System.loadLibrary(libName);
}
nInitJni();
ImDrawList.nInit();
ImDrawData.nInit();
nInitInputTextData();
}
private static String resolveFullLibName() {
final boolean isWin = System.getProperty("os.name").toLowerCase().contains("win");
final String libPrefix;
final String libSuffix;
if (isWin) {
libPrefix = "";
libSuffix = ".dll";
} else { // Only linux as an alternative OS
libPrefix = "lib";
libSuffix = ".so";
}
return System.getProperty(LIB_NAME_PROP, libPrefix + LIB_NAME_DEFAULT + libSuffix);
}
// This method tries to unpack the library binary from classpath into the temp dir.
private static String tryLoadFromClasspath(final String fullLibName) {
try (InputStream is = ImGui.class.getClassLoader().getResourceAsStream("io/imgui/java/native-bin/" + fullLibName)) {
if (is == null) {
return null;
}
final Path tmpDir = Paths.get(System.getProperty("java.io.tmpdir")).resolve(LIB_TMP_DIR_PREFIX);
tmpDir.toFile().mkdirs();
final Path libBin = tmpDir.resolve(fullLibName);
Files.copy(is, libBin, StandardCopyOption.REPLACE_EXISTING);
return libBin.toFile().getAbsolutePath();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
private ImGui() {
}
@@ -34,7 +91,7 @@ public final class ImGui {
#include "jni_callbacks.h"
*/
private static native void initJni(); /*
private static native void nInitJni(); /*
Jni::InitCommon(env);
Jni::InitCallbacks(env);
*/
-9
View File
@@ -63,14 +63,5 @@ bintray {
pkg {
repo = 'io.imgui.java'
name = 'lwjgl3'
userOrg = user
desc = 'Backend renderer for imgui-java based on LWJGL3'
licenses = ['Apache-2.0']
websiteUrl = 'https://github.com/SpaiR/imgui-java'
issueTrackerUrl = 'https://github.com/SpaiR/imgui-java/issues'
vcsUrl = 'https://github.com/SpaiR/imgui-java.git'
labels = ['dear-imgui', 'java', 'lwjgl3', 'opengl']
githubRepo = 'SpaiR/imgui-java'
publicDownloadNumbers = true
}
}
+1
View File
@@ -1,4 +1,5 @@
rootProject.name = 'imgui-java'
include 'imgui-binding'
include 'imgui-lwjgl3'
include 'imgui-binding-natives'