Minor tweaks

Muh infinitesimally small perf gainz found while waiting for next dota match
This commit is contained in:
Kurt 2021-07-26 23:33:56 -07:00
parent 97683932a1
commit 4ed0e30ace
14 changed files with 132 additions and 89 deletions

View file

@ -154,7 +154,7 @@ namespace PKHeX.Core
{
// Load Gen2 Gender Ratios into Gen1
PersonalInfo[] rb = RB.Table, y = Y.Table, gs = GS.Table;
for (int i = 0; i <= Legal.MaxSpeciesID_1; i++)
for (int i = Legal.MaxSpeciesID_1; i >= 0; i--)
rb[i].Gender = y[i].Gender = gs[i].Gender;
}
@ -164,10 +164,11 @@ namespace PKHeX.Core
var machine = BinLinker.Unpack(Util.GetBinaryResource("hmtm_g3.pkl"), "g3");
var tutors = BinLinker.Unpack(Util.GetBinaryResource("tutors_g3.pkl"), "g3");
var table = E.Table;
for (int i = 0; i <= Legal.MaxSpeciesID_3; i++)
for (int i = Legal.MaxSpeciesID_3; i >= 0; i--)
{
table[i].AddTMHM(machine[i]);
table[i].AddTypeTutors(tutors[i]);
var entry = table[i];
entry.AddTMHM(machine[i]);
entry.AddTypeTutors(tutors[i]);
}
}

View file

@ -22,7 +22,7 @@ namespace PKHeX.Core
public override IReadOnlyList<string> PKMExtensions => PKM.Extensions.Where(f =>
{
int gen = f.Last() - 0x30;
int gen = f[^1] - 0x30;
return gen is 1 or 2;
}).ToArray();

View file

