diff options
Diffstat (limited to 'src/SMAPI/Framework/StateTracking/FieldWatchers')
-rw-r--r-- | src/SMAPI/Framework/StateTracking/FieldWatchers/NetListWatcher.cs | 90 |
1 files changed, 49 insertions, 41 deletions
diff --git a/src/SMAPI/Framework/StateTracking/FieldWatchers/NetListWatcher.cs b/src/SMAPI/Framework/StateTracking/FieldWatchers/NetListWatcher.cs index cac66356..8aa0eab5 100644 --- a/src/SMAPI/Framework/StateTracking/FieldWatchers/NetListWatcher.cs +++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/NetListWatcher.cs @@ -1,21 +1,18 @@ -using System; using System.Collections.Generic; using Netcode; namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers { - internal class NetListWatcher<TKey, TValue> : BaseDisposableWatcher, ICollectionWatcher<TValue> + /// <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, Netcode.NetRef<TValue>> Field; - - public TKey Key { get; } + private readonly NetList<TValue, NetRef<TValue>> Field; /// <summary>The pairs added since the last reset.</summary> private readonly IList<TValue> AddedImpl = new List<TValue>(); @@ -23,26 +20,47 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers /// <summary>The pairs removed since the last reset.</summary> private readonly IList<TValue> RemovedImpl = new List<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, Netcode.NetRef<TValue>> field, TKey key) + public NetListWatcher(NetList<TValue, NetRef<TValue>> field) { this.Field = field; - this.Key = key; field.OnElementChanged += this.OnElementChanged; field.OnArrayReplaced += this.OnArrayReplaced; } - public bool IsChanged => this.AddedImpl.Count > 0 || this.RemovedImpl.Count > 0; - - public IEnumerable<TValue> Added => this.AddedImpl; + /// <summary>Set the current value as the baseline.</summary> + public void Reset() + { + this.AddedImpl.Clear(); + this.RemovedImpl.Clear(); + } - public IEnumerable<TValue> Removed => this.RemovedImpl; + /// <summary>Update the current value if needed.</summary> + public void Update() + { + this.AssertNotDisposed(); + } - public void Dispose() + /// <summary>Stop watching the field and release all references.</summary> + public override void Dispose() { if (!this.IsDisposed) { @@ -53,47 +71,37 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers base.Dispose(); } - public void Reset() - { - this.AddedImpl.Clear(); - this.RemovedImpl.Clear(); - } - - public void Update() - { - this.AssertNotDisposed(); - } /********* ** Private methods *********/ - private void OnArrayReplaced(NetList<TValue, Netcode.NetRef<TValue>> list, IList<TValue> before, IList<TValue> after) + /// <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) { this.AddedImpl.Clear(); this.RemovedImpl.Clear(); - foreach(var obj in after) - this.AddedImpl.Add(obj); + foreach (TValue value in newValues) + this.AddedImpl.Add(value); - foreach(var obj in before) - this.RemovedImpl.Add(obj); + foreach (TValue value in oldValues) + this.RemovedImpl.Add(value); } - private void OnElementChanged(NetList<TValue, Netcode.NetRef<TValue>> list, int index, TValue oldValue, TValue newValue) + /// <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) { - - /* checks for stack addition / subtraction changing stacks does not fire off an element changed event - if ((oldValue != null && newValue != null) && oldValue.CompareTo(newValue) < 0) - this.AddedImpl.Add(newValue); - //Stack Removed from - if ((oldValue != null && newValue != null) && oldValue.CompareTo(newValue) > 0) - this.RemovedImpl.Add(newValue); - */ - - if(newValue!=null) + if (newValue != null) this.AddedImpl.Add(newValue); - if(oldValue!=null) + if (oldValue != null) this.RemovedImpl.Add(oldValue); } } |