Simplify pkm constructors

remove Identifier param. 99% of the time, identifier is not provided,
resulting in a useless call

end result:
provide 'create new' and 'create from' constructors
This commit is contained in:
Kurt 2019-03-16 18:41:56 -07:00
parent fb76032a39
commit 8a08d32dff
17 changed files with 48 additions and 64 deletions

View file

@ -22,12 +22,11 @@ namespace PKHeX.Core
public override bool Valid => ChecksumValid || (Sanity == 0 && Species <= MaxSpeciesID);
public BK4(byte[] decryptedData, string ident = null)
public BK4(byte[] decryptedData)
{
Data = decryptedData;
uint sv = ((PID & 0x3E000) >> 0xD) % 24;
Data = PKX.ShuffleArray(Data, sv, PKX.SIZE_4BLOCK);
Identifier = ident;
if (Sanity != 0 && Species <= MaxSpeciesID && !ChecksumValid) // We can only hope
RefreshChecksum();
if (Valid && Sanity == 0)
@ -42,7 +41,7 @@ namespace PKHeX.Core
SetStats(GetStats(PersonalInfo));
}
public override PKM Clone() => new BK4((byte[])Encrypt().Clone(), Identifier);
public override PKM Clone() => new BK4((byte[])Encrypt().Clone()){Identifier = Identifier};
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

@ -21,14 +21,9 @@ namespace PKHeX.Core
public override int Format => 3;
public override PersonalInfo PersonalInfo => PersonalTable.RS[Species];
public CK3(byte[] decryptedData, string ident = null)
{
Data = decryptedData;
Identifier = ident;
}
public CK3(byte[] decryptedData) => Data = decryptedData;
public CK3() => Data = new byte[SIZE_PARTY];
public override PKM Clone() => new CK3((byte[])Data.Clone(), Identifier);
public override PKM Clone() => new CK3((byte[])Data.Clone()) {Identifier = Identifier};
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

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

View file

