From a6e2e08ecbb36a0e53831f2a35145cd1cc7c204b Mon Sep 17 00:00:00 2001 From: Kurt Date: Mon, 21 Apr 2025 01:10:57 -0500 Subject: [PATCH] Add pokepast.es url import why not --- PKHeX.Core/Editing/Showdown/PokepasteTeam.cs | 80 ++++++++++++++++++++ PKHeX.Core/Editing/Showdown/ShowdownTeam.cs | 4 +- PKHeX.WinForms/MainWindow/Main.cs | 4 +- 3 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 PKHeX.Core/Editing/Showdown/PokepasteTeam.cs diff --git a/PKHeX.Core/Editing/Showdown/PokepasteTeam.cs b/PKHeX.Core/Editing/Showdown/PokepasteTeam.cs new file mode 100644 index 000000000..9c8d53ceb --- /dev/null +++ b/PKHeX.Core/Editing/Showdown/PokepasteTeam.cs @@ -0,0 +1,80 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Globalization; + +namespace PKHeX.Core; + +public static class PokepasteTeam +{ + /// + /// Generates the raw URL for retrieving a team based on the supplied team identifier. + /// + /// The numeric identifier of the team. + /// A string containing the full URL to access the team data. + 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; + } + + /// + /// Determines if the provided text is a valid Showdown team URL. If valid, returns a normalized API URL. + /// + /// The text to evaluate. + /// When the method returns, contains the normalized API URL if the text represents a valid Showdown team URL; otherwise, null. + /// + /// true if the text is a valid Showdown team URL; otherwise, false. + public static bool IsURL(ReadOnlySpan 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); + } + + /// + /// Attempts to extract the team identifier from a Showdown web URL and converts it to a standard API URL. + /// + /// The Showdown web URL as a read-only span of characters. + /// When the method returns, contains the standardized API URL if extraction is successful; otherwise, null. + /// true if the team index is successfully extracted and converted; otherwise, false. + public static bool TryCheckWeb(ReadOnlySpan 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; + } + } +} diff --git a/PKHeX.Core/Editing/Showdown/ShowdownTeam.cs b/PKHeX.Core/Editing/Showdown/ShowdownTeam.cs index cc96609f9..51707f26b 100644 --- a/PKHeX.Core/Editing/Showdown/ShowdownTeam.cs +++ b/PKHeX.Core/Editing/Showdown/ShowdownTeam.cs @@ -21,11 +21,9 @@ public static class ShowdownTeam /// /// The URL to retrieve the team data from. /// When the method returns, contains the processed team data if retrieval and formatting succeed; otherwise, null. - /// The numeric identifier extracted from the URL or response. /// true if the team data is successfully retrieved and reformatted; otherwise, false. - 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; diff --git a/PKHeX.WinForms/MainWindow/Main.cs b/PKHeX.WinForms/MainWindow/Main.cs index 988063ac6..ac3b34863 100644 --- a/PKHeX.WinForms/MainWindow/Main.cs +++ b/PKHeX.WinForms/MainWindow/Main.cs @@ -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);