Git Product home page Git Product logo

Comments (2)

parataxia avatar parataxia commented on September 18, 2024

I believe this can be resolved by adding this environment variable to your launch script:

set JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF-8 - Windows
or
export JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF-8 - Linux/OS X

Ideally this would've been included initially when beatoraja came out, but instead we're too late in development ... it would break a lot of installs when the file is ANSI encoded rather than UTF-8. note, if your config.json is not UTF-8 there might be strange bugs (i.e: save/load of certain features don't work when ANSI!)

from beatoraja.

FelisCatusKR avatar FelisCatusKR commented on September 18, 2024

Okay, so after I studied Java by myself, I've figured out that a usage of FileWriter and FileReader causes this problem because that classes use the system encoding when its OS is Windows (which is obviously not UTF-8 in general condition.)

public static PlayerConfig readPlayerConfig(String playerpath, String playerid) {
PlayerConfig player = new PlayerConfig();
try (FileReader reader = new FileReader(Paths.get(playerpath + "/" + playerid + "/config.json").toFile())) {
Json json = new Json();
json.setIgnoreUnknownFields(true);
player = json.fromJson(PlayerConfig.class, reader);
player.setId(playerid);
player.validate();
} catch(Throwable e) {
e.printStackTrace();
}
return player;
}
public static void write(String playerpath, PlayerConfig player) {
try (FileWriter writer = new FileWriter(Paths.get(playerpath + "/" + player.getId() + "/config.json").toFile())) {
Json json = new Json();
json.setOutputType(JsonWriter.OutputType.json);
json.setUsePrototypes(false);
writer.write(json.prettyPrint(player));
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}

Of course, you can use such a 'UTF-8 fix', editing a batch file with an addition of an environment variable JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF-8. However, when it comes to executable binaries like the one in Mocha-Repository, we can't do that black magic.
Luckily, there are standard classes that can read/write files in specific locale, OutputStreamWriter and InputStreamReader. If these classes are used for this section, users won't need that 'UTF-8 fix' anymore.

try (OutputStreamWriter writer =
    new OutputStreamWriter(new FileOutputStream(Paths.get(playerpath + "/" + player.getId() + "/config.json"), StandardCharsets.UTF_8))
) {

The problem is, if users who haven't used a 'UTF-8 fix' (typically users who are using Windows in Shift-JIS encoding), their configuration file will be deleted because it has not been in UTF-8 encoding so they'll loose all their configurations.

This can be solved by using an external library such as icu4j, but I'm not sure it is proper solution for this case. Any ideas?

(Sorry for my poor English)

from beatoraja.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.