Skip to content

Commit f6a130a

Browse files
committed
add native lib loader
1 parent 989d707 commit f6a130a

File tree

8 files changed

+172
-2
lines changed

8 files changed

+172
-2
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*.zip
2020
*.tar.gz
2121
*.rar
22-
22+
test
2323
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2424
hs_err_pid*
2525

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dev.felnull.fnjl;
22

33
public class FNJLBuildIn {
4-
protected static final String VERSION = "1.31";
4+
protected static final String VERSION = "1.32";
55

66
protected static final int NATIVE_LIB_VERSION = 3;
77
}

common/src/main/java/dev/felnull/fnjl/os/OSs.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,23 @@ public static boolean isArm32() {
104104
return "arm".equalsIgnoreCase(getOSArch());
105105
}
106106

107+
/**
108+
* アーキテクスチャ名を取得
109+
*
110+
* @return アーキテクスチャ名
111+
*/
112+
public static String getArch() {
113+
if (isX64())
114+
return "x64";
115+
if (isX86())
116+
return "x86";
117+
if (isArm64())
118+
return "arm64";
119+
if (isArm32())
120+
return "arm32";
121+
return "no-support";
122+
}
123+
107124
public static enum Type {
108125
WINDOWS("windows", "dll"), LINUX("linux", "so"), MAC("mac", "jnilib"), OTHER(null, null);
109126
private final String name;

common/src/main/java/dev/felnull/fnjl/util/FNDataUtil.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,5 @@ public static byte[] urlLoadToProgress(URL url, Consumer<ProgressWriter.WritePro
181181
public static byte[] fileLoadToProgress(File file, Consumer<ProgressWriter.WriteProgressListener> progress) throws IOException {
182182
return loadToProgress(new FileInputStream(file), file.length(), progress);
183183
}
184+
184185
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package dev.felnull.fnjln;
2+
3+
public class FNJLNBuildIn {
4+
protected static final String VERSION = "1.32";
5+
6+
protected static final int NATIVE_LIBRARY_VERSION = 1;
7+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package dev.felnull.fnjln;
2+
3+
import dev.felnull.fnjl.os.OSs;
4+
import dev.felnull.fnjl.util.FNDataUtil;
5+
6+
import java.io.BufferedInputStream;
7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
12+
import java.nio.file.Paths;
13+
14+
/**
15+
* FelNullJavaLibraryのNativeライブラリ
16+
*
17+
* @author MORIMORI0317
18+
* @since 1.32
19+
*/
20+
public class FelNullJavaLibraryNative {
21+
private static boolean init;
22+
private static boolean loaded;
23+
24+
/**
25+
* Nativeライブラリを利用する前に最初に一度呼び出してください。
26+
*/
27+
public synchronized static void init() {
28+
init(Paths.get("."));
29+
}
30+
31+
/**
32+
* Nativeライブラリを利用する前に最初に一度呼び出してください。
33+
*
34+
* @param libraryPath ネイティブライブラリを展開するフォルダ
35+
*/
36+
public synchronized static void init(Path libraryPath) {
37+
init("dev/felnull/fnjln/natives/", libraryPath);
38+
}
39+
40+
/**
41+
* Nativeライブラリを利用する前に最初に一度呼び出してください。
42+
*
43+
* @param libraryLocation ライブラリが格納されているresourceバス
44+
* @param libraryPath ネイティブライブラリを展開するフォルダ
45+
*/
46+
public synchronized static void init(String libraryLocation, Path libraryPath) {
47+
if (init) return;
48+
OSs.Type os = OSs.getOS();
49+
String arch = OSs.getArch();
50+
String libName = String.format("FNJLNative%s.%s", arch, os.getLibName());
51+
String outLibName = String.format("FNJLNative%s%s.%s", getNativeLibraryVersion(), arch, os.getLibName());
52+
File outLibFIle = libraryPath.resolve(outLibName).toFile();
53+
54+
if (outLibFIle.exists() && loadLibrary(outLibFIle)) {
55+
loaded = true;
56+
init = true;
57+
return;
58+
}
59+
60+
InputStream stream = loadResource(libraryLocation + libName);
61+
if (stream != null) {
62+
stream = new BufferedInputStream(stream);
63+
try {
64+
byte[] data = FNDataUtil.streamToByteArray(stream);
65+
outLibFIle.delete();
66+
Files.write(outLibFIle.toPath(), data);
67+
loaded = loadLibrary(outLibFIle);
68+
} catch (IOException ex) {
69+
ex.printStackTrace();
70+
}
71+
}
72+
init = true;
73+
}
74+
75+
private static boolean loadLibrary(File file) {
76+
try {
77+
System.loadLibrary(file.getAbsolutePath());
78+
return true;
79+
} catch (UnsatisfiedLinkError er) {
80+
return false;
81+
}
82+
}
83+
84+
private static InputStream loadResource(String path) {
85+
InputStream stream = FelNullJavaLibraryNative.class.getResourceAsStream("/" + path);
86+
if (stream == null)
87+
stream = ClassLoader.getSystemResourceAsStream(path);
88+
return stream;
89+
}
90+
91+
/**
92+
* 初期化済みかどうか
93+
*
94+
* @return 初期化済みか
95+
*/
96+
public static boolean isInitialized() {
97+
return init;
98+
}
99+
100+
/**
101+
* 初期化し、読み込み完了したかどうか
102+
* 初期化後にFalseの場合ライブラリの読み込み失敗または非対応のOS、アーキテクスチャです
103+
*
104+
* @return ライブラリ読み込み成功
105+
*/
106+
public static boolean isLoadedSuccess() {
107+
return init && loaded;
108+
}
109+
110+
/**
111+
* ライブラリが読み込み済みかどうか
112+
*
113+
* @return ライブラリが読み込み済みか
114+
*/
115+
public static boolean isLoaded() {
116+
return loaded;
117+
}
118+
119+
/**
120+
* バージョン取得
121+
*
122+
* @return FelNullJavaLibrary-Nativeのバージョン
123+
*/
124+
public static String getVersion() {
125+
return FNJLNBuildIn.VERSION;
126+
}
127+
128+
/**
129+
* ネイティブライブラリのバージョン
130+
*
131+
* @return バージョン
132+
*/
133+
public static int getNativeLibraryVersion() {
134+
return FNJLNBuildIn.NATIVE_LIBRARY_VERSION;
135+
}
136+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package dev.felnull.fnjln.jni.windows;
2+
3+
public class WindowsNative {
4+
protected static native String getSpecialFolderPath(int num);
5+
6+
protected static native String getSystemFontFaceName(int num);
7+
8+
protected static native byte[] getOpenFileName(long hwndId, String title, String initDir, String initName, String defExt, String filter, int filterIndex, int flags);
9+
}
Binary file not shown.

0 commit comments

Comments
 (0)