清理 CPU 名称 (#3928)

This commit is contained in:
Glavo
2025-05-21 14:22:27 +08:00
committed by GitHub
parent 267da46050
commit 942f7b703e
6 changed files with 205 additions and 2 deletions

View File

@@ -52,6 +52,55 @@ public final class StringUtils {
return !isBlank(str);
}
public static String normalizeWhitespaces(String str) {
if (str == null)
return "";
int start = 0;
int end = str.length();
while (start < str.length() && Character.isWhitespace(str.charAt(start))) {
start++;
}
while (end > start && Character.isWhitespace(str.charAt(end - 1))) {
end--;
}
if (end == start) {
return "";
}
StringBuilder builder = null;
int i = start;
while (i < end) {
char ch = str.charAt(i);
if (Character.isWhitespace(ch)) {
int whitespaceEnd = i + 1;
while (whitespaceEnd < end && Character.isWhitespace(str.charAt(whitespaceEnd))) {
whitespaceEnd++;
}
if (whitespaceEnd - i > 1 || ch != ' ') {
if (builder == null) {
builder = new StringBuilder(end - start);
builder.append(str, start, i);
}
builder.append(' ');
i = whitespaceEnd ;
continue;
}
}
if (builder != null) {
builder.append(ch);
}
i++;
}
return builder != null ? builder.toString() : str.substring(start, end);
}
public static String capitalizeFirst(String str) {
if (str == null || str.isEmpty())
return str;

View File

@@ -17,12 +17,46 @@
*/
package org.jackhuang.hmcl.util.platform.hardware;
import org.jackhuang.hmcl.util.StringUtils;
import org.jetbrains.annotations.Nullable;
/**
* @author Glavo
*/
public final class CentralProcessor {
public static String cleanName(String name) {
if (name == null)
return null;
int idx = name.indexOf('@');
if (idx > 0)
name = name.substring(0, idx);
name = name.replaceFirst(" (\\d+|Dual|Quad|Six|Eight|Ten)-[Cc]ores?", "");
name = name.replaceAll(" (CPU|FPU|APU|Processor)", "");
if (name.contains("Intel")) {
name = name.replaceFirst("^(\\d+th Gen )?Intel(\\(R\\)|®)? ", "Intel ");
name = name.replaceAll(" ([a-zA-Z]+)\\((?:TM|R|™|®)\\) ", " $1 ");
name = name.replace("Core(TM)2", "Core 2");
} else if (name.contains("AMD")) {
name = name.replace("(tm)", "");
idx = name.indexOf(" w/ Radeon "); // Radeon 780M Graphics
if (idx < 0)
idx = name.indexOf(" with Radeon ");
if (idx < 0)
idx = name.indexOf(" with AMD Radeon ");
if (idx > 0)
name = name.substring(0, idx);
} else if (name.contains("Loongson")) {
name = name.replaceFirst("^Loongson-3A R\\d \\((Loongson-[^)]+)\\)", "$1");
}
return StringUtils.normalizeWhitespaces(name);
}
private final String name;
private final @Nullable HardwareVendor vendor;
private final @Nullable Cores cores;

View File

@@ -100,11 +100,13 @@ final class LinuxCPUDetector {
String modelName = firstCore.get("model name");
if (modelName == null)
modelName = firstCore.get("Model Name");
if (modelName == null)
modelName = firstCore.get("Model name");
if (modelName == null)
modelName = firstCore.get("cpu model");
if (modelName != null) {
builder.setName(modelName);
builder.setName(CentralProcessor.cleanName(modelName));
builder.setVendor(HardwareVendor.of(firstCore.get("vendor_id")));
if (builder.getVendor() == null && modelName.startsWith("Loongson"))

View File

@@ -33,7 +33,7 @@ final class WindowsCPUDetector {
Object vendor = reg.queryValue(WinReg.HKEY.HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", "VendorIdentifier");
if (name instanceof String)
builder.setName((String) name);
builder.setName(CentralProcessor.cleanName((String) name));
if (vendor instanceof String)
builder.setVendor(HardwareVendor.of((String) vendor));