From cba16f286845ad7613d7abca334432416b5ca450 Mon Sep 17 00:00:00 2001 From: Secretboy-SMR Date: Mon, 9 May 2022 17:30:51 +0800 Subject: [PATCH] Fix the following issues: 1. HashMap non-thread-safe issus 2. Fix the same problem in pr621, but use a better implementation Add the following functions: 1. There is now a language cache inside getLanguage to prepare for different languages corresponding to different time zones where the accounts in the server are located --- src/main/java/emu/grasscutter/Config.java | 2 +- src/main/java/emu/grasscutter/Grasscutter.java | 9 +-------- src/main/java/emu/grasscutter/utils/Language.java | 14 +++++++++++--- src/main/java/emu/grasscutter/utils/Utils.java | 9 +++++++++ 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/main/java/emu/grasscutter/Config.java b/src/main/java/emu/grasscutter/Config.java index 6473e2846..4ec16e0d1 100644 --- a/src/main/java/emu/grasscutter/Config.java +++ b/src/main/java/emu/grasscutter/Config.java @@ -22,7 +22,7 @@ public final class Config { public GameServerOptions GameServer = new GameServerOptions(); public DispatchServerOptions DispatchServer = new DispatchServerOptions(); public Locale LocaleLanguage = Locale.getDefault(); - public Locale DefaultLanguage = Locale.ENGLISH; + public Locale DefaultLanguage = Locale.US; public Boolean OpenStamina = true; public GameServerOptions getGameServerOptions() { diff --git a/src/main/java/emu/grasscutter/Grasscutter.java b/src/main/java/emu/grasscutter/Grasscutter.java index c593f5f13..f426d41af 100644 --- a/src/main/java/emu/grasscutter/Grasscutter.java +++ b/src/main/java/emu/grasscutter/Grasscutter.java @@ -149,14 +149,7 @@ public final class Grasscutter { public static void loadLanguage() { var locale = config.LocaleLanguage; - var languageTag = locale.toLanguageTag(); - - if (languageTag.equals("und")) { - Grasscutter.getLogger().error("Illegal locale language, using 'en-US' instead."); - language = Language.getLanguage("en-US"); - } else { - language = Language.getLanguage(languageTag); - } + language = Language.getLanguage(Utils.getLanguageCode(locale)); } public static void saveConfig() { diff --git a/src/main/java/emu/grasscutter/utils/Language.java b/src/main/java/emu/grasscutter/utils/Language.java index 70e32e658..04bd352f7 100644 --- a/src/main/java/emu/grasscutter/utils/Language.java +++ b/src/main/java/emu/grasscutter/utils/Language.java @@ -6,12 +6,13 @@ import emu.grasscutter.Grasscutter; import javax.annotation.Nullable; import java.io.InputStream; -import java.util.HashMap; +import java.util.concurrent.ConcurrentHashMap; import java.util.Map; public final class Language { private final JsonObject languageData; - private final Map cachedTranslations = new HashMap<>(); + private final Map cachedTranslations = new ConcurrentHashMap<>(); + private static final Map cachedLanguages = new ConcurrentHashMap<>(); /** * Creates a language instance from a code. @@ -19,7 +20,13 @@ public final class Language { * @return A language instance. */ public static Language getLanguage(String langCode) { - return new Language(langCode + ".json", Grasscutter.getConfig().DefaultLanguage.toLanguageTag() + ".json"); + if (cachedLanguages.containsKey(langCode)) { + return cachedLanguages.get(langCode); + } + + var languageInst = new Language(langCode + ".json", Utils.getLanguageCode(Grasscutter.getConfig().DefaultLanguage) + ".json"); + cachedLanguages.put(langCode, languageInst); + return languageInst; } /** @@ -42,6 +49,7 @@ public final class Language { /** * Reads a file and creates a language instance. * @param fileName The name of the language file. + * @param fallback The name of the fallback language file. */ private Language(String fileName, String fallback) { @Nullable JsonObject languageData = null; diff --git a/src/main/java/emu/grasscutter/utils/Utils.java b/src/main/java/emu/grasscutter/utils/Utils.java index 1d79c496e..764993255 100644 --- a/src/main/java/emu/grasscutter/utils/Utils.java +++ b/src/main/java/emu/grasscutter/utils/Utils.java @@ -9,6 +9,7 @@ import java.time.temporal.TemporalAdjusters; import java.util.HashMap; import java.util.Map; import java.util.Random; +import java.util.Locale; import emu.grasscutter.Config; import emu.grasscutter.Grasscutter; @@ -306,4 +307,12 @@ public final class Utils { return map; } + + /** + * get language code from Locale + */ + public static String getLanguageCode(Locale locale) { + return String.format("%s-%s", locale.getLanguage(), locale.getCountry()); + } + }