From a80a2489a4d674371adc708f6a433c2348cc2120 Mon Sep 17 00:00:00 2001 From: Evan Dixon Date: Fri, 24 Mar 2017 12:59:45 -0500 Subject: [PATCH 1/2] Refactored implementation of #959 --- PKHeX.WinForms/MainWindow/Main.cs | 3 +- PKHeX/Game/GameInfo.cs | 7 -- PKHeX/Legality/Analysis.cs | 2 +- PKHeX/Legality/Checks.cs | 2 +- ...heckStrings.cs => LegalityCheckStrings.cs} | 51 +---------- PKHeX/PKHeX.Core.csproj | 6 +- PKHeX/Properties/Resources.Designer.cs | 74 ++++++++------- PKHeX/Properties/Resources.resx | 8 +- ...ity_en.txt => LegalityCheckStrings_en.txt} | 0 ...ity_zh.txt => LegalityCheckStrings_zh.txt} | 0 PKHeX/Util/DataUtil.cs | 91 ++++++++++++++++++- 11 files changed, 138 insertions(+), 106 deletions(-) rename PKHeX/Legality/{CheckStrings.cs => LegalityCheckStrings.cs} (92%) rename PKHeX/Resources/text/en/{legality_en.txt => LegalityCheckStrings_en.txt} (100%) rename PKHeX/Resources/text/zh/{legality_zh.txt => LegalityCheckStrings_zh.txt} (100%) diff --git a/PKHeX.WinForms/MainWindow/Main.cs b/PKHeX.WinForms/MainWindow/Main.cs index 1ad982606..24ba6aed2 100644 --- a/PKHeX.WinForms/MainWindow/Main.cs +++ b/PKHeX.WinForms/MainWindow/Main.cs @@ -12,6 +12,7 @@ using PKHeX.Core; using PKHeX.Core.Properties; using System.Configuration; +using System.Threading.Tasks; namespace PKHeX.WinForms { @@ -1419,7 +1420,7 @@ private void InitializeStrings() // Update Legality Strings // Clipboard.SetText(string.Join(Environment.NewLine, CheckStrings.getLocalization())); - new Thread(() => { CheckStrings.setLocalization(GameInfo.getCheckStrings(l)); }).Start(); + Task.Run(() => Util.setLocalization(typeof(LegalityCheckStrings))); // Force an update to the met locations origintrack = GameVersion.Unknown; diff --git a/PKHeX/Game/GameInfo.cs b/PKHeX/Game/GameInfo.cs index 92162416e..8954de5f0 100644 --- a/PKHeX/Game/GameInfo.cs +++ b/PKHeX/Game/GameInfo.cs @@ -9,9 +9,7 @@ public static class GameInfo private static readonly string[] ptransp = { "ポケシフター", "Poké Transfer", "Poké Fret", "Pokétrasporto", "Poképorter", "Pokétransfer", "포케시프터", "宝可传送", "寶可傳送", "ポケシフター" }; public static readonly string[] lang_val = { "ja", "en", "fr", "it", "de", "es", "ko", "zh", "zh2", "pt" }; private const string DefaultLanguage = "en"; - private const string LegalityName = "legality_"; private static readonly GameStrings[] Languages = new GameStrings[lang_val.Length]; - private static readonly string[][] CheckStrings = new string[lang_val.Length][]; // Lazy fetch implementation private static int DefaultLanguageIndex => Array.IndexOf(lang_val, DefaultLanguage); @@ -25,11 +23,6 @@ public static GameStrings getStrings(string lang) int index = getLanguageIndex(lang); return Languages[index] ?? (Languages[index] = new GameStrings(lang_val[index])); } - public static IEnumerable getCheckStrings(string lang) - { - int index = getLanguageIndex(lang); - return CheckStrings[index] ?? (CheckStrings[index] = Util.getStringList(LegalityName + lang_val[index])); - } private static string getTransporterName(string lang) { int index = getLanguageIndex(lang); diff --git a/PKHeX/Legality/Analysis.cs b/PKHeX/Legality/Analysis.cs index 02293ec4e..1ea18e7fe 100644 --- a/PKHeX/Legality/Analysis.cs +++ b/PKHeX/Legality/Analysis.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; -using static PKHeX.Core.CheckStrings; +using static PKHeX.Core.LegalityCheckStrings; namespace PKHeX.Core { diff --git a/PKHeX/Legality/Checks.cs b/PKHeX/Legality/Checks.cs index 39d08d1b6..1f63c6c67 100644 --- a/PKHeX/Legality/Checks.cs +++ b/PKHeX/Legality/Checks.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; -using static PKHeX.Core.CheckStrings; +using static PKHeX.Core.LegalityCheckStrings; namespace PKHeX.Core { diff --git a/PKHeX/Legality/CheckStrings.cs b/PKHeX/Legality/LegalityCheckStrings.cs similarity index 92% rename from PKHeX/Legality/CheckStrings.cs rename to PKHeX/Legality/LegalityCheckStrings.cs index b934f439f..c91d46910 100644 --- a/PKHeX/Legality/CheckStrings.cs +++ b/PKHeX/Legality/LegalityCheckStrings.cs @@ -7,57 +7,8 @@ namespace PKHeX.Core { - public static class CheckStrings + public static class LegalityCheckStrings { - private const string splitter = " = "; - private static readonly Type t = typeof(CheckStrings); - private static string[] getProps(IEnumerable input) - { - return input.Select(l => l.Substring(0, l.IndexOf(splitter, StringComparison.Ordinal))).ToArray(); - } - private static IEnumerable DumpStrings() - { - var props = ReflectUtil.getPropertiesStartWithPrefix(t, "V"); - return props.Select(p => $"{p}{splitter}{ReflectUtil.GetValue(t, p).ToString()}"); - } - - public static void setLocalization(IEnumerable lines) - { - if (lines == null) - return; - foreach (var line in lines.Where(l => l != null)) - { - var index = line.IndexOf(splitter, StringComparison.Ordinal); - if (index < 0) - continue; - var prop = line.Substring(0, index); - var value = line.Substring(index + splitter.Length); - - try - { - ReflectUtil.SetValue(t, prop.ToUpper(), value); - } - catch - { - Console.WriteLine($"Property not present: {prop} || Value written: {value}"); - } - } - } - public static string[] getLocalization(string[] existingLines = null) - { - existingLines = existingLines ?? new string[0]; - var currentLines = DumpStrings().ToArray(); - var existing = getProps(existingLines); - var current = getProps(currentLines); - - var result = new string[currentLines.Length]; - for (int i = 0; i < current.Length; i++) - { - int index = Array.IndexOf(existing, current[i]); - result[i] = index < 0 ? currentLines[i] : existingLines[index]; - } - return result; - } #region General Strings diff --git a/PKHeX/PKHeX.Core.csproj b/PKHeX/PKHeX.Core.csproj index 855a7bc08..f39d07cb1 100644 --- a/PKHeX/PKHeX.Core.csproj +++ b/PKHeX/PKHeX.Core.csproj @@ -152,7 +152,7 @@ - + @@ -3521,8 +3521,8 @@ - - + +