@ -22,7 +22,7 @@ namespace PKHeX.Core
public override IReadOnlyList<string> PKMExtensions => PKM.Extensions.Where(f =>
{
int gen = f.Last() - 0x30;
int gen = f[^1] - 0x30;
if (Korean)
return gen == 2;
return gen is 1 or 2;

View file

@ -12,7 +12,7 @@ namespace PKHeX.Core
public abstract class SAV4 : SaveFile
{
protected internal override string ShortSummary => $"{OT} ({Version}) - {PlayTimeString}";
public override string Extension => ".sav";
public sealed override string Extension => ".sav";
// Blocks & Offsets
private readonly int GeneralBlockPosition; // Small Block
@ -22,17 +22,14 @@ namespace PKHeX.Core
// SaveData is chunked into two pieces.
protected readonly byte[] Storage;
public readonly byte[] General;
protected override byte[] BoxBuffer => Storage;
protected override byte[] PartyBuffer => General;
protected sealed override byte[] BoxBuffer => Storage;
protected sealed override byte[] PartyBuffer => General;
protected abstract int StorageStart { get; }
public abstract Zukan4 Dex { get; }
/// <inheritdoc />
public override bool GetFlag(int offset, int bitIndex) => FlagUtil.GetFlag(General, offset, bitIndex);
/// <inheritdoc />
public override void SetFlag(int offset, int bitIndex, bool value) => FlagUtil.SetFlag(General, offset, bitIndex, value);
public sealed override bool GetFlag(int offset, int bitIndex) => FlagUtil.GetFlag(General, offset, bitIndex);
public sealed override void SetFlag(int offset, int bitIndex, bool value) => FlagUtil.SetFlag(General, offset, bitIndex, value);
protected SAV4(int gSize, int sSize)
{
@ -53,7 +50,7 @@ namespace PKHeX.Core
}
// Configuration
protected override SaveFile CloneInternal()
protected sealed override SaveFile CloneInternal()
{
var sav = CloneInternal4();
SetData(sav.General, General, 0);
@ -63,7 +60,7 @@ namespace PKHeX.Core
protected abstract SAV4 CloneInternal4();
public override void CopyChangesFrom(SaveFile sav)
public sealed override void CopyChangesFrom(SaveFile sav)
{
SetData(sav.Data, 0);
var s4 = (SAV4)sav;
@ -71,31 +68,28 @@ namespace PKHeX.Core
SetData(Storage, s4.Storage, 0);
}
protected override int SIZE_STORED => PokeCrypto.SIZE_4STORED;
protected override int SIZE_PARTY => PokeCrypto.SIZE_4PARTY;
public override PKM BlankPKM => new PK4();
public override Type PKMType => typeof(PK4);
protected sealed override int SIZE_STORED => PokeCrypto.SIZE_4STORED;
protected sealed override int SIZE_PARTY => PokeCrypto.SIZE_4PARTY;
public sealed override PKM BlankPKM => new PK4();
public sealed override Type PKMType => typeof(PK4);
public override int BoxCount => 18;
public override int MaxEV => 255;
public override int Generation => 4;
protected override int EventFlagMax => 0xB60; // 2912
protected override int EventConstMax => (EventFlag - EventConst) >> 1;
protected override int GiftCountMax => 11;
public override int OTLength => 7;
public override int NickLength => 10;
public override int MaxMoney => 999999;
public override int MaxCoins => 50_000;
public sealed override int BoxCount => 18;
public sealed override int MaxEV => 255;
public sealed override int Generation => 4;
protected sealed override int EventFlagMax => 0xB60; // 2912
protected sealed override int EventConstMax => (EventFlag - EventConst) >> 1;
protected sealed override int GiftCountMax => 11;
public sealed override int OTLength => 7;
public sealed override int NickLength => 10;
public sealed override int MaxMoney => 999999;
public sealed override int MaxCoins => 50_000;
public override int MaxMoveID => Legal.MaxMoveID_4;
public override int MaxSpeciesID => Legal.MaxSpeciesID_4;
public sealed override int MaxMoveID => Legal.MaxMoveID_4;
public sealed override int MaxSpeciesID => Legal.MaxSpeciesID_4;
// MaxItemID
public override int MaxAbilityID => Legal.MaxAbilityID_4;
public override int MaxBallID => Legal.MaxBallID_4;
public override int MaxGameID => Legal.MaxGameID_4; // Colo/XD
public bool HGSS => Version == GameVersion.HGSS;
public bool DP => Version == GameVersion.DP;
public sealed override int MaxAbilityID => Legal.MaxAbilityID_4;
public sealed override int MaxBallID => Legal.MaxBallID_4;
public sealed override int MaxGameID => Legal.MaxGameID_4; // Colo/XD
// Checksums
protected abstract int FooterSize { get; }
@ -103,7 +97,7 @@ namespace PKHeX.Core
private static ushort GetBlockChecksumSaved(byte[] data) => BitConverter.ToUInt16(data, data.Length - 2);
private bool GetBlockChecksumValid(byte[] data) => CalcBlockChecksum(data) == GetBlockChecksumSaved(data);
protected override void SetChecksums()
protected sealed override void SetChecksums()
{
BitConverter.GetBytes(CalcBlockChecksum(General)).CopyTo(General, General.Length - 2);
BitConverter.GetBytes(CalcBlockChecksum(Storage)).CopyTo(Storage, Storage.Length - 2);
@ -113,7 +107,7 @@ namespace PKHeX.Core
Storage.CopyTo(Data, (StorageBlockPosition * PartitionSize) + StorageStart);
}
public override bool ChecksumsValid
public sealed override bool ChecksumsValid
{
get
{
@ -126,7 +120,7 @@ namespace PKHeX.Core
}
}
public override string ChecksumInfo
public sealed override string ChecksumInfo
{
get
{
@ -150,7 +144,7 @@ namespace PKHeX.Core
protected int AdventureInfo = int.MinValue;
protected int Seal = int.MinValue;
protected int Trainer1;
public int GTS { get; } = int.MinValue;
public int GTS { get; protected set; } = int.MinValue;
// Storage
public override int PartyCount
@ -159,7 +153,7 @@ namespace PKHeX.Core
protected set => General[Party - 4] = (byte)value;
}
public override int GetPartyOffset(int slot) => Party + (SIZE_PARTY * slot);
public sealed override int GetPartyOffset(int slot) => Party + (SIZE_PARTY * slot);
// Trainer Info
public override string OT
@ -245,10 +239,10 @@ namespace PKHeX.Core
public override uint SecondsToStart { get => BitConverter.ToUInt32(General, AdventureInfo + 0x34); set => BitConverter.GetBytes(value).CopyTo(General, AdventureInfo + 0x34); }
public override uint SecondsToFame { get => BitConverter.ToUInt32(General, AdventureInfo + 0x3C); set => BitConverter.GetBytes(value).CopyTo(General, AdventureInfo + 0x3C); }
protected override PKM GetPKM(byte[] data) => new PK4(data);
protected override byte[] DecryptPKM(byte[] data) => PokeCrypto.DecryptArray45(data);
protected sealed override PKM GetPKM(byte[] data) => new PK4(data);
protected sealed override byte[] DecryptPKM(byte[] data) => PokeCrypto.DecryptArray45(data);
protected override void SetPKM(PKM pkm, bool isParty = false)
protected sealed override void SetPKM(PKM pkm, bool isParty = false)
{
var pk4 = (PK4)pkm;
// Apply to this Save File
@ -365,7 +359,7 @@ namespace PKHeX.Core
}
}
protected override bool[] MysteryGiftReceivedFlags
protected sealed override bool[] MysteryGiftReceivedFlags
{
get
{
@ -390,7 +384,7 @@ namespace PKHeX.Core
}
}
protected override DataMysteryGift[] MysteryGiftCards
protected sealed override DataMysteryGift[] MysteryGiftCards
{
get
{
@ -420,9 +414,9 @@ namespace PKHeX.Core
}
}
protected override void SetDex(PKM pkm) => Dex.SetDex(pkm);
public override bool GetCaught(int species) => Dex.GetCaught(species);
public override bool GetSeen(int species) => Dex.GetSeen(species);
protected sealed override void SetDex(PKM pkm) => Dex.SetDex(pkm);
public sealed override bool GetCaught(int species) => Dex.GetCaught(species);
public sealed override bool GetSeen(int species) => Dex.GetSeen(species);
public int DexUpgraded
{
@ -477,9 +471,9 @@ namespace PKHeX.Core
}
}
public override string GetString(byte[] data, int offset, int length) => StringConverter4.GetString4(data, offset, length);
public sealed override string GetString(byte[] data, int offset, int length) => StringConverter4.GetString4(data, offset, length);
public override byte[] SetString(string value, int maxLength, int PadToSize = 0, ushort PadWith = 0)
public sealed override byte[] SetString(string value, int maxLength, int PadToSize = 0, ushort PadWith = 0)
{
if (PadToSize == 0)
PadToSize = maxLength + 1;
@ -487,7 +481,7 @@ namespace PKHeX.Core
}
/// <summary> All Event Constant values for the savegame </summary>
public override ushort[] GetEventConsts()
public sealed override ushort[] GetEventConsts()
{
if (EventConstMax <= 0)
return Array.Empty<ushort>();
@ -499,7 +493,7 @@ namespace PKHeX.Core
}
/// <summary> All Event Constant values for the savegame </summary>
public override void SetEventConsts(ushort[] value)
public sealed override void SetEventConsts(ushort[] value)
{
if (EventConstMax <= 0)
return;

View file

@ -16,7 +16,7 @@ namespace PKHeX.Core
public override IReadOnlyList<string> PKMExtensions => PKM.Extensions.Where(f =>
{
int gen = f.Last() - 0x30;
int gen = f[^1] - 0x30;
return gen <= 7 && f[1] != 'b'; // ignore PB7
}).ToArray();

View file

@ -107,11 +107,12 @@ namespace PKHeX.Core
public override StorageSlotFlag GetSlotFlags(int index)
{
var val = StorageSlotFlag.None;
if (Blocks.Storage.PokeListInfo[6] == index)
var header = Blocks.Storage.PokeListInfo;
int position = Array.IndexOf(header, index, 0, 6);
if (position >= 0)
val = (StorageSlotFlag)((int)StorageSlotFlag.Party1 << position);
if (header[PokeListHeader.STARTER] == index)
val |= StorageSlotFlag.Starter;
int position = Array.IndexOf(Blocks.Storage.PokeListInfo, index);
if ((uint) position < 6)
val |= (StorageSlotFlag)((int)StorageSlotFlag.Party1 << position);
return val;
}

View file

@ -15,7 +15,7 @@ namespace PKHeX.Core
public override IReadOnlyList<string> PKMExtensions => PKM.Extensions.Where(f =>
{
int gen = f.Last() - 0x30;
int gen = f[^1] - 0x30;
return gen <= 8;
}).ToArray();

View file

@ -50,7 +50,7 @@ namespace PKHeX.Core
public virtual IReadOnlyList<string> PKMExtensions => PKM.Extensions.Where(f =>
{
int gen = f.Last() - 0x30;
int gen = f[^1] - 0x30;
return 3 <= gen && gen <= Generation;
}).ToArray();
@ -273,13 +273,27 @@ namespace PKHeX.Core
public virtual bool HasParty => Party > -1;
public abstract int GetPartyOffset(int slot);
public bool IsPartyAllEggs(params int[] except)
public bool IsPartyAllEggs(int except = -1)
{
if (!HasParty)
return false;
var party = PartyData;
return party.Count == party.Where(t => t.Species != 0).Where((t, i) => t.IsEgg || except.Contains(i)).Count();
for (int i = 0; i < MaxPartyCount; i++)
{
if (i == except)
continue;
if (IsPartySlotNotEggOrEmpty(i))
return false;
}
return true;
}
private bool IsPartySlotNotEggOrEmpty(int i)
{
var slot = GetPartySlotAtIndex(i);
return !slot.IsEgg && slot.Species != 0;
}
private const int MaxPartyCount = 6;
@ -567,6 +581,10 @@ namespace PKHeX.Core
#region Storage Health & Metadata
protected int[] TeamSlots = Array.Empty<int>();
/// <summary>
/// Slot indexes that are protected from overwriting.
/// </summary>
protected virtual IList<int>[] SlotPointers => new[] { TeamSlots };
public virtual StorageSlotFlag GetSlotFlags(int index) => StorageSlotFlag.None;
public StorageSlotFlag GetSlotFlags(int box, int slot) => GetSlotFlags((box * BoxSlotCount) + slot);
@ -595,16 +613,33 @@ namespace PKHeX.Core
private bool IsRegionOverwriteProtected(int min, int max)
{
return SlotPointers.SelectMany(z => z)
.Where(z => GetSlotFlags(z).IsOverwriteProtected())
.Any(slot => ArrayUtil.WithinRange(slot, min, max));
foreach (var arrays in SlotPointers)
{
foreach (int slotIndex in arrays)
{
if (!GetSlotFlags(slotIndex).IsOverwriteProtected())
continue;
if (ArrayUtil.WithinRange(slotIndex, min, max))
return true;
}
}
return false;
}
public bool IsAnySlotLockedInBox(int BoxStart, int BoxEnd)
{
return SlotPointers.SelectMany(z => z)
.Where(z => GetSlotFlags(z).HasFlagFast(StorageSlotFlag.Locked))
.Any(slot => ArrayUtil.WithinRange(slot, BoxStart * BoxSlotCount, (BoxEnd + 1) * BoxSlotCount));
foreach (var arrays in SlotPointers)
{
foreach (int slotIndex in arrays)
{
if (!GetSlotFlags(slotIndex).HasFlagFast(StorageSlotFlag.Locked))
continue;
if (ArrayUtil.WithinRange(slotIndex, BoxStart * BoxSlotCount, (BoxEnd + 1) * BoxSlotCount))
return true;
}
}
return false;
}
#endregion

View file

@ -9,6 +9,10 @@ namespace PKHeX.Core
public const int RecordCount = 200;
protected override IReadOnlyList<byte> RecordMax { get; }
// Structure:
// uint[100];
// ushort[100];
public RecordBlock6(SAV6XY sav, int offset) : base(sav)
{
Offset = offset;
@ -76,4 +80,4 @@ namespace PKHeX.Core
}
}
}
}
}

