summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/StateTracking/FieldWatchers/ComparableWatcher.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/ComparableWatcher.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/ComparableWatcher.cs')
-rw-r--r--src/SMAPI/Framework/StateTracking/FieldWatchers/ComparableWatcher.cs62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/SMAPI/Framework/StateTracking/FieldWatchers/ComparableWatcher.cs b/src/SMAPI/Framework/StateTracking/FieldWatchers/ComparableWatcher.cs
new file mode 100644
index 00000000..d51fc2ac
--- /dev/null
+++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/ComparableWatcher.cs
@@ -0,0 +1,62 @@
+using System;
+using System.Collections.Generic;
+
+namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers
+{
+ /// <summary>A watcher which detects changes to a value using a specified <see cref="IEqualityComparer{T}"/> instance.</summary>
+ internal class ComparableWatcher<T> : IValueWatcher<T>
+ {
+ /*********
+ ** Properties
+ *********/
+ /// <summary>Get the current value.</summary>
+ private readonly Func<T> GetValue;
+
+ /// <summary>The equality comparer.</summary>
+ private readonly IEqualityComparer<T> Comparer;
+
+
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>The field value at the last reset.</summary>
+ public T PreviousValue { get; private set; }
+
+ /// <summary>The latest value.</summary>
+ public T CurrentValue { get; private set; }
+
+ /// <summary>Whether the value changed since the last reset.</summary>
+ public bool IsChanged { get; private set; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="getValue">Get the current value.</param>
+ /// <param name="comparer">The equality comparer which indicates whether two values are the same.</param>
+ public ComparableWatcher(Func<T> getValue, IEqualityComparer<T> comparer)
+ {
+ this.GetValue = getValue;
+ this.Comparer = comparer;
+ this.PreviousValue = getValue();
+ }
+
+ /// <summary>Update the current value if needed.</summary>
+ public void Update()
+ {
+ this.CurrentValue = this.GetValue();
+ this.IsChanged = !this.Comparer.Equals(this.PreviousValue, this.CurrentValue);
+ }
+
+ /// <summary>Set the current value as the baseline.</summary>
+ public void Reset()
+ {
+ this.PreviousValue = this.CurrentValue;
+ this.IsChanged = false;
+ }
+
+ /// <summary>Release any references if needed when the field is no longer needed.</summary>
+ public void Dispose() { }
+ }
+}