diff options
Diffstat (limited to 'src/SMAPI/Framework/StateTracking/FieldWatchers')
7 files changed, 111 insertions, 15 deletions
diff --git a/src/SMAPI/Framework/StateTracking/FieldWatchers/ComparableListWatcher.cs b/src/SMAPI/Framework/StateTracking/FieldWatchers/ComparableListWatcher.cs new file mode 100644 index 00000000..2ea6609a --- /dev/null +++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/ComparableListWatcher.cs @@ -0,0 +1,83 @@ +using System.Collections.Generic; +using System.Linq; + +namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers +{ + /// <summary>A watcher which detects changes to a collection of values using a specified <see cref="IEqualityComparer{T}"/> instance.</summary> + /// <typeparam name="TValue">The value type within the collection.</typeparam> + internal class ComparableListWatcher<TValue> : BaseDisposableWatcher, ICollectionWatcher<TValue> + { + /********* + ** Properties + *********/ + /// <summary>The collection to watch.</summary> + private readonly ICollection<TValue> CurrentValues; + + /// <summary>The values during the previous update.</summary> + private HashSet<TValue> LastValues; + + /// <summary>The pairs added since the last reset.</summary> + private readonly List<TValue> AddedImpl = new List<TValue>(); + + /// <summary>The pairs removed since the last reset.</summary> + private readonly List<TValue> RemovedImpl = new List<TValue>(); + + + /********* + ** Accessors + *********/ + /// <summary>Whether the value changed since the last reset.</summary> + public bool IsChanged => this.AddedImpl.Count > 0 || this.RemovedImpl.Count > 0; + + /// <summary>The values added since the last reset.</summary> + public IEnumerable<TValue> Added => this.AddedImpl; + + /// <summary>The values removed since the last reset.</summary> + public IEnumerable<TValue> Removed => this.RemovedImpl; + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="values">The collection to watch.</param> + /// <param name="comparer">The equality comparer which indicates whether two values are the same.</param> + public ComparableListWatcher(ICollection<TValue> values, IEqualityComparer<TValue> comparer) + { + this.CurrentValues = values; + this.LastValues = new HashSet<TValue>(comparer); + } + + /// <summary>Update the current value if needed.</summary> + public void Update() + { + this.AssertNotDisposed(); + + // optimise for zero items + if (this.CurrentValues.Count == 0) + { + if (this.LastValues.Count > 0) + { + this.AddedImpl.AddRange(this.LastValues); + this.LastValues.Clear(); + } + return; + } + + // detect changes + HashSet<TValue> curValues = new HashSet<TValue>(this.CurrentValues, this.LastValues.Comparer); + this.RemovedImpl.AddRange(from value in this.LastValues where !curValues.Contains(value) select value); + this.AddedImpl.AddRange(from value in curValues where !this.LastValues.Contains(value) select value); + this.LastValues = curValues; + } + + /// <summary>Set the current value as the baseline.</summary> + public void Reset() + { + this.AssertNotDisposed(); + + this.AddedImpl.Clear(); + this.RemovedImpl.Clear(); + } + } +} diff --git a/src/SMAPI/Framework/StateTracking/FieldWatchers/ComparableWatcher.cs b/src/SMAPI/Framework/StateTracking/FieldWatchers/ComparableWatcher.cs index d51fc2ac..dda30a15 100644 --- a/src/SMAPI/Framework/StateTracking/FieldWatchers/ComparableWatcher.cs +++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/ComparableWatcher.cs @@ -4,26 +4,27 @@ using System.Collections.Generic; namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers { /// <summary>A watcher which detects changes to a value using a specified <see cref="IEqualityComparer{T}"/> instance.</summary> - internal class ComparableWatcher<T> : IValueWatcher<T> + /// <typeparam name="TValue">The comparable value type.</typeparam> + internal class ComparableWatcher<TValue> : IValueWatcher<TValue> { /********* ** Properties *********/ /// <summary>Get the current value.</summary> - private readonly Func<T> GetValue; + private readonly Func<TValue> GetValue; /// <summary>The equality comparer.</summary> - private readonly IEqualityComparer<T> Comparer; + private readonly IEqualityComparer<TValue> Comparer; /********* ** Accessors *********/ /// <summary>The field value at the last reset.</summary> - public T PreviousValue { get; private set; } + public TValue PreviousValue { get; private set; } /// <summary>The latest value.</summary> - public T CurrentValue { get; private set; } + public TValue CurrentValue { get; private set; } /// <summary>Whether the value changed since the last reset.</summary> public bool IsChanged { get; private set; } @@ -35,7 +36,7 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers /// <summary>Construct an instance.</summary> /// <param name="getValue">Get the current value.</param> /// <param name="comparer">The equality comparer which indicates whether two values are the same.</param> - public ComparableWatcher(Func<T> getValue, IEqualityComparer<T> comparer) + public ComparableWatcher(Func<TValue> getValue, IEqualityComparer<TValue> comparer) { this.GetValue = getValue; this.Comparer = comparer; diff --git a/src/SMAPI/Framework/StateTracking/FieldWatchers/NetCollectionWatcher.cs b/src/SMAPI/Framework/StateTracking/FieldWatchers/NetCollectionWatcher.cs index 8a841a79..d3022a69 100644 --- a/src/SMAPI/Framework/StateTracking/FieldWatchers/NetCollectionWatcher.cs +++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/NetCollectionWatcher.cs @@ -4,6 +4,7 @@ using Netcode; namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers { /// <summary>A watcher which detects changes to a Netcode collection.</summary> + /// <typeparam name="TValue">The value type within the collection.</typeparam> internal class NetCollectionWatcher<TValue> : BaseDisposableWatcher, ICollectionWatcher<TValue> where TValue : class, INetObject<INetSerializable> { @@ -16,7 +17,7 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers /// <summary>The pairs added since the last reset.</summary> private readonly List<TValue> AddedImpl = new List<TValue>(); - /// <summary>The pairs demoved since the last reset.</summary> + /// <summary>The pairs removed since the last reset.</summary> private readonly List<TValue> RemovedImpl = new List<TValue>(); diff --git a/src/SMAPI/Framework/StateTracking/FieldWatchers/NetDictionaryWatcher.cs b/src/SMAPI/Framework/StateTracking/FieldWatchers/NetDictionaryWatcher.cs index 7a2bf84e..7a7ab89d 100644 --- a/src/SMAPI/Framework/StateTracking/FieldWatchers/NetDictionaryWatcher.cs +++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/NetDictionaryWatcher.cs @@ -20,7 +20,7 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers /// <summary>The pairs added since the last reset.</summary> private readonly IDictionary<TKey, TValue> PairsAdded = new Dictionary<TKey, TValue>(); - /// <summary>The pairs demoved since the last reset.</summary> + /// <summary>The pairs removed since the last reset.</summary> private readonly IDictionary<TKey, TValue> PairsRemoved = new Dictionary<TKey, TValue>(); /// <summary>The field being watched.</summary> diff --git a/src/SMAPI/Framework/StateTracking/FieldWatchers/NetValueWatcher.cs b/src/SMAPI/Framework/StateTracking/FieldWatchers/NetValueWatcher.cs index 188ed9f3..85099988 100644 --- a/src/SMAPI/Framework/StateTracking/FieldWatchers/NetValueWatcher.cs +++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/NetValueWatcher.cs @@ -3,13 +3,15 @@ using Netcode; namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers { /// <summary>A watcher which detects changes to a net value field.</summary> - internal class NetValueWatcher<T, TSelf> : BaseDisposableWatcher, IValueWatcher<T> where TSelf : NetFieldBase<T, TSelf> + /// <typeparam name="TValue">The value type wrapped by the net field.</typeparam> + /// <typeparam name="TNetField">The net field type.</typeparam> + internal class NetValueWatcher<TValue, TNetField> : BaseDisposableWatcher, IValueWatcher<TValue> where TNetField : NetFieldBase<TValue, TNetField> { /********* ** Properties *********/ /// <summary>The field being watched.</summary> - private readonly NetFieldBase<T, TSelf> Field; + private readonly NetFieldBase<TValue, TNetField> Field; /********* @@ -19,10 +21,10 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers public bool IsChanged { get; private set; } /// <summary>The field value at the last reset.</summary> - public T PreviousValue { get; private set; } + public TValue PreviousValue { get; private set; } /// <summary>The latest value.</summary> - public T CurrentValue { get; private set; } + public TValue CurrentValue { get; private set; } /********* @@ -30,7 +32,7 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers *********/ /// <summary>Construct an instance.</summary> /// <param name="field">The field to watch.</param> - public NetValueWatcher(NetFieldBase<T, TSelf> field) + public NetValueWatcher(NetFieldBase<TValue, TNetField> field) { this.Field = field; this.PreviousValue = field.Value; @@ -74,7 +76,7 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers /// <param name="field">The field being watched.</param> /// <param name="oldValue">The old field value.</param> /// <param name="newValue">The new field value.</param> - private void OnValueChanged(TSelf field, T oldValue, T newValue) + private void OnValueChanged(TNetField field, TValue oldValue, TValue newValue) { this.CurrentValue = newValue; this.IsChanged = true; diff --git a/src/SMAPI/Framework/StateTracking/FieldWatchers/ObservableCollectionWatcher.cs b/src/SMAPI/Framework/StateTracking/FieldWatchers/ObservableCollectionWatcher.cs index 34a97097..0c65789f 100644 --- a/src/SMAPI/Framework/StateTracking/FieldWatchers/ObservableCollectionWatcher.cs +++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/ObservableCollectionWatcher.cs @@ -6,6 +6,7 @@ using System.Linq; namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers { /// <summary>A watcher which detects changes to an observable collection.</summary> + /// <typeparam name="TValue">The value type within the collection.</typeparam> internal class ObservableCollectionWatcher<TValue> : BaseDisposableWatcher, ICollectionWatcher<TValue> { /********* @@ -17,7 +18,7 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers /// <summary>The pairs added since the last reset.</summary> private readonly List<TValue> AddedImpl = new List<TValue>(); - /// <summary>The pairs demoved since the last reset.</summary> + /// <summary>The pairs removed since the last reset.</summary> private readonly List<TValue> RemovedImpl = new List<TValue>(); diff --git a/src/SMAPI/Framework/StateTracking/FieldWatchers/WatcherFactory.cs b/src/SMAPI/Framework/StateTracking/FieldWatchers/WatcherFactory.cs index ab4ab0d5..8301351e 100644 --- a/src/SMAPI/Framework/StateTracking/FieldWatchers/WatcherFactory.cs +++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/WatcherFactory.cs @@ -36,6 +36,14 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers return new ComparableWatcher<T>(getValue, new ObjectReferenceComparer<T>()); } + /// <summary>Get a watcher which detects when an object reference in a collection changes.</summary> + /// <typeparam name="T">The value type.</typeparam> + /// <param name="collection">The observable collection.</param> + public static ComparableListWatcher<T> ForReferenceList<T>(ICollection<T> collection) + { + return new ComparableListWatcher<T>(collection, new ObjectReferenceComparer<T>()); + } + /// <summary>Get a watcher for an observable collection.</summary> /// <typeparam name="T">The value type.</typeparam> /// <param name="collection">The observable collection.</param> |