Make pkm.Data a readonly field rather than property

This commit is contained in:
Kurt 2020-09-26 12:09:02 -07:00
parent 82afefa335
commit a34434f7cb
22 changed files with 96 additions and 74 deletions

View file

@ -26,8 +26,6 @@ namespace PKHeX.Core
public override bool Valid => ChecksumValid || (Sanity == 0 && Species <= MaxSpeciesID);
public override byte[] Data { get; }
public static BK4 ReadUnshuffle(byte[] data)
{
var PID = BigEndian.ToUInt32(data, 0);
@ -38,9 +36,8 @@ namespace PKHeX.Core
return result;
}
public BK4(byte[] data)
public BK4(byte[] data) : base(data)
{
Data = data;
Sanity = 0x4000;
ResetPartyStats();
}

View file

@ -21,9 +21,8 @@ namespace PKHeX.Core
public override int SIZE_STORED => PokeCrypto.SIZE_3CSTORED;
public override int Format => 3;
public override PersonalInfo PersonalInfo => PersonalTable.RS[Species];
public override byte[] Data { get; }
public CK3(byte[] data) => Data = data;
public CK3() => Data = new byte[PokeCrypto.SIZE_3CSTORED];
public CK3(byte[] data) : base(data) { }
public CK3() : this(new byte[PokeCrypto.SIZE_3CSTORED]) { }
public override PKM Clone() => new CK3((byte[])Data.Clone()) {Identifier = Identifier};
private string GetString(int Offset, int Count) => StringConverter3.GetBEString3(Data, Offset, Count);

View file

@ -27,15 +27,16 @@ namespace PKHeX.Core
private const int SIZE = 260;
public override int Format => 7;
public override PersonalInfo PersonalInfo => PersonalTable.GG.GetFormeEntry(Species, AltForm);
public override byte[] Data { get; }
public PB7() => Data = new byte[SIZE];
public PB7(byte[] data)
public PB7() : base(SIZE) { }
public PB7(byte[] data) : base(DecryptParty(data)) { }
private static byte[] DecryptParty(byte[] data)
{
PokeCrypto.DecryptIfEncrypted67(ref data);
if (data.Length != SIZE)
Array.Resize(ref data, SIZE);
Data = data;
return data;
}
public override PKM Clone() => new PB7((byte[])Data.Clone()){Identifier = Identifier};

View file

@ -16,14 +16,21 @@ namespace PKHeX.Core
public override int Format => 1;
public PK1(bool jp = false) : base(new byte[PokeCrypto.SIZE_1PARTY], jp) { }
public PK1(byte[] decryptedData, bool jp = false) : base(decryptedData, jp) { }
public PK1(bool jp = false) : base(PokeCrypto.SIZE_1PARTY, jp) { }
public PK1(byte[] decryptedData, bool jp = false) : base(EnsurePartySize(decryptedData), jp) { }
private static byte[] EnsurePartySize(byte[] data)
{
if (data.Length != PokeCrypto.SIZE_1PARTY)
Array.Resize(ref data, PokeCrypto.SIZE_1PARTY);
return data;
}
public override PKM Clone() => new PK1((byte[])Data.Clone(), Japanese)
{
Identifier = Identifier,
otname = (byte[])otname.Clone(),
nick = (byte[])nick.Clone(),
OT_Trash = otname,
Nickname_Trash = nick,
};
protected override byte[] Encrypt() => new PokeList1(this).Write();

View file

@ -15,14 +15,21 @@ namespace PKHeX.Core
public override int Format => 2;
public PK2(bool jp = false) : base(new byte[PokeCrypto.SIZE_2PARTY], jp) { }
public PK2(byte[] decryptedData, bool jp = false) : base(decryptedData, jp) { }
public PK2(bool jp = false) : base(PokeCrypto.SIZE_2PARTY, jp) { }
public PK2(byte[] decryptedData, bool jp = false) : base(EnsurePartySize(decryptedData), jp) { }
private static byte[] EnsurePartySize(byte[] data)
{
if (data.Length != PokeCrypto.SIZE_2PARTY)
Array.Resize(ref data, PokeCrypto.SIZE_2PARTY);
return data;
}
public override PKM Clone() => new PK2((byte[])Data.Clone(), Japanese)
{
Identifier = Identifier,
otname = (byte[])otname.Clone(),
nick = (byte[])nick.Clone(),
OT_Trash = otname,
Nickname_Trash = nick,
IsEgg = IsEgg,
};
@ -112,8 +119,8 @@ namespace PKHeX.Core
pk1.Stat_Level = Stat_Level;
}
// Status = 0
pk1.otname = (byte[])otname.Clone();
pk1.nick = (byte[])nick.Clone();
pk1.OT_Trash = otname;
pk1.Nickname_Trash = nick;
pk1.ClearInvalidMoves();

