using System; using System.Collections.Generic; namespace PKHeX.Core { public static partial class Util { public static List GetCountryRegionList(string textFile, string lang) { string[] inputCSV = GetStringList(textFile); int index = GeoLocation.GetLanguageIndex(lang); var list = GetCBListFromCSV(inputCSV, index + 1); list.Sort(Comparer); return list; } private static List GetCBListFromCSV(IReadOnlyList inputCSV, int index) { var arr = new List(inputCSV.Count); foreach (var line in inputCSV) { var val = line[..3]; var text = StringUtil.GetNthEntry(line, index, 4); var item = new ComboItem(text, Convert.ToInt32(val)); arr.Add(item); } return arr; } public static List GetCBList(ReadOnlySpan inStrings) { var list = new List(inStrings.Length); for (int i = 0; i < inStrings.Length; i++) list.Add(new ComboItem(inStrings[i], i)); list.Sort(Comparer); return list; } public static List GetCBList(IReadOnlyList inStrings, IReadOnlyList allowed) { var list = new List(allowed.Count + 1) { new(inStrings[0], 0) }; foreach (var index in allowed) list.Add(new ComboItem(inStrings[index], index)); list.Sort(Comparer); return list; } public static List GetCBList(IReadOnlyList inStrings, int index, int offset = 0) { var list = new List(); AddCBWithOffset(list, inStrings, offset, index); return list; } public static IReadOnlyList GetUnsortedCBList(IReadOnlyList inStrings, IReadOnlyList allowed) { var count = allowed.Count; var list = new ComboItem[count]; for (var i = 0; i < allowed.Count; i++) { var index = allowed[i]; var item = new ComboItem(inStrings[index], index); list[i] = item; } return list; } public static List GetCBList(IReadOnlyList inStrings, int[] allowed) { var list = new List(allowed.Length); AddCB(list, inStrings, allowed); return list; } public static void AddCBWithOffset(List list, IReadOnlyList inStrings, int offset, int index) { var item = new ComboItem(inStrings[index - offset], index); list.Add(item); } public static void AddCBWithOffset(List cbList, IReadOnlyList inStrings, int offset, int[] allowed) { int beginCount = cbList.Count; foreach (var index in allowed) { var item = new ComboItem(inStrings[index - offset], index); cbList.Add(item); } cbList.Sort(beginCount, allowed.Length, Comparer); } public static void AddCBWithOffset(List cbList, Span inStrings, int offset) { int beginCount = cbList.Count; for (int i = 0; i < inStrings.Length; i++) { var x = inStrings[i]; var item = new ComboItem(x, i + offset); cbList.Add(item); } cbList.Sort(beginCount, inStrings.Length, Comparer); } public static void AddCB(List cbList, IReadOnlyList inStrings, int[] allowed) { int beginCount = cbList.Count; foreach (var index in allowed) { var item = new ComboItem(inStrings[index], index); cbList.Add(item); } cbList.Sort(beginCount, allowed.Length, Comparer); } public static List GetVariedCBListBall(string[] inStrings, ushort[] stringNum, byte[] stringVal) { const int forcedTop = 3; // 3 Balls are preferentially first var list = new List(forcedTop + stringNum.Length) { new(inStrings[4], (int)Ball.Poke), new(inStrings[3], (int)Ball.Great), new(inStrings[2], (int)Ball.Ultra), }; for (int i = 0; i < stringNum.Length; i++) { int index = stringNum[i]; var val = stringVal[i]; var txt = inStrings[index]; list.Add(new ComboItem(txt, val)); } list.Sort(forcedTop, stringNum.Length, Comparer); return list; } private static readonly FunctorComparer Comparer = new((a, b) => string.CompareOrdinal(a.Text, b.Text)); private sealed class FunctorComparer : IComparer { private readonly Comparison Comparison; public FunctorComparer(Comparison comparison) => Comparison = comparison; public int Compare(T x, T y) => Comparison(x, y); } } }