2022-11-24 17:42:17 -08:00
|
|
|
using System;
|
2020-12-21 19:13:18 -08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
2022-06-18 11:04:24 -07:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
public static partial class Util
|
2020-12-21 19:13:18 -08:00
|
|
|
{
|
2022-06-18 11:04:24 -07:00
|
|
|
public static List<ComboItem> GetCountryRegionList(string textFile, string lang)
|
2020-12-21 19:13:18 -08:00
|
|
|
{
|
2022-06-18 11:04:24 -07:00
|
|
|
string[] inputCSV = GetStringList(textFile);
|
|
|
|
int index = GeoLocation.GetLanguageIndex(lang);
|
2023-03-20 15:52:27 -07:00
|
|
|
var list = GetCBListFromCSV(inputCSV, index);
|
2023-03-10 19:34:49 -08:00
|
|
|
if (list.Count > 1)
|
|
|
|
list.Sort(1, list.Count - 1, Comparer); // keep null value as first
|
2022-06-18 11:04:24 -07:00
|
|
|
return list;
|
|
|
|
}
|
2020-12-21 19:13:18 -08:00
|
|
|
|
2023-03-21 18:19:55 -07:00
|
|
|
private static List<ComboItem> GetCBListFromCSV(ReadOnlySpan<string> inputCSV, int index)
|
2022-06-18 11:04:24 -07:00
|
|
|
{
|
2023-03-21 18:19:55 -07:00
|
|
|
var arr = new List<ComboItem>(inputCSV.Length);
|
2022-06-18 11:04:24 -07:00
|
|
|
foreach (var line in inputCSV)
|
2020-12-21 19:13:18 -08:00
|
|
|
{
|
2023-03-20 15:52:27 -07:00
|
|
|
var span = line.AsSpan();
|
|
|
|
// 3 digits index; 1 tab space, then the string entries.
|
|
|
|
var value = int.Parse(span[..3]);
|
|
|
|
var text = StringUtil.GetNthEntry(span[4..], index);
|
|
|
|
var item = new ComboItem(new string(text), value);
|
2022-06-18 11:04:24 -07:00
|
|
|
arr.Add(item);
|
2020-12-21 19:13:18 -08:00
|
|
|
}
|
2022-06-18 11:04:24 -07:00
|
|
|
return arr;
|
|
|
|
}
|
2020-12-21 19:13:18 -08:00
|
|
|
|
2022-06-18 11:04:24 -07: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-21 19:13:18 -08:00
|
|
|
|
2023-04-13 00:05:10 -07:00
|
|
|
public static List<ComboItem> GetCBList(ReadOnlySpan<string> inStrings, ReadOnlySpan<ushort> allowed)
|
2022-06-18 11:04:24 -07:00
|
|
|
{
|
2023-03-25 17:55:55 -07:00
|
|
|
var list = new List<ComboItem>(allowed.Length + 1) { new(inStrings[0], 0) };
|
2022-06-18 11:04:24 -07:00
|
|
|
foreach (var index in allowed)
|
|
|
|
list.Add(new ComboItem(inStrings[index], index));
|
|
|
|
list.Sort(Comparer);
|
|
|
|
return list;
|
|
|
|
}
|
2020-12-21 19:13:18 -08:00
|
|
|
|
2023-04-13 00:05:10 -07:00
|
|
|
public static List<ComboItem> GetCBList(ReadOnlySpan<string> inStrings, int index, int offset = 0)
|
2022-06-18 11:04:24 -07:00
|
|
|
{
|
|
|
|
var list = new List<ComboItem>();
|
|
|
|
AddCBWithOffset(list, inStrings, offset, index);
|
|
|
|
return list;
|
|
|
|
}
|
2020-12-21 19:13:18 -08:00
|
|
|
|
2023-04-13 00:05:10 -07:00
|
|
|
public static ComboItem[] GetUnsortedCBList(ReadOnlySpan<string> inStrings, ReadOnlySpan<byte> allowed)
|
2022-06-18 11:04:24 -07:00
|
|
|
{
|
|
|
|
var count = allowed.Length;
|
|
|
|
var list = new ComboItem[count];
|
|
|
|
for (var i = 0; i < allowed.Length; i++)
|
2020-12-28 13:32:36 -08:00
|
|
|
{
|
2022-06-18 11:04:24 -07:00
|
|
|
var index = allowed[i];
|
|
|
|
var item = new ComboItem(inStrings[index], index);
|
|
|
|
list[i] = item;
|
2020-12-28 13:32:36 -08:00
|
|
|
}
|
|
|
|
|
2022-06-18 11:04:24 -07:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2023-04-13 00:05:10 -07:00
|
|
|
public static void AddCBWithOffset(List<ComboItem> list, ReadOnlySpan<string> inStrings, int offset, int index)
|
2022-06-18 11:04:24 -07:00
|
|
|
{
|
|
|
|
var item = new ComboItem(inStrings[index - offset], index);
|
|
|
|
list.Add(item);
|
|
|
|
}
|
|
|
|
|
2023-04-13 00:05:10 -07:00
|
|
|
public static void AddCBWithOffset(List<ComboItem> cbList, ReadOnlySpan<string> inStrings, int offset, ReadOnlySpan<byte> allowed)
|
2022-11-24 17:42:17 -08: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);
|
|
|
|
}
|
|
|
|
|
2023-04-13 00:05:10 -07:00
|
|
|
public static void AddCBWithOffset(List<ComboItem> cbList, ReadOnlySpan<string> inStrings, int offset, ReadOnlySpan<ushort> allowed)
|
2022-06-18 11:04:24 -07:00
|
|
|
{
|
|
|
|
int beginCount = cbList.Count;
|
|
|
|
cbList.Capacity += allowed.Length;
|
|
|
|
foreach (var index in allowed)
|
2020-12-21 19:13:18 -08:00
|
|
|
{
|
|
|
|
var item = new ComboItem(inStrings[index - offset], index);
|
2022-06-18 11:04:24 -07:00
|
|
|
cbList.Add(item);
|
2020-12-21 19:13:18 -08:00
|
|
|
}
|
2022-06-18 11:04:24 -07:00
|
|
|
cbList.Sort(beginCount, allowed.Length, Comparer);
|
|
|
|
}
|
2020-12-21 19:13:18 -08:00
|
|
|
|
2022-06-18 11:04:24 -07: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-21 19:13:18 -08:00
|
|
|
{
|
2022-06-18 11:04:24 -07:00
|
|
|
var x = inStrings[i];
|
|
|
|
var item = new ComboItem(x, i + offset);
|
|
|
|
cbList.Add(item);
|
2020-12-21 19:13:18 -08:00
|
|
|
}
|
2022-06-18 11:04:24 -07:00
|
|
|
cbList.Sort(beginCount, inStrings.Length, Comparer);
|
|
|
|
}
|
2020-12-21 19:13:18 -08:00
|
|
|
|
2023-03-27 00:11:42 -07:00
|
|
|
public static ComboItem[] GetVariedCBListBall(ReadOnlySpan<string> itemNames, ReadOnlySpan<byte> ballIndex, ReadOnlySpan<ushort> ballItemID)
|
2022-06-18 11:04:24 -07:00
|
|
|
{
|
2023-03-27 00:11:42 -07:00
|
|
|
var list = new ComboItem[ballItemID.Length];
|
|
|
|
for (int i = 0; i < ballItemID.Length; i++)
|
|
|
|
list[i] = new ComboItem(itemNames[ballItemID[i]], ballIndex[i]);
|
2021-05-06 23:26:38 -07:00
|
|
|
|
2023-03-27 00:11:42 -07:00
|
|
|
// 3 Balls are preferentially first, sort Master Ball with the rest Alphabetically.
|
|
|
|
list.AsSpan(3).Sort(Comparer);
|
2022-06-18 11:04:24 -07:00
|
|
|
return list;
|
|
|
|
}
|
2020-12-21 19:13:18 -08:00
|
|
|
|
2022-06-18 11:04:24 -07: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-21 20:02:33 -08: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-21 19:13:18 -08:00
|
|
|
}
|
|
|
|
}
|