Misc clean

Refactor out some logic, make "en" literals reference one spot (where
appropriate)
This commit is contained in:
Kurt 2020-01-01 19:07:21 -08:00
parent 7e490b8dcc
commit 166f8d82e5
10 changed files with 58 additions and 56 deletions

View file

@ -19,7 +19,7 @@ namespace PKHeX.Core
private static readonly char[] ParenJunk = { '[', ']', '(', ')' };
private static readonly ushort[] DashedSpecies = {782, 783, 784, 250, 032, 029}; // Kommo-o, Ho-Oh, Nidoran-M, Nidoran-F
private const int MAX_SPECIES = (int)Core.Species.MAX_COUNT - 1;
private const string Language = "en";
private const string Language = GameLanguage.DefaultLanguage;
private const int DefaultLanguageID = (int)Core.LanguageID.English;
private static readonly GameStrings DefaultStrings = GameInfo.GetStrings(Language);

View file

@ -31,8 +31,8 @@ namespace PKHeX.Core
private static readonly MiscVerifier Misc = new MiscVerifier();
private static readonly TransferVerifier Transfer = new TransferVerifier();
public static string[] MoveStrings { internal get; set; } = Util.GetMovesList("en");
public static string[] SpeciesStrings { internal get; set; } = Util.GetSpeciesList("en");
public static string[] MoveStrings { internal get; set; } = Util.GetMovesList(GameLanguage.DefaultLanguage);
public static string[] SpeciesStrings { internal get; set; } = Util.GetSpeciesList(GameLanguage.DefaultLanguage);
internal static IEnumerable<string> GetMoveNames(IEnumerable<int> moves) => moves.Select(m => (uint)m >= MoveStrings.Length ? L_AError : MoveStrings[m]);
}
}

View file

