Skip to content

Commit 942f7b7

Browse files
authored
清理 CPU 名称 (#3928)
1 parent 267da46 commit 942f7b7

6 files changed

Lines changed: 205 additions & 2 deletions

File tree

HMCLCore/src/main/java/org/jackhuang/hmcl/util/StringUtils.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,55 @@ public static boolean isNotBlank(String str) {
5252
return !isBlank(str);
5353
}
5454

55+
public static String normalizeWhitespaces(String str) {
56+
if (str == null)
57+
return "";
58+
59+
int start = 0;
60+
int end = str.length();
61+
62+
while (start < str.length() && Character.isWhitespace(str.charAt(start))) {
63+
start++;
64+
}
65+
while (end > start && Character.isWhitespace(str.charAt(end - 1))) {
66+
end--;
67+
}
68+
69+
if (end == start) {
70+
return "";
71+
}
72+
73+
StringBuilder builder = null;
74+
75+
int i = start;
76+
while (i < end) {
77+
char ch = str.charAt(i);
78+
if (Character.isWhitespace(ch)) {
79+
int whitespaceEnd = i + 1;
80+
while (whitespaceEnd < end && Character.isWhitespace(str.charAt(whitespaceEnd))) {
81+
whitespaceEnd++;
82+
}
83+
84+
if (whitespaceEnd - i > 1 || ch != ' ') {
85+
if (builder == null) {
86+
builder = new StringBuilder(end - start);
87+
builder.append(str, start, i);
88+
}
89+
builder.append(' ');
90+
i = whitespaceEnd ;
91+
continue;
92+
}
93+
}
94+
95+
if (builder != null) {
96+
builder.append(ch);
97+
}
98+
i++;
99+
}
100+
101+
return builder != null ? builder.toString() : str.substring(start, end);
102+
}
103+
55104
public static String capitalizeFirst(String str) {
56105
if (str == null || str.isEmpty())
57106
return str;

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,46 @@
1717
*/
1818
package org.jackhuang.hmcl.util.platform.hardware;
1919

20+
import org.jackhuang.hmcl.util.StringUtils;
2021
import org.jetbrains.annotations.Nullable;
2122

2223
/**
2324
* @author Glavo
2425
*/
2526
public final class CentralProcessor {
27+
28+
public static String cleanName(String name) {
29+
if (name == null)
30+
return null;
31+
32+
int idx = name.indexOf('@');
33+
if (idx > 0)
34+
name = name.substring(0, idx);
35+
36+
name = name.replaceFirst(" (\\d+|Dual|Quad|Six|Eight|Ten)-[Cc]ores?", "");
37+
name = name.replaceAll(" (CPU|FPU|APU|Processor)", "");
38+
39+
if (name.contains("Intel")) {
40+
name = name.replaceFirst("^(\\d+th Gen )?Intel(\\(R\\)|®)? ", "Intel ");
41+
name = name.replaceAll(" ([a-zA-Z]+)\\((?:TM|R|™|®)\\) ", " $1 ");
42+
name = name.replace("Core(TM)2", "Core 2");
43+
} else if (name.contains("AMD")) {
44+
name = name.replace("(tm)", "");
45+
46+
idx = name.indexOf(" w/ Radeon "); // Radeon 780M Graphics
47+
if (idx < 0)
48+
idx = name.indexOf(" with Radeon ");
49+
if (idx < 0)
50+
idx = name.indexOf(" with AMD Radeon ");
51+
if (idx > 0)
52+
name = name.substring(0, idx);
53+
} else if (name.contains("Loongson")) {
54+
name = name.replaceFirst("^Loongson-3A R\\d \\((Loongson-[^)]+)\\)", "$1");
55+
}
56+
57+
return StringUtils.normalizeWhitespaces(name);
58+
}
59+
2660
private final String name;
2761
private final @Nullable HardwareVendor vendor;
2862
private final @Nullable Cores cores;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,13 @@ private static void detectName(CentralProcessor.Builder builder, TreeMap<Integer
100100
String modelName = firstCore.get("model name");
101101
if (modelName == null)
102102
modelName = firstCore.get("Model Name");
103+
if (modelName == null)
104+
modelName = firstCore.get("Model name");
103105
if (modelName == null)
104106
modelName = firstCore.get("cpu model");
105107

106108
if (modelName != null) {
107-
builder.setName(modelName);
109+
builder.setName(CentralProcessor.cleanName(modelName));
108110
builder.setVendor(HardwareVendor.of(firstCore.get("vendor_id")));
109111

110112
if (builder.getVendor() == null && modelName.startsWith("Loongson"))

HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/windows/WindowsCPUDetector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private static void detectName(CentralProcessor.Builder builder, WinReg reg) {
3333
Object vendor = reg.queryValue(WinReg.HKEY.HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", "VendorIdentifier");
3434

3535
if (name instanceof String)
36-
builder.setName((String) name);
36+
builder.setName(CentralProcessor.cleanName((String) name));
3737

3838
if (vendor instanceof String)
3939
builder.setVendor(HardwareVendor.of((String) vendor));
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Hello Minecraft! Launcher
3+
* Copyright (C) 2025 huangyuhui <huanghongxun2008@126.com> and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
package org.jackhuang.hmcl.util;
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
24+
/**
25+
* @author Glavo
26+
*/
27+
public final class StringUtilsTest {
28+
29+
@Test
30+
public void testNormalizeWhitespaces() {
31+
assertEquals("", StringUtils.normalizeWhitespaces(""));
32+
assertEquals("", StringUtils.normalizeWhitespaces(" "));
33+
assertEquals("", StringUtils.normalizeWhitespaces(" \t"));
34+
assertEquals("", StringUtils.normalizeWhitespaces(" \t "));
35+
36+
assertEquals("abc", StringUtils.normalizeWhitespaces("abc"));
37+
assertEquals("abc", StringUtils.normalizeWhitespaces(" abc"));
38+
assertEquals("abc", StringUtils.normalizeWhitespaces("abc "));
39+
assertEquals("abc", StringUtils.normalizeWhitespaces(" abc "));
40+
assertEquals("abc", StringUtils.normalizeWhitespaces(" \tabc \t"));
41+
42+
assertEquals("a bc", StringUtils.normalizeWhitespaces("a bc"));
43+
assertEquals("a bc", StringUtils.normalizeWhitespaces("a bc"));
44+
assertEquals("a bc", StringUtils.normalizeWhitespaces("a \tbc"));
45+
assertEquals("a bc", StringUtils.normalizeWhitespaces(" a \tbc "));
46+
assertEquals("a b c", StringUtils.normalizeWhitespaces(" a\tb c "));
47+
assertEquals("a b c", StringUtils.normalizeWhitespaces(" a \t b c "));
48+
assertEquals("a b c", StringUtils.normalizeWhitespaces(" a \t b c "));
49+
}
50+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Hello Minecraft! Launcher
3+
* Copyright (C) 2025 huangyuhui <huanghongxun2008@126.com> and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
package org.jackhuang.hmcl.util.platform.hardware;
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
import static org.jackhuang.hmcl.util.platform.hardware.CentralProcessor.cleanName;
23+
import static org.junit.jupiter.api.Assertions.assertEquals;
24+
25+
/**
26+
* @author Glavo
27+
*/
28+
public final class CentralProcessorTest {
29+
30+
@Test
31+
public void testCleanName() {
32+
assertEquals("Intel Core i7-12700K", cleanName("12th Gen Intel(R) Core(TM) i7-12700K"));
33+
assertEquals("Intel Core Ultra 9 285K", cleanName("Intel(R) Core(TM) Ultra 9 285K"));
34+
assertEquals("Intel Xeon Platinum 8380", cleanName("Intel(R) Xeon(R) Platinum 8380 CPU @ 2.30GHz"));
35+
assertEquals("Intel Xeon E5-2660 v2", cleanName("Intel(R) Xeon(R) CPU E5-2660 v2 @ 2.20GHz"));
36+
assertEquals("Intel Xeon Phi 7250", cleanName("Intel(R) Xeon Phi(TM) CPU 7250 @ 1.40GHz"));
37+
assertEquals("Intel Celeron N5105", cleanName("Intel(R) Celeron(R) N5105 @ 2.00GHz"));
38+
assertEquals("Intel Pentium Silver J5005", cleanName("Intel(R) Pentium(R) Silver J5005 CPU @ 1.50GHz"));
39+
assertEquals("Intel Atom E3940", cleanName("Intel(R) Atom(TM) Processor E3940 @ 1.60GHz"));
40+
assertEquals("Intel Core i7 X 990", cleanName("Intel(R) Core(TM) i7 CPU X 990 @ 3.47GHz"));
41+
assertEquals("Intel Core 2 Duo T7500", cleanName("Intel(R) Core(TM)2 Duo CPU T7500 @ 2.20GHz"));
42+
assertEquals("Intel Core 2 Quad Q9500", cleanName("Intel(R) Core(TM)2 Quad CPU Q9500 @ 2.83GHz"));
43+
44+
assertEquals("AMD Ryzen 7 7840HS", cleanName("AMD Ryzen 7 7840HS w/ Radeon 780M Graphics"));
45+
assertEquals("AMD Ryzen 7 6800U", cleanName("AMD Ryzen 7 6800U with Radeon Graphics"));
46+
assertEquals("AMD Ryzen 7 5800X", cleanName("AMD Ryzen 7 5800X 8-Core Processor"));
47+
assertEquals("AMD Ryzen 5 2400G", cleanName("AMD Ryzen 5 2400G with Radeon Vega Graphics"));
48+
assertEquals("AMD EPYC 7713", cleanName("AMD EPYC 7713 64-Core Processor"));
49+
assertEquals("AMD Ryzen Threadripper 3960X", cleanName("AMD Ryzen Threadripper 3960X 24-Core Processor"));
50+
assertEquals("AMD Ryzen Threadripper PRO 5995WX", cleanName("AMD Ryzen Threadripper PRO 5995WX 64-Cores"));
51+
assertEquals("AMD Ryzen Embedded V2748", cleanName("AMD Ryzen Embedded V2748 with Radeon Graphics"));
52+
assertEquals("AMD A8-7410", cleanName("AMD A8-7410 APU with AMD Radeon R5 Graphics"));
53+
assertEquals("AMD FX-8350", cleanName("AMD FX(tm)-8350 Eight-Core Processor"));
54+
assertEquals("AMD Phenom II X6 1055T", cleanName("AMD Phenom(tm) II X6 1055T Processor"));
55+
assertEquals("AMD Athlon 5350", cleanName("AMD Athlon(tm) 5350 APU with Radeon(tm) R3"));
56+
57+
assertEquals("Hygon C86 7285", cleanName("Hygon C86 7285 32-core Processor"));
58+
assertEquals("Hygon C86 3250", cleanName("Hygon C86 3250 8-core Processor"));
59+
60+
assertEquals("ZHAOXIN KaiXian KX-6640MA", cleanName("ZHAOXIN KaiXian KX-6640MA@2.2+GHz"));
61+
assertEquals("ZHAOXIN KaiXian KX-U6780A", cleanName("ZHAOXIN KaiXian KX-U6780A@2.7GHz"));
62+
assertEquals("ZHAOXIN KaiSheng KH-40000/16", cleanName("ZHAOXIN KaiSheng KH-40000/16@2.2GHz"));
63+
assertEquals("ZHAOXIN KaiSheng KH-37800D", cleanName("ZHAOXIN KaiSheng KH-37800D@2.7GHz"));
64+
65+
assertEquals("Loongson-3A3000", cleanName("Loongson-3A R3 (Loongson-3A3000) @ 1400MHz"));
66+
assertEquals("Loongson-3B4000", cleanName("Loongson-3A R4 (Loongson-3B4000) @ 1800MHz"));
67+
}
68+
}

0 commit comments

Comments
 (0)