2018-08-26 00:04:01 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace PKHeX.Core
|
|
|
|
|
{
|
2022-03-06 02:13:31 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Geolocation Utility for Generation 6/7 (3DS) Earth location values.
|
|
|
|
|
/// </summary>
|
2018-08-26 00:04:01 +00:00
|
|
|
|
public static class GeoLocation
|
|
|
|
|
{
|
2021-05-20 15:52:47 +00:00
|
|
|
|
private static readonly string[]?[] CountryList = GetCountryList();
|
2020-12-22 03:13:18 +00:00
|
|
|
|
internal static readonly string[] lang_geo = { "ja", "en", "fr", "de", "it", "es", "zh", "ko" };
|
2021-05-20 15:52:47 +00:00
|
|
|
|
private static readonly string[]?[]?[] RegionList = new string[CountryList.Length][][];
|
2020-12-23 17:34:29 +00:00
|
|
|
|
|
2022-03-06 02:13:31 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the index of which the <see cref="language"/> is in the country/region list.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static int GetLanguageIndex(string language) => Array.IndexOf(lang_geo, language);
|
|
|
|
|
private static int GetLanguageIndex(LanguageID language) => GetLanguageIndex(language.GetLanguage2CharName());
|
2018-08-26 00:04:01 +00:00
|
|
|
|
|
|
|
|
|
private const string INVALID = nameof(INVALID);
|
|
|
|
|
|
2021-05-20 15:52:47 +00:00
|
|
|
|
private static string[]?[] GetCountryList()
|
2018-08-26 00:04:01 +00:00
|
|
|
|
{
|
|
|
|
|
var input = Util.GetStringList("countries");
|
|
|
|
|
return UnpackList(input);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-20 15:52:47 +00:00
|
|
|
|
private static string[]?[] GetRegionList(int country)
|
2018-08-26 00:04:01 +00:00
|
|
|
|
{
|
|
|
|
|
var input = Util.GetStringList($"sr_{country:000}");
|
|
|
|
|
return UnpackList(input);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-20 15:52:47 +00:00
|
|
|
|
private static string[]?[] UnpackList(string[] input)
|
2018-08-26 00:04:01 +00:00
|
|
|
|
{
|
2021-05-14 22:30:55 +00:00
|
|
|
|
var last = GetEntry(input[^1], out var lastIndex);
|
2021-05-20 15:52:47 +00:00
|
|
|
|
string[]?[] list = new string[lastIndex+1][];
|
2018-08-26 00:04:01 +00:00
|
|
|
|
list[lastIndex] = last;
|
2021-05-20 15:52:47 +00:00
|
|
|
|
foreach (var line in input)
|
2018-08-26 00:04:01 +00:00
|
|
|
|
{
|
|
|
|
|
var entry = GetEntry(line, out var index);
|
|
|
|
|
list[index] = entry;
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string[] GetEntry(string line, out int index)
|
|
|
|
|
{
|
|
|
|
|
var entries = line.Split(',');
|
|
|
|
|
index = int.Parse(entries[0]);
|
|
|
|
|
return entries;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-17 02:46:22 +00:00
|
|
|
|
private static string GetCountryName(int country, int l)
|
2018-08-26 00:04:01 +00:00
|
|
|
|
{
|
|
|
|
|
if (l < 0)
|
|
|
|
|
return INVALID;
|
2021-05-20 15:52:47 +00:00
|
|
|
|
if ((uint)country >= CountryList.Length)
|
2018-08-26 00:04:01 +00:00
|
|
|
|
return INVALID;
|
2020-06-17 02:46:22 +00:00
|
|
|
|
var countryNames = CountryList[country];
|
2021-05-20 15:52:47 +00:00
|
|
|
|
if (countryNames is not null && l < countryNames.Length)
|
2018-08-26 00:04:01 +00:00
|
|
|
|
return countryNames[l + 1];
|
|
|
|
|
return INVALID;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-17 02:46:22 +00:00
|
|
|
|
private static string GetRegionName(int country, int region, int l)
|
2018-08-26 00:04:01 +00:00
|
|
|
|
{
|
|
|
|
|
if (l < 0)
|
|
|
|
|
return INVALID;
|
2021-05-20 15:52:47 +00:00
|
|
|
|
if ((uint)country >= RegionList.Length)
|
2018-08-26 00:04:01 +00:00
|
|
|
|
return INVALID;
|
2020-11-12 05:01:41 +00:00
|
|
|
|
var regionNames = RegionList[country] ??= GetRegionList(country);
|
2021-05-20 15:52:47 +00:00
|
|
|
|
if ((uint)region >= regionNames.Length)
|
2018-08-26 00:04:01 +00:00
|
|
|
|
return INVALID;
|
2020-06-17 02:46:22 +00:00
|
|
|
|
var localized = regionNames[region];
|
2021-05-20 15:52:47 +00:00
|
|
|
|
if (localized is not null && l < localized.Length)
|
2018-08-26 00:04:01 +00:00
|
|
|
|
return localized[l + 1];
|
|
|
|
|
return INVALID;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-06 02:13:31 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets an array of all country names for the requested <see cref="language"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static string[]? GetCountryList(string language)
|
|
|
|
|
{
|
|
|
|
|
int index = GetLanguageIndex(language);
|
|
|
|
|
return CountryList[index];
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-26 00:04:01 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the Country string for a given Country ID
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="language">Language ID</param>
|
2019-10-26 19:33:58 +00:00
|
|
|
|
/// <param name="country">Country ID</param>
|
2018-08-26 00:04:01 +00:00
|
|
|
|
/// <returns>Country ID string</returns>
|
|
|
|
|
public static string GetCountryName(string language, int country) => GetCountryName(country, GetLanguageIndex(language));
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the Region string for a specified country ID.
|
|
|
|
|
/// </summary>
|
2019-10-26 19:33:58 +00:00
|
|
|
|
/// <param name="language">Language ID</param>
|
2018-08-26 00:04:01 +00:00
|
|
|
|
/// <param name="country">Country ID</param>
|
|
|
|
|
/// <param name="region">Region ID</param>
|
|
|
|
|
/// <returns>Region ID string</returns>
|
|
|
|
|
public static string GetRegionName(string language, int country, int region) => GetRegionName(country, region, GetLanguageIndex(language));
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the Country string for a given Country ID
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="language">Language ID</param>
|
2019-10-26 19:33:58 +00:00
|
|
|
|
/// <param name="country">Country ID</param>
|
2018-08-26 00:04:01 +00:00
|
|
|
|
/// <returns>Country ID string</returns>
|
|
|
|
|
public static string GetCountryName(LanguageID language, int country) => GetCountryName(country, GetLanguageIndex(language));
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the Region string for a specified country ID.
|
|
|
|
|
/// </summary>
|
2019-10-08 01:40:09 +00:00
|
|
|
|
/// <param name="language">Language ID</param>
|
2018-08-26 00:04:01 +00:00
|
|
|
|
/// <param name="country">Country ID</param>
|
|
|
|
|
/// <param name="region">Region ID</param>
|
|
|
|
|
/// <returns>Region ID string</returns>
|
|
|
|
|
public static string GetRegionName(LanguageID language, int country, int region) => GetRegionName(country, region, GetLanguageIndex(language));
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets Country and Region strings for corresponding IDs and language.
|
|
|
|
|
/// </summary>
|
2020-06-17 02:46:22 +00:00
|
|
|
|
/// <param name="country">Country ID</param>
|
|
|
|
|
/// <param name="region">Region ID</param>
|
2018-08-26 00:04:01 +00:00
|
|
|
|
/// <param name="language">Language ID</param>
|
2019-02-07 07:28:02 +00:00
|
|
|
|
/// <returns>Tuple containing country and region</returns>
|
2021-05-18 20:29:55 +00:00
|
|
|
|
public static (string Country, string Region) GetCountryRegionText(int country, int region, string language)
|
2018-08-26 00:04:01 +00:00
|
|
|
|
{
|
|
|
|
|
// Get Language we're fetching for
|
|
|
|
|
int lang = Array.IndexOf(lang_geo, language);
|
2020-06-17 02:46:22 +00:00
|
|
|
|
var countryName = GetCountryName(country, lang);
|
|
|
|
|
var regionName = GetRegionName(country, region, lang);
|
2021-05-18 20:29:55 +00:00
|
|
|
|
return (countryName, regionName);
|
2018-08-26 00:04:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|