From dad67e213e68eb85c534d7c1c4035dfde90ff822 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 4 Dec 2018 23:16:13 -0500 Subject: fix world events in the mines (#603) --- .../FieldWatchers/ComparableListWatcher.cs | 82 ++++++++++++++++++++++ .../StateTracking/FieldWatchers/WatcherFactory.cs | 8 +++ 2 files changed, 90 insertions(+) create mode 100644 src/SMAPI/Framework/StateTracking/FieldWatchers/ComparableListWatcher.cs (limited to 'src/SMAPI/Framework/StateTracking/FieldWatchers') diff --git a/src/SMAPI/Framework/StateTracking/FieldWatchers/ComparableListWatcher.cs b/src/SMAPI/Framework/StateTracking/FieldWatchers/ComparableListWatcher.cs new file mode 100644 index 00000000..95e9ef16 --- /dev/null +++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/ComparableListWatcher.cs @@ -0,0 +1,82 @@ +using System.Collections.Generic; +using System.Linq; + +namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers +{ + /// A watcher which detects changes to a collection of values using a specified instance. + internal class ComparableListWatcher : BaseDisposableWatcher, ICollectionWatcher + { + /********* + ** Properties + *********/ + /// The collection to watch. + private readonly ICollection CurrentValues; + + /// The values during the previous update. + private HashSet LastValues; + + /// The pairs added since the last reset. + private readonly List AddedImpl = new List(); + + /// The pairs demoved since the last reset. + private readonly List RemovedImpl = new List(); + + + /********* + ** Accessors + *********/ + /// Whether the value changed since the last reset. + public bool IsChanged => this.AddedImpl.Count > 0 || this.RemovedImpl.Count > 0; + + /// The values added since the last reset. + public IEnumerable Added => this.AddedImpl; + + /// The values removed since the last reset. + public IEnumerable Removed => this.RemovedImpl; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The collection to watch. + /// The equality comparer which indicates whether two values are the same. + public ComparableListWatcher(ICollection values, IEqualityComparer comparer) + { + this.CurrentValues = values; + this.LastValues = new HashSet(comparer); + } + + /// Update the current value if needed. + 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 curValues = new HashSet(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; + } + + /// Set the current value as the baseline. + public void Reset() + { + this.AssertNotDisposed(); + + this.AddedImpl.Clear(); + this.RemovedImpl.Clear(); + } + } +} 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(getValue, new ObjectReferenceComparer()); } + /// Get a watcher which detects when an object reference in a collection changes. + /// The value type. + /// The observable collection. + public static ComparableListWatcher ForReferenceList(ICollection collection) + { + return new ComparableListWatcher(collection, new ObjectReferenceComparer()); + } + /// Get a watcher for an observable collection. /// The value type. /// The observable collection. -- cgit