From 5675f9fcebfdeb03510c347712df8e9934b362c6 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 23 May 2022 12:57:33 -0400 Subject: add watcher names to simplify troubleshooting --- .../FieldWatchers/ComparableListWatcher.cs | 7 +++- .../FieldWatchers/ComparableWatcher.cs | 7 +++- .../FieldWatchers/ImmutableCollectionWatcher.cs | 3 ++ .../FieldWatchers/NetCollectionWatcher.cs | 7 +++- .../FieldWatchers/NetDictionaryWatcher.cs | 7 +++- .../StateTracking/FieldWatchers/NetListWatcher.cs | 7 +++- .../StateTracking/FieldWatchers/NetValueWatcher.cs | 7 +++- .../FieldWatchers/ObservableCollectionWatcher.cs | 7 +++- .../StateTracking/FieldWatchers/WatcherFactory.cs | 45 +++++++++++++--------- 9 files changed, 72 insertions(+), 25 deletions(-) (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 index 3d178a36..0b13434a 100644 --- a/src/SMAPI/Framework/StateTracking/FieldWatchers/ComparableListWatcher.cs +++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/ComparableListWatcher.cs @@ -26,6 +26,9 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers /********* ** Accessors *********/ + /// + public string Name { get; } + /// public bool IsChanged => this.AddedImpl.Count > 0 || this.RemovedImpl.Count > 0; @@ -40,10 +43,12 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers ** Public methods *********/ /// Construct an instance. + /// A name which identifies what the watcher is watching, used for troubleshooting. /// The collection to watch. /// The equality comparer which indicates whether two values are the same. - public ComparableListWatcher(ICollection values, IEqualityComparer comparer) + public ComparableListWatcher(string name, ICollection values, IEqualityComparer comparer) { + this.Name = name; this.CurrentValues = values; this.LastValues = new HashSet(comparer); } diff --git a/src/SMAPI/Framework/StateTracking/FieldWatchers/ComparableWatcher.cs b/src/SMAPI/Framework/StateTracking/FieldWatchers/ComparableWatcher.cs index e51c46c1..e2f6c7fd 100644 --- a/src/SMAPI/Framework/StateTracking/FieldWatchers/ComparableWatcher.cs +++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/ComparableWatcher.cs @@ -20,6 +20,9 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers /********* ** Accessors *********/ + /// + public string Name { get; } + /// public TValue PreviousValue { get; private set; } @@ -34,10 +37,12 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers ** Public methods *********/ /// Construct an instance. + /// A name which identifies what the watcher is watching, used for troubleshooting. /// Get the current value. /// The equality comparer which indicates whether two values are the same. - public ComparableWatcher(Func getValue, IEqualityComparer comparer) + public ComparableWatcher(string name, Func getValue, IEqualityComparer comparer) { + this.Name = name; this.GetValue = getValue; this.Comparer = comparer; this.CurrentValue = getValue(); diff --git a/src/SMAPI/Framework/StateTracking/FieldWatchers/ImmutableCollectionWatcher.cs b/src/SMAPI/Framework/StateTracking/FieldWatchers/ImmutableCollectionWatcher.cs index b46e0b24..9c2ba9bc 100644 --- a/src/SMAPI/Framework/StateTracking/FieldWatchers/ImmutableCollectionWatcher.cs +++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/ImmutableCollectionWatcher.cs @@ -13,6 +13,9 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers /// A singleton collection watcher instance. public static ImmutableCollectionWatcher Instance { get; } = new(); + /// + public string Name => nameof(ImmutableCollectionWatcher); + /// public bool IsChanged { get; } = false; diff --git a/src/SMAPI/Framework/StateTracking/FieldWatchers/NetCollectionWatcher.cs b/src/SMAPI/Framework/StateTracking/FieldWatchers/NetCollectionWatcher.cs index e278fa99..1d5e4851 100644 --- a/src/SMAPI/Framework/StateTracking/FieldWatchers/NetCollectionWatcher.cs +++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/NetCollectionWatcher.cs @@ -24,6 +24,9 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers /********* ** Accessors *********/ + /// + public string Name { get; } + /// public bool IsChanged => this.AddedImpl.Count > 0 || this.RemovedImpl.Count > 0; @@ -38,9 +41,11 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers ** Public methods *********/ /// Construct an instance. + /// A name which identifies what the watcher is watching, used for troubleshooting. /// The field to watch. - public NetCollectionWatcher(NetCollection field) + public NetCollectionWatcher(string name, NetCollection field) { + this.Name = name; this.Field = field; field.OnValueAdded += this.OnValueAdded; field.OnValueRemoved += this.OnValueRemoved; diff --git a/src/SMAPI/Framework/StateTracking/FieldWatchers/NetDictionaryWatcher.cs b/src/SMAPI/Framework/StateTracking/FieldWatchers/NetDictionaryWatcher.cs index a0fcc236..3bd8e09d 100644 --- a/src/SMAPI/Framework/StateTracking/FieldWatchers/NetDictionaryWatcher.cs +++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/NetDictionaryWatcher.cs @@ -31,6 +31,9 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers /********* ** Accessors *********/ + /// + public string Name { get; } + /// public bool IsChanged => this.PairsAdded.Count > 0 || this.PairsRemoved.Count > 0; @@ -45,9 +48,11 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers ** Public methods *********/ /// Construct an instance. + /// A name which identifies what the watcher is watching, used for troubleshooting. /// The field to watch. - public NetDictionaryWatcher(NetDictionary field) + public NetDictionaryWatcher(string name, NetDictionary field) { + this.Name = name; this.Field = field; field.OnValueAdded += this.OnValueAdded; diff --git a/src/SMAPI/Framework/StateTracking/FieldWatchers/NetListWatcher.cs b/src/SMAPI/Framework/StateTracking/FieldWatchers/NetListWatcher.cs index 3badb533..5b6a3e1f 100644 --- a/src/SMAPI/Framework/StateTracking/FieldWatchers/NetListWatcher.cs +++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/NetListWatcher.cs @@ -25,6 +25,9 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers /********* ** Accessors *********/ + /// + public string Name { get; } + /// public bool IsChanged => this.AddedImpl.Count > 0 || this.RemovedImpl.Count > 0; @@ -39,9 +42,11 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers ** Public methods *********/ /// Construct an instance. + /// A name which identifies what the watcher is watching, used for troubleshooting. /// The field to watch. - public NetListWatcher(NetList> field) + public NetListWatcher(string name, NetList> field) { + this.Name = name; this.Field = field; field.OnElementChanged += this.OnElementChanged; field.OnArrayReplaced += this.OnArrayReplaced; diff --git a/src/SMAPI/Framework/StateTracking/FieldWatchers/NetValueWatcher.cs b/src/SMAPI/Framework/StateTracking/FieldWatchers/NetValueWatcher.cs index d00f4582..e4219dda 100644 --- a/src/SMAPI/Framework/StateTracking/FieldWatchers/NetValueWatcher.cs +++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/NetValueWatcher.cs @@ -17,6 +17,9 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers /********* ** Accessors *********/ + /// + public string Name { get; } + /// public bool IsChanged { get; private set; } @@ -31,9 +34,11 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers ** Public methods *********/ /// Construct an instance. + /// A name which identifies what the watcher is watching, used for troubleshooting. /// The field to watch. - public NetValueWatcher(NetFieldBase field) + public NetValueWatcher(string name, NetFieldBase field) { + this.Name = name; this.Field = field; this.PreviousValue = field.Value; this.CurrentValue = field.Value; diff --git a/src/SMAPI/Framework/StateTracking/FieldWatchers/ObservableCollectionWatcher.cs b/src/SMAPI/Framework/StateTracking/FieldWatchers/ObservableCollectionWatcher.cs index f503c47f..1f95ac89 100644 --- a/src/SMAPI/Framework/StateTracking/FieldWatchers/ObservableCollectionWatcher.cs +++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/ObservableCollectionWatcher.cs @@ -28,6 +28,9 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers /********* ** Accessors *********/ + /// + public string Name { get; } + /// public bool IsChanged => this.AddedImpl.Count > 0 || this.RemovedImpl.Count > 0; @@ -42,9 +45,11 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers ** Public methods *********/ /// Construct an instance. + /// A name which identifies what the watcher is watching, used for troubleshooting. /// The field to watch. - public ObservableCollectionWatcher(ObservableCollection field) + public ObservableCollectionWatcher(string name, ObservableCollection field) { + this.Name = name; this.Field = field; field.CollectionChanged += this.OnCollectionChanged; } diff --git a/src/SMAPI/Framework/StateTracking/FieldWatchers/WatcherFactory.cs b/src/SMAPI/Framework/StateTracking/FieldWatchers/WatcherFactory.cs index c4a4d0b9..c31be1fc 100644 --- a/src/SMAPI/Framework/StateTracking/FieldWatchers/WatcherFactory.cs +++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/WatcherFactory.cs @@ -17,37 +17,41 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers ****/ /// Get a watcher which compares values using their method. This method should only be used when won't work, since this doesn't validate whether they're comparable. /// The value type. + /// A name which identifies what the watcher is watching, used for troubleshooting. /// Get the current value. - public static IValueWatcher ForGenericEquality(Func getValue) + public static IValueWatcher ForGenericEquality(string name, Func getValue) where T : struct { - return new ComparableWatcher(getValue, new GenericEqualsComparer()); + return new ComparableWatcher(name, getValue, new GenericEqualsComparer()); } /// Get a watcher for an value. /// The value type. + /// A name which identifies what the watcher is watching, used for troubleshooting. /// Get the current value. - public static IValueWatcher ForEquatable(Func getValue) + public static IValueWatcher ForEquatable(string name, Func getValue) where T : IEquatable { - return new ComparableWatcher(getValue, new EquatableComparer()); + return new ComparableWatcher(name, getValue, new EquatableComparer()); } /// Get a watcher which detects when an object reference changes. /// The value type. + /// A name which identifies what the watcher is watching, used for troubleshooting. /// Get the current value. - public static IValueWatcher ForReference(Func getValue) + public static IValueWatcher ForReference(string name, Func getValue) { - return new ComparableWatcher(getValue, new ObjectReferenceComparer()); + return new ComparableWatcher(name, getValue, new ObjectReferenceComparer()); } /// Get a watcher for a net collection. /// The value type. /// The net field instance type. + /// A name which identifies what the watcher is watching, used for troubleshooting. /// The net collection. - public static IValueWatcher ForNetValue(NetFieldBase field) where TSelf : NetFieldBase + public static IValueWatcher ForNetValue(string name, NetFieldBase field) where TSelf : NetFieldBase { - return new NetValueWatcher(field); + return new NetValueWatcher(name, field); } /**** @@ -55,18 +59,20 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers ****/ /// Get a watcher which detects when an object reference in a collection changes. /// The value type. + /// A name which identifies what the watcher is watching, used for troubleshooting. /// The observable collection. - public static ICollectionWatcher ForReferenceList(ICollection collection) + public static ICollectionWatcher ForReferenceList(string name, ICollection collection) { - return new ComparableListWatcher(collection, new ObjectReferenceComparer()); + return new ComparableListWatcher(name, collection, new ObjectReferenceComparer()); } /// Get a watcher for an observable collection. /// The value type. + /// A name which identifies what the watcher is watching, used for troubleshooting. /// The observable collection. - public static ICollectionWatcher ForObservableCollection(ObservableCollection collection) + public static ICollectionWatcher ForObservableCollection(string name, ObservableCollection collection) { - return new ObservableCollectionWatcher(collection); + return new ObservableCollectionWatcher(name, collection); } /// Get a watcher for a collection that never changes. @@ -78,36 +84,39 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers /// Get a watcher for a net collection. /// The value type. + /// A name which identifies what the watcher is watching, used for troubleshooting. /// The net collection. - public static ICollectionWatcher ForNetCollection(NetCollection collection) + public static ICollectionWatcher ForNetCollection(string name, NetCollection collection) where T : class, INetObject { - return new NetCollectionWatcher(collection); + return new NetCollectionWatcher(name, collection); } /// Get a watcher for a net list. /// The value type. + /// A name which identifies what the watcher is watching, used for troubleshooting. /// The net list. - public static ICollectionWatcher ForNetList(NetList> collection) + public static ICollectionWatcher ForNetList(string name, NetList> collection) where T : class, INetObject { - return new NetListWatcher(collection); + return new NetListWatcher(name, collection); } /// Get a watcher for a net dictionary. + /// A name which identifies what the watcher is watching, used for troubleshooting. /// The dictionary key type. /// The dictionary value type. /// The net type equivalent to . /// The serializable dictionary type that can store the keys and values. /// The net field instance type. /// The net field. - public static NetDictionaryWatcher ForNetDictionary(NetDictionary field) + public static NetDictionaryWatcher ForNetDictionary(string name, NetDictionary field) where TKey : notnull where TField : class, INetObject, new() where TSerialDict : IDictionary, new() where TSelf : NetDictionary { - return new NetDictionaryWatcher(field); + return new NetDictionaryWatcher(name, field); } } } -- cgit