-
Notifications
You must be signed in to change notification settings - Fork 911
[Bugfix] 修复 #6348 资源包管理处理非UTF-8编码的options.txt文件时出错
#6351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
79de248
9420f70
34d122b
c1c1672
79dc5c9
7672227
4d7c031
a28e79d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| package org.jackhuang.hmcl.addon.resourcepack; | ||
|
|
||
| import com.google.gson.annotations.SerializedName; | ||
| import kala.encdet.EncodingDetector; | ||
| import org.jackhuang.hmcl.game.GameRepository; | ||
| import org.jackhuang.hmcl.addon.LocalAddonManager; | ||
| import org.jackhuang.hmcl.addon.meta.PackMcMeta; | ||
|
|
@@ -36,6 +37,8 @@ | |
| import org.jetbrains.annotations.Unmodifiable; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.UncheckedIOException; | ||
| import java.nio.charset.Charset; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.*; | ||
| import java.util.*; | ||
|
|
@@ -224,33 +227,44 @@ public ResourcePackManager(GameRepository repository, String id) { | |
| this.optionsFile = repository.getRunDirectory(id).resolve("options.txt"); | ||
| } | ||
|
|
||
| private @Nullable Charset optionsFileEncoding; | ||
|
|
||
| @NotNull | ||
| private Map<String, String> loadOptions() { | ||
| getMinecraftVersion(); | ||
| Map<String, String> options = new LinkedHashMap<>(); | ||
| if (!Files.isRegularFile(optionsFile)) return options; | ||
| try (var stream = Files.lines(optionsFile, StandardCharsets.UTF_8)) { | ||
| stream.forEach(s -> { | ||
| if (StringUtils.isNotBlank(s)) { | ||
| var entry = s.split(":", 2); | ||
| if (entry.length == 2) { | ||
| options.put(entry[0], entry[1]); | ||
| } | ||
| } | ||
| }); | ||
| } catch (IOException e) { | ||
| byte[] bytes; | ||
| try { | ||
| bytes = Files.readAllBytes(optionsFile); | ||
| } catch (IOException | UncheckedIOException e) { | ||
| LOG.warning("Failed to read instance options file", e); | ||
| return options; | ||
| } | ||
|
|
||
| EncodingDetector.Encoding bestEncoding = EncodingDetector.MODERN_WEB.detect(bytes).bestEncoding(); | ||
|
|
||
| optionsFileEncoding = bestEncoding != null && bestEncoding.approximateCharset() != null | ||
| ? bestEncoding.approximateCharset() : StandardCharsets.UTF_8; | ||
| //noinspection DataFlowIssue | ||
| new String(bytes, optionsFileEncoding).lines().forEach(s -> { | ||
| if (StringUtils.isNotBlank(s)) { | ||
| var entry = s.split(":", 2); | ||
| if (entry.length == 2) { | ||
| options.put(entry[0], entry[1]); | ||
| } | ||
| } | ||
| }); | ||
| return options; | ||
| } | ||
|
|
||
| private void saveOptions(@NotNull Map<String, String> options) { | ||
| StringBuilder sb = new StringBuilder(); | ||
| for (var entry : options.entrySet()) { | ||
| sb.append(entry.getKey()).append(":").append(entry.getValue()).append(System.lineSeparator()); | ||
| } | ||
| try { | ||
| StringBuilder sb = new StringBuilder(); | ||
| for (var entry : options.entrySet()) { | ||
| sb.append(entry.getKey()).append(":").append(entry.getValue()).append(System.lineSeparator()); | ||
| } | ||
| FileUtils.saveSafely(optionsFile, sb.toString()); | ||
| FileUtils.saveSafely(optionsFile, sb.toString(), optionsFileEncoding); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When an existing Useful? React with 👍 / 👎. |
||
| } catch (IOException e) { | ||
| LOG.warning("Failed to save instance options file", e); | ||
| } | ||
|
|
@@ -277,7 +291,8 @@ public PackMcMeta.PackVersion getRequiredVersion() { | |
| if (requiredVersion == null) { | ||
| lock.lock(); | ||
| try { | ||
| if (requiredVersion == null) requiredVersion = getPackVersion(getMinecraftVersion(), repository.getVersionJar(id)); | ||
| if (requiredVersion == null) | ||
| requiredVersion = getPackVersion(getMinecraftVersion(), repository.getVersionJar(id)); | ||
| } finally { | ||
| lock.unlock(); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bestEncoding()is explicitly handled as possibly null on the following line, but this local variable omits@Nullable, leaving its nullability implicit contrary to the repository's Java nullability requirement; annotate the local declaration accordingly.AGENTS.md reference: AGENTS.md:L7-L9
Useful? React with 👍 / 👎.