PKHeX/PKHeX.Core/PKM/Util/EntityBlank.cs
Kurt 88830e0d00
Update from .NET Framework 4.6 to .NET 7 (#3729)
Updates from net46->net7, dropping support for mono in favor of using the latest runtime (along with the performance/API improvements). Releases will be posted as 64bit only for now.

Refactors a good amount of internal API methods to be more performant and more customizable for future updates & fixes.

Adds functionality for Batch Editor commands to `>`, `<` and <=/>=

TID/SID properties renamed to TID16/SID16 for clarity; other properties exposed for Gen7 / display variants.

Main window has a new layout to account for DPI scaling (8 point grid)

Fixed: Tatsugiri and Paldean Tauros now output Showdown form names as Showdown expects
Changed: Gen9 species now interact based on the confirmed National Dex IDs (closes #3724)
Fixed: Pokedex set all no longer clears species with unavailable non-base forms (closes #3720)
Changed: Hyper Training suggestions now apply for level 50 in SV. (closes #3714)
Fixed: B2/W2 hatched egg met locations exclusive to specific versions are now explicitly checked (closes #3691)
Added: Properties for ribbon/mark count (closes #3659)
Fixed: Traded SV eggs are now checked correctly (closes #3692)
2023-01-21 20:02:33 -08:00

70 lines
2.4 KiB
C#

using System;
using System.Linq;
using System.Reflection;
namespace PKHeX.Core;
/// <summary>
/// Reflection utility to create blank <see cref="PKM"/> without specifying a constructor.
/// </summary>
public static class EntityBlank
{
/// <summary>
/// Gets a Blank <see cref="PKM"/> object of the specified type.
/// </summary>
/// <param name="type">Type of <see cref="PKM"/> instance desired.</param>
/// <returns>New instance of a blank <see cref="PKM"/> object.</returns>
public static PKM GetBlank(Type type)
{
var constructors = type.GetTypeInfo().DeclaredConstructors.Where(z => !z.IsStatic);
var argCount = constructors.Min(z => z.GetParameters().Length);
return (PKM)(Activator.CreateInstance(type, new object[argCount]) ?? throw new ArgumentException(null, nameof(type)));
}
public static PKM GetBlank(int gen, GameVersion ver) => gen switch
{
1 when ver == GameVersion.BU => new PK1(true),
7 when GameVersion.Gen7b.Contains(ver) => new PB7(),
8 when GameVersion.BDSP.Contains(ver) => new PB8(),
8 when GameVersion.PLA == ver => new PA8(),
_ => GetBlank(gen),
};
/// <summary>
/// Gets a Blank <see cref="PKM"/> object compatible with the provided inputs.
/// </summary>
public static PKM GetBlank(ITrainerInfo tr)
{
if (tr is SaveFile s)
return s.BlankPKM;
return GetBlank(tr.Generation, tr.Game);
}
/// <inheritdoc cref="GetBlank(ITrainerInfo)"/>
public static PKM GetBlank(int gen, int ver) => GetBlank(gen, (GameVersion)ver);
/// <inheritdoc cref="GetBlank(ITrainerInfo)"/>
public static PKM GetBlank(int gen)
{
var type = Type.GetType($"PKHeX.Core.PK{gen}");
if (type is null)
throw new InvalidCastException($"Unable to get the type for PK{gen}.");
return GetBlank(type);
}
public static PKM GetIdealBlank(ushort species, byte form)
{
if (PersonalTable.LA.IsPresentInGame(species, form))
return new PA8();
if (PersonalTable.SWSH.IsPresentInGame(species, form))
return new PK8();
if (PersonalTable.BDSP.IsPresentInGame(species, form))
return new PB8();
if (PersonalTable.USUM.IsPresentInGame(species, form))
return new PK7();
if (PersonalTable.SV.IsPresentInGame(species, form))
return new PK9();
return new PB7();
}
}