Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,24 @@ private FastFetchUtils() {
}

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)
Comment on lines 43 to 45
Copy link

Copilot AI May 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Invoking which on every call can be costly; consider caching the resolved fastfetch path in a static field to avoid repeated lookups.

Copilot uses AI. Check for mistakes.
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);
return null;
}

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;
Comment thread
Glavo marked this conversation as resolved.

List<Result<T>> list = JsonUtils.GSON.fromJson(json, JsonUtils.listTypeOf(Result.typeOf(resultType)));

Result<T> result;
Expand All @@ -68,7 +72,7 @@ private static <T> T get(String type, TypeToken<T> 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);
Comment thread
Glavo marked this conversation as resolved.
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@
*/
@SuppressWarnings("ALL")
public class HardwareDetector {
private static final boolean USE_FAST_FETCH = "true".equalsIgnoreCase(System.getProperty("hmcl.hardware.fastfetch", "true"));
Comment thread
Glavo marked this conversation as resolved.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里到时候要写到文档吗


public @Nullable CentralProcessor detectCentralProcessor() {
return FastFetchUtils.detectCentralProcessor();
return USE_FAST_FETCH ? FastFetchUtils.detectCentralProcessor() : null;
}

public @Nullable List<GraphicsCard> detectGraphicsCards() {
return FastFetchUtils.detectGraphicsCards();
return USE_FAST_FETCH ? FastFetchUtils.detectGraphicsCards() : null;
}

public long getTotalMemorySize() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<GraphicsCard> detectGraphicsCards() {
if (OperatingSystem.CURRENT_OS != OperatingSystem.LINUX)
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");
Expand Down