mirror of
https://github.com/kwsch/PKHeX.git
synced 2026-08-23 05:04:19 -05:00
Extract pkm filename'ing logic to allow behavior replacing
injecting dependencies replace the virtual inheritance with a type check
This commit is contained in:
@@ -365,15 +365,7 @@ public int FlawlessIVCount
|
||||
|
||||
public string FileName => $"{FileNameWithoutExtension}.{Extension}";
|
||||
|
||||
public virtual string FileNameWithoutExtension
|
||||
{
|
||||
get
|
||||
{
|
||||
string form = Form > 0 ? $"-{Form:00}" : string.Empty;
|
||||
string star = IsShiny ? " ★" : string.Empty;
|
||||
return $"{Species:000}{form}{star} - {Nickname} - {Checksum:X4}{EncryptionConstant:X8}";
|
||||
}
|
||||
}
|
||||
public string FileNameWithoutExtension => EntityFileNamer.GetName(this);
|
||||
|
||||
public int[] IVs
|
||||
{
|
||||
|
||||
@@ -20,16 +20,6 @@ public abstract class GBPKM : PKM
|
||||
|
||||
public sealed override IReadOnlyList<ushort> ExtraBytes => Array.Empty<ushort>();
|
||||
|
||||
public sealed override string FileNameWithoutExtension
|
||||
{
|
||||
get
|
||||
{
|
||||
string form = Form > 0 ? $"-{Form:00}" : string.Empty;
|
||||
string star = IsShiny ? " ★" : string.Empty;
|
||||
return $"{Species:000}{form}{star} - {Nickname} - {Checksums.CRC16_CCITT(Encrypt()):X4}";
|
||||
}
|
||||
}
|
||||
|
||||
protected GBPKM(int size) : base(size) { }
|
||||
protected GBPKM(byte[] data) : base(data) { }
|
||||
|
||||
|
||||
53
PKHeX.Core/PKM/Util/EntityFileNamer.cs
Normal file
53
PKHeX.Core/PKM/Util/EntityFileNamer.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
namespace PKHeX.Core
|
||||
{
|
||||
public static class EntityFileNamer
|
||||
{
|
||||
/// <summary>
|
||||
/// Object that converts the <see cref="PKM"/> data into a <see cref="string"/> file name.
|
||||
/// </summary>
|
||||
public static IFileNamer<PKM> Namer { get; set; } = new DefaultEntityNamer();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the file name (without extension) for the input <see cref="pk"/> data.
|
||||
/// </summary>
|
||||
/// <param name="pk">Input entity to create a file name for.</param>
|
||||
/// <returns>File name for the <see cref="pk"/> data</returns>
|
||||
public static string GetName(PKM pk) => Namer.GetName(pk);
|
||||
}
|
||||
|
||||
public sealed class DefaultEntityNamer : IFileNamer<PKM>
|
||||
{
|
||||
public string GetName(PKM obj)
|
||||
{
|
||||
if (obj is GBPKM gb)
|
||||
return GetGBPKM(gb);
|
||||
return GetRegular(obj);
|
||||
}
|
||||
|
||||
private static string GetRegular(PKM pk)
|
||||
{
|
||||
string form = pk.Form > 0 ? $"-{pk.Form:00}" : string.Empty;
|
||||
string star = pk.IsShiny ? " ★" : string.Empty;
|
||||
return $"{pk.Species:000}{form}{star} - {pk.Nickname} - {pk.Checksum:X4}{pk.EncryptionConstant:X8}";
|
||||
}
|
||||
|
||||
private static string GetGBPKM(GBPKM gb)
|
||||
{
|
||||
string form = gb.Form > 0 ? $"-{gb.Form:00}" : string.Empty;
|
||||
string star = gb.IsShiny ? " ★" : string.Empty;
|
||||
var raw = gb switch
|
||||
{
|
||||
PK1 pk1 => new PokeList1(pk1).Write(),
|
||||
PK2 pk2 => new PokeList2(pk2).Write(),
|
||||
_ => gb.Data
|
||||
};
|
||||
var checksum = Checksums.CRC16_CCITT(raw);
|
||||
return $"{gb.Species:000}{form}{star} - {gb.Nickname} - {checksum:X4}";
|
||||
}
|
||||
}
|
||||
|
||||
public interface IFileNamer<in T>
|
||||
{
|
||||
string GetName(T obj);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user