diff options
Diffstat (limited to 'src/SMAPI/Framework/StateTracking/FieldWatchers')
3 files changed, 175 insertions, 4 deletions
diff --git a/src/SMAPI/Framework/StateTracking/FieldWatchers/NetListWatcher.cs b/src/SMAPI/Framework/StateTracking/FieldWatchers/NetListWatcher.cs new file mode 100644 index 00000000..0b4d3030 --- /dev/null +++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/NetListWatcher.cs @@ -0,0 +1,143 @@ +using System.Collections.Generic; +using Netcode; +using StardewModdingAPI.Framework.StateTracking.Comparers; + +namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers +{ + /// <summary>A watcher which detects changes to a net list field.</summary> + /// <typeparam name="TValue">The list value type.</typeparam> + internal class NetListWatcher<TValue> : BaseDisposableWatcher, ICollectionWatcher<TValue> + where TValue : class, INetObject<INetSerializable> + { + /********* + ** Fields + *********/ + /// <summary>The field being watched.</summary> + private readonly NetList<TValue, NetRef<TValue>> Field; + + /// <summary>The pairs added since the last reset.</summary> + private readonly ISet<TValue> AddedImpl = new HashSet<TValue>(new ObjectReferenceComparer<TValue>()); + + /// <summary>The pairs removed since the last reset.</summary> + private readonly ISet<TValue> RemovedImpl = new HashSet<TValue>(new ObjectReferenceComparer<TValue>()); + + + /********* + ** Accessors + *********/ + /// <summary>Whether the collection 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="field">The field to watch.</param> + public NetListWatcher(NetList<TValue, NetRef<TValue>> field) + { + this.Field = field; + field.OnElementChanged += this.OnElementChanged; + field.OnArrayReplaced += this.OnArrayReplaced; + } + + /// <summary>Set the current value as the baseline.</summary> + public void Reset() + { + this.AddedImpl.Clear(); + this.RemovedImpl.Clear(); + } + + /// <summary>Update the current value if needed.</summary> + public void Update() + { + this.AssertNotDisposed(); + } + + /// <summary>Stop watching the field and release all references.</summary> + public override void Dispose() + { + if (!this.IsDisposed) + { + this.Field.OnElementChanged -= this.OnElementChanged; + this.Field.OnArrayReplaced -= this.OnArrayReplaced; + } + + base.Dispose(); + } + + + /********* + ** Private methods + *********/ + /// <summary>A callback invoked when the value list is replaced.</summary> + /// <param name="list">The net field whose values changed.</param> + /// <param name="oldValues">The previous list of values.</param> + /// <param name="newValues">The new list of values.</param> + private void OnArrayReplaced(NetList<TValue, NetRef<TValue>> list, IList<TValue> oldValues, IList<TValue> newValues) + { + ISet<TValue> oldSet = new HashSet<TValue>(oldValues, new ObjectReferenceComparer<TValue>()); + ISet<TValue> changed = new HashSet<TValue>(newValues, new ObjectReferenceComparer<TValue>()); + + foreach (TValue value in oldSet) + { + if (!changed.Contains(value)) + this.Remove(value); + } + foreach (TValue value in changed) + { + if (!oldSet.Contains(value)) + this.Add(value); + } + } + + /// <summary>A callback invoked when an entry is replaced.</summary> + /// <param name="list">The net field whose values changed.</param> + /// <param name="index">The list index which changed.</param> + /// <param name="oldValue">The previous value.</param> + /// <param name="newValue">The new value.</param> + private void OnElementChanged(NetList<TValue, NetRef<TValue>> list, int index, TValue oldValue, TValue newValue) + { + this.Remove(oldValue); + this.Add(newValue); + } + + /// <summary>Track an added item.</summary> + /// <param name="value">The value that was added.</param> + private void Add(TValue value) + { + if (value == null) + return; + + if (this.RemovedImpl.Contains(value)) + { + this.AddedImpl.Remove(value); + this.RemovedImpl.Remove(value); + } + else + this.AddedImpl.Add(value); + } + + /// <summary>Track a removed item.</summary> + /// <param name="value">The value that was removed.</param> + private void Remove(TValue value) + { + if (value == null) + return; + + if (this.AddedImpl.Contains(value)) + { + this.AddedImpl.Remove(value); + this.RemovedImpl.Remove(value); + } + else + this.RemovedImpl.Add(value); + } + } +} diff --git a/src/SMAPI/Framework/StateTracking/FieldWatchers/ObservableCollectionWatcher.cs b/src/SMAPI/Framework/StateTracking/FieldWatchers/ObservableCollectionWatcher.cs index 883b1023..c29d2783 100644 --- a/src/SMAPI/Framework/StateTracking/FieldWatchers/ObservableCollectionWatcher.cs +++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/ObservableCollectionWatcher.cs @@ -21,6 +21,9 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers /// <summary>The pairs removed since the last reset.</summary> private readonly List<TValue> RemovedImpl = new List<TValue>(); + /// <summary>The previous values as of the last update.</summary> + private readonly List<TValue> PreviousValues = new List<TValue>(); + /********* ** Accessors @@ -78,10 +81,27 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers /// <param name="e">The event arguments.</param> private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { - if (e.NewItems != null) - this.AddedImpl.AddRange(e.NewItems.Cast<TValue>()); - if (e.OldItems != null) - this.RemovedImpl.AddRange(e.OldItems.Cast<TValue>()); + if (e.Action == NotifyCollectionChangedAction.Reset) + { + this.RemovedImpl.AddRange(this.PreviousValues); + this.PreviousValues.Clear(); + } + else + { + TValue[] added = e.NewItems?.Cast<TValue>().ToArray(); + TValue[] removed = e.OldItems?.Cast<TValue>().ToArray(); + + if (removed != null) + { + this.RemovedImpl.AddRange(removed); + this.PreviousValues.RemoveRange(e.OldStartingIndex, removed.Length); + } + if (added != null) + { + this.AddedImpl.AddRange(added); + this.PreviousValues.InsertRange(e.NewStartingIndex, added); + } + } } } } diff --git a/src/SMAPI/Framework/StateTracking/FieldWatchers/WatcherFactory.cs b/src/SMAPI/Framework/StateTracking/FieldWatchers/WatcherFactory.cs index 314ff7f5..bde43486 100644 --- a/src/SMAPI/Framework/StateTracking/FieldWatchers/WatcherFactory.cs +++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/WatcherFactory.cs @@ -82,6 +82,14 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers return new NetCollectionWatcher<T>(collection); } + /// <summary>Get a watcher for a net list.</summary> + /// <typeparam name="T">The value type.</typeparam> + /// <param name="collection">The net list.</param> + public static ICollectionWatcher<T> ForNetList<T>(NetList<T, NetRef<T>> collection) where T : class, INetObject<INetSerializable> + { + return new NetListWatcher<T>(collection); + } + /// <summary>Get a watcher for a net dictionary.</summary> /// <typeparam name="TKey">The dictionary key type.</typeparam> /// <typeparam name="TValue">The dictionary value type.</typeparam> |