View file

@ -18,14 +18,14 @@ namespace PKHeX.Core
public override IReadOnlyList<ushort> ExtraBytes => Unused;
public override byte[] Data { get; }
public PK3() => Data = new byte[PokeCrypto.SIZE_3PARTY];
public PK3() : base(PokeCrypto.SIZE_3PARTY) { }
public PK3(byte[] data) : base(DecryptParty(data)) { }
public PK3(byte[] data)
private static byte[] DecryptParty(byte[] data)
{
PokeCrypto.DecryptIfEncrypted3(ref data);
Array.Resize(ref data, PokeCrypto.SIZE_3PARTY);
Data = data;
return data;
}
public override PKM Clone()

View file

@ -19,14 +19,14 @@ namespace PKHeX.Core
public override int Format => 4;
public override PersonalInfo PersonalInfo => PersonalTable.HGSS.GetFormeEntry(Species, AltForm);
public override byte[] Data { get; }
public PK4() => Data = new byte[PokeCrypto.SIZE_4PARTY];
public PK4() : base(PokeCrypto.SIZE_4PARTY) { }
public PK4(byte[] data) : base(DecryptParty(data)) { }
public PK4(byte[] data)
private static byte[] DecryptParty(byte[] data)
{
PokeCrypto.DecryptIfEncrypted45(ref data);
Array.Resize(ref data, PokeCrypto.SIZE_4PARTY);
Data = data;
return data;
}
public override PKM Clone() => new PK4((byte[])Data.Clone()){Identifier = Identifier};

View file

@ -24,14 +24,14 @@ namespace PKHeX.Core
public override int Format => 5;
public override PersonalInfo PersonalInfo => PersonalTable.B2W2.GetFormeEntry(Species, AltForm);
public override byte[] Data { get; }
public PK5() => Data = new byte[PokeCrypto.SIZE_5PARTY];
public PK5() : base(PokeCrypto.SIZE_5PARTY) { }
public PK5(byte[] data) : base(DecryptParty(data)) { }
public PK5(byte[] data)
private static byte[] DecryptParty(byte[] data)
{
PokeCrypto.DecryptIfEncrypted45(ref data);
Array.Resize(ref data, PokeCrypto.SIZE_5PARTY);
Data = data;
return data;
}
public override PKM Clone() => new PK5((byte[])Data.Clone()){Identifier = Identifier};

View file

@ -17,15 +17,14 @@ namespace PKHeX.Core
public override int Format => 6;
public override PersonalInfo PersonalInfo => PersonalTable.AO.GetFormeEntry(Species, AltForm);
public PK6() => Data = new byte[PokeCrypto.SIZE_6PARTY];
public PK6() : base(PokeCrypto.SIZE_6PARTY) { }
public PK6(byte[] data) : base(DecryptParty(data)) { }
public override byte[] Data { get; }
public PK6(byte[] data)
private static byte[] DecryptParty(byte[] data)
{
PokeCrypto.DecryptIfEncrypted67(ref data);
Array.Resize(ref data, PokeCrypto.SIZE_6PARTY);
Data = data;
return data;
}
public override PKM Clone() => new PK6((byte[])Data.Clone()){Identifier = Identifier};

View file

@ -18,15 +18,14 @@ namespace PKHeX.Core
public override int Format => 7;
public override PersonalInfo PersonalInfo => PersonalTable.USUM.GetFormeEntry(Species, AltForm);
public override byte[] Data { get; }
public PK7() : base(PokeCrypto.SIZE_6PARTY) { }
public PK7(byte[] data) : base(DecryptParty(data)) { }
public PK7() => Data = new byte[PokeCrypto.SIZE_6PARTY];
public PK7(byte[] data)
private static byte[] DecryptParty(byte[] data)
{
PokeCrypto.DecryptIfEncrypted67(ref data);
Array.Resize(ref data, PokeCrypto.SIZE_6PARTY);
Data = data;
return data;
}
public override PKM Clone() => new PK7((byte[])Data.Clone()){Identifier = Identifier};

View file

@ -33,12 +33,14 @@ namespace PKHeX.Core
public override int Format => 8;
public override PersonalInfo PersonalInfo => PersonalTable.SWSH.GetFormeEntry(Species, AltForm);
public override byte[] Data { get; }
public PK8() : base(PokeCrypto.SIZE_8PARTY) => AffixedRibbon = -1; // 00 would make it show Kalos Champion :)
public PK8(byte[] data) : base(DecryptParty(data)) { }
public PK8()
private static byte[] DecryptParty(byte[] data)
{
Data = new byte[PokeCrypto.SIZE_8PARTY];
AffixedRibbon = -1; // 00 would make it show Kalos Champion :)
PokeCrypto.DecryptIfEncrypted8(ref data);
Array.Resize(ref data, PokeCrypto.SIZE_8PARTY);
return data;
}
protected override ushort CalculateChecksum()
@ -56,13 +58,6 @@ namespace PKHeX.Core
set { if (CurrentHandler == 0) OT_Friendship = value; else HT_Friendship = value; }
}
public PK8(byte[] data)
{
PokeCrypto.DecryptIfEncrypted8(ref data);
Array.Resize(ref data, PokeCrypto.SIZE_8PARTY);
Data = data;
}
public override PKM Clone() => new PK8((byte[])Data.Clone()) { Identifier = Identifier };
private string GetString(int Offset, int Count) => StringConverter.GetString7(Data, Offset, Count);

