From 41b34fa6f6a1855dbdd5add7b137313148591432 Mon Sep 17 00:00:00 2001 From: Sten Laane <21343173+StenAL@users.noreply.github.com> Date: Fri, 20 Sep 2024 08:41:23 +0300 Subject: [PATCH] Client: Remove unused code path for loading .loc localization files The repository has no .loc localization files so there's no point in keeping the code to load them around as it just makes TextManager harder to comprehend. Also, this enables simplifying the code path that is actually taken in loadLanguageFiles. --- .../java/com/aapeli/client/TextManager.java | 196 ++---------------- 1 file changed, 19 insertions(+), 177 deletions(-) diff --git a/client/src/main/java/com/aapeli/client/TextManager.java b/client/src/main/java/com/aapeli/client/TextManager.java index 1479a879..8d374d65 100644 --- a/client/src/main/java/com/aapeli/client/TextManager.java +++ b/client/src/main/java/com/aapeli/client/TextManager.java @@ -4,13 +4,6 @@ import com.aapeli.tools.Tools; import com.aapeli.tools.XmlUnit; import java.applet.Applet; -import java.io.BufferedReader; -import java.io.FileNotFoundException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.UnsupportedEncodingException; -import java.net.MalformedURLException; -import java.net.URL; import java.util.Calendar; import java.util.Date; import java.util.Hashtable; @@ -20,23 +13,11 @@ public final class TextManager implements Runnable { private Parameters parameters; private Thread textLoaderThread; private String language; - private Hashtable gameTable; - private Hashtable sharedTable; + private Hashtable gameTable; + private Hashtable sharedTable; private String errorMessage; - private boolean useLanguageFiles; // true == use language xml files, false == use locale .loc files private boolean debug; - public TextManager(Applet applet, String var2) { - this(applet, var2, false); - } - - public TextManager(Applet applet, String var2, boolean debug) { - this(debug); - this.language = var2; - this.useLanguageFiles = false; - this.loadTexts(applet); - } - public TextManager(Parameters parameters) { this(parameters, false, false); } @@ -48,14 +29,7 @@ public TextManager(Parameters parameters, boolean debug) { public TextManager(Parameters parameters, boolean loadTextsInSeparateThread, boolean debug) { this(debug); this.parameters = parameters; - String language = parameters.getTranslationLocale().toString(); - if (language != null) { - this.language = language; - this.useLanguageFiles = true; - } else { - this.language = parameters.language().toString(); - this.useLanguageFiles = false; - } + this.language = parameters.getTranslationLocale().toString(); if (loadTextsInSeparateThread) { this.textLoaderThread = new Thread(this); @@ -67,8 +41,8 @@ public TextManager(Parameters parameters, boolean loadTextsInSeparateThread, boo private TextManager(boolean debug) { this.debug = debug; - this.gameTable = new Hashtable(); - this.sharedTable = new Hashtable(); + this.gameTable = new Hashtable<>(); + this.sharedTable = new Hashtable<>(); this.errorMessage = null; this.textLoaderThread = null; } @@ -540,168 +514,36 @@ private String method1728(long var1, int var3) { protected String getText(String key, int quantity) { key = key.toLowerCase(); - if (this.useLanguageFiles) { - LocalizationNode localizationNode = (LocalizationNode) this.gameTable.get(key); - return localizationNode == null ? null : localizationNode.getLocalization(quantity); - } else { - return (String) this.gameTable.get(key); - } + LocalizationNode localizationNode = this.gameTable.get(key); + return localizationNode == null ? null : localizationNode.getLocalization(quantity); } protected String getSharedString(String key, int quantity) { key = key.toLowerCase(); - if (this.useLanguageFiles) { - LocalizationNode localizationNode = (LocalizationNode) this.sharedTable.get(key); - return localizationNode == null ? null : localizationNode.getLocalization(quantity); - } else { - return (String) this.sharedTable.get(key); - } + LocalizationNode localizationNode = this.sharedTable.get(key); + return localizationNode == null ? null : localizationNode.getLocalization(quantity); } private void loadTexts(Applet applet) { - if (this.useLanguageFiles) { - this.loadLanguageFiles(applet); - } else { - this.loadLocaleFiles(applet); - } - } - - private void loadLocaleFiles(Applet applet) { - URL codeBase = applet.getCodeBase(); - this.gameTable = this.loadLocalizationTable(codeBase); - - try { - if (FileUtil.isFileUrl(codeBase)) { - codeBase = new URL(codeBase, FileUtil.RESOURCE_DIR); - } else { - codeBase = new URL(codeBase, "../Shared/"); - } - } catch (MalformedURLException e) { - } - - this.sharedTable = this.loadLocalizationTable(codeBase); - } - - private Hashtable loadLocalizationTable(URL baseUrl) { - Hashtable localizationTable = new Hashtable<>(); - BufferedReader reader = null; - String languageFileName = this.language + ".loc"; - - try { - URL localeUrl = new URL(baseUrl, "locale/"); - localeUrl = new URL(localeUrl, languageFileName); - InputStream inputStream = localeUrl.openStream(); - - InputStreamReader inputStreamReader; - try { - inputStreamReader = new InputStreamReader(inputStream, "Cp1252"); - } catch (UnsupportedEncodingException e) { - inputStreamReader = new InputStreamReader(inputStream); - } - - reader = new BufferedReader(inputStreamReader); - - String line; - while ((line = reader.readLine()) != null) { - line = line.trim(); - if (line.length() > 0 && line.charAt(0) != '#') { - int equalSignLocation = line.indexOf('='); - if (equalSignLocation <= 0) { - if (this.debug) { - System.out.println( - "Missing '='-character in \"" + this.language + "\"-locale file: \"" + line + "\""); - Thread.dumpStack(); - } - } else { - String key = line.substring(0, equalSignLocation).trim(); - if (key.length() == 0) { - if (this.debug) { - System.out.println( - "Empty key in \"" + this.language + "\"-locale file: \"" + line + "\""); - Thread.dumpStack(); - } - } else { - localizationTable.put( - key.toLowerCase(), - line.substring(equalSignLocation + 1).trim()); - } - } - } - } - } catch (FileNotFoundException e) { - if (this.debug) { - System.out.println("Missing localization file \"" + languageFileName + "\""); - } - - this.errorMessage = "Texts for '" + this.language + "' not available"; - localizationTable = null; - } catch (Exception e) { - if (this.debug) { - e.printStackTrace(); - } - - this.errorMessage = e.toString(); - localizationTable = null; - } - - try { - reader.close(); - } catch (Exception e) { - } - - return localizationTable; + this.loadLanguageFiles(applet); } private void loadLanguageFiles(Applet applet) { - URL codeBase = applet.getCodeBase(); - String var5 = null; - int slashLocation = this.language.indexOf('/'); - if (slashLocation > 0) { - var5 = this.language.substring(slashLocation + 1); - this.language = this.language.substring(0, slashLocation); + String codeBasePath = applet.getCodeBase().toString(); + if (codeBasePath.endsWith("/")) { + codeBasePath = codeBasePath.substring(0, codeBasePath.length() - 1); } - String languageDirectory; - String gameFilename; - String var7; - int var8; - if (FileUtil.isFileUrl(codeBase)) { - var7 = codeBase.toString(); - var8 = var7.indexOf(':', var7.indexOf(':') + 1) + 2; - int var9 = var7.indexOf('/', var8); - - try { - URL var10 = new URL(codeBase, FileUtil.LANGUAGE_DIR); - var10 = new URL(var10, this.language + "/"); - languageDirectory = var10.toExternalForm(); - } catch (MalformedURLException e) { - languageDirectory = "file:" + FileUtil.LANGUAGE_DIR + this.language + "/"; - } - - gameFilename = var7.substring(var8, var9); - } else { - var7 = codeBase.toString(); - var8 = var7.length(); - if (var7.charAt(var8 - 1) == '/') { - var7 = var7.substring(0, var8 - 1); - --var8; - } - - int slashLocation2 = var7.lastIndexOf('/'); - languageDirectory = var7.substring(0, slashLocation2 + 1) + "l10n/" + this.language + "/"; - gameFilename = var7.substring(slashLocation2 + 1); - } - - if (var5 != null) { - gameFilename = var5; - } + int slashLocation = codeBasePath.lastIndexOf('/'); + String languageDirectoryPath = codeBasePath.substring(0, slashLocation + 1) + "l10n/" + this.language + "/"; + String gameFilename = codeBasePath.substring(slashLocation + 1); - this.gameTable = this.readTable(languageDirectory + gameFilename + ".xml"); - this.sharedTable = this.readTable(languageDirectory + "Shared.xml"); + this.gameTable = this.readTable(languageDirectoryPath + gameFilename + ".xml"); + this.sharedTable = this.readTable(languageDirectoryPath + "Shared.xml"); } private Hashtable readTable(String fileUrl) { - EncodedXmlReader reader = new EncodedXmlReader(fileUrl, /*this.aBoolean1519*/ true); + EncodedXmlReader reader = new EncodedXmlReader(fileUrl, true); XmlUnit unit = reader.readXmlUnit(); if (unit == null) { System.out.println("Failed to read localization file '" + fileUrl + "'");