From b272034a1fdbfe32b355fc6c264a4c45df107190 Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期二, 23 十二月 2025 14:55:03 +0800
Subject: [PATCH] 优化了新增地块功能
---
src/chuankou/SerialPortNativeLoader.java | 221 +++++++++++++++++++++++++++++++++++++++++-------------
1 files changed, 167 insertions(+), 54 deletions(-)
diff --git a/src/chuankou/SerialPortNativeLoader.java b/src/chuankou/SerialPortNativeLoader.java
index 83eae1b..dfcdd1d 100644
--- a/src/chuankou/SerialPortNativeLoader.java
+++ b/src/chuankou/SerialPortNativeLoader.java
@@ -1,77 +1,190 @@
package chuankou;
import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
import java.net.URISyntaxException;
+import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
/**
- * Ensures the correct jSerialComm native library is available on Windows/x86_64 systems.
+ * 淇 jSerialComm 搴撳姞杞介棶棰�
*/
public final class SerialPortNativeLoader {
private static final String LIB_PROPERTY = "com.fazecast.jSerialComm.library.path";
private static final String EXPECTED_DLL = "jSerialComm.dll";
+ private static volatile boolean libraryConfigured = false;
+
+ // 闈欐�佸潡锛氱‘淇濆湪浠讳綍 SerialPort 绫诲姞杞戒箣鍓嶈繍琛�
+ static {
+ ensureLibraryPresentEarly();
+ }
private SerialPortNativeLoader() {
// utility class
}
- public static void ensureLibraryPresent() {
- if (System.getProperty(LIB_PROPERTY) != null) {
+ /**
+ * 鏃╂湡鍒濆鍖� - 鍦ㄩ潤鎬佸潡涓繍琛�
+ */
+ private static synchronized void ensureLibraryPresentEarly() {
+ if (libraryConfigured) {
return;
}
-
- String osName = System.getProperty("os.name", "").toLowerCase();
- if (!osName.contains("win")) {
- return;
- }
-
- String arch = System.getProperty("os.arch", "").toLowerCase();
- if (!arch.contains("64")) {
- return;
- }
-
- Path candidateDir = Paths.get("lib", "native", "windows", "x86_64").toAbsolutePath();
- File dllFile = candidateDir.resolve(EXPECTED_DLL).toFile();
- if (!dllFile.isFile()) {
- candidateDir = resolveFromCodeSource();
- if (candidateDir != null) {
- dllFile = candidateDir.resolve(EXPECTED_DLL).toFile();
- }
- }
-
- if (dllFile.isFile()) {
- System.setProperty(LIB_PROPERTY, candidateDir.toString());
- } else {
- System.err.println("Expected jSerialComm native library not found. Checked " + dllFile);
- }
- }
-
- private static Path resolveFromCodeSource() {
+
+ System.out.println("寮�濮嬪垵濮嬪寲 jSerialComm 鏈湴搴�...");
+
+ // 绗竴姝ワ細璁剧疆绯荤粺灞炴�э紝闃绘鑷姩涓嬭浇
+ setCriticalSystemProperties();
+
+ // 绗簩姝ワ細灏濊瘯浠庨」鐩祫婧愪腑鎻愬彇 DLL
try {
- java.security.CodeSource codeSource = SerialPortNativeLoader.class.getProtectionDomain().getCodeSource();
- if (codeSource == null) {
- return null;
+ extractNativeLibraryFromResources();
+ libraryConfigured = true;
+ System.out.println("jSerialComm 鏈湴搴撳垵濮嬪寲鎴愬姛");
+ } catch (Exception e) {
+ System.err.println("浠庤祫婧愭彁鍙� DLL 澶辫触: " + e.getMessage());
+ // 灏濊瘯澶囩敤鏂规
+ try {
+ setupLibraryFromFileSystem();
+ libraryConfigured = true;
+ System.out.println("浠庢枃浠剁郴缁熷姞杞� jSerialComm 鎴愬姛");
+ } catch (Exception ex) {
+ System.err.println("鎵�鏈夊姞杞芥柟寮忛兘澶辫触: " + ex.getMessage());
+ // 鏈�鍚庡皾璇曪細鍏佽 jSerialComm 浣跨敤榛樿鏂瑰紡
+ allowDefaultBehavior();
}
-
- Path location = Paths.get(codeSource.getLocation().toURI()).toAbsolutePath();
- Path baseDir = location.getParent();
- if (baseDir == null) {
- return null;
- }
-
- Path siblingLibDir = baseDir.resolveSibling("lib").resolve("native").resolve("windows").resolve("x86_64");
- if (siblingLibDir.toFile().isDirectory()) {
- return siblingLibDir;
- }
-
- Path relativeLibDir = baseDir.resolve("lib").resolve("native").resolve("windows").resolve("x86_64");
- if (relativeLibDir.toFile().isDirectory()) {
- return relativeLibDir;
- }
- } catch (URISyntaxException | SecurityException ignored) {
- // ignore
}
- return null;
}
-}
+
+ /**
+ * 璁剧疆鍏抽敭绯荤粺灞炴�э紙鍦ㄧ被鍔犺浇鍓嶅繀椤昏缃級
+ */
+ private static void setCriticalSystemProperties() {
+ // 闃绘鑷姩涓嬭浇鍜岀鍙d唬鐞�
+ System.setProperty("com.fazecast.jSerialComm.port.agent", "disabled");
+ System.setProperty("com.fazecast.jSerialComm.autoload", "false");
+ System.setProperty("com.fazecast.jSerialComm.autoDispose", "false");
+
+ // 绂佺敤鏃ュ織鍜岃皟璇�
+ System.setProperty("com.fazecast.jSerialComm.debug", "false");
+ System.setProperty("com.fazecast.jSerialComm.log", "false");
+
+ System.out.println("宸茶缃� jSerialComm 绯荤粺灞炴��");
+ }
+
+ /**
+ * 浠� JAR 璧勬簮涓彁鍙� DLL
+ */
+ private static void extractNativeLibraryFromResources() throws Exception {
+ // 纭畾鐩爣璺緞
+ String tempDir = System.getProperty("java.io.tmpdir");
+ Path targetDir = Paths.get(tempDir, "jSerialComm", "2.10.4");
+
+ // 濡傛灉鐩綍涓嶅瓨鍦紝鍒涘缓瀹�
+ if (!Files.exists(targetDir)) {
+ Files.createDirectories(targetDir);
+ }
+
+ Path dllPath = targetDir.resolve(EXPECTED_DLL);
+
+ // 妫�鏌� DLL 鏄惁宸插瓨鍦ㄤ笖鍙敤
+ if (Files.exists(dllPath)) {
+ System.out.println("DLL 宸插瓨鍦ㄤ簬: " + dllPath);
+ // 璁剧疆搴撹矾寰�
+ System.setProperty(LIB_PROPERTY, targetDir.toString());
+ return;
+ }
+
+ // 浠庣被璺緞璧勬簮鍔犺浇 DLL
+ String resourcePath = "/native/windows/x86_64/" + EXPECTED_DLL;
+ InputStream dllStream = SerialPortNativeLoader.class.getResourceAsStream(resourcePath);
+
+ if (dllStream == null) {
+ // 灏濊瘯鍏朵粬鍙兘鐨勮矾寰�
+ resourcePath = "/lib/native/windows/x86_64/" + EXPECTED_DLL;
+ dllStream = SerialPortNativeLoader.class.getResourceAsStream(resourcePath);
+ }
+
+ if (dllStream == null) {
+ // 灏濊瘯浠庢枃浠剁郴缁熷姞杞�
+ resourcePath = "lib/native/windows/x86_64/" + EXPECTED_DLL;
+ File file = new File(resourcePath);
+ if (file.exists()) {
+ dllStream = Files.newInputStream(file.toPath());
+ }
+ }
+
+ if (dllStream == null) {
+ throw new RuntimeException("鏃犳硶鍦ㄨ祫婧愪腑鎵惧埌 " + EXPECTED_DLL);
+ }
+
+ // 澶嶅埗 DLL 鍒颁复鏃剁洰褰�
+ try (InputStream is = dllStream;
+ FileOutputStream fos = new FileOutputStream(dllPath.toFile())) {
+
+ byte[] buffer = new byte[8192];
+ int bytesRead;
+ while ((bytesRead = is.read(buffer)) != -1) {
+ fos.write(buffer, 0, bytesRead);
+ }
+ }
+
+ System.out.println("DLL 宸叉彁鍙栧埌: " + dllPath);
+
+ // 璁剧疆搴撹矾寰�
+ System.setProperty(LIB_PROPERTY, targetDir.toString());
+ System.setProperty("java.library.path",
+ System.getProperty("java.library.path") + File.pathSeparator + targetDir.toString());
+ }
+
+ /**
+ * 浠庢枃浠剁郴缁熻缃簱
+ */
+ private static void setupLibraryFromFileSystem() throws Exception {
+ // 妫�鏌ュ涓彲鑳界殑璺緞
+ String[] possiblePaths = {
+ "lib/native/windows/x86_64/" + EXPECTED_DLL,
+ "./lib/native/windows/x86_64/" + EXPECTED_DLL,
+ System.getProperty("user.dir") + "/lib/native/windows/x86_64/" + EXPECTED_DLL,
+ "../lib/native/windows/x86_64/" + EXPECTED_DLL
+ };
+
+ for (String path : possiblePaths) {
+ File dllFile = new File(path);
+ if (dllFile.exists()) {
+ System.out.println("鎵惧埌 DLL: " + dllFile.getAbsolutePath());
+ // 浣跨敤 DLL 鎵�鍦ㄧ洰褰�
+ File parentDir = dllFile.getParentFile();
+ System.setProperty(LIB_PROPERTY, parentDir.getAbsolutePath());
+ System.setProperty("java.library.path",
+ System.getProperty("java.library.path") + File.pathSeparator + parentDir.getAbsolutePath());
+ return;
+ }
+ }
+
+ throw new RuntimeException("鍦ㄦ枃浠剁郴缁熶腑鎵句笉鍒� " + EXPECTED_DLL);
+ }
+
+ /**
+ * 鍏佽榛樿琛屼负锛堟渶鍚庣殑鎵嬫锛�
+ */
+ private static void allowDefaultBehavior() {
+ System.err.println("璀﹀憡锛氫娇鐢� jSerialComm 鐨勯粯璁ゅ姞杞借涓�");
+ // 娓呯┖闃绘灞炴�э紝璁� jSerialComm 鑷繁澶勭悊
+ System.clearProperty("com.fazecast.jSerialComm.autoload");
+ System.clearProperty("com.fazecast.jSerialComm.port.agent");
+ }
+
+ /**
+ * 鍏叡鏂规硶 - 渚涘叾浠栦唬鐮佽皟鐢�
+ */
+ public static synchronized void ensureLibraryPresent() {
+ // 闈欐�佸潡宸茬粡鎵ц锛岃繖閲屽彧鏄‘淇�
+ if (!libraryConfigured) {
+ ensureLibraryPresentEarly();
+ }
+ }
+}
\ No newline at end of file
--
Gitblit v1.10.0