From 02014273b8a70f5d3ce0a9c9c0fd0debbc6b793c Mon Sep 17 00:00:00 2001 From: Kurt Date: Sat, 21 Sep 2019 10:31:59 -0700 Subject: [PATCH] Remove unused abstractions (slotview) --- .../Editing/Saves/SlotView/SlotArray.cs | 13 -- .../Editing/Saves/SlotView/SlotBoxes.cs | 13 -- .../Editing/Saves/SlotView/SlotDaycare.cs | 39 ---- PKHeX.Core/Editing/Saves/SlotView/SlotList.cs | 14 -- PKHeX.Core/Editing/Saves/SlotView/SlotView.cs | 175 ------------------ .../Editing/Saves/SlotView/SlotViewSet.cs | 85 --------- 6 files changed, 339 deletions(-) delete mode 100644 PKHeX.Core/Editing/Saves/SlotView/SlotArray.cs delete mode 100644 PKHeX.Core/Editing/Saves/SlotView/SlotBoxes.cs delete mode 100644 PKHeX.Core/Editing/Saves/SlotView/SlotDaycare.cs delete mode 100644 PKHeX.Core/Editing/Saves/SlotView/SlotList.cs delete mode 100644 PKHeX.Core/Editing/Saves/SlotView/SlotView.cs delete mode 100644 PKHeX.Core/Editing/Saves/SlotView/SlotViewSet.cs diff --git a/PKHeX.Core/Editing/Saves/SlotView/SlotArray.cs b/PKHeX.Core/Editing/Saves/SlotView/SlotArray.cs deleted file mode 100644 index 679c93e35..000000000 --- a/PKHeX.Core/Editing/Saves/SlotView/SlotArray.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System.Collections.Generic; - -namespace PKHeX.Core -{ - public class SlotArray : SlotList - { - public SlotArray(SaveFile sav, IReadOnlyList slots, bool isReadOnly) - : base(sav, slots, isReadOnly) { } - - protected override void ShiftDown(int startIndex) { } - protected override void ShiftUp(int startIndex) { } - } -} \ No newline at end of file diff --git a/PKHeX.Core/Editing/Saves/SlotView/SlotBoxes.cs b/PKHeX.Core/Editing/Saves/SlotView/SlotBoxes.cs deleted file mode 100644 index cba21347f..000000000 --- a/PKHeX.Core/Editing/Saves/SlotView/SlotBoxes.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace PKHeX.Core -{ - public class SlotBoxes : SlotView - { - public SlotBoxes(SaveFile sav) - : base(sav, sav.SlotCount, false) { } - - protected override int GetOffset(int index) => SAV.GetBoxSlotOffset(index); - - protected override void ShiftDown(int startIndex) { } - protected override void ShiftUp(int startIndex) { } - } -} \ No newline at end of file diff --git a/PKHeX.Core/Editing/Saves/SlotView/SlotDaycare.cs b/PKHeX.Core/Editing/Saves/SlotView/SlotDaycare.cs deleted file mode 100644 index bf6a0286f..000000000 --- a/PKHeX.Core/Editing/Saves/SlotView/SlotDaycare.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections.Generic; - -namespace PKHeX.Core -{ - public class SlotDaycare : SlotView - { - private readonly List Slots = new List(); - - public SlotDaycare(SaveFile sav, bool locked) - : base(sav, 2, locked) - { - ReloadDaycareSlots(); - } - - private void ReloadDaycareSlots() - { - var s1 = SAV.GetDaycareSlotOffset(CurrentDaycare, 0); - if (s1 < 0) - return; - Slots.Add(new SlotInfoMisc(0, s1) { Type = StorageSlotType.Daycare }); - var s2 = SAV.GetDaycareSlotOffset(CurrentDaycare, 1); - if (s2 < 0) - return; - Slots.Add(new SlotInfoMisc(1, s2) { Type = StorageSlotType.Daycare }); - Capacity = Slots.Count; - } - - public int CurrentDaycare { get; private set; } - public bool CanSwitch => SAV.HasTwoDaycares; - - public void SwitchCurrent() - { - CurrentDaycare ^= 1; - ReloadDaycareSlots(); - } - - protected override int GetOffset(int index) => Slots[index].Offset; - } -} \ No newline at end of file diff --git a/PKHeX.Core/Editing/Saves/SlotView/SlotList.cs b/PKHeX.Core/Editing/Saves/SlotView/SlotList.cs deleted file mode 100644 index 6fbec46fe..000000000 --- a/PKHeX.Core/Editing/Saves/SlotView/SlotList.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System.Collections.Generic; - -namespace PKHeX.Core -{ - public class SlotList : SlotView - { - private readonly IReadOnlyList Slots; - - public SlotList(SaveFile sav, IReadOnlyList slots, bool isReadOnly) - : base(sav, slots.Count, isReadOnly) => Slots = slots; - - protected override int GetOffset(int index) => Slots[index].Offset; - } -} \ No newline at end of file diff --git a/PKHeX.Core/Editing/Saves/SlotView/SlotView.cs b/PKHeX.Core/Editing/Saves/SlotView/SlotView.cs deleted file mode 100644 index 5d0c68c07..000000000 --- a/PKHeX.Core/Editing/Saves/SlotView/SlotView.cs +++ /dev/null @@ -1,175 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; - -namespace PKHeX.Core -{ - public abstract class SlotView : IList - { - public static readonly SlotArray Empty = new SlotArray(null, Array.Empty(), true); - - protected readonly SaveFile SAV; - public StorageSlotType Type { get; internal set; } - public bool IsParty { get; internal set; } - - public int Capacity { get; protected set; } - public bool IsReadOnly { get; } - - public bool Full => Count == Capacity; - public decimal PercentFull => Count / (decimal)Capacity; - public IEnumerable Indexes => Enumerable.Range(0, Capacity); - public IEnumerable Offsets => Indexes.Select(GetOffset); - public IEnumerable Stream => Indexes.Select(z => this[z]); - public PKM[] Items => Stream.ToArray(); - - protected abstract int GetOffset(int index); - - protected bool IsPKMPresent(int index) - { - int offset = GetOffset(index); - return SAV.IsPKMPresent(SAV.Data, offset); - } - - public int FirstEmptyIndex(int start = 0) - { - for (int i = start; i < Capacity; i++) - { - int offset = GetOffset(i); - if (!IsPKMPresent(offset)) - return i; - } - return -1; - } - - protected SlotView(SaveFile sav, int capacity, bool isReadOnly) - { - SAV = sav; - IsReadOnly = isReadOnly; - Capacity = capacity; - } - - #region IList Implementation - - public virtual int Count => Offsets.Count(z => SAV.IsPKMPresent(SAV.Data, z)); - public bool Contains(PKM item) => IndexOf(item) >= 0; - public IEnumerator GetEnumerator() => Stream.GetEnumerator(); - IEnumerator IEnumerable.GetEnumerator() => Stream.GetEnumerator(); - - public PKM this[int index] - { - get => GetIndex(index); - set => SetIndex(value, index); - } - - protected void SetIndex(PKM value, int index) - { - if (IsReadOnly) - throw new ArgumentException(nameof(IsReadOnly)); - - int offset = GetOffset(index); - - if (!IsParty) - { - SAV.SetStoredSlot(value, offset); - return; - } - - value.ForcePartyData(); - SAV.SetPartySlot(value, offset); - } - - private PKM GetIndex(int index) => GetFromOffset(GetOffset(index)); - private PKM GetFromOffset(int offset) => IsParty ? SAV.GetPartySlot(offset) : SAV.GetStoredSlot(offset); - - public void Insert(int index, PKM item) - { - int offset = GetOffset(index); - if (SAV.IsPKMPresent(SAV.Data, offset)) - ShiftDown(index); - this[index] = item; - } - - public void RemoveAt(int index) - { - int offset = GetOffset(index); - if (SAV.IsPKMPresent(SAV.Data, offset)) - this[index] = SAV.BlankPKM; - ShiftUp(index); - } - - /// - /// Adds the item to the first empty index. - /// - /// Item to add. - public void Add(PKM item) - { - var index = FirstEmptyIndex(); - if (index < 0) - throw new ArgumentException(nameof(item)); - this[index] = item; - } - - public void Clear() - { - var blank = SAV.BlankPKM; - if (blank == null) - throw new ArgumentException(nameof(blank)); - for (int i = 0; i < Capacity; i++) - this[i] = blank; - } - - public bool Remove(PKM item) - { - var index = IndexOf(item); - if (index < 0) - return false; - RemoveAt(index); - return true; - } - - public virtual void CopyTo(PKM[] array, int arrayIndex) - { - for (int i = arrayIndex, ctr = 0; i < Capacity; i++, ctr++) - array[i] = this[ctr]; - } - - public int IndexOf(PKM item) - { - int ctr = 0; - foreach (var obj in Stream) - { - if (obj.Equals(item)) - return ctr; - ctr++; - } - return -1; - } - - protected virtual void ShiftUp(int startIndex) - { - // something was deleted, move all slots up starting at index - var end = FirstEmptyIndex(startIndex + 1); - if (end < 0) - end = Capacity - 1; - - for (int i = startIndex; i < end; i++) - this[i] = this[i + 1]; - this[Capacity - 1] = SAV.BlankPKM; - } - - protected virtual void ShiftDown(int startIndex) - { - // something needs to be inserted, move all slots down starting at index - var end = FirstEmptyIndex(startIndex + 1); - if (end < 0) - end = Capacity - 1; - - for (int i = end; i >= startIndex; i--) - this[i + 1] = this[i]; - this[Capacity - 1] = SAV.BlankPKM; - } - - #endregion - } -} diff --git a/PKHeX.Core/Editing/Saves/SlotView/SlotViewSet.cs b/PKHeX.Core/Editing/Saves/SlotView/SlotViewSet.cs deleted file mode 100644 index 3afbd612b..000000000 --- a/PKHeX.Core/Editing/Saves/SlotView/SlotViewSet.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System.Collections.Generic; -using System.Linq; - -namespace PKHeX.Core -{ - public class SlotViewSet - { - public readonly SlotView Box = SlotView.Empty; - public readonly SlotView Party = SlotView.Empty; - public readonly SlotView BattleBox = SlotView.Empty; - public readonly SlotView Daycare = SlotView.Empty; - public readonly SlotView[] Other; - - public readonly SaveFile SAV; - - private const int StackMax = 10; - - public SlotViewSet(SaveFile sav, bool HaX = false) - { - SAV = sav; - - if (sav.HasBox) - Box = new SlotBoxes(sav) { Type = StorageSlotType.Box }; - - if (sav.HasParty) - Party = GetPartyList(sav); - - if (sav.HasBattleBox) - BattleBox = GetBattleBox(sav); - - if (sav.HasDaycare) - Daycare = new SlotDaycare(sav, !sav.BattleBoxLocked); - Other = GetOtherSlots(sav, HaX); - - SetChangeOutput(); - } - - private void SetChangeOutput() - { - } - - private static SlotView[] GetOtherSlots(SaveFile sav, bool HaX) - { - return sav.GetExtraSlots(HaX).GroupBy(z => z.Type) - .Select(z => new SlotArray(sav, z.ToArray(), true) { Type = z.Key }) - .Cast().ToArray(); - } - - private static SlotList GetBattleBox(SaveFile sav) - { - var party = Enumerable.Range(0, 6).Select(sav.GetBattleBoxOffset) - .Select(z => GetStorageDetails(z, false, StorageSlotType.BattleBox)) - .ToArray(); - return new SlotList(sav, party, !sav.BattleBoxLocked) { Type = StorageSlotType.BattleBox }; - } - - private static SlotList GetPartyList(SaveFile sav) - { - var party = Enumerable.Range(0, 6).Select(sav.GetPartyOffset) - .Select(z => GetStorageDetails(z, true, StorageSlotType.Party)) - .ToArray(); - return new SlotList(sav, party, false) { Type = StorageSlotType.Party }; - } - - private static SlotInfoMisc GetStorageDetails(int z, bool party, StorageSlotType type) - { - return new SlotInfoMisc(-1, z, party) {Type = type}; - } - - public IEnumerable Viewers - { - get - { - yield return Box; - yield return Party; - yield return BattleBox; - yield return Daycare; - foreach (var o in Other) - yield return o; - } - } - - public IEnumerable AllPKM => Viewers.SelectMany(z => z).Where(z => z.Species > 0 && z.ChecksumValid); - } -} \ No newline at end of file