fix(app): make instances with non-UTF8 text file encodings launcheable and importable #3721
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
As reported in issue #794, the Modrinth App fails to launch a Minecraft instance if its
options.txt
file is not encoded in UTF-8, and settings that require modifying such file, such as launching the game in fullscreen mod, are enabled.While non-UTF-8 encodings are becoming less common every day, and UTF-8's backward compatibility with 7-bit ASCII often masks encoding mismatch issues effectively, older Minecraft versions used the JVM's default system encoding when manipulating this file, and on Windows, this default is often a locale-dependent extended ASCII encoding (i.e., a code page), unlike (in my opinion, superior) Unix-like systems (Linux, macOS, BSDs), which default to UTF-8.
This PR addresses the issue by enhancing the internal utility function responsible for reading files into strings so that the file's encoding is detected, and text on it automatically decoded with the guessed encoding. Experimentally, I've found that UTF-8, Windows-1252/ISO 8859-1, and Shift JIS encodings are accurately identified with no relevant false positives. That function now also returns the detected encoding, allowing the file to be modified and saved without changing its original encoding, which is necessary because Minecraft may rely on that encoding staying the same.
Additionally, since this utility function is also used when reading metadata files while importing instances from another launchers, these changes also fix similar issues during that process.
Testing
I manually tested the changes by generating several variations of an
options.txt
file in different encodings. In each case, not only was the reported issue resolved, but the modified file also retained its original encoding.