using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Netcode;
using StardewModdingAPI.Framework.StateTracking.Comparers;
namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers
{
/// Provides convenience wrappers for creating watchers.
internal static class WatcherFactory
{
/*********
** Public methods
*********/
/****
** Values
****/
/// 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(string name, Func getValue)
where T : struct
{
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(string name, Func getValue)
where T : IEquatable
{
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(string name, Func getValue)
{
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(string name, NetFieldBase field) where TSelf : NetFieldBase
{
return new NetValueWatcher(name, field);
}
/****
** Collections
****/
/// 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(string name, ICollection collection)
{
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(string name, ObservableCollection collection)
{
return new ObservableCollectionWatcher(name, collection);
}
/// Get a watcher for a collection that never changes.
/// The value type.
public static ICollectionWatcher ForImmutableCollection()
{
return ImmutableCollectionWatcher.Instance;
}
/// 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(string name, NetCollection collection)
where T : class, INetObject
{
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(string name, NetList> collection)
where T : class, INetObject
{
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(string name, NetDictionary field)
where TKey : notnull
where TField : class, INetObject, new()
where TSerialDict : IDictionary, new()
where TSelf : NetDictionary
{
return new NetDictionaryWatcher(name, field);
}
}
}