From a10d1df9ca6b287ffe27a198d7bd8908be040296 Mon Sep 17 00:00:00 2001 From: Kurt Date: Wed, 28 Jul 2021 19:28:56 -0700 Subject: [PATCH] Extract pkm filename'ing logic to allow behavior replacing injecting dependencies replace the virtual inheritance with a type check --- PKHeX.Core/PKM/PKM.cs | 10 +---- PKHeX.Core/PKM/Shared/GBPKM.cs | 10 ----- PKHeX.Core/PKM/Util/EntityFileNamer.cs | 53 ++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 19 deletions(-) create mode 100644 PKHeX.Core/PKM/Util/EntityFileNamer.cs diff --git a/PKHeX.Core/PKM/PKM.cs b/PKHeX.Core/PKM/PKM.cs index 24134c2ec..f007801fe 100644 --- a/PKHeX.Core/PKM/PKM.cs +++ b/PKHeX.Core/PKM/PKM.cs @@ -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 { diff --git a/PKHeX.Core/PKM/Shared/GBPKM.cs b/PKHeX.Core/PKM/Shared/GBPKM.cs index e847ee88c..8ac797103 100644 --- a/PKHeX.Core/PKM/Shared/GBPKM.cs +++ b/PKHeX.Core/PKM/Shared/GBPKM.cs @@ -20,16 +20,6 @@ public abstract class GBPKM : PKM public sealed override IReadOnlyList ExtraBytes => Array.Empty(); - 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) { } diff --git a/PKHeX.Core/PKM/Util/EntityFileNamer.cs b/PKHeX.Core/PKM/Util/EntityFileNamer.cs new file mode 100644 index 000000000..0ff5a6bbb --- /dev/null +++ b/PKHeX.Core/PKM/Util/EntityFileNamer.cs @@ -0,0 +1,53 @@ +namespace PKHeX.Core +{ + public static class EntityFileNamer + { + /// + /// Object that converts the data into a file name. + /// + public static IFileNamer Namer { get; set; } = new DefaultEntityNamer(); + + /// + /// Gets the file name (without extension) for the input data. + /// + /// Input entity to create a file name for. + /// File name for the data + public static string GetName(PKM pk) => Namer.GetName(pk); + } + + public sealed class DefaultEntityNamer : IFileNamer + { + 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 + { + string GetName(T obj); + } +}