Remove unnecessary array cloning

new objects would always clone the input array; only clone on object
cloning (keep the original array reference on initial creation from
bytes)
This commit is contained in:
Kurt 2017-12-26 21:38:19 -08:00
parent 5ec99b6c1c
commit 3445b46526
27 changed files with 43 additions and 51 deletions

View file

@ -13,9 +13,7 @@ namespace PKHeX.Core
public PGF(byte[] data = null)
{
Data = (byte[])(data?.Clone() ?? new byte[Size]);
if (data == null) Data = new byte[Size];
else Data = (byte[])data.Clone();
Data = data ?? new byte[Size];
}
public override int TID { get => BitConverter.ToUInt16(Data, 0x00); set => BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x00); }

View file

@ -29,7 +29,7 @@ namespace PKHeX.Core
public PCD(byte[] data = null)
{
Data = (byte[])(data?.Clone() ?? new byte[Size]);
Data = data ?? new byte[Size];
}
public PGT Gift
@ -158,7 +158,7 @@ namespace PKHeX.Core
public PGT(byte[] data = null)
{
Data = (byte[])(data?.Clone() ?? new byte[Size]);
Data = data ?? new byte[Size];
}
public byte CardType { get => Data[0]; set => Data[0] = value; }

View file

@ -19,7 +19,7 @@ namespace PKHeX.Core
public readonly byte[] Data;
public PL6(byte[] data = null)
{
Data = (byte[])(data?.Clone() ?? new byte[Size]);
Data = data ?? new byte[Size];
}
/// <summary>
/// Pokémon Link Flag
@ -196,7 +196,7 @@ namespace PKHeX.Core
public readonly byte[] Data;
public PL6_PKM(byte[] data = null)
{
Data = (byte[])(data?.Clone() ?? new byte[Size]);
Data = data ?? new byte[Size];
}
public int TID {

View file

@ -16,7 +16,7 @@ namespace PKHeX.Core
public WC6(byte[] data = null)
{
Data = (byte[])(data?.Clone() ?? new byte[Size]);
Data = data ?? new byte[Size];
if (Data.Length == SizeFull)
{
byte[] wc6 = new byte[Size];

View file

@ -15,7 +15,7 @@ namespace PKHeX.Core
public WC7(byte[] data = null)
{
Data = (byte[])(data?.Clone() ?? new byte[Size]);
Data = data ?? new byte[Size];
if (Data.Length == SizeFull)
{
byte[] wc6 = new byte[Size];

View file

@ -22,7 +22,7 @@ namespace PKHeX.Core
public BK4(byte[] decryptedData = null, string ident = null)
{
Data = (byte[])(decryptedData ?? new byte[SIZE_PARTY]).Clone();
Data = decryptedData ?? new byte[SIZE_PARTY];
uint sv = ((PID & 0x3E000) >> 0xD) % 24;
Data = PKX.ShuffleArray45(Data, sv);
Identifier = ident;
@ -33,7 +33,7 @@ namespace PKHeX.Core
if (Data.Length != SIZE_PARTY)
Array.Resize(ref Data, SIZE_PARTY);
}
public override PKM Clone() => new BK4(Encrypt());
public override PKM Clone() => new BK4((byte[])Encrypt().Clone());
public string GetString(int Offset, int Count) => StringConverter.GetBEString4(Data, Offset, Count);
public byte[] SetString(string value, int maxLength) => StringConverter.SetBEString4(value, maxLength);

View file

@ -20,13 +20,13 @@ namespace PKHeX.Core
public CK3(byte[] decryptedData = null, string ident = null)
{
Data = (byte[])(decryptedData ?? new byte[SIZE_PARTY]).Clone();
Data = decryptedData ?? new byte[SIZE_PARTY];
PKMConverter.CheckEncrypted(ref Data);
Identifier = ident;
if (Data.Length != SIZE_PARTY)
Array.Resize(ref Data, SIZE_PARTY);
}
public override PKM Clone() => new CK3(Data);
public override PKM Clone() => new CK3((byte[])Data.Clone());
private string GetString(int Offset, int Count) => StringConverter.GetBEString3(Data, Offset, Count);
private byte[] SetString(string value, int maxLength) => StringConverter.SetBEString3(value, maxLength);

View file

@ -38,7 +38,7 @@ namespace PKHeX.Core
public PK1(byte[] decryptedData = null, string ident = null, bool jp = false)
{
Data = (byte[])(decryptedData ?? new byte[SIZE_PARTY]).Clone();
Data = decryptedData ?? new byte[SIZE_PARTY];
Identifier = ident;
if (Data.Length != SIZE_PARTY)
Array.Resize(ref Data, SIZE_PARTY);
@ -49,7 +49,7 @@ namespace PKHeX.Core
public override PKM Clone()
{
PK1 new_pk1 = new PK1(Data, Identifier, Japanese);
PK1 new_pk1 = new PK1((byte[])Data.Clone(), Identifier, Japanese);
Array.Copy(otname, 0, new_pk1.otname, 0, otname.Length);
Array.Copy(nick, 0, new_pk1.nick, 0, nick.Length);
return new_pk1;

View file

@ -56,7 +56,7 @@ namespace PKHeX.Core
public PK2(byte[] decryptedData = null, string ident = null, bool jp = false)
{
Data = (byte[])(decryptedData ?? new byte[SIZE_PARTY]).Clone();
Data = decryptedData ?? new byte[SIZE_PARTY];
Identifier = ident;
if (Data.Length != SIZE_PARTY)
Array.Resize(ref Data, SIZE_PARTY);
@ -67,7 +67,7 @@ namespace PKHeX.Core
public override PKM Clone()
{
PK2 new_pk2 = new PK2(Data, Identifier, Japanese);
PK2 new_pk2 = new PK2((byte[])Data.Clone(), Identifier, Japanese);
Array.Copy(otname, 0, new_pk2.otname, 0, otname.Length);
Array.Copy(nick, 0, new_pk2.nick, 0, nick.Length);
new_pk2.IsEgg = IsEgg;

View file

@ -16,13 +16,13 @@ namespace PKHeX.Core
public PK3(byte[] decryptedData = null, string ident = null)
{
Data = (byte[])(decryptedData ?? new byte[SIZE_PARTY]).Clone();
Data = decryptedData ?? new byte[SIZE_PARTY];
PKMConverter.CheckEncrypted(ref Data);
Identifier = ident;
if (Data.Length != SIZE_PARTY)
Array.Resize(ref Data, SIZE_PARTY);
}
public override PKM Clone() => new PK3(Data);
public override PKM Clone() => new PK3((byte[])Data.Clone());
private string GetString(int Offset, int Count) => StringConverter.GetString3(Data, Offset, Count, Japanese);
private byte[] SetString(string value, int maxLength) => StringConverter.SetString3(value, maxLength, Japanese);

View file

@ -17,13 +17,13 @@ namespace PKHeX.Core
public PK4(byte[] decryptedData = null, string ident = null)
{
Data = (byte[])(decryptedData ?? new byte[SIZE_PARTY]).Clone();
Data = decryptedData ?? new byte[SIZE_PARTY];
PKMConverter.CheckEncrypted(ref Data);
Identifier = ident;
if (Data.Length != SIZE_PARTY)
Array.Resize(ref Data, SIZE_PARTY);
}
public override PKM Clone() => new PK4(Data);
public override PKM Clone() => new PK4((byte[])Data.Clone());
private string GetString(int Offset, int Count) => StringConverter.GetString4(Data, Offset, Count);
private byte[] SetString(string value, int maxLength) => StringConverter.SetString4(value, maxLength);

View file

@ -17,13 +17,13 @@ namespace PKHeX.Core
public PK5(byte[] decryptedData = null, string ident = null)
{
Data = (byte[])(decryptedData ?? new byte[SIZE_PARTY]).Clone();
Data = decryptedData ?? new byte[SIZE_PARTY];
PKMConverter.CheckEncrypted(ref Data);
Identifier = ident;
if (Data.Length != SIZE_PARTY)
Array.Resize(ref Data, SIZE_PARTY);
}
public override PKM Clone() => new PK5(Data);
public override PKM Clone() => new PK5((byte[])Data.Clone());
private string GetString(int Offset, int Count) => StringConverter.GetString5(Data, Offset, Count);
private byte[] SetString(string value, int maxLength) => StringConverter.SetString5(value, maxLength);

View file

@ -18,13 +18,13 @@ namespace PKHeX.Core
public PK6(byte[] decryptedData = null, string ident = null)
{
Data = (byte[])(decryptedData ?? new byte[SIZE_PARTY]).Clone();
Data = decryptedData ?? new byte[SIZE_PARTY];
PKMConverter.CheckEncrypted(ref Data);
Identifier = ident;
if (Data.Length != SIZE_PARTY)
Array.Resize(ref Data, SIZE_PARTY);
}
public override PKM Clone() => new PK6(Data);
public override PKM Clone() => new PK6((byte[])Data.Clone());
private string GetString(int Offset, int Count) => StringConverter.GetString6(Data, Offset, Count);
private byte[] SetString(string value, int maxLength) => StringConverter.SetString6(value, maxLength);

View file

@ -19,13 +19,13 @@ namespace PKHeX.Core
public PK7(byte[] decryptedData = null, string ident = null)
{
Data = (byte[])(decryptedData ?? new byte[SIZE_PARTY]).Clone();
Data = decryptedData ?? new byte[SIZE_PARTY];
PKMConverter.CheckEncrypted(ref Data);
Identifier = ident;
if (Data.Length != SIZE_PARTY)
Array.Resize(ref Data, SIZE_PARTY);
}
public override PKM Clone() => new PK7(Data);
public override PKM Clone() => new PK7((byte[])Data.Clone());
private string GetString(int Offset, int Count) => StringConverter.GetString7(Data, Offset, Count);
private byte[] SetString(string value, int maxLength, bool chinese = false) => StringConverter.SetString7(value, maxLength, Language, chinese: chinese);

View file

@ -18,13 +18,13 @@ namespace PKHeX.Core
public override PersonalInfo PersonalInfo => PersonalTable.RS[Species];
public XK3(byte[] decryptedData = null, string ident = null)
{
Data = (byte[])(decryptedData ?? new byte[SIZE_PARTY]).Clone();
Data = decryptedData ?? new byte[SIZE_PARTY];
PKMConverter.CheckEncrypted(ref Data);
Identifier = ident;
if (Data.Length != SIZE_PARTY)
Array.Resize(ref Data, SIZE_PARTY);
}
public override PKM Clone() => new XK3(Data) {Purification = Purification};
public override PKM Clone() => new XK3((byte[])Data.Clone()) {Purification = Purification};
private string GetString(int Offset, int Count) => StringConverter.GetBEString3(Data, Offset, Count);
private byte[] SetString(string value, int maxLength) => StringConverter.SetBEString3(value, maxLength);

View file

@ -19,7 +19,7 @@ namespace PKHeX.Core
public SAV1(byte[] data = null, GameVersion versionOverride = GameVersion.Any)
{
Data = data == null ? new byte[SaveUtil.SIZE_G1RAW] : (byte[])data.Clone();
Data = data ?? new byte[SaveUtil.SIZE_G1RAW];
BAK = (byte[])Data.Clone();
Exportable = !Data.All(z => z == 0);

View file

@ -21,7 +21,7 @@ namespace PKHeX.Core
public SAV2(byte[] data = null, GameVersion versionOverride = GameVersion.Any)
{
Data = data == null ? new byte[SaveUtil.SIZE_G2RAW_U] : (byte[])data.Clone();
Data = data ?? new byte[SaveUtil.SIZE_G2RAW_U];
BAK = (byte[])Data.Clone();
Exportable = !Data.All(z => z == 0);

View file

@ -42,7 +42,7 @@ namespace PKHeX.Core
public SAV3(byte[] data = null, GameVersion versionOverride = GameVersion.Any)
{
Data = data == null ? new byte[SaveUtil.SIZE_G3RAW] : (byte[])data.Clone();
Data = data ?? new byte[SaveUtil.SIZE_G3RAW];
BAK = (byte[])Data.Clone();
Exportable = !Data.All(z => z == 0);

View file

@ -46,15 +46,13 @@ namespace PKHeX.Core
public SAV3Colosseum(byte[] data, SAV3GCMemoryCard MC) : this(data) { this.MC = MC; BAK = MC.Data; }
public SAV3Colosseum(byte[] data = null)
{
Data = data == null ? new byte[SaveUtil.SIZE_G3COLO] : (byte[])data.Clone();
Data = data ?? new byte[SaveUtil.SIZE_G3COLO];
BAK = (byte[])Data.Clone();
Exportable = !Data.All(z => z == 0);
if (SaveUtil.GetIsG3COLOSAV(Data) != GameVersion.COLO)
return;
OriginalData = (byte[])Data.Clone();
// Scan all 3 save slots for the highest counter
for (int i = 0; i < SLOT_COUNT; i++)
{
@ -113,7 +111,6 @@ namespace PKHeX.Core
PartyCount++;
}
private readonly byte[] OriginalData;
public override byte[] Write(bool DSV, bool GCI)
{
StrategyMemo.FinalData.CopyTo(Data, Memo);
@ -124,7 +121,7 @@ namespace PKHeX.Core
byte[] newSAV = EncryptColosseum(Data, digest);
// Put save slot back in original save data
byte[] newFile = (byte[])OriginalData.Clone();
byte[] newFile = (byte[])BAK.Clone();
Array.Copy(newSAV, 0, newFile, SLOT_START + SaveIndex*SLOT_SIZE, newSAV.Length);
// Return the gci if Memory Card is not being exported

View file

@ -24,7 +24,7 @@ namespace PKHeX.Core
public SAV3RSBox(byte[] data, SAV3GCMemoryCard MC) : this(data) { this.MC = MC; BAK = MC.Data; }
public SAV3RSBox(byte[] data = null)
{
Data = data == null ? new byte[SaveUtil.SIZE_G3BOX] : (byte[])data.Clone();
Data = data ?? new byte[SaveUtil.SIZE_G3BOX];
BAK = (byte[])Data.Clone();
Exportable = !Data.All(z => z == 0);

View file

@ -38,15 +38,13 @@ namespace PKHeX.Core
public SAV3XD(byte[] data, SAV3GCMemoryCard MC) : this(data) { this.MC = MC; BAK = MC.Data; }
public SAV3XD(byte[] data = null)
{
Data = data == null ? new byte[SaveUtil.SIZE_G3XD] : (byte[])data.Clone();
Data = data ?? new byte[SaveUtil.SIZE_G3XD];
BAK = (byte[])Data.Clone();
Exportable = !Data.All(z => z == 0);
if (SaveUtil.GetIsG3XDSAV(Data) != GameVersion.XD)
return;
OriginalData = (byte[])Data.Clone();
// Scan all 3 save slots for the highest counter
for (int i = 0; i < SLOT_COUNT; i++)
{
@ -121,7 +119,6 @@ namespace PKHeX.Core
PartyCount++;
}
private readonly byte[] OriginalData;
public override byte[] Write(bool DSV, bool GCI)
{
// Set Memo Back
@ -136,7 +133,7 @@ namespace PKHeX.Core
byte[] newSAV = SaveUtil.EncryptGC(Data, 0x10, 0x27FD8, keys);
// Put save slot back in original save data
byte[] newFile = (byte[])OriginalData.Clone();
byte[] newFile = (byte[])BAK.Clone();
Array.Copy(newSAV, 0, newFile, SLOT_START + SaveIndex * SLOT_SIZE, newSAV.Length);
// Return the gci if Memory Card is not being exported

View file

@ -14,7 +14,7 @@ namespace PKHeX.Core
public override string Extension => ".sav";
public SAV4(byte[] data = null, GameVersion versionOverride = GameVersion.Any)
{
Data = data == null ? new byte[SaveUtil.SIZE_G4RAW] : (byte[])data.Clone();
Data = data ?? new byte[SaveUtil.SIZE_G4RAW];
BAK = (byte[])Data.Clone();
Exportable = !Data.All(z => z == 0);

View file

@ -17,9 +17,9 @@ namespace PKHeX.Core
private const int SAVE_COUNT = 4;
public SAV4BR(byte[] data = null)
{
Data = data == null ? new byte[SaveUtil.SIZE_G4BR] : (byte[])data.Clone();
Data = data ?? new byte[SaveUtil.SIZE_G4BR];
BAK = (byte[])Data.Clone();
Exportable = !Data.SequenceEqual(new byte[Data.Length]);
Exportable = !Data.All(z => z == 0);
if (SaveUtil.GetIsG4BRSAV(Data) != GameVersion.BATREV)
return;

View file

@ -17,7 +17,7 @@ namespace PKHeX.Core
public override string Extension => ".sav";
public SAV5(byte[] data = null, GameVersion versionOverride = GameVersion.Any)
{
Data = data == null ? new byte[SaveUtil.SIZE_G5RAW] : (byte[])data.Clone();
Data = data ?? new byte[SaveUtil.SIZE_G5RAW];
BAK = (byte[])Data.Clone();
Exportable = !Data.All(z => z == 0);

View file

@ -17,7 +17,7 @@ namespace PKHeX.Core
public override string Extension => "";
public SAV6(byte[] data = null)
{
Data = data == null ? new byte[SaveUtil.SIZE_G6ORAS] : (byte[])data.Clone();
Data = data ?? new byte[SaveUtil.SIZE_G6ORAS];
BAK = (byte[])Data.Clone();
Exportable = !Data.All(z => z == 0);

View file

@ -22,7 +22,7 @@ namespace PKHeX.Core
public SAV7(byte[] data = null)
{
Data = data == null ? new byte[SaveUtil.SIZE_G7SM] : (byte[])data.Clone();
Data = data ?? new byte[SaveUtil.SIZE_G7SM];
BAK = (byte[])Data.Clone();
Exportable = !Data.All(z => z == 0);

View file

@ -51,7 +51,7 @@ namespace PKHeX.Core
internal const int SIZE_ENTRY = 72;
public ShadowInfoEntryXD(byte[] data = null)
{
Data = (byte[])(data?.Clone() ?? new byte[SIZE_ENTRY]);
Data = data ?? new byte[SIZE_ENTRY];
}
public bool IsSnagged => Data[0] >> 6 != 0;
@ -70,7 +70,7 @@ namespace PKHeX.Core
internal const int SIZE_ENTRY = 12;
public ShadowInfoEntryColo(byte[] data = null)
{
Data = (byte[])(data?.Clone() ?? new byte[SIZE_ENTRY]);
Data = data ?? new byte[SIZE_ENTRY];
}
public uint PID { get => BigEndian.ToUInt32(Data, 0x00); set => BigEndian.GetBytes(value).CopyTo(Data, 0x00); }
public int Met_Location { get => BigEndian.ToUInt16(Data, 0x06); set => BigEndian.GetBytes((ushort)value).CopyTo(Data, 0x06); }