From 4e87fd7ecaeff334255de3af3760b86dde95de3e Mon Sep 17 00:00:00 2001 From: Kurt Date: Mon, 4 Mar 2024 23:46:11 -0600 Subject: [PATCH] Misc fixes ty matt & foohyfooh --- .../ByGeneration/EncounterGenerator3.cs | 1 - .../ByGeneration/EncounterGenerator4.cs | 11 +++---- .../Saves/Access/SaveBlockAccessor5B2W2.cs | 1 + .../Saves/Access/SaveBlockAccessor5BW.cs | 1 + PKHeX.Core/Saves/Access/SaveBlockMetadata.cs | 6 ++-- .../Encryption/SwishCrypto/SCBlockCompare.cs | 6 ++-- .../Encryption/SwishCrypto/SCBlockMetadata.cs | 6 ++-- PKHeX.Core/Saves/SAV5.cs | 1 + PKHeX.Core/Saves/SAV5B2W2.cs | 1 + PKHeX.Core/Saves/SAV5BW.cs | 1 + PKHeX.Core/Saves/SAV7b.cs | 2 +- PKHeX.Core/Saves/SAV8LA.cs | 2 +- .../Saves/Substructures/Gen5/EventWork5.cs | 4 +-- .../Saves/Substructures/Gen5/PlayerData5.cs | 32 +++++-------------- .../Saves/Substructures/Gen6/EventWork6.cs | 2 +- .../Saves/Substructures/Gen7/LGPE/GP1.cs | 2 +- .../Saves/Substructures/Gen8/SWSH/Fused8.cs | 8 +++-- PKHeX.Core/Util/ReflectUtil.cs | 17 ++++++++-- .../PKM Editor/EntityInstructionBuilder.cs | 9 ++++-- .../Gen1/SAV_EventReset1.Designer.cs | 6 ++-- .../Save Editors/Gen6/SAV_PokedexORAS.cs | 2 +- .../Save Editors/SAV_SimpleTrainer.cs | 4 +-- 22 files changed, 64 insertions(+), 61 deletions(-) diff --git a/PKHeX.Core/Legality/Encounters/Generator/ByGeneration/EncounterGenerator3.cs b/PKHeX.Core/Legality/Encounters/Generator/ByGeneration/EncounterGenerator3.cs index 483c5932a..b565f4a7c 100644 --- a/PKHeX.Core/Legality/Encounters/Generator/ByGeneration/EncounterGenerator3.cs +++ b/PKHeX.Core/Legality/Encounters/Generator/ByGeneration/EncounterGenerator3.cs @@ -46,7 +46,6 @@ public IEnumerable GetEncounters(PKM pk, EvoCriteria[] chain, Le deferType ??= e; continue; } - if (e is not EncounterSlot3 slot) { yield return e; diff --git a/PKHeX.Core/Legality/Encounters/Generator/ByGeneration/EncounterGenerator4.cs b/PKHeX.Core/Legality/Encounters/Generator/ByGeneration/EncounterGenerator4.cs index 2fce4308d..dad2113f3 100644 --- a/PKHeX.Core/Legality/Encounters/Generator/ByGeneration/EncounterGenerator4.cs +++ b/PKHeX.Core/Legality/Encounters/Generator/ByGeneration/EncounterGenerator4.cs @@ -44,17 +44,16 @@ public IEnumerable GetEncounters(PKM pk, EvoCriteria[] chain, Le deferTile ??= e; continue; } - + if (!IsTypeCompatible(e, pk, info.PIDIV.Type)) + { + deferType ??= e; + continue; + } if (e is not EncounterSlot4 slot) { yield return e; continue; } - if (!IsTypeCompatible(e, pk, info.PIDIV.Type)) - { - deferSlot ??= slot; - continue; - } var evo = LeadFinder.GetLevelConstraint(pk, chain, slot, 4); var lead = LeadFinder.GetLeadInfo4(pk, slot, info.PIDIV, evo); diff --git a/PKHeX.Core/Saves/Access/SaveBlockAccessor5B2W2.cs b/PKHeX.Core/Saves/Access/SaveBlockAccessor5B2W2.cs index 4410f47f3..756b6fcd7 100644 --- a/PKHeX.Core/Saves/Access/SaveBlockAccessor5B2W2.cs +++ b/PKHeX.Core/Saves/Access/SaveBlockAccessor5B2W2.cs @@ -91,6 +91,7 @@ public sealed class SaveBlockAccessor5B2W2(SAV5B2W2 sav) public BoxLayout5 BoxLayout { get; } = new(sav, Block(sav, 0)); public MyItem5B2W2 Items { get; } = new(sav, Block(sav, 25)); public PlayerData5 PlayerData { get; } = new(sav, Block(sav, 27)); + public PlayerPosition5 PlayerPosition { get; } = new(sav, Block(sav, 28)); public UnityTower5 UnityTower { get; } = new(sav, Block(sav, 29)); public MysteryBlock5 Mystery { get; } = new(sav, Block(sav, 34)); public GlobalLink5 GlobalLink { get; } = new(sav, Block(sav, 35)); diff --git a/PKHeX.Core/Saves/Access/SaveBlockAccessor5BW.cs b/PKHeX.Core/Saves/Access/SaveBlockAccessor5BW.cs index 49a30fc18..60e88b325 100644 --- a/PKHeX.Core/Saves/Access/SaveBlockAccessor5BW.cs +++ b/PKHeX.Core/Saves/Access/SaveBlockAccessor5BW.cs @@ -86,6 +86,7 @@ public sealed class SaveBlockAccessor5BW(SAV5BW sav) : ISaveBlockAccessorType of accessor public sealed class SaveBlockMetadata { - private readonly Dictionary BlockList; + private readonly Dictionary BlockList; public SaveBlockMetadata(ISaveBlockAccessor accessor) { @@ -19,8 +19,8 @@ public SaveBlockMetadata(ISaveBlockAccessor accessor) public IEnumerable GetSortedBlockList() { - return BlockList.Select(z => z.Value).OrderBy(z => z); + return BlockList.Select(z => z.Key).OrderBy(z => z); } - public IDataIndirect GetBlock(string name) => BlockList.First(z => z.Value == name).Key; + public IDataIndirect GetBlock(string name) => BlockList.First(z => z.Key == name).Value; } diff --git a/PKHeX.Core/Saves/Encryption/SwishCrypto/SCBlockCompare.cs b/PKHeX.Core/Saves/Encryption/SwishCrypto/SCBlockCompare.cs index 93b4408a5..72103db51 100644 --- a/PKHeX.Core/Saves/Encryption/SwishCrypto/SCBlockCompare.cs +++ b/PKHeX.Core/Saves/Encryption/SwishCrypto/SCBlockCompare.cs @@ -101,12 +101,12 @@ private void LoadChanged(SCBlockAccessor s1, SCBlockAccessor s2, IEnumerable list, IEnumerable blocks) + void ReplaceLabels(Dictionary list, IEnumerable blocks) { foreach (var b in blocks) { - var match = list.FirstOrDefault(z => z.Key.Equals(b.Data)); - if (match.Value is not { } x) + var match = list.FirstOrDefault(z => z.Value.Equals(b.Data)); + if (match.Key is not { } x) continue; ref var exist = ref CollectionsMarshal.GetValueRefOrNullRef(names, b.Key); if (!Unsafe.IsNullRef(ref exist)) diff --git a/PKHeX.Core/Saves/Encryption/SwishCrypto/SCBlockMetadata.cs b/PKHeX.Core/Saves/Encryption/SwishCrypto/SCBlockMetadata.cs index 39feba4d6..779d8b66e 100644 --- a/PKHeX.Core/Saves/Encryption/SwishCrypto/SCBlockMetadata.cs +++ b/PKHeX.Core/Saves/Encryption/SwishCrypto/SCBlockMetadata.cs @@ -11,7 +11,7 @@ namespace PKHeX.Core; /// public sealed class SCBlockMetadata { - private readonly Dictionary BlockList; + private readonly Dictionary BlockList; private readonly Dictionary ValueList; private readonly SCBlockAccessor Accessor; @@ -98,8 +98,8 @@ private string GetBlockHint(SCBlock z, int index) var obj = BlockList.FirstOrDefault(z => z.Key.Equals(block.Data)); if (obj is not (null, null)) { - saveBlock = obj.Key; - return obj.Value; + saveBlock = obj.Value; + return obj.Key; } } diff --git a/PKHeX.Core/Saves/SAV5.cs b/PKHeX.Core/Saves/SAV5.cs index 7efe91d7a..f05d5628a 100644 --- a/PKHeX.Core/Saves/SAV5.cs +++ b/PKHeX.Core/Saves/SAV5.cs @@ -178,6 +178,7 @@ public byte[] CGearSkinData public abstract Daycare5 Daycare { get; } public abstract BoxLayout5 BoxLayout { get; } public abstract PlayerData5 PlayerData { get; } + public abstract PlayerPosition5 PlayerPosition { get; } public abstract BattleSubway5 BattleSubway { get; } public abstract Entralink5 Entralink { get; } public abstract Musical5 Musical { get; } diff --git a/PKHeX.Core/Saves/SAV5B2W2.cs b/PKHeX.Core/Saves/SAV5B2W2.cs index 9ec81715c..4483f8996 100644 --- a/PKHeX.Core/Saves/SAV5B2W2.cs +++ b/PKHeX.Core/Saves/SAV5B2W2.cs @@ -43,6 +43,7 @@ private void Initialize() public override Daycare5 Daycare => Blocks.Daycare; public override BoxLayout5 BoxLayout => Blocks.BoxLayout; public override PlayerData5 PlayerData => Blocks.PlayerData; + public override PlayerPosition5 PlayerPosition => Blocks.PlayerPosition; public override BattleSubway5 BattleSubway => Blocks.BattleSubway; public override Entralink5 Entralink => Blocks.Entralink; public override Musical5 Musical => Blocks.Musical; diff --git a/PKHeX.Core/Saves/SAV5BW.cs b/PKHeX.Core/Saves/SAV5BW.cs index b4fcd1f49..30a71f0ed 100644 --- a/PKHeX.Core/Saves/SAV5BW.cs +++ b/PKHeX.Core/Saves/SAV5BW.cs @@ -42,6 +42,7 @@ private void Initialize() public override Daycare5 Daycare => Blocks.Daycare; public override BoxLayout5 BoxLayout => Blocks.BoxLayout; public override PlayerData5 PlayerData => Blocks.PlayerData; + public override PlayerPosition5 PlayerPosition => Blocks.PlayerPosition; public override BattleSubway5 BattleSubway => Blocks.BattleSubway; public override Entralink5BW Entralink => Blocks.Entralink; public override Musical5 Musical => Blocks.Musical; diff --git a/PKHeX.Core/Saves/SAV7b.cs b/PKHeX.Core/Saves/SAV7b.cs index ab5cba001..a51c2bcd9 100644 --- a/PKHeX.Core/Saves/SAV7b.cs +++ b/PKHeX.Core/Saves/SAV7b.cs @@ -14,7 +14,7 @@ public sealed class SAV7b : SAV_BEEF, ISaveBlock7b, IGameSync, IMysteryGiftStora public override Type PKMType => typeof(PB7); public override PB7 BlankPKM => new(); - protected override int SIZE_STORED => PokeCrypto.SIZE_6PARTY; + protected override int SIZE_STORED => PokeCrypto.SIZE_6STORED; protected override int SIZE_PARTY => PokeCrypto.SIZE_6PARTY; public override int SIZE_BOXSLOT => PokeCrypto.SIZE_6PARTY; public override byte[] GetDataForBox(PKM pk) => pk.EncryptedPartyData; diff --git a/PKHeX.Core/Saves/SAV8LA.cs b/PKHeX.Core/Saves/SAV8LA.cs index e963be5fa..fd6f753a5 100644 --- a/PKHeX.Core/Saves/SAV8LA.cs +++ b/PKHeX.Core/Saves/SAV8LA.cs @@ -6,7 +6,7 @@ namespace PKHeX.Core; /// /// Generation 8 object for games. /// -public sealed class SAV8LA : SaveFile, ISaveBlock8LA, ISCBlockArray, ISaveFileRevision, IBoxDetailName +public sealed class SAV8LA : SaveFile, ISaveBlock8LA, ISCBlockArray, ISaveFileRevision, IBoxDetailName, IBoxDetailWallpaper { protected internal override string ShortSummary => $"{OT} ({Version}) - {LastSaved.LastSavedTime}"; public override string Extension => string.Empty; diff --git a/PKHeX.Core/Saves/Substructures/Gen5/EventWork5.cs b/PKHeX.Core/Saves/Substructures/Gen5/EventWork5.cs index 6d697c152..e2cfd1c35 100644 --- a/PKHeX.Core/Saves/Substructures/Gen5/EventWork5.cs +++ b/PKHeX.Core/Saves/Substructures/Gen5/EventWork5.cs @@ -20,7 +20,7 @@ public abstract class EventWork5(SAV5 sav, Memory raw) : SaveBlock(s public sealed class EventWork5BW(SAV5BW sav, Memory raw) : EventWork5(sav, raw) { public const int OffsetEventWork = 0; - public const int OffsetEventFlag = OffsetEventWork + (CountEventFlag * sizeof(ushort)); + public const int OffsetEventFlag = OffsetEventWork + (CountEventWork * sizeof(ushort)); public const int CountEventWork = 0x13E; public const int CountEventFlag = 0xB60; @@ -39,7 +39,7 @@ public sealed class EventWork5BW(SAV5BW sav, Memory raw) : EventWork5(sav, public sealed class EventWork5B2W2(SAV5B2W2 sav, Memory raw) : EventWork5(sav, raw) { public const int OffsetEventWork = 0; - public const int OffsetEventFlag = OffsetEventWork + (CountEventFlag * sizeof(ushort)); + public const int OffsetEventFlag = OffsetEventWork + (CountEventWork * sizeof(ushort)); public const int CountEventWork = 0x1AF; public const int CountEventFlag = 0xBF8; diff --git a/PKHeX.Core/Saves/Substructures/Gen5/PlayerData5.cs b/PKHeX.Core/Saves/Substructures/Gen5/PlayerData5.cs index 820d9ffa6..11f8c9f3c 100644 --- a/PKHeX.Core/Saves/Substructures/Gen5/PlayerData5.cs +++ b/PKHeX.Core/Saves/Substructures/Gen5/PlayerData5.cs @@ -83,28 +83,12 @@ public int PlayedSeconds get => Data[0x24 + 3]; set => Data[0x24 + 3] = (byte)value; } - - public int M - { - get => ReadInt32LittleEndian(Data[0x180..]); - set => WriteUInt16LittleEndian(Data[0x180..], (ushort)value); - } - - public int X - { - get => ReadUInt16LittleEndian(Data[0x186..]); - set => WriteUInt16LittleEndian(Data[0x186..], (ushort)value); - } - - public int Z - { - get => ReadUInt16LittleEndian(Data[0x18A..]); - set => WriteUInt16LittleEndian(Data[0x18A..], (ushort)value); - } - - public int Y - { - get => ReadUInt16LittleEndian(Data[0x18E..]); - set => WriteUInt16LittleEndian(Data[0x18E..], (ushort)value); - } +} + +public sealed class PlayerPosition5(SAV5 sav, Memory raw) : SaveBlock(sav, raw) +{ + public int M { get => ReadInt32LittleEndian(Data[0x80..]); set => WriteUInt16LittleEndian(Data[0x80..], (ushort)value); } + public int X { get => ReadUInt16LittleEndian(Data[0x86..]); set => WriteUInt16LittleEndian(Data[0x86..], (ushort)value); } + public int Z { get => ReadUInt16LittleEndian(Data[0x8A..]); set => WriteUInt16LittleEndian(Data[0x8A..], (ushort)value); } + public int Y { get => ReadUInt16LittleEndian(Data[0x8E..]); set => WriteUInt16LittleEndian(Data[0x8E..], (ushort)value); } } diff --git a/PKHeX.Core/Saves/Substructures/Gen6/EventWork6.cs b/PKHeX.Core/Saves/Substructures/Gen6/EventWork6.cs index e610541ad..a7336219c 100644 --- a/PKHeX.Core/Saves/Substructures/Gen6/EventWork6.cs +++ b/PKHeX.Core/Saves/Substructures/Gen6/EventWork6.cs @@ -6,7 +6,7 @@ namespace PKHeX.Core; public sealed class EventWork6(SAV6 sav, Memory raw) : SaveBlock(sav, raw), IEventFlag37 { public const int OffsetEventWork = 0; - public const int OffsetEventFlag = OffsetEventWork + (CountEventFlag * sizeof(ushort)); + public const int OffsetEventFlag = OffsetEventWork + (CountEventWork * sizeof(ushort)); public const int CountEventWork = 0x178; public const int CountEventFlag = 0xD00; // 3328 diff --git a/PKHeX.Core/Saves/Substructures/Gen7/LGPE/GP1.cs b/PKHeX.Core/Saves/Substructures/Gen7/LGPE/GP1.cs index a40dd2458..db3b49c79 100644 --- a/PKHeX.Core/Saves/Substructures/Gen7/LGPE/GP1.cs +++ b/PKHeX.Core/Saves/Substructures/Gen7/LGPE/GP1.cs @@ -125,7 +125,7 @@ public string FileNameWithoutExtension { string form = Form > 0 ? $"-{Form:00}" : string.Empty; string star = IsShiny ? " ★" : string.Empty; - return $"{Species:000}{form}{star} - {NickStr} - Lv. {Level:00} - {IV_HP:00}.{IV_ATK:00}.{IV_DEF:00} - CP {CP:0000} (Moves {Move1:000}, {Move2:000})"; + return $"{Species:0000}{form}{star} - {NickStr} - Lv. {Level:00} - {IV_HP:00}.{IV_ATK:00}.{IV_DEF:00} - CP {CP:0000} (Moves {Move1:000}, {Move2:000})"; } } diff --git a/PKHeX.Core/Saves/Substructures/Gen8/SWSH/Fused8.cs b/PKHeX.Core/Saves/Substructures/Gen8/SWSH/Fused8.cs index 2aea938cc..9cb71ec43 100644 --- a/PKHeX.Core/Saves/Substructures/Gen8/SWSH/Fused8.cs +++ b/PKHeX.Core/Saves/Substructures/Gen8/SWSH/Fused8.cs @@ -7,15 +7,17 @@ namespace PKHeX.Core; /// public sealed class Fused8(SAV8SWSH sav, SCBlock block) : SaveBlock(sav, block.Data) { + private const int SizeStored = PokeCrypto.SIZE_8PARTY; + public static int GetFusedSlotOffset(int slot) { if ((uint)slot >= 3) return -1; - return PokeCrypto.SIZE_8PARTY * slot; + return SizeStored * slot; } - public Memory this[int i] => Raw.Slice(GetFusedSlotOffset(i), PokeCrypto.SIZE_8STORED); - private Span GetSlotSpan(int index) => Data.Slice(GetFusedSlotOffset(index), PokeCrypto.SIZE_8STORED); + public Memory this[int i] => Raw.Slice(GetFusedSlotOffset(i), SizeStored); + private Span GetSlotSpan(int index) => Data.Slice(GetFusedSlotOffset(index), SizeStored); private PK8 GetStoredSlot(int index) => (PK8)SAV.GetStoredSlot(GetSlotSpan(index)); private void SetStoredSlot(PK8 pk, int index) => pk.EncryptedBoxData.CopyTo(GetSlotSpan(index)); diff --git a/PKHeX.Core/Util/ReflectUtil.cs b/PKHeX.Core/Util/ReflectUtil.cs index af457b1ee..a6c97a17e 100644 --- a/PKHeX.Core/Util/ReflectUtil.cs +++ b/PKHeX.Core/Util/ReflectUtil.cs @@ -193,10 +193,21 @@ private static IEnumerable GetAll(this TypeInfo typeInfo, Func (T)(z.GetRawConstantValue() ?? throw new NullReferenceException(nameof(z.Name))), z => z.Name); } - public static Dictionary GetAllPropertiesOfType(this Type type, object obj) where T : class + public static Dictionary GetAllPropertiesOfType(this Type type, object obj) where T : class { var props = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly); - var ofType = props.Where(fi => typeof(T).IsAssignableFrom(fi.PropertyType)); - return ofType.ToDictionary(x => (T)(x.GetValue(obj) ?? throw new NullReferenceException(nameof(x.Name))), z => z.Name); + var result = new Dictionary(props.Length); + foreach (var pi in props) + { + if (!typeof(T).IsAssignableFrom(pi.PropertyType)) + continue; + + var name = pi.Name; + var value = pi.GetValue(obj); + if (value is not T t) + continue; + result.TryAdd(name, t); + } + return result; } } diff --git a/PKHeX.WinForms/Controls/PKM Editor/EntityInstructionBuilder.cs b/PKHeX.WinForms/Controls/PKM Editor/EntityInstructionBuilder.cs index 43f5407ed..c1e621269 100644 --- a/PKHeX.WinForms/Controls/PKM Editor/EntityInstructionBuilder.cs +++ b/PKHeX.WinForms/Controls/PKM Editor/EntityInstructionBuilder.cs @@ -67,10 +67,13 @@ private void CB_Property_SelectedIndexChanged(object sender, EventArgs e) private static bool GetPropertyDisplayText(PropertyInfo pi, PKM pk, out string display) { var type = pi.PropertyType; - if (type.IsGenericType && typeof(Span<>) == type.GetGenericTypeDefinition()) + if (type.IsGenericType) { - display = pi.PropertyType.ToString(); - return false; + if (type.GetGenericTypeDefinition().IsByRefLike) // Span, ReadOnlySpan + { + display = pi.PropertyType.ToString(); + return false; + } } var value = pi.GetValue(pk); diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen1/SAV_EventReset1.Designer.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen1/SAV_EventReset1.Designer.cs index 9c0e350ec..0ad5b2157 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen1/SAV_EventReset1.Designer.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen1/SAV_EventReset1.Designer.cs @@ -45,13 +45,13 @@ private void InitializeComponent() AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit; ClientSize = new System.Drawing.Size(284, 261); Controls.Add(FLP_List); - Icon = global::PKHeX.WinForms.Properties.Resources.Icon; + Icon = Properties.Resources.Icon; MaximizeBox = false; Name = "SAV_EventReset1"; + StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; Text = "Event Resetter"; - FormClosing += new System.Windows.Forms.FormClosingEventHandler(SAV_EventReset1_FormClosing); + FormClosing += SAV_EventReset1_FormClosing; ResumeLayout(false); - } #endregion diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_PokedexORAS.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_PokedexORAS.cs index 1def92c8c..de8a400ec 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_PokedexORAS.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_PokedexORAS.cs @@ -155,7 +155,7 @@ private void GetEntry() private void SetEntry() { - if (species == 0) + if ((short)species <= 0) return; Zukan.SetCaught(species, CP[0].Checked); diff --git a/PKHeX.WinForms/Subforms/Save Editors/SAV_SimpleTrainer.cs b/PKHeX.WinForms/Subforms/Save Editors/SAV_SimpleTrainer.cs index f04164b51..43b23efc8 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/SAV_SimpleTrainer.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/SAV_SimpleTrainer.cs @@ -146,7 +146,7 @@ public SAV_SimpleTrainer(SaveFile sav) L_Coins.Text = "BP"; // no translation boo MT_Coins.Text = s.BattleSubway.BP.ToString(); - var pd = s.PlayerData; + var pd = s.PlayerPosition; NUD_M.Value = pd.M; NUD_X.Value = pd.X; NUD_Z.Value = pd.Z; @@ -261,7 +261,7 @@ private void B_Save_Click(object sender, EventArgs e) { if (MapUpdated) { - var pd = s.PlayerData; + var pd = s.PlayerPosition; pd.M = (int)NUD_M.Value; pd.X = (int)NUD_X.Value; pd.Z = (int)NUD_Z.Value;