From 54f8be467d53ef53e07bc045ba15b8c12340a3f1 Mon Sep 17 00:00:00 2001 From: Glavo Date: Thu, 29 May 2025 11:53:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20Linux=20=E5=B9=B3=E5=8F=B0?= =?UTF-8?q?=E7=A1=AC=E4=BB=B6=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hmcl/util/platform/hardware/FastFetchUtils.java | 12 ++++++++---- .../util/platform/hardware/HardwareDetector.java | 6 ++++-- .../util/platform/linux/LinuxHardwareDetector.java | 11 +++++++++-- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/hardware/FastFetchUtils.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/hardware/FastFetchUtils.java index 3c5cd24f02..8612a15a72 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/hardware/FastFetchUtils.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/hardware/FastFetchUtils.java @@ -41,13 +41,13 @@ private FastFetchUtils() { } private static T get(String type, TypeToken resultType) { - Path fastfetch = SystemUtils.which("fastfetch"); + Path fastfetch = SystemUtils.which(OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS ? "fastfetch.exe" : "fastfetch"); if (fastfetch == null) return null; - String json; + String output; try { - json = SystemUtils.run(Arrays.asList(fastfetch.toString(), "--structure", type, "--format", "json"), + output = SystemUtils.run(Arrays.asList(fastfetch.toString(), "--structure", type, "--format", "json"), inputStream -> IOUtils.readFullyAsString(inputStream, OperatingSystem.NATIVE_CHARSET)); } catch (Throwable e) { LOG.warning("Failed to get result from fastfetch", e); @@ -55,6 +55,10 @@ private static T get(String type, TypeToken resultType) { } try { + // Sometimes there is some garbage before the output JSON, we should filter it out + int idx = output.indexOf('['); + String json = idx >= 0 ? output.substring(idx) : output; + List> list = JsonUtils.GSON.fromJson(json, JsonUtils.listTypeOf(Result.typeOf(resultType))); Result result; @@ -68,7 +72,7 @@ private static T get(String type, TypeToken resultType) { return result.result; } catch (Throwable e) { - LOG.warning("Failed to parse fastfetch output: " + json, e); + LOG.warning("Failed to parse fastfetch output: " + output, e); return null; } } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/hardware/HardwareDetector.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/hardware/HardwareDetector.java index 644796e82e..206328b9c4 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/hardware/HardwareDetector.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/hardware/HardwareDetector.java @@ -28,12 +28,14 @@ */ @SuppressWarnings("ALL") public class HardwareDetector { + private static final boolean USE_FAST_FETCH = "true".equalsIgnoreCase(System.getProperty("hmcl.hardware.fastfetch", "true")); + public @Nullable CentralProcessor detectCentralProcessor() { - return FastFetchUtils.detectCentralProcessor(); + return USE_FAST_FETCH ? FastFetchUtils.detectCentralProcessor() : null; } public @Nullable List detectGraphicsCards() { - return FastFetchUtils.detectGraphicsCards(); + return USE_FAST_FETCH ? FastFetchUtils.detectGraphicsCards() : null; } public long getTotalMemorySize() { diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/linux/LinuxHardwareDetector.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/linux/LinuxHardwareDetector.java index 486c78da1a..bbf30f7458 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/linux/LinuxHardwareDetector.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/linux/LinuxHardwareDetector.java @@ -43,14 +43,21 @@ public final class LinuxHardwareDetector extends HardwareDetector { public @Nullable CentralProcessor detectCentralProcessor() { if (OperatingSystem.CURRENT_OS != OperatingSystem.LINUX) return null; - return LinuxCPUDetector.detect(); + CentralProcessor cpu = LinuxCPUDetector.detect(); + return cpu != null ? cpu : super.detectCentralProcessor(); } @Override public List detectGraphicsCards() { if (OperatingSystem.CURRENT_OS != OperatingSystem.LINUX) return null; - return LinuxGPUDetector.detect(); + List cards = LinuxGPUDetector.detect(); + if (cards == null || cards.isEmpty()) { + List fastfetchResults = super.detectGraphicsCards(); + if (fastfetchResults != null) // Avoid overwriting empty lists with null + cards = fastfetchResults; + } + return cards; } private static final Path MEMINFO = Paths.get("/proc/meminfo");