summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/StateTracking/FieldWatchers/WatcherFactory.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2018-04-21 20:37:17 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2018-04-21 20:37:17 -0400
commiteead352af26d0fcc5cac147d0eb5ec384854d931 (patch)
tree78895ff201953f065667976c0e861168dddcebc1 /src/SMAPI/Framework/StateTracking/FieldWatchers/WatcherFactory.cs
parentb346d28d3858b79c6c4cde55faac34ecdedeaff1 (diff)
downloadSMAPI-eead352af26d0fcc5cac147d0eb5ec384854d931.tar.gz
SMAPI-eead352af26d0fcc5cac147d0eb5ec384854d931.tar.bz2
SMAPI-eead352af26d0fcc5cac147d0eb5ec384854d931.zip
rewrite world/player state tracking (#453)
Diffstat (limited to 'src/SMAPI/Framework/StateTracking/FieldWatchers/WatcherFactory.cs')
-rw-r--r--src/SMAPI/Framework/StateTracking/FieldWatchers/WatcherFactory.cs54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/SMAPI/Framework/StateTracking/FieldWatchers/WatcherFactory.cs b/src/SMAPI/Framework/StateTracking/FieldWatchers/WatcherFactory.cs
new file mode 100644
index 00000000..bf261bb5
--- /dev/null
+++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/WatcherFactory.cs
@@ -0,0 +1,54 @@
+using System;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using Netcode;
+using StardewModdingAPI.Framework.StateTracking.Comparers;
+
+namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers
+{
+ /// <summary>Provides convenience wrappers for creating watchers.</summary>
+ internal static class WatcherFactory
+ {
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Get a watcher for an <see cref="IEquatable{T}"/> value.</summary>
+ /// <typeparam name="T">The value type.</typeparam>
+ /// <param name="getValue">Get the current value.</param>
+ public static ComparableWatcher<T> ForEquatable<T>(Func<T> getValue) where T : IEquatable<T>
+ {
+ return new ComparableWatcher<T>(getValue, new EquatableComparer<T>());
+ }
+
+ /// <summary>Get a watcher which detects when an object reference changes.</summary>
+ /// <typeparam name="T">The value type.</typeparam>
+ /// <param name="getValue">Get the current value.</param>
+ public static ComparableWatcher<T> ForReference<T>(Func<T> getValue)
+ {
+ return new ComparableWatcher<T>(getValue, 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>
+ public static ObservableCollectionWatcher<T> ForObservableCollection<T>(ObservableCollection<T> collection)
+ {
+ return new ObservableCollectionWatcher<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>
+ /// <typeparam name="TField">The net type equivalent to <typeparamref name="TValue"/>.</typeparam>
+ /// <typeparam name="TSerialDict">The serializable dictionary type that can store the keys and values.</typeparam>
+ /// <typeparam name="TSelf">The net field instance type.</typeparam>
+ /// <param name="field">The net field.</param>
+ public static NetDictionaryWatcher<TKey, TValue, TField, TSerialDict, TSelf> ForNetDictionary<TKey, TValue, TField, TSerialDict, TSelf>(NetDictionary<TKey, TValue, TField, TSerialDict, TSelf> field)
+ where TField : class, INetObject<INetSerializable>, new()
+ where TSerialDict : IDictionary<TKey, TValue>, new()
+ where TSelf : NetDictionary<TKey, TValue, TField, TSerialDict, TSelf>
+ {
+ return new NetDictionaryWatcher<TKey, TValue, TField, TSerialDict, TSelf>(field);
+ }
+ }
+}