using System.Collections.Generic; using System.Linq; using StardewModdingAPI.Framework.StateTracking.Comparers; using StardewValley; namespace StardewModdingAPI.Framework.StateTracking.Snapshots { /// A frozen snapshot of the tracked game locations. internal class WorldLocationsSnapshot { /********* ** Fields *********/ /// A map of tracked locations. private readonly Dictionary LocationsDict = new Dictionary(new ObjectReferenceComparer()); /********* ** Accessors *********/ /// Tracks changes to the location list. public SnapshotListDiff LocationList { get; } = new SnapshotListDiff(); /// The tracked locations. public IEnumerable Locations => this.LocationsDict.Values; /********* ** Public methods *********/ /// Update the tracked values. /// The watcher to snapshot. public void Update(WorldLocationsTracker watcher) { // update location list this.LocationList.Update(watcher.IsLocationListChanged, watcher.Added, watcher.Removed); // remove missing locations foreach (var key in this.LocationsDict.Keys.Where(key => !watcher.HasLocationTracker(key)).ToArray()) this.LocationsDict.Remove(key); // update locations foreach (LocationTracker locationWatcher in watcher.Locations) { if (!this.LocationsDict.TryGetValue(locationWatcher.Location, out LocationSnapshot snapshot)) this.LocationsDict[locationWatcher.Location] = snapshot = new LocationSnapshot(locationWatcher); snapshot.Update(locationWatcher); } } } }