Skip to content

Commit d027c32

Browse files
committed
feat: add device model identifier for deviceinfo
1 parent 5112ffe commit d027c32

8 files changed

Lines changed: 93 additions & 19 deletions

File tree

src/main/java/org/damon233/performtrackermod/collector/SystemInfoCollector.java

Lines changed: 81 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class SystemInfoCollector {
4242
// Permanent caches - CPU info and memory never change at runtime
4343
private static String cachedCpuName;
4444
private static long cachedPhysicalMemory = -1;
45+
private static String cachedDeviceModel;
4546

4647
// SHA-256 checksums for chip rules files (update when file content changes)
4748
private static final String PHONE_CHIPS_SHA = "428b078c2ba8395b84f2dd2a7ed964ee80feef685ecc6335e9679606ec63162a";
@@ -79,7 +80,8 @@ public static SystemInfo collect() {
7980
javaVersion,
8081
jvmName,
8182
minecraftVersion,
82-
modVersion
83+
modVersion,
84+
getDeviceModel()
8385
);
8486

8587
return cachedInfo;
@@ -118,14 +120,14 @@ private static String getCpuName() {
118120

119121
private static String getWindowsCpuName() {
120122
String[] commands = {
121-
"powershell -NoProfile -Command Get-CimInstance -ClassName Win32_Processor | Select-Object -ExpandProperty Name",
122-
"powershell -NoProfile -Command (Get-WmiObject Win32_Processor).Name"
123+
"powershell -NoProfile -Command \"Get-CimInstance Win32_Processor | Select-Object -ExpandProperty Name\"",
124+
"powershell -NoProfile -Command \"(Get-WmiObject Win32_Processor).Name\""
123125
};
124126

125127
for (String command : commands) {
126128
String result = runCommand(command);
127129
if (result != null && !result.trim().isEmpty()) {
128-
return result.trim();
130+
return result.lines().findFirst().orElse(result).trim();
129131
}
130132
}
131133

@@ -136,23 +138,87 @@ private static String getLinuxCpuName() {
136138
String output = readFile("/proc/cpuinfo");
137139
if (output == null) return "Unknown";
138140

139-
String[] prefixes = {"model name", "Model name", "Processor", "Hardware", "model", "Model"};
140-
for (String prefix : prefixes) {
141-
for (String line : output.split("\n")) {
142-
if (line.startsWith(prefix)) {
143-
int colon = line.indexOf(':');
144-
if (colon > 0) {
145-
return line.substring(colon + 1).trim();
146-
}
147-
}
141+
for (String line : output.split("\n")) {
142+
line = line.trim();
143+
144+
if (line.startsWith("model name") || line.startsWith("Model name")) {
145+
return line.split(":", 2)[1].trim();
146+
}
147+
148+
if (line.startsWith("Hardware") || line.startsWith("Processor")) {
149+
return line.split(":", 2)[1].trim();
148150
}
149151
}
150-
return "Unknown";
152+
153+
return System.getProperty("os.arch");
151154
}
152155

153156
private static String getMacCpuName() {
154157
String result = runCommand("sysctl -n machdep.cpu.brand_string");
155-
return result != null ? result.trim() : System.getProperty("os.arch");
158+
if (result != null && !result.isBlank()) {
159+
return result.trim();
160+
}
161+
return System.getProperty("os.arch");
162+
}
163+
164+
private static String getDeviceModel() {
165+
if (cachedDeviceModel != null) {
166+
return cachedDeviceModel;
167+
}
168+
169+
String osName = System.getProperty("os.name").toLowerCase();
170+
171+
if (osName.contains("mac") || osName.contains("darwin")) {
172+
cachedDeviceModel = getMacDeviceModel();
173+
} else if (osName.contains("linux")) {
174+
cachedDeviceModel = getLinuxDeviceModel();
175+
} else if (osName.contains("windows")) {
176+
cachedDeviceModel = getWindowsDeviceModel();
177+
} else {
178+
cachedDeviceModel = "Unknown";
179+
}
180+
181+
if (cachedDeviceModel.equalsIgnoreCase("To Be Filled By O.E.M.") || cachedDeviceModel.equalsIgnoreCase("Default String")) {
182+
cachedDeviceModel = "Unknown";
183+
}
184+
185+
return cachedDeviceModel;
186+
}
187+
188+
private static String getMacDeviceModel() {
189+
String result = runCommand("sysctl -n hw.model");
190+
return result != null ? result.trim() : "Unknown";
191+
}
192+
193+
private static String getLinuxDeviceModel() {
194+
String model = readFile("/sys/devices/virtual/dmi/id/product_name");
195+
if (model != null && !model.isBlank()) {
196+
return model.trim();
197+
}
198+
199+
model = readFile("/proc/device-tree/model");
200+
if (model != null && !model.isBlank()) {
201+
return model.trim();
202+
}
203+
204+
return "Unknown";
205+
}
206+
207+
private static String getWindowsDeviceModel() {
208+
String[] commands = {
209+
"powershell -NoProfile -Command \"(Get-CimInstance Win32_ComputerSystem).Model\"",
210+
"powershell -NoProfile -Command \"(Get-WmiObject Win32_ComputerSystem).Model\"",
211+
"cmd /c for /f \"tokens=2 delims==\" %A in ('wmic computersystem get model /value') do @echo %A"
212+
};
213+
214+
for (String command : commands) {
215+
String result = runCommand(command);
216+
if (result != null && !result.trim().isEmpty()) {
217+
return result.trim();
218+
}
219+
}
220+
221+
return "Unknown";
156222
}
157223

158224
private static String runCommand(String command) {

src/main/java/org/damon233/performtrackermod/command/PtrackerCommand.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,20 +204,22 @@ public static void register() {
204204

205205
ptracker.then(CommandManager.literal("deviceinfo").executes(ctx -> {
206206
SystemInfo info = SystemInfoCollector.collect();
207-
207+
208208
String title = Text.translatable("performtracker.device.title").getString();
209209
String unknown = Text.translatable("performtracker.device.unknown").getString();
210210
String typeLabel = Text.translatable("performtracker.device.type").getString();
211+
String modelLabel = Text.translatable("performtracker.device.model").getString();
211212
String cpuLabel = Text.translatable("performtracker.device.cpu").getString();
212213
String gpuLabel = Text.translatable("performtracker.device.gpu").getString();
213214
String coresLabel = Text.translatable("performtracker.device.cpu_cores").getString();
214215
String memLabel = Text.translatable("performtracker.device.memory").getString();
215216
String osLabel = Text.translatable("performtracker.device.os").getString();
216217
String javaLabel = Text.translatable("performtracker.device.java").getString();
217218
String typeValue = Text.translatable("performtracker.device.type." + info.deviceType().getCode()).getString();
218-
219+
219220
ctx.getSource().sendFeedback(() -> Text.literal(TranslationService.colorLabel(title)), false);
220221
ctx.getSource().sendFeedback(() -> Text.literal(TranslationService.colorLabel(typeLabel) + TranslationService.colorValue(typeValue)), false);
222+
ctx.getSource().sendFeedback(() -> Text.literal(TranslationService.colorLabel(modelLabel) + TranslationService.colorValue(info.deviceModel() != null ? info.deviceModel() : unknown)), false);
221223
ctx.getSource().sendFeedback(() -> Text.literal(TranslationService.colorLabel(cpuLabel) + TranslationService.colorValue(info.cpuName() != null ? info.cpuName() : unknown)), false);
222224
ctx.getSource().sendFeedback(() -> Text.literal(TranslationService.colorLabel(gpuLabel) + TranslationService.colorValue(info.gpuName() != null ? info.gpuName() : unknown)), false);
223225
ctx.getSource().sendFeedback(() -> Text.literal(TranslationService.colorLabel(coresLabel) + TranslationService.colorValue(String.valueOf(info.cpuCores()))), false);

src/main/java/org/damon233/performtrackermod/data/SystemInfo.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public record SystemInfo(
1313
String javaVersion,
1414
String jvmName,
1515
String minecraftVersion,
16-
String modVersion
16+
String modVersion,
17+
String deviceModel
1718
) {
1819
public static String formatBytes(long bytes) {
1920
if (bytes >= 1073741824) {

src/main/java/org/damon233/performtrackermod/network/HttpService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,12 @@ public void handle(HttpExchange exchange) throws IOException {
201201
SystemInfo info = SystemInfoCollector.collect();
202202
boolean chipRulesTrusted = SystemInfoCollector.isChipRulesValid();
203203
String json = String.format(
204-
"{\"deviceType\":\"%s\",\"cpuName\":\"%s\",\"gpuName\":\"%s\",\"cpuCores\":%d,\"memory\":%d," +
204+
"{\"deviceType\":\"%s\",\"deviceModel\":\"%s\",\"cpuName\":\"%s\",\"gpuName\":\"%s\",\"cpuCores\":%d,\"memory\":%d," +
205205
"\"os\":\"%s\",\"osVersion\":\"%s\",\"osArch\":\"%s\"," +
206206
"\"javaVersion\":\"%s\",\"minecraftVersion\":\"%s\",\"modVersion\":\"%s\"," +
207207
"\"chipRulesTrusted\":%b}",
208208
JsonFormatter.escapeJson(info.deviceType().getCode()),
209+
JsonFormatter.escapeJson(info.deviceModel() != null ? info.deviceModel() : "Unknown"),
209210
JsonFormatter.escapeJson(info.cpuName() != null ? info.cpuName() : "Unknown"),
210211
JsonFormatter.escapeJson(info.gpuName() != null ? info.gpuName() : "Unknown"),
211212
info.cpuCores(),

src/main/resources/assets/performtracker/lang/en_us.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"performtracker.device.type.HWM": "HarmonyOS Device",
5858
"performtracker.device.title": "=== Device Info ===",
5959
"performtracker.device.type": "Type: ",
60+
"performtracker.device.model": "Model: ",
6061
"performtracker.device.cpu": "CPU: ",
6162
"performtracker.device.gpu": "GPU: ",
6263
"performtracker.device.cpu_cores": "CPU Cores: ",

src/main/resources/assets/performtracker/lang/lzh.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"performtracker.device.type.HWM": "鴻蒙機",
5858
"performtracker.device.title": "=== 器械錄 ===",
5959
"performtracker.device.type": "類: ",
60+
"performtracker.device.model": "型: ",
6061
"performtracker.device.cpu": "算牒: ",
6162
"performtracker.device.gpu": "圖牒: ",
6263
"performtracker.device.cpu_cores": "算核: ",

src/main/resources/assets/performtracker/lang/zh_cn.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"performtracker.device.type.HWM": "鸿蒙设备",
5858
"performtracker.device.title": "=== 设备信息 ===",
5959
"performtracker.device.type": "类型: ",
60+
"performtracker.device.model": "型号: ",
6061
"performtracker.device.cpu": "处理器: ",
6162
"performtracker.device.gpu": "显卡: ",
6263
"performtracker.device.cpu_cores": "核心数: ",

src/main/resources/assets/performtracker/lang/zh_tw.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"performtracker.device.type.HWM": "鴻蒙裝置",
5858
"performtracker.device.title": "=== 裝置資訊 ===",
5959
"performtracker.device.type": "類型: ",
60+
"performtracker.device.model": "型號: ",
6061
"performtracker.device.cpu": "處理器: ",
6162
"performtracker.device.gpu": "顯示卡: ",
6263
"performtracker.device.cpu_cores": "核心數: ",

0 commit comments

Comments
 (0)