2022-11-25 01:42:17 +00:00
|
|
|
using System;
|
2020-12-22 03:13:18 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
public static partial class Util
|
2020-12-22 03:13:18 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
public static List<ComboItem> GetCountryRegionList(string textFile, string lang)
|
2020-12-22 03:13:18 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
string[] inputCSV = GetStringList(textFile);
|
|
|
|
int index = GeoLocation.GetLanguageIndex(lang);
|
|
|
|
var list = GetCBListFromCSV(inputCSV, index + 1);
|
2023-02-27 01:41:22 +00:00
|
|
|
list.Sort(1, list.Count - 1, Comparer); // keep null value as first
|
2022-06-18 18:04:24 +00:00
|
|
|
return list;
|
|
|
|
}
|
2020-12-22 03:13:18 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static List<ComboItem> GetCBListFromCSV(IReadOnlyList<string> inputCSV, int index)
|
|
|
|
{
|
|
|
|
var arr = new List<ComboItem>(inputCSV.Count);
|
|
|
|
foreach (var line in inputCSV)
|
2020-12-22 03:13:18 +00:00
|
|
|
{
|
2023-02-27 01:41:22 +00:00
|
|
|
var text = StringUtil.GetNthEntry(line, index, 4);
|
2022-06-18 18:04:24 +00:00
|
|
|
var value = line.AsSpan(0, 3);
|
|
|
|
var item = new ComboItem(text, ToInt32(value));
|
|
|
|
arr.Add(item);
|
2020-12-22 03:13:18 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return arr;
|
|
|
|
}
|
2020-12-22 03:13:18 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static List<ComboItem> GetCBList(ReadOnlySpan<string> inStrings)
|
|
|
|
{
|
|
|
|
var list = new List<ComboItem>(inStrings.Length);
|
|
|
|
for (int i = 0; i < inStrings.Length; i++)
|
|
|
|
list.Add(new ComboItem(inStrings[i], i));
|
|
|
|
list.Sort(Comparer);
|
|
|
|
return list;
|
|
|
|
}
|
2020-12-22 03:13:18 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static List<ComboItem> GetCBList(IReadOnlyList<string> inStrings, IReadOnlyList<ushort> allowed)
|
|
|
|
{
|
|
|
|
var list = new List<ComboItem>(allowed.Count + 1) { new(inStrings[0], 0) };
|
|
|
|
foreach (var index in allowed)
|
|
|
|
list.Add(new ComboItem(inStrings[index], index));
|
|
|
|
list.Sort(Comparer);
|
|
|
|
return list;
|
|
|
|
}
|
2020-12-22 03:13:18 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static List<ComboItem> GetCBList(IReadOnlyList<string> inStrings, int index, int offset = 0)
|
|
|
|
{
|
|
|
|
var list = new List<ComboItem>();
|
|
|
|
AddCBWithOffset(list, inStrings, offset, index);
|
|
|
|
return list;
|
|
|
|
}
|
2020-12-22 03:13:18 +00:00
|
|
|
|
2022-11-25 01:42:17 +00:00
|
|
|
public static ComboItem[] GetUnsortedCBList(IReadOnlyList<string> inStrings, ReadOnlySpan<byte> allowed)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
var count = allowed.Length;
|
|
|
|
var list = new ComboItem[count];
|
|
|
|
for (var i = 0; i < allowed.Length; i++)
|
2020-12-28 21:32:36 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var index = allowed[i];
|
|
|
|
var item = new ComboItem(inStrings[index], index);
|
|
|
|
list[i] = item;
|
2020-12-28 21:32:36 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void AddCBWithOffset(List<ComboItem> list, IReadOnlyList<string> inStrings, int offset, int index)
|
|
|
|
{
|
|
|
|
var item = new ComboItem(inStrings[index - offset], index);
|
|
|
|
list.Add(item);
|
|
|
|
}
|
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
public static void AddCBWithOffset(List<ComboItem> cbList, IReadOnlyList<string> inStrings, int offset, ReadOnlySpan<byte> allowed)
|
2022-11-25 01:42:17 +00:00
|
|
|
{
|
|
|
|
int beginCount = cbList.Count;
|
|
|
|
cbList.Capacity += allowed.Length;
|
|
|
|
foreach (var index in allowed)
|
|
|
|
{
|
|
|
|
var item = new ComboItem(inStrings[index - offset], index);
|
|
|
|
cbList.Add(item);
|
|
|
|
}
|
|
|
|
cbList.Sort(beginCount, allowed.Length, Comparer);
|
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static void AddCBWithOffset(List<ComboItem> cbList, IReadOnlyList<string> inStrings, int offset, ushort[] allowed)
|
|
|
|
{
|
|
|
|
int beginCount = cbList.Count;
|
|
|
|
cbList.Capacity += allowed.Length;
|
|
|
|
foreach (var index in allowed)
|
2020-12-22 03:13:18 +00:00
|
|
|
{
|
|
|
|
var item = new ComboItem(inStrings[index - offset], index);
|
2022-06-18 18:04:24 +00:00
|
|
|
cbList.Add(item);
|
2020-12-22 03:13:18 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
cbList.Sort(beginCount, allowed.Length, Comparer);
|
|
|
|
}
|
2020-12-22 03:13:18 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static void AddCBWithOffset(List<ComboItem> cbList, ReadOnlySpan<string> inStrings, int offset)
|
|
|
|
{
|
|
|
|
int beginCount = cbList.Count;
|
|
|
|
cbList.Capacity += inStrings.Length;
|
|
|
|
for (int i = 0; i < inStrings.Length; i++)
|
2020-12-22 03:13:18 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var x = inStrings[i];
|
|
|
|
var item = new ComboItem(x, i + offset);
|
|
|
|
cbList.Add(item);
|
2020-12-22 03:13:18 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
cbList.Sort(beginCount, inStrings.Length, Comparer);
|
|
|
|
}
|
2020-12-22 03:13:18 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static ComboItem[] GetVariedCBListBall(string[] inStrings, ReadOnlySpan<ushort> stringNum, ReadOnlySpan<byte> stringVal)
|
|
|
|
{
|
|
|
|
const int forcedTop = 3; // 3 Balls are preferentially first
|
|
|
|
var list = new ComboItem[forcedTop + stringNum.Length];
|
|
|
|
list[0] = new ComboItem(inStrings[4], (int)Ball.Poke);
|
|
|
|
list[1] = new ComboItem(inStrings[3], (int)Ball.Great);
|
|
|
|
list[2] = new ComboItem(inStrings[2], (int)Ball.Ultra);
|
2021-05-07 06:26:38 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
for (int i = 0; i < stringNum.Length; i++)
|
2020-12-22 03:13:18 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
int index = stringNum[i];
|
|
|
|
var value = stringVal[i];
|
|
|
|
var text = inStrings[index];
|
|
|
|
list[i + 3] = new ComboItem(text, value);
|
2020-12-22 03:13:18 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
Array.Sort(list, 3, list.Length - 3, Comparer);
|
|
|
|
return list;
|
|
|
|
}
|
2020-12-22 03:13:18 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static readonly FunctorComparer<ComboItem> Comparer =
|
|
|
|
new((a, b) => string.CompareOrdinal(a.Text, b.Text));
|
|
|
|
|
|
|
|
private sealed class FunctorComparer<T> : IComparer<T>
|
|
|
|
{
|
|
|
|
private readonly Comparison<T> Comparison;
|
|
|
|
public FunctorComparer(Comparison<T> comparison) => Comparison = comparison;
|
2023-01-22 04:02:33 +00:00
|
|
|
public int Compare(T? x, T? y)
|
|
|
|
{
|
|
|
|
if (x == null)
|
|
|
|
return y == null ? 0 : -1;
|
|
|
|
return y == null ? 1 : Comparison(x, y);
|
|
|
|
}
|
2020-12-22 03:13:18 +00:00
|
|
|
}
|
|
|
|
}
|