Skip to content

Commit 45a40ee

Browse files
authored
优化 Linux 平台硬件检测 (#3946)
1 parent 4a75f1d commit 45a40ee

3 files changed

Lines changed: 21 additions & 8 deletions

File tree

HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/hardware/FastFetchUtils.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,24 @@ private FastFetchUtils() {
4141
}
4242

4343
private static <T> T get(String type, TypeToken<T> resultType) {
44-
Path fastfetch = SystemUtils.which("fastfetch");
44+
Path fastfetch = SystemUtils.which(OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS ? "fastfetch.exe" : "fastfetch");
4545
if (fastfetch == null)
4646
return null;
4747

48-
String json;
48+
String output;
4949
try {
50-
json = SystemUtils.run(Arrays.asList(fastfetch.toString(), "--structure", type, "--format", "json"),
50+
output = SystemUtils.run(Arrays.asList(fastfetch.toString(), "--structure", type, "--format", "json"),
5151
inputStream -> IOUtils.readFullyAsString(inputStream, OperatingSystem.NATIVE_CHARSET));
5252
} catch (Throwable e) {
5353
LOG.warning("Failed to get result from fastfetch", e);
5454
return null;
5555
}
5656

5757
try {
58+
// Sometimes there is some garbage before the output JSON, we should filter it out
59+
int idx = output.indexOf('[');
60+
String json = idx >= 0 ? output.substring(idx) : output;
61+
5862
List<Result<T>> list = JsonUtils.GSON.fromJson(json, JsonUtils.listTypeOf(Result.typeOf(resultType)));
5963

6064
Result<T> result;
@@ -68,7 +72,7 @@ private static <T> T get(String type, TypeToken<T> resultType) {
6872

6973
return result.result;
7074
} catch (Throwable e) {
71-
LOG.warning("Failed to parse fastfetch output: " + json, e);
75+
LOG.warning("Failed to parse fastfetch output: " + output, e);
7276
return null;
7377
}
7478
}

HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/hardware/HardwareDetector.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@
2828
*/
2929
@SuppressWarnings("ALL")
3030
public class HardwareDetector {
31+
private static final boolean USE_FAST_FETCH = "true".equalsIgnoreCase(System.getProperty("hmcl.hardware.fastfetch", "true"));
32+
3133
public @Nullable CentralProcessor detectCentralProcessor() {
32-
return FastFetchUtils.detectCentralProcessor();
34+
return USE_FAST_FETCH ? FastFetchUtils.detectCentralProcessor() : null;
3335
}
3436

3537
public @Nullable List<GraphicsCard> detectGraphicsCards() {
36-
return FastFetchUtils.detectGraphicsCards();
38+
return USE_FAST_FETCH ? FastFetchUtils.detectGraphicsCards() : null;
3739
}
3840

3941
public long getTotalMemorySize() {

HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/linux/LinuxHardwareDetector.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,21 @@ public final class LinuxHardwareDetector extends HardwareDetector {
4343
public @Nullable CentralProcessor detectCentralProcessor() {
4444
if (OperatingSystem.CURRENT_OS != OperatingSystem.LINUX)
4545
return null;
46-
return LinuxCPUDetector.detect();
46+
CentralProcessor cpu = LinuxCPUDetector.detect();
47+
return cpu != null ? cpu : super.detectCentralProcessor();
4748
}
4849

4950
@Override
5051
public List<GraphicsCard> detectGraphicsCards() {
5152
if (OperatingSystem.CURRENT_OS != OperatingSystem.LINUX)
5253
return null;
53-
return LinuxGPUDetector.detect();
54+
List<GraphicsCard> cards = LinuxGPUDetector.detect();
55+
if (cards == null || cards.isEmpty()) {
56+
List<GraphicsCard> fastfetchResults = super.detectGraphicsCards();
57+
if (fastfetchResults != null) // Avoid overwriting empty lists with null
58+
cards = fastfetchResults;
59+
}
60+
return cards;
5461
}
5562

5663
private static final Path MEMINFO = Paths.get("/proc/meminfo");

0 commit comments

Comments
 (0)