misc refactoring

simplification / splitting, also more usum prep and other readability
updates
This commit is contained in:
Kurt 2017-10-17 23:19:34 -07:00
parent 0cc60e6006
commit 886b2ef632
5 changed files with 134 additions and 122 deletions

View file

@ -110,6 +110,7 @@ namespace PKHeX.Core
.Zip(Pouch_ZCrystalHeld_SM, (k, v) => new { Key = (int)k, Value = (int)v })
.ToDictionary(x => x.Key, x => x.Value);
internal static readonly ushort[] HeldItems_SM = new ushort[1].Concat(Pouch_Items_SM).Concat(Pouch_Berries_SM).Concat(Pouch_Medicine_SM).Concat(Pouch_ZCrystalHeld_SM).ToArray();
internal static readonly ushort[] HeldItems_USUM = HeldItems_SM; // todo
private static readonly HashSet<int> WildPokeballs7 = new HashSet<int> {
0x01, 0x02, 0x03, 0x04, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
@ -346,6 +347,7 @@ namespace PKHeX.Core
};
internal static readonly HashSet<int> ValidMet_USUM = new HashSet<int>
{
// todo
};
private static readonly int[] TMHM_SM =
@ -491,6 +493,6 @@ namespace PKHeX.Core
715, // Fairy Gem
};
#endregion
internal static readonly bool[] ReleasedHeldItems_7 = Enumerable.Range(0, MaxItemID_7_USUM+1).Select(i => HeldItems_SM.Contains((ushort)i) && !UnreleasedHeldItems_7.Contains(i)).ToArray();
internal static readonly bool[] ReleasedHeldItems_7 = Enumerable.Range(0, MaxItemID_7_USUM+1).Select(i => HeldItems_USUM.Contains((ushort)i) && !UnreleasedHeldItems_7.Contains(i)).ToArray();
}
}

View file

@ -229,13 +229,8 @@ namespace PKHeX.Core
{
// Check to see if date is valid
if (!Util.IsDateValid(2000 + Egg_Year, Egg_Month, Egg_Day))
{
return null;
}
else
{
return new DateTime(2000 + Egg_Year, Egg_Month, Egg_Day);
}
return new DateTime(2000 + Egg_Year, Egg_Month, Egg_Day);
}
set
{
@ -634,10 +629,10 @@ namespace PKHeX.Core
{
ReorderMoves();
if (Move1 == 0) { Move1_PP = 0; Move1_PPUps = 0; }
if (Move2 == 0) { Move2_PP = 0; Move2_PPUps = 0; }
if (Move3 == 0) { Move3_PP = 0; Move3_PPUps = 0; }
if (Move4 == 0) { Move4_PP = 0; Move4_PPUps = 0; }
if (Move1 == 0) Move1_PP = Move1_PPUps = 0;
if (Move2 == 0) Move2_PP = Move2_PPUps = 0;
if (Move3 == 0) Move3_PP = Move3_PPUps = 0;
if (Move4 == 0) Move4_PP = Move4_PPUps = 0;
}
/// <summary>

View file

@ -102,18 +102,17 @@ namespace PKHeX.Core
SetChecksum(Data, 0x1C0000, 0x100, 0x1C0008);
SetChecksum(Data, 0x1C0000, 0x1C0000, 0x1BFF80 + 0x1C0000);
}
public override bool ChecksumsValid
{
get {
bool valid = VerifyChecksum(Data, 0, 0x1C0000, 0x1BFF80);
valid &= VerifyChecksum(Data, 0, 0x100, 8);
valid &= VerifyChecksum(Data, 0x1C0000, 0x1C0000, 0x1BFF80 + 0x1C0000);
valid &= VerifyChecksum(Data, 0x1C0000, 0x100, 0x1C0008);
return valid;
}
}
public override bool ChecksumsValid => IsChecksumsValid(Data);
public override string ChecksumInfo => $"Checksums valid: {ChecksumsValid}.";
public static bool IsChecksumsValid(byte[] sav)
{
return VerifyChecksum(sav, 0x000000, 0x1C0000, 0x1BFF80)
&& VerifyChecksum(sav, 0x000000, 0x000100, 0x000008)
&& VerifyChecksum(sav, 0x1C0000, 0x1C0000, 0x1BFF80 + 0x1C0000)
&& VerifyChecksum(sav, 0x1C0000, 0x000100, 0x1C0008);
}
// Trainer Info
public override GameVersion Version { get => GameVersion.BATREV; protected set { } }