@ -16,10 +16,12 @@ namespace PKHeX.Core
public override int Format => 1;
public PK1(byte[] decryptedData = null, string ident = null, bool jp = false) : base(decryptedData, ident, jp) { }
public PK1(byte[] decryptedData, bool jp = false) : base(decryptedData, jp) { }
public PK1(bool jp = false) : base(new byte[PKX.SIZE_1PARTY], jp) { }
public override PKM Clone() => new PK1((byte[])Data.Clone(), Identifier, Japanese)
public override PKM Clone() => new PK1((byte[])Data.Clone(), Japanese)
{
Identifier = Identifier,
otname = (byte[])otname.Clone(),
nick = (byte[])nick.Clone(),
};
@ -109,7 +111,7 @@ namespace PKHeX.Core
public PK2 ConvertToPK2()
{
PK2 pk2 = new PK2(null, Identifier, Japanese) {Species = Species};
PK2 pk2 = new PK2(null, Japanese) {Species = Species};
Array.Copy(Data, 0x7, pk2.Data, 0x1, 0x1A);
otname.CopyTo(pk2.otname, 0);
nick.CopyTo(pk2.nick, 0);

View file

@ -15,10 +15,12 @@ namespace PKHeX.Core
public override int Format => 2;
public PK2(byte[] decryptedData = null, string ident = null, bool jp = false) : base(decryptedData, ident, jp) { }
public PK2(byte[] decryptedData, bool jp = false) : base(decryptedData, jp) { }
public PK2(bool jp = false) : base(new byte[PKX.SIZE_2PARTY], jp) { }
public override PKM Clone() => new PK2((byte[])Data.Clone(), Identifier, Japanese)
public override PKM Clone() => new PK2((byte[])Data.Clone(), Japanese)
{
Identifier = Identifier,
otname = (byte[])otname.Clone(),
nick = (byte[])nick.Clone(),
IsEgg = IsEgg,
@ -96,7 +98,7 @@ namespace PKHeX.Core
public PK1 ConvertToPK1()
{
PK1 pk1 = new PK1(null, Identifier, Japanese) {TradebackStatus = TradebackType.WasTradeback};
PK1 pk1 = new PK1(Japanese) {TradebackStatus = TradebackType.WasTradeback};
Array.Copy(Data, 0x1, pk1.Data, 0x7, 0x1A);
pk1.Species = Species; // This will take care of Typing :)

View file

@ -19,16 +19,15 @@ namespace PKHeX.Core
public PK3() => Data = new byte[PKX.SIZE_3PARTY];
public PK3(byte[] decryptedData, string ident = null)
public PK3(byte[] decryptedData)
{
Data = decryptedData;
PKMConverter.CheckEncrypted(ref Data, Format);
Identifier = ident;
if (Data.Length != SIZE_PARTY)
Array.Resize(ref Data, SIZE_PARTY);
}
public override PKM Clone() => new PK3((byte[])Data.Clone(), Identifier);
public override PKM Clone() => new PK3((byte[])Data.Clone()){Identifier = Identifier};
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

@ -20,16 +20,15 @@ namespace PKHeX.Core
public PK4() => Data = new byte[PKX.SIZE_4PARTY];
public PK4(byte[] decryptedData, string ident = null)
public PK4(byte[] decryptedData)
{
Data = decryptedData;
PKMConverter.CheckEncrypted(ref Data, Format);
Identifier = ident;
if (Data.Length != SIZE_PARTY)
Array.Resize(ref Data, SIZE_PARTY);
}
public override PKM Clone() => new PK4((byte[])Data.Clone(), Identifier);
public override PKM Clone() => new PK4((byte[])Data.Clone()){Identifier = Identifier};
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

@ -26,16 +26,15 @@ namespace PKHeX.Core
public PK5() => Data = new byte[PKX.SIZE_5PARTY];
public PK5(byte[] decryptedData, string ident = null)
public PK5(byte[] decryptedData)
{
Data = decryptedData;
PKMConverter.CheckEncrypted(ref Data, Format);
Identifier = ident;
if (Data.Length != SIZE_PARTY)
Array.Resize(ref Data, SIZE_PARTY);
}
public override PKM Clone() => new PK5((byte[])Data.Clone(), Identifier);
public override PKM Clone() => new PK5((byte[])Data.Clone()){Identifier = Identifier};
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

@ -17,16 +17,15 @@ namespace PKHeX.Core
public PK6() => Data = new byte[PKX.SIZE_6PARTY];
public PK6(byte[] decryptedData, string ident = null)
public PK6(byte[] decryptedData)
{
Data = decryptedData;
PKMConverter.CheckEncrypted(ref Data, Format);
Identifier = ident;
if (Data.Length != SIZE_PARTY)
Array.Resize(ref Data, SIZE_PARTY);
}
public override PKM Clone() => new PK6((byte[])Data.Clone(), Identifier);
public override PKM Clone() => new PK6((byte[])Data.Clone()){Identifier = Identifier};
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

@ -18,16 +18,15 @@ namespace PKHeX.Core
public PK7() => Data = new byte[PKX.SIZE_6PARTY];
public PK7(byte[] decryptedData, string ident = null)
public PK7(byte[] decryptedData)
{
Data = decryptedData;
PKMConverter.CheckEncrypted(ref Data, Format);
Identifier = ident;
if (Data.Length != SIZE_PARTY)
Array.Resize(ref Data, SIZE_PARTY);
}
public override PKM Clone() => new PK7((byte[])Data.Clone(), Identifier);
public override PKM Clone() => new PK7((byte[])Data.Clone()){Identifier = Identifier};
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

@ -31,11 +31,10 @@ namespace PKHeX.Core
private int StringLength => Japanese ? STRLEN_J : STRLEN_U;
public override bool Japanese => otname.Length == STRLEN_J;
protected _K12(byte[] decryptedData, string ident = null, bool jp = false)
protected _K12(byte[] decryptedData, bool jp = false)
{
int partySize = SIZE_PARTY;
Data = decryptedData ?? new byte[partySize];
Identifier = ident;
Data = decryptedData;
if (Data.Length != partySize)
Array.Resize(ref Data, partySize);
int strLen = jp ? STRLEN_J : STRLEN_U;

View file

@ -80,40 +80,36 @@ namespace PKHeX.Core
/// <param name="ident">Optional identifier for the Pokemon. Usually the full path of the source file.</param>
/// <param name="prefer">Optional identifier for the preferred generation. Usually the generation of the destination save file.</param>
/// <returns>An instance of <see cref="PKM"/> created from the given <paramref name="data"/>, or null if <paramref name="data"/> is invalid.</returns>
public static PKM GetPKMfromBytes(byte[] data, string ident = null, int prefer = 7)
public static PKM GetPKMfromBytes(byte[] data, int prefer = 7)
{
int format = GetPKMDataFormat(data);
switch (format)
{
case 1:
var PL1 = new PokeList1(data);
if (ident != null)
PL1[0].Identifier = ident;
return PL1[0];
case 2:
var PL2 = new PokeList2(data);
if (ident != null)
PL2[0].Identifier = ident;
return PL2[0];
case 3:
switch (data.Length) {
case PKX.SIZE_3CSTORED: return new CK3(data, ident);
case PKX.SIZE_3XSTORED: return new XK3(data, ident);
default: return new PK3(data, ident);
case PKX.SIZE_3CSTORED: return new CK3(data);
case PKX.SIZE_3XSTORED: return new XK3(data);
default: return new PK3(data);
}
case 4:
var pk = new PK4(data, ident);
var pk = new PK4(data);
if (!pk.Valid || pk.Sanity != 0)
{
var bk = new BK4(data, ident);
var bk = new BK4(data);
if (bk.Valid)
return bk;
}
return pk;
case 5:
return new PK5(data, ident);
return new PK5(data);
case 6:
var pkx = new PK6(data, ident);
var pkx = new PK6(data);
return CheckPKMFormat7(pkx, prefer);
default:
return null;
@ -130,7 +126,7 @@ namespace PKHeX.Core
{
if (GameVersion.GG.Contains(pk.Version))
return new PB7(pk.Data);
return IsPK6FormatReallyPK7(pk, prefer) ? new PK7(pk.Data, pk.Identifier) : (PKM)pk;
return IsPK6FormatReallyPK7(pk, prefer) ? new PK7(pk.Data) : (PKM)pk;
}
/// <summary>

View file

@ -20,14 +20,9 @@ namespace PKHeX.Core
public override int Format => 3;
public override PersonalInfo PersonalInfo => PersonalTable.RS[Species];
public XK3(byte[] decryptedData, string ident = null)
{
Data = decryptedData;
Identifier = ident;
}
public XK3(byte[] decryptedData) => Data = decryptedData;
public XK3() => Data = new byte[SIZE_PARTY];
public override PKM Clone() => new XK3((byte[])Data.Clone(), Identifier) {Purification = Purification};
public override PKM Clone() => new XK3((byte[])Data.Clone()){Identifier = Identifier, 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

@ -180,7 +180,7 @@ namespace PKHeX.Core
private int SIZE_STOREDBOX => PokeList1.GetDataLength(Japanese ? PokeListType.StoredJP : PokeListType.Stored, Japanese);
private int SIZE_STOREDPARTY => PokeList1.GetDataLength(PokeListType.Party, Japanese);
public override PKM BlankPKM => new PK1(null, null, Japanese);
public override PKM BlankPKM => new PK1(Japanese);
public override Type PKMType => typeof(PK1);
public override int MaxMoveID => Legal.MaxMoveID_1;

View file

@ -92,7 +92,7 @@ namespace PKHeX.Core
{
int offset = start + (PKX.SIZE_6PARTY*((t * 6) + p));
offset += 8*(((t * 6) + p)/6); // 8 bytes padding between teams
Teams[t][p] = new PK6(Data.Skip(offset).Take(PKX.SIZE_6PARTY).ToArray(), $"Team {t}, Slot {p}");
Teams[t][p] = new PK6(Data.Skip(offset).Take(PKX.SIZE_6PARTY).ToArray()) {Identifier = $"Team {t}, Slot {p}"};
}
}
return Teams;

View file

@ -34,7 +34,7 @@ namespace PKHeX.Core
for (int p = 0; p < 6; p++)
{
int offset = offsets[t] + (PKX.SIZE_6PARTY * p);
Teams[t][p] = new PK7(Data.Skip(offset).Take(PKX.SIZE_6STORED).ToArray(), $"Team {t}, Slot {p}");
Teams[t][p] = new PK7(Data.Skip(offset).Take(PKX.SIZE_6STORED).ToArray()) {Identifier = $"Team {t}, Slot {p}"};
}
}
return Teams;

View file

@ -344,13 +344,15 @@ namespace PKHeX.WinForms
var extensions = new HashSet<string>(PKM.Extensions.Select(z => $".{z}"));
Parallel.ForEach(files, file =>
{
FileInfo fi = new FileInfo(file);
var fi = new FileInfo(file);
if (!extensions.Contains(fi.Extension) || !PKX.IsPKM(fi.Length)) return;
var data = File.ReadAllBytes(file);
var prefer = PKX.GetPKMFormatFromExtension(fi.Extension, SAV.Generation);
var pk = PKMConverter.GetPKMfromBytes(data, file, prefer);
if (pk?.Species > 0)
dbTemp.Add(pk);
var pk = PKMConverter.GetPKMfromBytes(data, prefer);
if (!(pk?.Species > 0))
return;
pk.Identifier = file;
dbTemp.Add(pk);
});
#if LOADALL