2022-10-02 20:14:42 +00:00
|
|
|
using System;
|
2022-06-07 10:33:45 +00:00
|
|
|
using System.Collections.Generic;
|
2022-05-07 03:38:55 +00:00
|
|
|
|
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
public static class EntityFileExtension
|
|
|
|
{
|
|
|
|
private const string ExtensionPB7 = "pb7";
|
|
|
|
private const string ExtensionPB8 = "pb8";
|
|
|
|
private const string ExtensionPA8 = "pa8";
|
|
|
|
|
|
|
|
public static IReadOnlyList<string> Extensions7b => new[] { ExtensionPB7 };
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets an array of valid <see cref="PKM"/> file extensions.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="maxGeneration">Maximum Generation to permit</param>
|
|
|
|
/// <returns>Valid <see cref="PKM"/> file extensions.</returns>
|
|
|
|
public static string[] GetExtensions(int maxGeneration = PKX.Generation)
|
|
|
|
{
|
|
|
|
int min = maxGeneration is <= 2 or >= 7 ? 1 : 3;
|
2022-05-07 18:47:01 +00:00
|
|
|
int size = maxGeneration - min + 1 + 6;
|
|
|
|
var result = new List<string>(size);
|
2022-05-07 03:38:55 +00:00
|
|
|
for (int i = min; i <= maxGeneration; i++)
|
|
|
|
result.Add($"pk{i}");
|
|
|
|
|
|
|
|
if (maxGeneration >= 3)
|
|
|
|
{
|
|
|
|
result.Add("ck3"); // colosseum
|
|
|
|
result.Add("xk3"); // xd
|
|
|
|
}
|
|
|
|
if (maxGeneration >= 4)
|
2022-10-02 20:14:42 +00:00
|
|
|
{
|
2022-05-07 03:38:55 +00:00
|
|
|
result.Add("bk4"); // battle revolution
|
2022-10-02 20:14:42 +00:00
|
|
|
result.Add("rk4"); // My Pokemon Ranch
|
|
|
|
}
|
2022-05-07 03:38:55 +00:00
|
|
|
if (maxGeneration >= 7)
|
|
|
|
result.Add(ExtensionPB7); // let's go
|
|
|
|
if (maxGeneration >= 8)
|
|
|
|
result.Add(ExtensionPB8); // Brilliant Diamond & Shining Pearl
|
|
|
|
if (maxGeneration >= 8)
|
|
|
|
result.Add(ExtensionPA8); // Legends: Arceus
|
|
|
|
|
|
|
|
return result.ToArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Roughly detects the PKM format from the file's extension.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="ext">File extension.</param>
|
|
|
|
/// <param name="prefer">Preference if not a valid extension, usually the highest acceptable format.</param>
|
|
|
|
/// <returns>Format hint that the file is.</returns>
|
2023-01-22 04:02:33 +00:00
|
|
|
public static EntityContext GetContextFromExtension(ReadOnlySpan<char> ext, EntityContext prefer = EntityContext.None)
|
2022-05-07 03:38:55 +00:00
|
|
|
{
|
2022-05-07 18:47:01 +00:00
|
|
|
if (ext.Length == 0)
|
2022-05-07 03:38:55 +00:00
|
|
|
return prefer;
|
2022-06-07 10:33:45 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
static bool Is(ReadOnlySpan<char> ext, ReadOnlySpan<char> str) => ext.EndsWith(str, StringComparison.InvariantCultureIgnoreCase);
|
2022-06-07 10:33:45 +00:00
|
|
|
if (Is(ext, "b8")) return EntityContext.Gen8b;
|
|
|
|
if (Is(ext, "k8")) return EntityContext.Gen8;
|
|
|
|
if (Is(ext, "b7")) return EntityContext.Gen7b;
|
|
|
|
if (Is(ext, "k7")) return EntityContext.Gen7;
|
|
|
|
if (Is(ext, "k6")) return EntityContext.Gen6;
|
|
|
|
|
|
|
|
return (EntityContext)GetFormatFromExtension(ext[^1], prefer);
|
2022-05-07 03:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Roughly detects the PKM format from the file's extension.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="last">Last character of the file's extension.</param>
|
|
|
|
/// <param name="prefer">Preference if not a valid extension, usually the highest acceptable format.</param>
|
|
|
|
/// <returns>Format hint that the file is.</returns>
|
2022-06-08 06:32:57 +00:00
|
|
|
private static int GetFormatFromExtension(char last, EntityContext prefer)
|
2022-05-07 03:38:55 +00:00
|
|
|
{
|
|
|
|
if (last is >= '1' and <= '9')
|
|
|
|
return last - '0';
|
2022-06-07 10:33:45 +00:00
|
|
|
if (prefer.Generation() <= 7 && last == 'x')
|
|
|
|
return 6;
|
|
|
|
return (int)prefer;
|
2022-05-07 03:38:55 +00:00
|
|
|
}
|
|
|
|
}
|