mirror of
https://github.com/kwsch/PKHeX.git
synced 2026-09-15 02:35:20 -05:00
Add showdown team import from url
This commit is contained in:
165
PKHeX.Core/Editing/Showdown/ShowdownTeam.cs
Normal file
165
PKHeX.Core/Editing/Showdown/ShowdownTeam.cs
Normal file
@@ -0,0 +1,165 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace PKHeX.Core;
|
||||
|
||||
/// <summary>
|
||||
/// Logic for retrieving Showdown teams from URLs.
|
||||
/// </summary>
|
||||
public static class ShowdownTeam
|
||||
{
|
||||
/// <summary>
|
||||
/// Generates the API URL for retrieving a Showdown 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 via the API.</returns>
|
||||
public static string GetTeamURL(int team) => $"https://play.pokemonshowdown.com/api/getteam?teamid={team}&raw=1";
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to retrieve the Showdown team data from a specified URL, and reformats it.
|
||||
/// </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 TryGetTeams(string url, [NotNullWhen(true)] out string? content, out int team)
|
||||
{
|
||||
team = 0;
|
||||
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);
|
||||
if (content == null)
|
||||
return false;
|
||||
|
||||
return GetTeamsFromReply(ref content);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extracts the team data from the API reply and reformats it by replacing escaped newline
|
||||
/// characters with system-specific line breaks.
|
||||
/// </summary>
|
||||
/// <param name="content">
|
||||
/// A reference to the API response string. On successful extraction, the value is replaced
|
||||
/// with the reformatted team data; otherwise, it remains unchanged.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// <c>true</c> if the team data is successfully extracted and reformatted; otherwise, <c>false</c>.
|
||||
/// </returns>
|
||||
public static bool GetTeamsFromReply(ref string content)
|
||||
{
|
||||
// reformat
|
||||
const string startText = """
|
||||
"team":"
|
||||
""";
|
||||
var start = content.IndexOf(startText, StringComparison.Ordinal);
|
||||
if (start == -1)
|
||||
return false;
|
||||
start += startText.Length; // skip to the start of the team
|
||||
|
||||
var end = content.LastIndexOf("\\n", StringComparison.Ordinal);
|
||||
if (end == -1 || end <= start)
|
||||
return false;
|
||||
|
||||
content = content[start..end].Replace("\\n", Environment.NewLine);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <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>
|
||||
/// <returns><c>true</c> if the text is a valid Showdown team URL; otherwise, <c>false</c>.</returns>
|
||||
public static bool IsShowdownTeamURL(ReadOnlySpan<char> text, [NotNullWhen(true)] out string? url)
|
||||
{
|
||||
text = text.Trim();
|
||||
if (text.StartsWith("https://psim.us/t/") || // short link
|
||||
text.StartsWith("https://teams.pokemonshowdown.com/"))
|
||||
return TryGetTeamFromWebURL(text, out url);
|
||||
|
||||
if (text.StartsWith("https://play.pokemonshowdown.com/api/getteam?teamid="))
|
||||
return TryGetTeamFromAPITeamURL(text, out url);
|
||||
|
||||
url = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <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 TryGetTeamFromWebURL(ReadOnlySpan<char> text, [NotNullWhen(true)] out string? url)
|
||||
{
|
||||
url = null;
|
||||
if (!TryGetTeamIndexWeb(text, out var team))
|
||||
return false;
|
||||
url = GetTeamURL(team);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to extract the team identifier from a Showdown API URL and returns a standardized API URL.
|
||||
/// </summary>
|
||||
/// <param name="text">The Showdown API 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 the URL normalized; otherwise, <c>false</c>.</returns>
|
||||
public static bool TryGetTeamFromAPITeamURL(ReadOnlySpan<char> text, [NotNullWhen(true)] out string? url)
|
||||
{
|
||||
url = null;
|
||||
if (!TryGetTeamIndexAPI(text, out var team))
|
||||
return false;
|
||||
url = GetTeamURL(team);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extracts the team identifier from a Showdown web URL.
|
||||
/// </summary>
|
||||
/// <param name="text">The Showdown web URL provided as a read-only span of characters.</param>
|
||||
/// <param name="team">When the method returns, contains the extracted team identifier if successful; otherwise, zero.</param>
|
||||
/// <returns><c>true</c> if the team identifier is successfully extracted; otherwise, <c>false</c>.</returns>
|
||||
public static bool TryGetTeamIndexWeb(ReadOnlySpan<char> text, out int team)
|
||||
{
|
||||
team = 0;
|
||||
if (text.EndsWith('/'))
|
||||
text = text[..^1]; // remove trailing slash
|
||||
if (text.EndsWith("/raw"))
|
||||
text = text[..^4]; // remove trailing /raw
|
||||
|
||||
int start = text.LastIndexOf('/'); // seek back to =
|
||||
if (start == -1)
|
||||
return false;
|
||||
|
||||
var number = text[(start + 1)..];
|
||||
if (!int.TryParse(number, out team))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extracts the team identifier from a Showdown API URL.
|
||||
/// </summary>
|
||||
/// <param name="text">The Showdown API URL as a read-only span of characters.</param>
|
||||
/// <param name="team">When the method returns, contains the extracted team identifier if successful; otherwise, zero.</param>
|
||||
/// <returns><c>true</c> if the team identifier is successfully extracted; otherwise, <c>false</c>.</returns>
|
||||
public static bool TryGetTeamIndexAPI(ReadOnlySpan<char> text, out int team)
|
||||
{
|
||||
team = 0;
|
||||
if (!text.EndsWith("&raw=1"))
|
||||
return false;
|
||||
|
||||
text = text[..^6];
|
||||
int start = text.LastIndexOf('='); // seek back to =
|
||||
if (start == -1)
|
||||
return false;
|
||||
|
||||
var number = text[(start + 1)..];
|
||||
if (!int.TryParse(number, out team))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,7 @@ public static class NetUtil
|
||||
{
|
||||
// The GitHub API will fail if no user agent is provided
|
||||
using var client = new HttpClient();
|
||||
client.Timeout = TimeSpan.FromSeconds(3);
|
||||
const string agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36";
|
||||
client.DefaultRequestHeaders.Add("User-Agent", agent);
|
||||
var response = client.GetAsync(url).Result;
|
||||
|
||||
@@ -524,7 +524,11 @@ private void ClickShowdownImportPKM(object sender, EventArgs e)
|
||||
|
||||
// Get Simulator Data
|
||||
var text = Clipboard.GetText();
|
||||
var set = new ShowdownSet(text);
|
||||
ShowdownSet set;
|
||||
if (ShowdownTeam.IsShowdownTeamURL(text, out var url) && ShowdownTeam.TryGetTeams(url, out var content, out _))
|
||||
set = ShowdownParsing.GetShowdownSets(content).FirstOrDefault() ?? new(""); // take only first set
|
||||
else
|
||||
set = new ShowdownSet(text);
|
||||
|
||||
if (set.Species == 0)
|
||||
{ WinFormsUtil.Alert(MsgSimulatorFailClipboard); return; }
|
||||
|
||||
Reference in New Issue
Block a user