2022-11-25 01:42:17 +00:00
|
|
|
using System;
|
2020-12-22 03:13:18 +00:00
|
|
|
using System.Collections.Generic;
|
2024-06-01 22:56:20 +00:00
|
|
|
using static PKHeX.Core.MessageStrings;
|
|
|
|
using static PKHeX.Core.GeonetPoint;
|
2020-12-22 03:13:18 +00:00
|
|
|
|
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);
|
2023-03-20 22:52:27 +00:00
|
|
|
var list = GetCBListFromCSV(inputCSV, index);
|
2023-03-11 03:34:49 +00:00
|
|
|
if (list.Count > 1)
|
|
|
|
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
|
|
|
|
2024-06-01 22:56:20 +00:00
|
|
|
public static List<ComboItem> GetGeonetPointList() =>
|
|
|
|
[
|
|
|
|
new (MsgGeonetPointNone, (int)None),
|
|
|
|
new (MsgGeonetPointBlue, (int)Blue),
|
|
|
|
new (MsgGeonetPointYellow, (int)Yellow),
|
|
|
|
new (MsgGeonetPointRed, (int)Red),
|
|
|
|
];
|
|
|
|
|
2023-03-22 01:19:55 +00:00
|
|
|
private static List<ComboItem> GetCBListFromCSV(ReadOnlySpan<string> inputCSV, int index)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-03-22 01:19:55 +00:00
|
|
|
var arr = new List<ComboItem>(inputCSV.Length);
|
2022-06-18 18:04:24 +00:00
|
|
|
foreach (var line in inputCSV)
|
2020-12-22 03:13:18 +00:00
|
|
|
{
|
2023-03-20 22:52:27 +00: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 18:04:24 +00:00
|
|
|
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
|
|
|
|
2023-04-13 07:05:10 +00:00
|
|
|
public static List<ComboItem> GetCBList(ReadOnlySpan<string> inStrings, ReadOnlySpan<ushort> allowed)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-03-26 00:55:55 +00:00
|
|
|
var list = new List<ComboItem>(allowed.Length + 1) { new(inStrings[0], 0) };
|
2022-06-18 18:04:24 +00:00
|
|
|
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
|
|
|
|
2023-04-13 07:05:10 +00:00
|
|
|
public static List<ComboItem> GetCBList(ReadOnlySpan<string> inStrings, int index, int offset = 0)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
var list = new List<ComboItem>();
|
|
|
|
AddCBWithOffset(list, inStrings, offset, index);
|
|
|
|
return list;
|
|
|
|
}
|
2020-12-22 03:13:18 +00:00
|
|
|
|
2023-04-13 07:05:10 +00:00
|
|
|
public static ComboItem[] GetUnsortedCBList(ReadOnlySpan<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;
|
|
|
|
}
|
|
|
|
|
2023-04-13 07:05:10 +00:00
|
|
|
public static void AddCBWithOffset(List<ComboItem> list, ReadOnlySpan<string> inStrings, int offset, int index)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
var item = new ComboItem(inStrings[index - offset], index);
|
|
|
|
list.Add(item);
|
|
|
|
}
|
|
|
|
|
2023-04-13 07:05:10 +00:00
|
|
|
public static void AddCBWithOffset(List<ComboItem> cbList, ReadOnlySpan<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);
|
|
|
|
}
|
|
|
|
|
2023-04-13 07:05:10 +00:00
|
|
|
public static void AddCBWithOffset(List<ComboItem> cbList, ReadOnlySpan<string> inStrings, int offset, ReadOnlySpan<ushort> allowed)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2023-03-27 07:11:42 +00:00
|
|
|
public static ComboItem[] GetVariedCBListBall(ReadOnlySpan<string> itemNames, ReadOnlySpan<byte> ballIndex, ReadOnlySpan<ushort> ballItemID)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-03-27 07:11:42 +00: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-07 06:26:38 +00:00
|
|
|
|
2023-03-27 07:11:42 +00:00
|
|
|
// 3 Balls are preferentially first, sort Master Ball with the rest Alphabetically.
|
|
|
|
list.AsSpan(3).Sort(Comparer);
|
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 readonly FunctorComparer<ComboItem> Comparer =
|
|
|
|
new((a, b) => string.CompareOrdinal(a.Text, b.Text));
|
|
|
|
|
2023-12-04 04:13:20 +00:00
|
|
|
private sealed class FunctorComparer<T>(Comparison<T> Comparison) : IComparer<T>
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|