B.A.S.E

B.A.S.E

46M Downloads

B.A.S.E.'s BaseFileUtils is writing file using system default charset while reading it using UTF-8

3TUSK opened this issue ยท 1 comments

commented

Synopsis

BaseFileUtils uses potentially different charset for reading and writing files (specifically the FileUtils.writeStringToFile(file, string, Charset.defaultCharset()); part), which may cause issues when dealing with multiple languages.

public static void writeStringToFile(String string, File file) {
boolean exists = file.exists();
if (!exists) {
try {
file.getParentFile().mkdirs();
exists = file.createNewFile();
} catch (IOException e) {
Platform.attemptLogExceptionToCurrentMod(e);
}
}
if (exists) {
try {
FileUtils.writeStringToFile(file, string, Charset.defaultCharset());
} catch (IOException e) {
Platform.attemptLogExceptionToCurrentMod(e);
}
} else {
Platform.attemptLogErrorToCurrentMod("Couldn't create File: " + file.getName());
}
}

Reproduction

@wormzjl has the following setup:

  1. He is using Windows OS with (most likely) a European locale. The exact locale is still unknown so far.
  2. He was editing zh_cn.lang using the ${game_dir}/resources folder-based resourcepack which is provided by B.A.S.E., for his items created via ContentTweaker.
  3. He saves files using UTF-8 encoding.
  4. After game launches, the file becomes gibberish. It is suspected that the fixLang functionality is the culprit:
    private void fixLangFile(File file) {
    String fileString = BaseFileUtils.readFileToString(file);
    fileString = pattern.matcher(fileString).replaceAll("=");
    BaseFileUtils.writeStringToFile(fileString, file);
    }
    which in turns invokes BaseFileUtils.writeStringToFile as shown above.

Analysis

See above. TLDR: asymmetrical I/O handling to the extent of charset.

Proposed fix

FileUtils.writeStringToFile(file, string, java.nio.charset.StandardCharsets.UTF_8);

Final words

N/A

commented

I thought I fixed this. Guess I only did the reading as UTF-8, not writing...