View file

@ -17,11 +17,14 @@ namespace PKHeX.Core
public virtual IReadOnlyList<ushort> ExtraBytes => Array.Empty<ushort>();
// Internal Attributes set on creation
public abstract byte[] Data { get; } // Raw Storage
public readonly byte[] Data; // Raw Storage
public string? Identifier; // User or Form Custom Attribute
public int Box { get; set; } = -1; // Batch Editor
public int Slot { get; set; } = -1; // Batch Editor
protected PKM(byte[] data) => Data = data;
protected PKM(int size) => Data = new byte[size];
public virtual byte[] EncryptedPartyData => ArrayUtil.Truncate(Encrypt(), SIZE_PARTY);
public virtual byte[] EncryptedBoxData => ArrayUtil.Truncate(Encrypt(), SIZE_STORED);
public virtual byte[] DecryptedPartyData => ArrayUtil.Truncate(Write(), SIZE_PARTY);

View file

@ -5,6 +5,9 @@
/// </summary>
public abstract class G3PKM : PKM, IRibbonSetEvent3, IRibbonSetCommon3, IRibbonSetUnique3, IRibbonSetOnly3, IContestStats
{
protected G3PKM(byte[] data) : base(data) { }
protected G3PKM(int size) : base(size) { }
// Maximums
public sealed override int MaxMoveID => Legal.MaxMoveID_3;
public sealed override int MaxSpeciesID => Legal.MaxSpeciesID_3;

View file

@ -2,6 +2,9 @@
{
public abstract class G4PKM : PKM, IRibbonSetEvent3, IRibbonSetEvent4, IRibbonSetUnique3, IRibbonSetUnique4, IRibbonSetCommon3, IRibbonSetCommon4, IContestStats
{
protected G4PKM(byte[] data) : base(data) { }
protected G4PKM(int size) : base(size) { }
// Maximums
public sealed override int MaxMoveID => Legal.MaxMoveID_4;
public sealed override int MaxSpeciesID => Legal.MaxSpeciesID_4;

View file

@ -7,6 +7,8 @@ namespace PKHeX.Core
{
public override int SIZE_PARTY => PokeCrypto.SIZE_6PARTY;
public override int SIZE_STORED => PokeCrypto.SIZE_6STORED;
protected G6PKM(byte[] data) : base(data) { }
protected G6PKM(int size) : base(size) { }
// Trash Bytes
public sealed override byte[] Nickname_Trash { get => GetData(0x40, 24); set { if (value.Length == 24) value.CopyTo(Data, 0x40); } }

View file

@ -31,14 +31,9 @@ namespace PKHeX.Core
private int StringLength => Japanese ? STRLEN_J : STRLEN_U;
public sealed override bool Japanese => otname.Length == STRLEN_J;
public sealed override byte[] Data { get; }
protected GBPKM(byte[] data, bool jp = false)
protected GBPKM(int size, bool jp = false) : base(size)
{
int partySize = SIZE_PARTY;
if (data.Length != partySize)
Array.Resize(ref data, partySize);
Data = data;
int strLen = jp ? STRLEN_J : STRLEN_U;
// initialize string buffers
@ -48,12 +43,23 @@ namespace PKHeX.Core
otname[i] = nick[i] = 0x50;
}
internal byte[] otname;
internal byte[] nick;
protected GBPKM(byte[] data, bool jp = false) : base(data)
{
int strLen = jp ? STRLEN_J : STRLEN_U;
// initialize string buffers
otname = new byte[strLen];
nick = new byte[strLen];
for (int i = 0; i < otname.Length; i++)
otname[i] = nick[i] = 0x50;
}
internal readonly byte[] otname;
internal readonly byte[] nick;
// Trash Bytes
public sealed override byte[] Nickname_Trash { get => nick; set { if (value.Length == nick.Length) nick = value; } }
public sealed override byte[] OT_Trash { get => otname; set { if (value.Length == otname.Length) otname = value; } }
public sealed override byte[] Nickname_Trash { get => nick; set { if (value.Length == nick.Length) value.CopyTo(nick, 0); } }
public sealed override byte[] OT_Trash { get => otname; set { if (value.Length == otname.Length) value.CopyTo(otname, 0); } }
public sealed override byte[] EncryptedPartyData => Encrypt();
public sealed override byte[] EncryptedBoxData => Encrypt();
@ -217,8 +223,8 @@ namespace PKHeX.Core
public sealed override int IV_SPA { get => IV_SPC; set => IV_SPC = value; }
public sealed override int IV_SPD { get => IV_SPC; set { } }
public void SetNotNicknamed() => nick = GetNonNickname(GuessedLanguage()).ToArray();
public void SetNotNicknamed(int language) => nick = GetNonNickname(language).ToArray();
public void SetNotNicknamed() => GetNonNickname(GuessedLanguage()).CopyTo(nick);
public void SetNotNicknamed(int language) => GetNonNickname(language).CopyTo(nick);
private IEnumerable<byte> GetNonNickname(int language)
{

View file

@ -3,7 +3,7 @@ namespace PKHeX.Core
public sealed class PokeList1 : PokeListGB<PK1>
{
protected override byte GetSpeciesBoxIdentifier(PK1 pk) => SpeciesConverter.SetG1Species(pk.Species);
protected override PK1 GetEntry(byte[] dat, byte[] otname, byte[] nick, bool egg) => new PK1(dat) { otname = otname, nick = nick };
protected override PK1 GetEntry(byte[] dat, byte[] otname, byte[] nick, bool egg) => new PK1(dat, Japanese) { OT_Trash = otname, Nickname_Trash = nick };
protected override int GetEntrySize() => GetEntrySize(IsFormatParty);
public PokeList1(byte[] d, PokeListType c = PokeListType.Single, bool jp = false) : base(d, c, jp) { }

View file

@ -3,7 +3,7 @@ namespace PKHeX.Core
public sealed class PokeList2 : PokeListGB<PK2>
{
protected override byte GetSpeciesBoxIdentifier(PK2 pk) => pk.IsEgg ? (byte)0xFD : (byte)pk.Species;
protected override PK2 GetEntry(byte[] dat, byte[] otname, byte[] nick, bool egg) => new PK2(dat) { otname = otname, nick = nick, IsEgg = egg };
protected override PK2 GetEntry(byte[] dat, byte[] otname, byte[] nick, bool egg) => new PK2(dat, Japanese) { OT_Trash = otname, Nickname_Trash = nick, IsEgg = egg };
protected override int GetEntrySize() => GetEntrySize(IsFormatParty);
public PokeList2(byte[] d, PokeListType c = PokeListType.Single, bool jp = false) : base(d, c, jp) { }

View file

@ -19,6 +19,7 @@ namespace PKHeX.Core
private readonly byte[] Data;
private readonly byte Capacity;
private readonly int Entry_Size;
protected readonly bool Japanese;
public readonly T[] Pokemon;
@ -31,6 +32,7 @@ namespace PKHeX.Core
protected PokeListGB(byte[]? d, PokeListType c = PokeListType.Single, bool jp = false)
{
Japanese = jp;
Capacity = (byte)c;
Entry_Size = GetEntrySize();
StringLength = GetStringLength(jp);

View file

@ -20,9 +20,8 @@ namespace PKHeX.Core
public override int SIZE_STORED => PokeCrypto.SIZE_3XSTORED;
public override int Format => 3;
public override PersonalInfo PersonalInfo => PersonalTable.RS[Species];
public override byte[] Data { get; }
public XK3(byte[] data) => Data = data;
public XK3() => Data = new byte[PokeCrypto.SIZE_3XSTORED];
public XK3(byte[] data) : base(data) { }
public XK3() : base(PokeCrypto.SIZE_3XSTORED) { }
public override PKM Clone() => new XK3((byte[])Data.Clone()){Identifier = Identifier, Purification = Purification};
private string GetString(int Offset, int Count) => StringConverter3.GetBEString3(Data, Offset, Count);

View file

@ -55,7 +55,7 @@ namespace PKHeX.Core
int len = StringLength;
var nick = data.Slice(0x21, len);
var ot = data.Slice(0x21 + len, len);
return new PK1(data, Japanese) {otname = ot, nick = nick};
return new PK1(data, Japanese) {OT_Trash = ot, Nickname_Trash = nick};
}
protected override byte[] DecryptPKM(byte[] data) => data;

View file

@ -151,7 +151,7 @@ namespace PKHeX.Core
Array.Copy(Data, offset, ot, 0, ot.Length); offset += ot.Length;
Array.Copy(Data, offset, pk, 0, pk.Length);
return new PK2(pk, jp: Japanese) { otname = ot, nick = nick };
return new PK2(pk, jp: Japanese) { OT_Trash = ot, Nickname_Trash = nick };
}
private const int SIZE_RESERVED = 0x8000; // unpacked box data