View file

@ -386,11 +386,11 @@ namespace PKHeX.Core
// Daycare
public int DaycareIndex = 0;
public virtual bool HasTwoDaycares => false;
public virtual int GetDaycareSlotOffset(int loc, int slot) { return -1; }
public virtual uint? GetDaycareEXP(int loc, int slot) { return null; }
public virtual string GetDaycareRNGSeed(int loc) { return null; }
public virtual bool? IsDaycareHasEgg(int loc) { return null; }
public virtual bool? IsDaycareOccupied(int loc, int slot) { return null; }
public virtual int GetDaycareSlotOffset(int loc, int slot) => -1;
public virtual uint? GetDaycareEXP(int loc, int slot) => null;
public virtual string GetDaycareRNGSeed(int loc) => null;
public virtual bool? IsDaycareHasEgg(int loc) => null;
public virtual bool? IsDaycareOccupied(int loc, int slot) => null;
public virtual void SetDaycareEXP(int loc, int slot, uint EXP) { }
public virtual void SetDaycareRNGSeed(int loc, string seed) { }
@ -449,18 +449,7 @@ namespace PKHeX.Core
if (box1 >= BoxCount || box2 >= BoxCount) // invalid box positions
return false;
int min = BoxSlotCount * box1;
int max = BoxSlotCount * box1 + BoxSlotCount;
if (LockedSlots.Any(slot => min <= slot && slot < max)) // locked slot within box
return false;
if (TeamSlots.Any(slot => min <= slot && slot < max)) // team slot within box
return false;
min = BoxSlotCount * box2;
max = BoxSlotCount * box2 + BoxSlotCount;
if (LockedSlots.Any(slot => min <= slot && slot < max)) // locked slot within box
return false;
if (TeamSlots.Any(slot => min <= slot && slot < max)) // team slot within box
if (!IsBoxAbleToMove(box1) || !IsBoxAbleToMove(box2))
return false;
// Data
@ -483,6 +472,16 @@ namespace PKHeX.Core
SetBoxWallpaper(box2, b1w);
return true;
}
private bool IsBoxAbleToMove(int box)
{
int min = BoxSlotCount * box;
int max = BoxSlotCount * box + BoxSlotCount;
if (LockedSlots.Any(slot => min <= slot && slot < max)) // locked slot within box
return false;
if (TeamSlots.Any(slot => min <= slot && slot < max)) // team slot within box
return false;
return true;
}
protected virtual int GetBoxWallpaperOffset(int box) { return -1; }
public virtual int GetBoxWallpaper(int box)
@ -519,22 +518,29 @@ namespace PKHeX.Core
SetDex(pkm);
SetPartyValues(pkm, isParty: true);
for (int i = 0; i < 6; i++)
if (GetPartyOffset(i) == offset)
{
if (pkm.Species != 0)
{
if (PartyCount <= i)
PartyCount = i + 1;
}
else if (PartyCount > i)
PartyCount = i;
break;
}
int i = GetPartyIndex(offset);
if (i <= -1)
throw new ArgumentException("Invalid Party offset provided; unable to resolve party slot index.");
// update party count
if (pkm.Species != 0)
{
if (PartyCount <= i)
PartyCount = i + 1;
}
else if (PartyCount > i)
PartyCount = i;
SetData(pkm.EncryptedPartyData, offset);
Edited = true;
}
private int GetPartyIndex(int offset)
{
for (int i = 0; i < 6; i++)
if (GetPartyOffset(i) == offset)
return i;
return -1;
}
public virtual void SetStoredSlot(PKM pkm, int offset, bool? trade = null, bool? dex = null)
{
if (pkm == null) return;
@ -562,12 +568,12 @@ namespace PKHeX.Core
SetStoredSlot(BlankPKM, GetPartyOffset(5), false, false);
PartyCount -= 1;
}
public virtual bool IsSlotLocked(int box, int slot) { return false; }
public virtual bool IsSlotLocked(int box, int slot) => false;
public bool IsAnySlotLockedInBox(int BoxStart, int BoxEnd)
{
return LockedSlots.Any(slot => BoxStart*BoxSlotCount <= slot && slot < (BoxEnd + 1)*BoxSlotCount);
}
public virtual bool IsSlotInBattleTeam(int box, int slot) { return false; }
public virtual bool IsSlotInBattleTeam(int box, int slot) => false;
public void SortBoxes(int BoxStart = 0, int BoxEnd = -1)
{

View file

@ -39,7 +39,7 @@ namespace PKHeX.Core
public const int SIZE_G1BAT = 0x802C;
private static readonly HashSet<int> SIZES = new HashSet<int>
{
SIZE_G7SM,
SIZE_G7SM, SIZE_G7USUM,
SIZE_G6XY, SIZE_G6ORAS, SIZE_G6ORASDEMO,
SIZE_G5RAW, SIZE_G5BW, SIZE_G5B2W2,
SIZE_G4BR, SIZE_G4RAW,
@ -224,7 +224,7 @@ namespace PKHeX.Core
/// <returns>Version Identifier or Invalid if type cannot be determined.</returns>
internal static GameVersion GetIsG3BOXSAV(byte[] data)
{
if (!new[] { SIZE_G3BOX, SIZE_G3BOXGCI }.Contains(data.Length))
if (data.Length != SIZE_G3BOX && data.Length != SIZE_G3BOXGCI)
return GameVersion.Invalid;
byte[] sav = data;
@ -248,7 +248,7 @@ namespace PKHeX.Core
/// <returns>Version Identifier or Invalid if type cannot be determined.</returns>
internal static GameVersion GetIsG3COLOSAV(byte[] data)
{
if (!new[] { SIZE_G3COLO, SIZE_G3COLOGCI }.Contains(data.Length))
if (data.Length != SIZE_G3COLO && data.Length != SIZE_G3COLOGCI)
return GameVersion.Invalid;
// Check the intro bytes for each save slot
@ -266,7 +266,7 @@ namespace PKHeX.Core
/// <returns>Version Identifier or Invalid if type cannot be determined.</returns>
internal static GameVersion GetIsG3XDSAV(byte[] data)
{
if (!new[] { SIZE_G3XD, SIZE_G3XDGCI }.Contains(data.Length))
if (data.Length != SIZE_G3XD && data.Length != SIZE_G3XDGCI)
return GameVersion.Invalid;
// Check the intro bytes for each save slot
@ -322,13 +322,7 @@ namespace PKHeX.Core
return GameVersion.Invalid;
byte[] sav = SAV4BR.DecryptPBRSaveData(data);
bool valid = SAV4BR.VerifyChecksum(sav, 0, 0x1C0000, 0x1BFF80);
valid &= SAV4BR.VerifyChecksum(sav, 0, 0x100, 8);
valid &= SAV4BR.VerifyChecksum(sav, 0x1C0000, 0x1C0000, 0x1BFF80 + 0x1C0000);
valid &= SAV4BR.VerifyChecksum(sav, 0x1C0000, 0x100, 0x1C0008);
return valid ? GameVersion.BATREV : GameVersion.Invalid;
return SAV4BR.IsChecksumsValid(sav) ? GameVersion.BATREV : GameVersion.Invalid;
}
/// <summary>Determines the type of 5th gen save</summary>
/// <param name="data">Save data of which to determine the type</param>
@ -375,9 +369,20 @@ namespace PKHeX.Core
/// <returns>Version Identifier or Invalid if type cannot be determined.</returns>
private static GameVersion GetIsG7SAV(byte[] data)
{
if (!new [] {SIZE_G7SM}.Contains(data.Length))
if (data.Length != SIZE_G7SM && data.Length != SIZE_G7USUM)
return GameVersion.Invalid;
return GameVersion.SM;
if (BitConverter.ToUInt32(data, data.Length - 0x1F0) != BEEF)
return GameVersion.Invalid;
switch (data.Length)
{
case SIZE_G7SM:
return GameVersion.SM;
case SIZE_G7USUM:
return GameVersion.USUM;
}
return GameVersion.Invalid;
}
@ -689,6 +694,59 @@ namespace PKHeX.Core
bool IsGameMatchHeader(IEnumerable<string> headers, byte[] data) => headers.Contains(Encoding.ASCII.GetString(data, 0, 4));
}
private static readonly ushort[] formtable_SM = // u16 species, u16 formcount
{
0x0003, 0x0002, 0x0006, 0x0003, 0x0009, 0x0002, 0x000F, 0x0002,
0x0012, 0x0002, 0x0013, 0x0002, 0x0014, 0x0003, 0x0019, 0x0007,
0x001A, 0x0002, 0x001B, 0x0002, 0x001C, 0x0002, 0x0025, 0x0002,
0x0026, 0x0002, 0x0032, 0x0002, 0x0033, 0x0002, 0x0034, 0x0002,
0x0035, 0x0002, 0x0041, 0x0002, 0x004A, 0x0002, 0x004B, 0x0002,
0x004C, 0x0002, 0x0050, 0x0002, 0x0058, 0x0002, 0x0059, 0x0002,
0x005E, 0x0002, 0x0067, 0x0002, 0x0069, 0x0002, 0x0073, 0x0002,
0x007F, 0x0002, 0x0082, 0x0002, 0x008E, 0x0002, 0x0096, 0x0003,
0x00B5, 0x0002, 0x00C9, 0x001C, 0x00D0, 0x0002, 0x00D4, 0x0002,
0x00D6, 0x0002, 0x00E5, 0x0002, 0x00F8, 0x0002, 0x00FE, 0x0002,
0x0101, 0x0002, 0x0104, 0x0002, 0x011A, 0x0002, 0x012E, 0x0002,
0x012F, 0x0002, 0x0132, 0x0002, 0x0134, 0x0002, 0x0136, 0x0002,
0x013F, 0x0002, 0x0143, 0x0002, 0x014E, 0x0002, 0x015F, 0x0004,
0x0162, 0x0002, 0x0167, 0x0002, 0x016A, 0x0002, 0x0175, 0x0002,
0x0178, 0x0002, 0x017C, 0x0002, 0x017D, 0x0002, 0x017E, 0x0002,
0x017F, 0x0002, 0x0180, 0x0002, 0x0182, 0x0004, 0x019C, 0x0003,
0x019D, 0x0003, 0x01A5, 0x0002, 0x01A6, 0x0002, 0x01A7, 0x0002,
0x01AC, 0x0002, 0x01BD, 0x0002, 0x01C0, 0x0002, 0x01CC, 0x0002,
0x01DB, 0x0002, 0x01DF, 0x0006, 0x01E7, 0x0002, 0x01EC, 0x0002,
0x01ED, 0x0012, 0x0213, 0x0002, 0x0226, 0x0002, 0x022B, 0x0002,
0x0249, 0x0004, 0x024A, 0x0004, 0x0281, 0x0002, 0x0282, 0x0002,
0x0285, 0x0002, 0x0286, 0x0003, 0x0287, 0x0002, 0x0288, 0x0002,
0x0289, 0x0005, 0x0292, 0x0003, 0x029A, 0x0014, 0x029D, 0x0005,
0x029E, 0x0006, 0x029F, 0x0005, 0x02A4, 0x000A, 0x02A6, 0x0002,
0x02A9, 0x0002, 0x02C6, 0x0004, 0x02C7, 0x0004, 0x02CC, 0x0002,
0x02CE, 0x0005, 0x02CF, 0x0002, 0x02D0, 0x0002, 0x02DF, 0x0002,
0x02E2, 0x0002, 0x02E5, 0x0004, 0x02E9, 0x0002, 0x02EA, 0x0002,
0x02F2, 0x0002, 0x02F6, 0x0002, 0x0305, 0x0012, 0x0306, 0x000E,
0x030A, 0x0004, 0x0310, 0x0002, 0x0321, 0x0002,
};
private static readonly ushort[] formtable_USUM = // u16 species, u16 formcount
{
// todo
};
private static int GetDexFormBitIndex(int species, int formct, int start, IReadOnlyList<ushort> formtable)
{
int formindex = start;
int f = 0;
for (int i = 0; i < formtable.Count; i += 2)
{
int s = formtable[i];
f = formtable[i + 1];
if (s == species)
break;
formindex += f - 1;
}
if (f > formct)
return -1;
return formindex;
}
public static int GetDexFormIndexBW(int species, int formct)
{
if (formct < 1 || species < 0)
@ -807,55 +865,8 @@ namespace PKHeX.Core
default: return GetDexFormIndexXY(species, formct);
}
}
public static int GetDexFormIndexSM(int species, int formct, int start)
{
ushort[] formtable = // u16 species, u16 formcount
{
0x0003, 0x0002, 0x0006, 0x0003, 0x0009, 0x0002, 0x000F, 0x0002,
0x0012, 0x0002, 0x0013, 0x0002, 0x0014, 0x0003, 0x0019, 0x0007,
0x001A, 0x0002, 0x001B, 0x0002, 0x001C, 0x0002, 0x0025, 0x0002,
0x0026, 0x0002, 0x0032, 0x0002, 0x0033, 0x0002, 0x0034, 0x0002,
0x0035, 0x0002, 0x0041, 0x0002, 0x004A, 0x0002, 0x004B, 0x0002,
0x004C, 0x0002, 0x0050, 0x0002, 0x0058, 0x0002, 0x0059, 0x0002,
0x005E, 0x0002, 0x0067, 0x0002, 0x0069, 0x0002, 0x0073, 0x0002,
0x007F, 0x0002, 0x0082, 0x0002, 0x008E, 0x0002, 0x0096, 0x0003,
0x00B5, 0x0002, 0x00C9, 0x001C, 0x00D0, 0x0002, 0x00D4, 0x0002,
0x00D6, 0x0002, 0x00E5, 0x0002, 0x00F8, 0x0002, 0x00FE, 0x0002,
0x0101, 0x0002, 0x0104, 0x0002, 0x011A, 0x0002, 0x012E, 0x0002,
0x012F, 0x0002, 0x0132, 0x0002, 0x0134, 0x0002, 0x0136, 0x0002,
0x013F, 0x0002, 0x0143, 0x0002, 0x014E, 0x0002, 0x015F, 0x0004,
0x0162, 0x0002, 0x0167, 0x0002, 0x016A, 0x0002, 0x0175, 0x0002,
0x0178, 0x0002, 0x017C, 0x0002, 0x017D, 0x0002, 0x017E, 0x0002,
0x017F, 0x0002, 0x0180, 0x0002, 0x0182, 0x0004, 0x019C, 0x0003,
0x019D, 0x0003, 0x01A5, 0x0002, 0x01A6, 0x0002, 0x01A7, 0x0002,
0x01AC, 0x0002, 0x01BD, 0x0002, 0x01C0, 0x0002, 0x01CC, 0x0002,
0x01DB, 0x0002, 0x01DF, 0x0006, 0x01E7, 0x0002, 0x01EC, 0x0002,
0x01ED, 0x0012, 0x0213, 0x0002, 0x0226, 0x0002, 0x022B, 0x0002,
0x0249, 0x0004, 0x024A, 0x0004, 0x0281, 0x0002, 0x0282, 0x0002,
0x0285, 0x0002, 0x0286, 0x0003, 0x0287, 0x0002, 0x0288, 0x0002,
0x0289, 0x0005, 0x0292, 0x0003, 0x029A, 0x0014, 0x029D, 0x0005,
0x029E, 0x0006, 0x029F, 0x0005, 0x02A4, 0x000A, 0x02A6, 0x0002,
0x02A9, 0x0002, 0x02C6, 0x0004, 0x02C7, 0x0004, 0x02CC, 0x0002,
0x02CE, 0x0005, 0x02CF, 0x0002, 0x02D0, 0x0002, 0x02DF, 0x0002,
0x02E2, 0x0002, 0x02E5, 0x0004, 0x02E9, 0x0002, 0x02EA, 0x0002,
0x02F2, 0x0002, 0x02F6, 0x0002, 0x0305, 0x0012, 0x0306, 0x000E,
0x030A, 0x0004, 0x0310, 0x0002, 0x0321, 0x0002,
};
int formindex = start;
int f = 0;
for (int i = 0; i < formtable.Length; i += 2)
{
int s = formtable[i];
f = formtable[i + 1];
if (s == species)
break;
formindex += f - 1;
}
if (f > formct)
return -1;
return formindex;
}
public static int GetDexFormIndexSM(int species, int formct, int start) => GetDexFormBitIndex(species, formct, start, formtable_SM);
public static int GetDexFormIndexUSUM(int species, int formct, int start) => GetDexFormBitIndex(species, formct, start, formtable_USUM);
public static int GetCXDVersionID(int gen3version)
{
@ -922,14 +933,13 @@ namespace PKHeX.Core
oldKeys[2] += 0x17;
oldKeys[3] += 0x13;
ushort[] keys = new ushort[4];
keys[0] = (ushort)(oldKeys[0] & 0xf | oldKeys[1] << 4 & 0xf0 | oldKeys[2] << 8 & 0xf00 | oldKeys[3] << 12 & 0xf000);
keys[1] = (ushort)(oldKeys[0] >> 4 & 0xf | oldKeys[1] & 0xf0 | oldKeys[2] << 4 & 0xf00 | oldKeys[3] << 8 & 0xf000);
keys[2] = (ushort)(oldKeys[2] & 0xf00 | (oldKeys[1] & 0xf00) >> 4 | (oldKeys[0] & 0xf00) >> 8 | oldKeys[3] << 4 & 0xf000);
keys[3] = (ushort)(oldKeys[0] >> 12 & 0xf | oldKeys[1] >> 8 & 0xf0 | oldKeys[2] >> 4 & 0xf00 | oldKeys[3] & 0xf000);
return keys;
return new[]
{
(ushort)(oldKeys[0] & 0xf | oldKeys[1] << 4 & 0xf0 | oldKeys[2] << 8 & 0xf00 | oldKeys[3] << 12 & 0xf000),
(ushort)(oldKeys[0] >> 4 & 0xf | oldKeys[1] & 0xf0 | oldKeys[2] << 4 & 0xf00 | oldKeys[3] << 8 & 0xf000),
(ushort)(oldKeys[2] & 0xf00 | (oldKeys[1] & 0xf00) >> 4 | (oldKeys[0] & 0xf00) >> 8 | oldKeys[3] << 4 & 0xf000),
(ushort)(oldKeys[0] >> 12 & 0xf | oldKeys[1] >> 8 & 0xf0 | oldKeys[2] >> 4 & 0xf00 | oldKeys[3] & 0xf000),
};
}
/// <summary>