Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.*;
Expand Down Expand Up @@ -224,33 +227,46 @@ 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();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Mark the nullable detector result explicitly

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 👍 / 👎.

if (bestEncoding == EncodingDetector.Encoding.ASCII)
bestEncoding = EncodingDetector.Encoding.UTF_8;

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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Use a Unicode-capable charset for ASCII-only options

When an existing options.txt contains only ASCII (the normal case for a fresh instance), EncodingDetector.MODERN_WEB selects US-ASCII; if the user then enables a resource pack such as 中文.zip, this save silently replaces the newly inserted filename characters with ?. Minecraft consequently receives the wrong resource-pack ID and the activation does not persist, so ASCII detection should fall back to UTF-8 or the selected charset should be upgraded when it cannot encode the resulting content.

Useful? React with 👍 / 👎.

} catch (IOException e) {
LOG.warning("Failed to save instance options file", e);
}
Expand All @@ -277,7 +293,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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.jetbrains.annotations.Nullable;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.PosixFileAttributeView;
Expand Down Expand Up @@ -486,13 +488,17 @@ public static Path tmpSaveFile(Path file) {
}

public static void saveSafely(Path file, String content) throws IOException {
saveSafely(file, content, StandardCharsets.UTF_8);
}

public static void saveSafely(Path file, String content, @Nullable Charset charset) throws IOException {
Path parent = file.toAbsolutePath().getParent();
if (parent != null) {
Files.createDirectories(parent);
}

Path tmpFile = tmpSaveFile(file);
try (BufferedWriter writer = Files.newBufferedWriter(tmpFile, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE)) {
try (BufferedWriter writer = Files.newBufferedWriter(tmpFile, charset != null ? charset : StandardCharsets.UTF_8, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE)) {
writer.write(content);
}

Expand Down