mirror of
https://github.com/kwsch/PKHeX.git
synced 2026-08-28 02:04:51 -05:00
Add pokepast.es url import
why not
This commit is contained in:
80
PKHeX.Core/Editing/Showdown/PokepasteTeam.cs
Normal file
80
PKHeX.Core/Editing/Showdown/PokepasteTeam.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Globalization;
|
||||
|
||||
namespace PKHeX.Core;
|
||||
|
||||
public static class PokepasteTeam
|
||||
{
|
||||
/// <summary>
|
||||
/// Generates the raw URL for retrieving a team based on the supplied team identifier.
|
||||
/// </summary>
|
||||
/// <param name="team">The numeric identifier of the team.</param>
|
||||
/// <returns>A string containing the full URL to access the team data.</returns>
|
||||
public static string GetURL(ulong team) => $"https://pokepast.es/{team:x16}/raw";
|
||||
public static string GetURLOld(int team) => $"https://pokepast.es/{team}/raw";
|
||||
|
||||
public static bool TryGetSets(string url, [NotNullWhen(true)] out string? content)
|
||||
{
|
||||
content = null;
|
||||
if (!Uri.TryCreate(url, UriKind.Absolute, out var uriResult) || (uriResult.Scheme != Uri.UriSchemeHttp && uriResult.Scheme != Uri.UriSchemeHttps))
|
||||
return false;
|
||||
|
||||
content = NetUtil.GetStringFromURL(uriResult);
|
||||
return content != null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the provided text is a valid Showdown team URL. If valid, returns a normalized API URL.
|
||||
/// </summary>
|
||||
/// <param name="text">The text to evaluate.</param>
|
||||
/// <param name="url">When the method returns, contains the normalized API URL if the text represents a valid Showdown team URL; otherwise, null.</param>
|
||||
/// <param name="hash"></param>
|
||||
/// <returns><c>true</c> if the text is a valid Showdown team URL; otherwise, <c>false</c>.</returns>
|
||||
public static bool IsURL(ReadOnlySpan<char> text, [NotNullWhen(true)] out string? url)
|
||||
{
|
||||
text = text.Trim();
|
||||
url = null;
|
||||
if (!text.StartsWith("https://pokepast.es/")) // short link
|
||||
return false;
|
||||
|
||||
return TryCheckWeb(text, out url);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to extract the team identifier from a Showdown web URL and converts it to a standard API URL.
|
||||
/// </summary>
|
||||
/// <param name="text">The Showdown web URL as a read-only span of characters.</param>
|
||||
/// <param name="url">When the method returns, contains the standardized API URL if extraction is successful; otherwise, null.</param>
|
||||
/// <returns><c>true</c> if the team index is successfully extracted and converted; otherwise, <c>false</c>.</returns>
|
||||
public static bool TryCheckWeb(ReadOnlySpan<char> text, [NotNullWhen(true)] out string? url)
|
||||
{
|
||||
// if ends with `/`, remove.
|
||||
if (text.EndsWith('/'))
|
||||
text = text[..^1]; // remove trailing slash
|
||||
// if ends with `/raw`, remove.
|
||||
if (text.EndsWith("/raw"))
|
||||
text = text[..^4]; // remove trailing /raw
|
||||
|
||||
url = null;
|
||||
|
||||
// seek back to `/`
|
||||
int start = text.LastIndexOf('/'); // seek back to /
|
||||
if (start == -1)
|
||||
return false;
|
||||
|
||||
// get the substring after
|
||||
var number = text[(start + 1)..];
|
||||
switch (number.Length)
|
||||
{
|
||||
case 16 when ulong.TryParse(number, NumberStyles.HexNumber, null, out var hash):
|
||||
url = GetURL(hash);
|
||||
return true;
|
||||
case <= 8 when int.TryParse(number, out var team):
|
||||
url = GetURLOld(team);
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,11 +21,9 @@ public static class ShowdownTeam
|
||||
/// </summary>
|
||||
/// <param name="url">The URL to retrieve the team data from.</param>
|
||||
/// <param name="content">When the method returns, contains the processed team data if retrieval and formatting succeed; otherwise, null.</param>
|
||||
/// <param name="team">The numeric identifier extracted from the URL or response.</param>
|
||||
/// <returns><c>true</c> if the team data is successfully retrieved and reformatted; otherwise, <c>false</c>.</returns>
|
||||
public static bool TryGetSets(string url, [NotNullWhen(true)] out string? content, out int team)
|
||||
public static bool TryGetSets(string url, [NotNullWhen(true)] out string? content)
|
||||
{
|
||||
team = 0;
|
||||
content = null;
|
||||
if (!Uri.TryCreate(url, UriKind.Absolute, out var uriResult) || (uriResult.Scheme != Uri.UriSchemeHttp && uriResult.Scheme != Uri.UriSchemeHttps))
|
||||
return false;
|
||||
|
||||
@@ -525,7 +525,9 @@ private void ClickShowdownImportPKM(object sender, EventArgs e)
|
||||
// Get Simulator Data
|
||||
var text = Clipboard.GetText();
|
||||
ShowdownSet set;
|
||||
if (ShowdownTeam.IsURL(text, out var url) && ShowdownTeam.TryGetSets(url, out var content, out _))
|
||||
if (ShowdownTeam.IsURL(text, out var url) && ShowdownTeam.TryGetSets(url, out var content))
|
||||
set = ShowdownParsing.GetShowdownSets(content).FirstOrDefault() ?? new(""); // take only first set
|
||||
else if (PokepasteTeam.IsURL(text, out url) && PokepasteTeam.TryGetSets(url, out content))
|
||||
set = ShowdownParsing.GetShowdownSets(content).FirstOrDefault() ?? new(""); // take only first set
|
||||
else
|
||||
set = new ShowdownSet(text);
|
||||
|
||||
Reference in New Issue
Block a user