View file

@ -23,7 +23,5 @@
/* 18 @ 0x4E200, len = 0x69780 */ GoParkEntities,
/* 19 @ 0xB7A00, len = 0x000B0 */ GoGoParkNames,
/* 20 @ 0xB7C00, len = 0x00940 */ _20, // Go Park Names
Record,
}
}

View file

@ -21,7 +21,7 @@ namespace PKHeX.Core
/// </summary>
internal readonly int[] PokeListInfo;
private const int STARTER = 6;
public const int STARTER = 6;
private const int COUNT = 7;
private const int MAX_SLOTS = 1000;
private const int SLOT_EMPTY = 1001;

View file

@ -65,7 +65,7 @@ namespace PKHeX.WinForms
var prefix = StringInstruction.Prefixes;
string s = prefix[CB_Require.SelectedIndex] + CB_Property.Items[CB_Property.SelectedIndex].ToString() + StringInstruction.SplitInstruction;
if (RTB_Instructions.Lines.Length != 0 && RTB_Instructions.Lines.Last().Length > 0)
if (RTB_Instructions.Lines.Length != 0 && RTB_Instructions.Lines[^1].Length > 0)
s = Environment.NewLine + s;
RTB_Instructions.AppendText(s);

View file

@ -450,7 +450,7 @@ namespace PKHeX.WinForms
new[] { "Singles", "Doubles", "Multi (Trainer)", "Multi (Friend)", "Wi-Fi" },
};
BFN = new[] { "Tower", "Factory", "Hall", "Castle", "Arcade" };
if (SAV.DP) BFN = BFN.Take(1).ToArray();
if (SAV is SAV4DP) BFN = BFN.Take(1).ToArray();
StatNUDA = new[] { NUD_Stat0, NUD_Stat1, NUD_Stat2, NUD_Stat3 };
StatLabelA = new[] { L_Stat0, L_Stat1, L_Stat2, L_Stat3 };
StatRBA = new[] { RB_Stats3_01, RB_Stats3_02 };
@ -623,7 +623,7 @@ namespace PKHeX.WinForms
int addrVal = BFF[Facility][2] + (BFF[Facility][3] * BattleType) + (RBi << 3);
int addrFlag = BFF[Facility][4];
byte maskFlag = (byte)(1 << BattleType + (RBi << 2));
int TowerContinueCountOfs = SAV.DP ? 3 : 1;
int TowerContinueCountOfs = SAV is SAV4DP ? 3 : 1;
if (SetSavToVal)
{

View file

@ -1,7 +1,6 @@
using System;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
using PKHeX.Core;
using PKHeX.Drawing;
@ -76,7 +75,7 @@ namespace PKHeX.WinForms
break;
}
CLB_Phrases.Items.Clear();
CLB_Phrases.Items.Add(res.Last(), SAV.Festa.GetFestaPhraseUnlocked(106)); //add Lv100 before TentPhrases
CLB_Phrases.Items.Add(res[^1], SAV.Festa.GetFestaPhraseUnlocked(106)); //add Lv100 before TentPhrases
for (int i = 0; i < res.Length - 1; i++)
CLB_Phrases.Items.Add(res[i], SAV.Festa.GetFestaPhraseUnlocked(i));
@ -85,7 +84,7 @@ namespace PKHeX.WinForms
string[] res2 = { "Rank 4: missions","Rank 8: facility","Rank 10: fashion","Rank 20: rename","Rank 30: special menu","Rank 40: BGM","Rank 50: theme Glitz","Rank 60: theme Fairy","Rank 70: theme Tone","Rank 100: phrase","Current Rank", };
CLB_Reward.Items.Clear();
CLB_Reward.Items.Add(res2.Last(), (CheckState)RewardState[SAV.Festa.GetFestPrizeReceived(10)]); //add CurrentRank before const-rewards
CLB_Reward.Items.Add(res2[^1], (CheckState)RewardState[SAV.Festa.GetFestPrizeReceived(10)]); //add CurrentRank before const-rewards
for (int i = 0; i < res2.Length - 1; i++)
CLB_Reward.Items.Add(res2[i], (CheckState)RewardState[SAV.Festa.GetFestPrizeReceived(i)]);
@ -127,18 +126,24 @@ namespace PKHeX.WinForms
CB_FacilityType.Items.Clear();
for (int k = 0; k < RES_FacilityLevelType.Length - (SAV is SAV7USUM ? 0 : 1); k++) //Exchange is USUM only
{
for (int j = 0; j < RES_FacilityLevelType[k].Length; j++)
var arr = RES_FacilityLevelType[k];
for (int j = 0; j < arr.Length; j++)
{
if (RES_FacilityLevelType[k][j] != 4)
var x = res6[k];
var y = res7[k];
var name = $"{x} {y[j]}";
var count = arr[j];
if (count == 4)
{
for (int i = 0; i < RES_FacilityLevelType[k][j]; i++)
CB_FacilityType.Items.Add($"{res6[k]} {res7[k][j]} {i + 1}");
CB_FacilityType.Items.Add($"{name} 1");
CB_FacilityType.Items.Add($"{name} 3");
CB_FacilityType.Items.Add($"{name} 5");
}
else
{
CB_FacilityType.Items.Add($"{res6[k]} {res7[k][j]} 1");
CB_FacilityType.Items.Add($"{res6[k]} {res7[k][j]} 3");
CB_FacilityType.Items.Add($"{res6[k]} {res7[k][j]} 5");
for (int i = 0; i < count; i++)
CB_FacilityType.Items.Add($"{name} {i + 1}");
}
}
}
@ -171,7 +176,12 @@ namespace PKHeX.WinForms
private readonly byte[] RewardState = { 0, 2, 1 }; // CheckState.Indeterminate <-> CheckState.Checked
private readonly int typeMAX;
private readonly FestaFacility[] f = new FestaFacility[7];
private readonly string[] RES_Color = { "Red", "Blue", "Gold", "Black", "Purple", "Yellow", "Brown", "Green", "Orange", "NavyBlue", "Pink", "White" };
private readonly string[] RES_Color = Enum.GetNames(typeof(FestivalPlazaFacilityColor));
public enum FestivalPlazaFacilityColor : byte
{
Red, Blue, Gold, Black, Purple, Yellow, Brown, Green, Orange, NavyBlue, Pink, White
}
private readonly byte[][] RES_FacilityColor = //facility appearance
{