mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-09-09 03:46:18 -04:00
优化 Linux 平台硬件检测 (#3946)
This commit is contained in:
parent
4a75f1d462
commit
45a40ee675
@ -41,13 +41,13 @@ final class FastFetchUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static <T> T get(String type, TypeToken<T> resultType) {
|
private static <T> T get(String type, TypeToken<T> resultType) {
|
||||||
Path fastfetch = SystemUtils.which("fastfetch");
|
Path fastfetch = SystemUtils.which(OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS ? "fastfetch.exe" : "fastfetch");
|
||||||
if (fastfetch == null)
|
if (fastfetch == null)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
String json;
|
String output;
|
||||||
try {
|
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));
|
inputStream -> IOUtils.readFullyAsString(inputStream, OperatingSystem.NATIVE_CHARSET));
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
LOG.warning("Failed to get result from fastfetch", e);
|
LOG.warning("Failed to get result from fastfetch", e);
|
||||||
@ -55,6 +55,10 @@ final class FastFetchUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
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<Result<T>> list = JsonUtils.GSON.fromJson(json, JsonUtils.listTypeOf(Result.typeOf(resultType)));
|
List<Result<T>> list = JsonUtils.GSON.fromJson(json, JsonUtils.listTypeOf(Result.typeOf(resultType)));
|
||||||
|
|
||||||
Result<T> result;
|
Result<T> result;
|
||||||
@ -68,7 +72,7 @@ final class FastFetchUtils {
|
|||||||
|
|
||||||
return result.result;
|
return result.result;
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
LOG.warning("Failed to parse fastfetch output: " + json, e);
|
LOG.warning("Failed to parse fastfetch output: " + output, e);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,12 +28,14 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
@SuppressWarnings("ALL")
|
@SuppressWarnings("ALL")
|
||||||
public class HardwareDetector {
|
public class HardwareDetector {
|
||||||
|
private static final boolean USE_FAST_FETCH = "true".equalsIgnoreCase(System.getProperty("hmcl.hardware.fastfetch", "true"));
|
||||||
|
|
||||||
public @Nullable CentralProcessor detectCentralProcessor() {
|
public @Nullable CentralProcessor detectCentralProcessor() {
|
||||||
return FastFetchUtils.detectCentralProcessor();
|
return USE_FAST_FETCH ? FastFetchUtils.detectCentralProcessor() : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public @Nullable List<GraphicsCard> detectGraphicsCards() {
|
public @Nullable List<GraphicsCard> detectGraphicsCards() {
|
||||||
return FastFetchUtils.detectGraphicsCards();
|
return USE_FAST_FETCH ? FastFetchUtils.detectGraphicsCards() : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getTotalMemorySize() {
|
public long getTotalMemorySize() {
|
||||||
|
@ -43,14 +43,21 @@ public final class LinuxHardwareDetector extends HardwareDetector {
|
|||||||
public @Nullable CentralProcessor detectCentralProcessor() {
|
public @Nullable CentralProcessor detectCentralProcessor() {
|
||||||
if (OperatingSystem.CURRENT_OS != OperatingSystem.LINUX)
|
if (OperatingSystem.CURRENT_OS != OperatingSystem.LINUX)
|
||||||
return null;
|
return null;
|
||||||
return LinuxCPUDetector.detect();
|
CentralProcessor cpu = LinuxCPUDetector.detect();
|
||||||
|
return cpu != null ? cpu : super.detectCentralProcessor();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<GraphicsCard> detectGraphicsCards() {
|
public List<GraphicsCard> detectGraphicsCards() {
|
||||||
if (OperatingSystem.CURRENT_OS != OperatingSystem.LINUX)
|
if (OperatingSystem.CURRENT_OS != OperatingSystem.LINUX)
|
||||||
return null;
|
return null;
|
||||||
return LinuxGPUDetector.detect();
|
List<GraphicsCard> cards = LinuxGPUDetector.detect();
|
||||||
|
if (cards == null || cards.isEmpty()) {
|
||||||
|
List<GraphicsCard> 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");
|
private static final Path MEMINFO = Paths.get("/proc/meminfo");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user