2023-01-22 04:02:33 +00:00
|
|
|
using System;
|
2020-12-22 03:13:18 +00:00
|
|
|
using System.Collections.Generic;
|
2023-01-22 04:02:33 +00:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2020-12-22 03:13:18 +00:00
|
|
|
using System.IO;
|
|
|
|
using System.Reflection;
|
|
|
|
|
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
|
|
|
private static readonly Assembly thisAssembly = typeof(Util).GetTypeInfo().Assembly;
|
|
|
|
private static readonly Dictionary<string, string> resourceNameMap = BuildLookup(thisAssembly.GetManifestResourceNames());
|
2021-05-15 18:56:06 +00:00
|
|
|
|
2023-11-13 05:11:11 +00:00
|
|
|
private static Dictionary<string, string> BuildLookup(ReadOnlySpan<string> manifestNames)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-11-13 05:11:11 +00:00
|
|
|
var result = new Dictionary<string, string>(manifestNames.Length);
|
2022-06-18 18:04:24 +00:00
|
|
|
foreach (var resName in manifestNames)
|
2021-05-15 18:56:06 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var fileName = GetFileName(resName);
|
|
|
|
result.Add(fileName, resName);
|
2021-05-15 18:56:06 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return result;
|
|
|
|
}
|
2021-05-15 18:56:06 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static string GetFileName(string resName)
|
|
|
|
{
|
|
|
|
var period = resName.LastIndexOf('.', resName.Length - 5);
|
|
|
|
var start = period + 1;
|
|
|
|
System.Diagnostics.Debug.Assert(start != 0);
|
2020-12-22 03:13:18 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// text file fetch excludes ".txt" (mixed case...); other extensions are used (all lowercase).
|
|
|
|
return resName.EndsWith(".txt", StringComparison.Ordinal) ? resName[start..^4].ToLowerInvariant() : resName[start..];
|
|
|
|
}
|
2020-12-22 03:13:18 +00:00
|
|
|
|
2023-12-04 04:13:20 +00:00
|
|
|
private static readonly Dictionary<string, string[]> stringListCache = [];
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
private static readonly object getStringListLoadLock = new();
|
|
|
|
|
|
|
|
#region String Lists
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a list of all Pokémon species names.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="language">Language of the Pokémon species names to select (e.g. "en", "fr", "jp", etc.)</param>
|
|
|
|
/// <returns>An array of strings whose indexes correspond to the IDs of each Pokémon species name.</returns>
|
|
|
|
public static string[] GetSpeciesList(string language) => GetStringList("species", language);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a list of all move names.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="language">Language of the move names to select (e.g. "en", "fr", "jp", etc.)</param>
|
|
|
|
/// <returns>An array of strings whose indexes correspond to the IDs of each move name.</returns>
|
|
|
|
public static string[] GetMovesList(string language) => GetStringList("moves", language);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a list of all Pokémon ability names.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="language">Language of the Pokémon ability names to select (e.g. "en", "fr", "jp", etc.)</param>
|
|
|
|
/// <returns>An array of strings whose indexes correspond to the IDs of each Pokémon ability name.</returns>
|
|
|
|
public static string[] GetAbilitiesList(string language) => GetStringList("abilities", language);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a list of all Pokémon nature names.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="language">Language of the Pokémon nature names to select (e.g. "en", "fr", "jp", etc.)</param>
|
|
|
|
/// <returns>An array of strings whose indexes correspond to the IDs of each Pokémon nature name.</returns>
|
|
|
|
public static string[] GetNaturesList(string language) => GetStringList("natures", language);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a list of all Pokémon form names.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="language">Language of the Pokémon form names to select (e.g. "en", "fr", "jp", etc.)</param>
|
|
|
|
/// <returns>An array of strings whose indexes correspond to the IDs of each Pokémon form name.</returns>
|
|
|
|
public static string[] GetFormsList(string language) => GetStringList("forms", language);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a list of all Pokémon type names.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="language">Language of the Pokémon type names to select (e.g. "en", "fr", "jp", etc.)</param>
|
|
|
|
/// <returns>An array of strings whose indexes correspond to the IDs of each Pokémon type name.</returns>
|
|
|
|
public static string[] GetTypesList(string language) => GetStringList("types", language);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a list of all Pokémon characteristic.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="language">Language of the Pokémon characteristic to select (e.g. "en", "fr", "jp", etc.)</param>
|
|
|
|
/// <returns>An array of strings whose indexes correspond to the IDs of each Pokémon characteristic.</returns>
|
|
|
|
public static string[] GetCharacteristicsList(string language) => GetStringList("character", language);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a list of all items.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="language">Language of the items to select (e.g. "en", "fr", "jp", etc.)</param>
|
|
|
|
/// <returns>An array of strings whose indexes correspond to the IDs of each item.</returns>
|
|
|
|
public static string[] GetItemsList(string language) => GetStringList("items", language);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Retrieves the localization index list for all requested strings for the <see cref="fileName"/> through Spanish.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="fileName">Base file name</param>
|
|
|
|
/// <remarks>Ignores Korean Language.</remarks>
|
2024-05-27 15:33:25 +00:00
|
|
|
public static string[][] GetLanguageStrings7([ConstantExpected] string fileName) =>
|
2023-12-04 04:13:20 +00:00
|
|
|
[
|
|
|
|
[], // 0 - None
|
2022-06-18 18:04:24 +00:00
|
|
|
GetStringList(fileName, "ja"), // 1
|
|
|
|
GetStringList(fileName, "en"), // 2
|
|
|
|
GetStringList(fileName, "fr"), // 3
|
|
|
|
GetStringList(fileName, "it"), // 4
|
|
|
|
GetStringList(fileName, "de"), // 5
|
2023-12-04 04:13:20 +00:00
|
|
|
[], // 6 - None
|
2022-06-18 18:04:24 +00:00
|
|
|
GetStringList(fileName, "es"), // 7
|
2023-12-04 04:13:20 +00:00
|
|
|
];
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Retrieves the localization index list for all requested strings for the <see cref="fileName"/> through Korean.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="fileName">Base file name</param>
|
2024-05-27 15:33:25 +00:00
|
|
|
public static string[][] GetLanguageStrings8([ConstantExpected] string fileName) =>
|
2023-12-04 04:13:20 +00:00
|
|
|
[
|
|
|
|
[], // 0 - None
|
2022-06-18 18:04:24 +00:00
|
|
|
GetStringList(fileName, "ja"), // 1
|
|
|
|
GetStringList(fileName, "en"), // 2
|
|
|
|
GetStringList(fileName, "fr"), // 3
|
|
|
|
GetStringList(fileName, "it"), // 4
|
|
|
|
GetStringList(fileName, "de"), // 5
|
2023-12-04 04:13:20 +00:00
|
|
|
[], // 6 - None
|
2022-06-18 18:04:24 +00:00
|
|
|
GetStringList(fileName, "es"), // 7
|
|
|
|
GetStringList(fileName, "ko"), // 8
|
2023-12-04 04:13:20 +00:00
|
|
|
];
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Retrieves the localization index list for all requested strings for the <see cref="fileName"/> through Chinese.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="fileName">Base file name</param>
|
|
|
|
/// <param name="zh2">String to use for the second Chinese localization.</param>
|
2024-05-27 15:33:25 +00:00
|
|
|
public static string[][] GetLanguageStrings10([ConstantExpected] string fileName, string zh2 = "zh") =>
|
2023-12-04 04:13:20 +00:00
|
|
|
[
|
|
|
|
[], // 0 - None
|
2022-06-18 18:04:24 +00:00
|
|
|
GetStringList(fileName, "ja"), // 1
|
|
|
|
GetStringList(fileName, "en"), // 2
|
|
|
|
GetStringList(fileName, "fr"), // 3
|
|
|
|
GetStringList(fileName, "it"), // 4
|
|
|
|
GetStringList(fileName, "de"), // 5
|
2023-12-04 04:13:20 +00:00
|
|
|
[], // 6 - None
|
2022-06-18 18:04:24 +00:00
|
|
|
GetStringList(fileName, "es"), // 7
|
|
|
|
GetStringList(fileName, "ko"), // 8
|
|
|
|
GetStringList(fileName, "zh"), // 9
|
|
|
|
GetStringList(fileName, zh2), // 10
|
2023-12-04 04:13:20 +00:00
|
|
|
];
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
public static string[] GetStringList(string fileName)
|
|
|
|
{
|
|
|
|
if (IsStringListCached(fileName, out var result))
|
|
|
|
return result;
|
|
|
|
var txt = GetStringResource(fileName); // Fetch File, \n to list.
|
2024-06-04 04:32:45 +00:00
|
|
|
if (txt is null)
|
|
|
|
return [];
|
2022-06-18 18:04:24 +00:00
|
|
|
return LoadStringList(fileName, txt);
|
|
|
|
}
|
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
public static bool IsStringListCached(string fileName, [NotNullWhen(true)] out string[]? result)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
lock (getStringListLoadLock) // Make sure only one thread can read the cache
|
|
|
|
return stringListCache.TryGetValue(fileName, out result);
|
|
|
|
}
|
2020-12-22 03:13:18 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Loads a text <see cref="file"/> into the program with a value of <see cref="txt"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>Caches the result array for future fetches.</remarks>
|
2024-06-04 04:32:45 +00:00
|
|
|
private static string[] LoadStringList(string file, string txt)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
string[] raw = FastSplit(txt);
|
2020-12-22 03:13:18 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
// Make sure only one thread can write to the cache
|
|
|
|
lock (getStringListLoadLock)
|
|
|
|
stringListCache.TryAdd(file, raw);
|
2022-06-18 18:04:24 +00:00
|
|
|
return raw;
|
|
|
|
}
|
2020-12-22 03:13:18 +00:00
|
|
|
|
2024-05-27 15:33:25 +00:00
|
|
|
public static string[] GetStringList(string fileName, string lang2char, [ConstantExpected] string type = "text") => GetStringList(GetFullResourceName(fileName, lang2char, type));
|
2020-12-22 03:13:18 +00:00
|
|
|
|
2024-05-27 15:33:25 +00:00
|
|
|
private static string GetFullResourceName(string fileName, string lang2char, [ConstantExpected] string type) => $"{type}_{fileName}_{lang2char}";
|
2021-08-06 00:05:39 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static byte[] GetBinaryResource(string name)
|
|
|
|
{
|
|
|
|
if (!resourceNameMap.TryGetValue(name, out var resName))
|
2023-12-04 04:13:20 +00:00
|
|
|
return [];
|
2020-12-22 03:13:18 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
using var resource = thisAssembly.GetManifestResourceStream(resName);
|
|
|
|
if (resource is null)
|
2023-12-04 04:13:20 +00:00
|
|
|
return [];
|
2021-05-15 18:56:06 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
var buffer = new byte[resource.Length];
|
2023-01-22 04:02:33 +00:00
|
|
|
resource.ReadExactly(buffer);
|
2022-06-18 18:04:24 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
2021-05-15 18:56:06 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static string? GetStringResource(string name)
|
|
|
|
{
|
|
|
|
if (!resourceNameMap.TryGetValue(name.ToLowerInvariant(), out var resourceName))
|
|
|
|
return null;
|
|
|
|
|
|
|
|
using var resource = thisAssembly.GetManifestResourceStream(resourceName);
|
|
|
|
if (resource is null)
|
|
|
|
return null;
|
|
|
|
using var reader = new StreamReader(resource);
|
|
|
|
return reader.ReadToEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static string[] FastSplit(ReadOnlySpan<char> s)
|
|
|
|
{
|
|
|
|
// Get Count
|
|
|
|
if (s.Length == 0)
|
2023-12-04 04:13:20 +00:00
|
|
|
return [];
|
2022-06-18 18:04:24 +00:00
|
|
|
|
2024-05-27 15:33:25 +00:00
|
|
|
var count = 1 + s.Count('\n');
|
2022-06-18 18:04:24 +00:00
|
|
|
var result = new string[count];
|
|
|
|
|
|
|
|
var i = 0;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
var index = s.IndexOf('\n');
|
|
|
|
var length = index == -1 ? s.Length : index;
|
|
|
|
var slice = s[..length];
|
|
|
|
if (slice.Length != 0 && slice[^1] == '\r')
|
|
|
|
slice = slice[..^1];
|
|
|
|
|
|
|
|
result[i++] = slice.ToString();
|
|
|
|
if (i == count)
|
|
|
|
return result;
|
|
|
|
s = s[(index + 1)..];
|
2020-12-22 03:13:18 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2020-12-22 03:13:18 +00:00
|
|
|
}
|