mirror of
https://github.com/kwsch/PKHeX
synced 2024-11-27 14:30:56 +00:00
9166d0eb64
Rewrites a good amount of legality APIs pertaining to: * Legal moves that can be learned * Evolution chains & cross-generation paths * Memory validation with forgotten moves In generation 8, there are 3 separate contexts an entity can exist in: SW/SH, BD/SP, and LA. Not every entity can cross between them, and not every entity from generation 7 can exist in generation 8 (Gogoat, etc). By creating class models representing the restrictions to cross each boundary, we are able to better track and validate data. The old implementation of validating moves was greedy: it would iterate for all generations and evolutions, and build a full list of every move that can be learned, storing it on the heap. Now, we check one game group at a time to see if the entity can learn a move that hasn't yet been validated. End result is an algorithm that requires 0 allocation, and a smaller/quicker search space. The old implementation of storing move parses was inefficient; for each move that was parsed, a new object is created and adjusted depending on the parse. Now, move parse results are `struct` and store the move parse contiguously in memory. End result is faster parsing and 0 memory allocation. * `PersonalTable` objects have been improved with new API methods to check if a species+form can exist in the game. * `IEncounterTemplate` objects have been improved to indicate the `EntityContext` they originate in (similar to `Generation`). * Some APIs have been extended to accept `Span<T>` instead of Array/IEnumerable
122 lines
4.1 KiB
C#
122 lines
4.1 KiB
C#
using System;
|
|
using System.Text;
|
|
|
|
namespace PKHeX.Core;
|
|
|
|
/// <summary>
|
|
/// QR Message reading & writing logic
|
|
/// </summary>
|
|
public static class QRMessageUtil
|
|
{
|
|
private const string QR6PathBad = "null/#";
|
|
private const string QR6Path = "http://lunarcookies.github.io/b1s1.html#";
|
|
private const string QR6PathWC = "http://lunarcookies.github.io/wc.html#";
|
|
private static string GetExploitURLPrefixPKM(int format) => format == 6 ? QR6Path : QR6PathBad;
|
|
private static string GetExploitURLPrefixWC(int format) => format == 6 ? QR6PathWC : QR6PathBad;
|
|
|
|
/// <summary>
|
|
/// Gets the <see cref="PKM"/> data from the message that is encoded in a QR.
|
|
/// </summary>
|
|
/// <param name="message">QR Message</param>
|
|
/// <param name="context">Preferred <see cref="PKM.Context"/> to expect.</param>
|
|
/// <returns>Decoded <see cref="PKM"/> object, null if invalid.</returns>
|
|
public static PKM? GetPKM(string message, EntityContext context)
|
|
{
|
|
var data = DecodeMessagePKM(message);
|
|
if (data == null)
|
|
return null;
|
|
return EntityFormat.GetFromBytes(data, context);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a QR Message from the input <see cref="PKM"/> data.
|
|
/// </summary>
|
|
/// <param name="pk">Pokémon to encode</param>
|
|
/// <returns>QR Message</returns>
|
|
public static string GetMessage(PKM pk)
|
|
{
|
|
if (pk is PK7 pk7)
|
|
{
|
|
byte[] payload = QR7.GenerateQRData(pk7);
|
|
return GetMessage(payload);
|
|
}
|
|
|
|
var server = GetExploitURLPrefixPKM(pk.Format);
|
|
var data = pk.EncryptedBoxData;
|
|
return GetMessageBase64(data, server);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a QR Message from the input <see cref="byte"/> data.
|
|
/// </summary>
|
|
/// <param name="payload">Data to encode</param>
|
|
/// <returns>QR Message</returns>
|
|
public static string GetMessage(ReadOnlySpan<byte> payload)
|
|
{
|
|
var sb = new StringBuilder(payload.Length);
|
|
foreach (var b in payload)
|
|
sb.Append((char)b);
|
|
return sb.ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a QR Message from the input <see cref="MysteryGift"/> data.
|
|
/// </summary>
|
|
/// <param name="mg">Gift data to encode</param>
|
|
/// <returns>QR Message</returns>
|
|
public static string GetMessage(DataMysteryGift mg)
|
|
{
|
|
var server = GetExploitURLPrefixWC(mg.Generation);
|
|
var data = mg.Write();
|
|
return GetMessageBase64(data, server);
|
|
}
|
|
|
|
public static string GetMessageBase64(byte[] data, string server)
|
|
{
|
|
string payload = Convert.ToBase64String(data);
|
|
return server + payload;
|
|
}
|
|
|
|
private static byte[]? DecodeMessagePKM(string message)
|
|
{
|
|
if (message.Length < 32) // arbitrary length check; everything should be greater than this
|
|
return null;
|
|
if (message.StartsWith(QR6PathBad, StringComparison.Ordinal)) // fake url
|
|
return DecodeMessageDataBase64(message);
|
|
if (message.StartsWith("http", StringComparison.Ordinal)) // inject url
|
|
return DecodeMessageDataBase64(message);
|
|
|
|
const int g7size = 0xE8;
|
|
const int g7intro = 0x30;
|
|
if (message.StartsWith("POKE", StringComparison.Ordinal) && message.Length > g7intro + g7size) // G7 data
|
|
return GetBytesFromMessage(message.AsSpan(g7intro), g7size);
|
|
return null;
|
|
}
|
|
|
|
private static byte[]? DecodeMessageDataBase64(string url)
|
|
{
|
|
if (url.Length == 0 || url[^1] == '#')
|
|
return null;
|
|
|
|
try
|
|
{
|
|
int payloadBegin = url.IndexOf('#');
|
|
if (payloadBegin < 0) // bad URL, need the payload separator
|
|
return null;
|
|
url = url[(payloadBegin + 1)..]; // Trim URL to right after #
|
|
return Convert.FromBase64String(url);
|
|
}
|
|
catch (FormatException)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private static byte[] GetBytesFromMessage(ReadOnlySpan<char> input, int count)
|
|
{
|
|
byte[] data = new byte[count];
|
|
for (int i = data.Length - 1; i >= 0; i--)
|
|
data[i] = (byte)input[i];
|
|
return data;
|
|
}
|
|
}
|