Skip to content

Commit d70712a

Browse files
authored
Fix #3546: 修复游戏崩溃窗口乱码的问题 (#3867)
1 parent 05c5b53 commit d70712a

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

HMCL/src/main/java/org/jackhuang/hmcl/ui/GameCrashWindow.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ private void analyzeCrashReport() {
142142

143143
String log;
144144
try {
145-
log = FileUtils.readText(latestLog);
145+
log = FileUtils.readTextMaybeNativeEncoding(latestLog);
146146
} catch (IOException e) {
147147
LOG.warning("Failed to read logs/latest.log", e);
148148
return pair(new HashSet<CrashReportAnalyzer.Result>(), new HashSet<String>());

HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/FileUtils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
package org.jackhuang.hmcl.util.io;
1919

20+
import org.glavo.chardet.DetectedCharset;
21+
import org.glavo.chardet.UniversalDetector;
2022
import org.jackhuang.hmcl.util.Lang;
2123
import org.jackhuang.hmcl.util.StringUtils;
2224
import org.jackhuang.hmcl.util.function.ExceptionalConsumer;
@@ -128,6 +130,24 @@ public static String readText(Path file, Charset charset) throws IOException {
128130
return new String(Files.readAllBytes(file), charset);
129131
}
130132

133+
public static String readTextMaybeNativeEncoding(Path file) throws IOException {
134+
byte[] bytes = Files.readAllBytes(file);
135+
136+
if (OperatingSystem.NATIVE_CHARSET == UTF_8)
137+
return new String(bytes, UTF_8);
138+
139+
UniversalDetector detector = new UniversalDetector();
140+
detector.handleData(bytes);
141+
detector.dataEnd();
142+
143+
DetectedCharset detectedCharset = detector.getDetectedCharset();
144+
if (detectedCharset != null && detectedCharset.isSupported()
145+
&& (detectedCharset == DetectedCharset.UTF_8 || detectedCharset == DetectedCharset.US_ASCII))
146+
return new String(bytes, UTF_8);
147+
else
148+
return new String(bytes, OperatingSystem.NATIVE_CHARSET);
149+
}
150+
131151
/**
132152
* Write plain text to file. Characters are encoded into bytes using UTF-8.
133153
* <p>

0 commit comments

Comments
 (0)