@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Collections.Generic;
using System.Linq;
namespace PKHeX.Core
@ -13,15 +11,13 @@ namespace PKHeX.Core
EncounterEvent.RefreshMGDB();
}
private const string DefaultLang = "en";
public static bool CanLearn(string species, IEnumerable<string> moves, string lang = DefaultLang)
public static bool CanLearn(string species, IEnumerable<string> moves, string lang = GameLanguage.DefaultLanguage)
{
var encs = GetLearn(species, moves, lang);
return encs.Any();
}
public static IEnumerable<string> GetLearnSummary(string species, IEnumerable<string> moves, string lang = DefaultLang)
public static IEnumerable<string> GetLearnSummary(string species, IEnumerable<string> moves, string lang = GameLanguage.DefaultLanguage)
{
var encs = GetLearn(species, moves, lang);
var msg = Summarize(encs).ToList();
@ -30,17 +26,17 @@ namespace PKHeX.Core
return msg;
}
public static IEnumerable<IEncounterable> GetLearn(string species, IEnumerable<string> moves, string lang = DefaultLang)
public static IEnumerable<IEncounterable> GetLearn(string species, IEnumerable<string> moves, string lang = GameLanguage.DefaultLanguage)
{
var str = GameInfo.GetStrings(lang);
if (str == null)
return Enumerable.Empty<IEncounterable>();
var spec = FindIndexIgnoreCase(str.specieslist, species);
var spec = StringUtil.FindIndexIgnoreCase(str.specieslist, species);
if (spec <= 0)
return Enumerable.Empty<IEncounterable>();
var moveIDs = moves.Select(z => FindIndexIgnoreCase(str.movelist, z)).Where(z => z > 0).ToArray();
var moveIDs = moves.Select(z => StringUtil.FindIndexIgnoreCase(str.movelist, z)).Where(z => z > 0).ToArray();
return GetLearn(spec, moveIDs);
}
@ -54,20 +50,6 @@ namespace PKHeX.Core
return EncounterMovesetGenerator.GenerateEncounters(blank, moveIDs, vers);
}
private static int FindIndexIgnoreCase(string[] arr, string val)
{
static bool Match(string item, string find)
{
if (item.Length != find.Length)
return false;
const CompareOptions options = CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase | CompareOptions.IgnoreSymbols;
var compare = CultureInfo.CurrentCulture.CompareInfo.Compare(item, find, options);
return compare == 0;
}
return Array.FindIndex(arr, i => Match(i, val));
}
public static IEnumerable<string> Summarize(IEnumerable<IEncounterable> encounters, bool advanced = false)
{
var types = encounters.GroupBy(z => z.Name);

View file

@ -64,7 +64,7 @@ namespace PKHeX.Core
{
switch (lang)
{
default: return "en";
default: return GameLanguage.DefaultLanguage;
case LanguageID.Japanese: return "ja";
case LanguageID.French: return "fr";

View file

@ -307,34 +307,13 @@ namespace PKHeX.Core
var zeroth = line.IndexOf(',');
var val = line.Substring(0, zeroth);
var text = GetNthEntry(line, index, zeroth);
var text = StringUtil.GetNthEntry(line, index, zeroth);
var item = new ComboItem(text, Convert.ToInt32(val));
arr.Add(item);
}
return arr;
}
private static string GetNthEntry(string line, int nth, int start)
{
if (nth != 1)
start = line.IndexOfNth(',', nth - 1, start + 1);
var end = line.IndexOfNth(',', 1, start + 1);
return end < 0 ? line.Substring(start + 1) : line.Substring(start + 1, end - start - 1);
}
private static int IndexOfNth(this string s, char t, int n, int start)
{
int count = 0;
for (int i = start; i < s.Length; i++)
{
if (s[i] != t)
continue;
if (++count == n)
return i;
}
return -1;
}
public static List<ComboItem> GetCBList(IReadOnlyList<string> inStrings)
{
var list = GetCBListUnsorted(inStrings);

View file

@ -0,0 +1,41 @@
using System;
using System.Globalization;
namespace PKHeX.Core
{
public static class StringUtil
{
private static readonly CultureInfo SearchCulture = CultureInfo.CurrentCulture;
public static int FindIndexIgnoreCase(string[] arr, string value) => Array.FindIndex(arr, z => IsMatchIgnoreCase(z, value));
private static bool IsMatchIgnoreCase(string string1, string string2)
{
if (string1.Length != string2.Length)
return false;
const CompareOptions options = CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase | CompareOptions.IgnoreSymbols;
var compare = SearchCulture.CompareInfo.Compare(string1, string2, options);
return compare == 0;
}
public static string GetNthEntry(string line, int nth, int start, char separator = ',')
{
if (nth != 1)
start = line.IndexOfNth(separator, nth - 1, start + 1);
var end = line.IndexOfNth(separator, 1, start + 1);
return end < 0 ? line.Substring(start + 1) : line.Substring(start + 1, end - start - 1);
}
private static int IndexOfNth(this string s, char t, int n, int start)
{
int count = 0;
for (int i = start; i < s.Length; i++)
{
if (s[i] != t)
continue;
if (++count == n)
return i;
}
return -1;
}
}
}

View file

@ -460,7 +460,7 @@ namespace PKHeX.WinForms
NUD_HallType07, NUD_HallType08, NUD_HallType09, NUD_HallType10, NUD_HallType11, NUD_HallType12,
NUD_HallType13, NUD_HallType14, NUD_HallType15, NUD_HallType16, NUD_HallType17
};
string[] TypeName = Util.GetTypesList("en");
string[] TypeName = GameInfo.Strings.types;
int[] typenameIndex = new[] { 0, 9, 10, 12, 11, 14, 1, 3, 4, 2, 13, 6, 5, 7, 15, 16, 8 };
for (int i = 0; i < HallNUDA.Length; i++)
tip2.SetToolTip(HallNUDA[i], TypeName[typenameIndex[i]]);

View file

@ -15,7 +15,7 @@ namespace PKHeX.WinForms
}
private static readonly string[] Languages = {"ja", "fr", "it", "de", "es", "ko", "zh"};
private const string DefaultLanguage = "en";
private const string DefaultLanguage = GameLanguage.DefaultLanguage;
public static bool IsUpdatingTranslations { get; private set; }

View file

@ -14,9 +14,9 @@ namespace PKHeX.Tests.General
}
[Fact]
public void StringsLoad() => GameInfo.GetStrings("en");
public void StringsLoad() => GameInfo.GetStrings(GameLanguage.DefaultLanguage);
[Fact]
public void SourcesLoad() => GameInfo.Strings = GameInfo.GetStrings("en");
public void SourcesLoad() => GameInfo.Strings = GameInfo.GetStrings(GameLanguage.DefaultLanguage);
}
}

View file

@ -9,14 +9,14 @@ namespace PKHeX.Tests.Util
[Fact]
public void GetsCorrectNumberOfSpeciesNames()
{
var names = Core.Util.GetSpeciesList("en");
var names = Core.Util.GetSpeciesList(GameLanguage.DefaultLanguage);
names.Length.Should().Be((int)Species.MAX_COUNT);
}
[Fact]
public void GetsCorrectNumberOfAbilityNames()
{
var names = Core.Util.GetAbilitiesList("en");
var names = Core.Util.GetAbilitiesList(GameLanguage.DefaultLanguage);
names.Length.Should().Be((int)Ability.MAX_COUNT);
}
}