using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using StardewModdingAPI.Framework.StateTracking.Comparers; using StardewModdingAPI.Framework.StateTracking.FieldWatchers; using StardewValley; using StardewValley.Buildings; using StardewValley.Locations; namespace StardewModdingAPI.Framework.StateTracking { /// Detects changes to the game's locations. internal class WorldLocationsTracker : IWatcher { /********* ** Properties *********/ /// Tracks changes to the location list. private readonly ICollectionWatcher LocationListWatcher; /// A lookup of the tracked locations. private IDictionary LocationDict { get; } = new Dictionary(new ObjectReferenceComparer()); /// A lookup of registered buildings and their indoor location. private readonly IDictionary BuildingIndoors = new Dictionary(new ObjectReferenceComparer()); /********* ** Accessors *********/ /// Whether locations were added or removed since the last reset. public bool IsLocationListChanged => this.Added.Any() || this.Removed.Any(); /// Whether any tracked location data changed since the last reset. public bool IsChanged => this.IsLocationListChanged || this.Locations.Any(p => p.IsChanged); /// The tracked locations. public IEnumerable Locations => this.LocationDict.Values; /// The locations removed since the last update. public ICollection Added { get; } = new HashSet(new ObjectReferenceComparer()); /// The locations added since the last update. public ICollection Removed { get; } = new HashSet(new ObjectReferenceComparer()); /********* ** Public methods *********/ /// Construct an instance. /// The game's list of locations. public WorldLocationsTracker(ObservableCollection locations) { this.LocationListWatcher = WatcherFactory.ForObservableCollection(locations); } /// Update the current value if needed. public void Update() { // detect location changes if (this.LocationListWatcher.IsChanged) { this.Remove(this.LocationListWatcher.Removed); this.Add(this.LocationListWatcher.Added); } // detect building changes foreach (LocationTracker watcher in this.Locations.ToArray()) { if (watcher.BuildingsWatcher.IsChanged) { this.Remove(watcher.BuildingsWatcher.Removed); this.Add(watcher.BuildingsWatcher.Added); } } // detect building interior changed (e.g. construction completed) foreach (KeyValuePair pair in this.BuildingIndoors.Where(p => !object.Equals(p.Key.indoors.Value, p.Value))) { GameLocation oldIndoors = pair.Value; GameLocation newIndoors = pair.Key.indoors.Value; if (oldIndoors != null) this.Added.Add(oldIndoors); if (newIndoors != null) this.Removed.Add(newIndoors); } // update watchers foreach (IWatcher watcher in this.Locations) watcher.Update(); } /// Set the current location list as the baseline. public void ResetLocationList() { this.Removed.Clear(); this.Added.Clear(); this.LocationListWatcher.Reset(); } /// Set the current value as the baseline. public void Reset() { this.ResetLocationList(); foreach (IWatcher watcher in this.Locations) watcher.Reset(); } /// Stop watching the player fields and release all references. public void Dispose() { this.LocationListWatcher.Dispose(); foreach (IWatcher watcher in this.Locations) watcher.Dispose(); } /********* ** Private methods *********/ /**** ** Enumerable wrappers ****/ /// Add the given buildings. /// The buildings to add. public void Add(IEnumerable buildings) { foreach (Building building in buildings) this.Add(building); } /// Add the given locations. /// The locations to add. public void Add(IEnumerable locations) { foreach (GameLocation location in locations) this.Add(location); } /// Remove the given buildings. /// The buildings to remove. public void Remove(IEnumerable buildings) { foreach (Building building in buildings) this.Remove(building); } /// Remove the given locations. /// The locations to remove. public void Remove(IEnumerable locations) { foreach (GameLocation location in locations) this.Remove(location); } /**** ** Main add/remove logic ****/ /// Add the given building. /// The building to add. public void Add(Building building) { if (building == null) return; GameLocation indoors = building.indoors.Value; this.BuildingIndoors[building] = indoors; this.Add(indoors); } /// Add the given location. /// The location to add. public void Add(GameLocation location) { if (location == null) return; // remove old location if needed this.Remove(location); // track change this.Added.Add(location); // add this.LocationDict[location] = new LocationTracker(location); if (location is BuildableGameLocation buildableLocation) this.Add(buildableLocation.buildings); } /// Remove the given building. /// The building to remove. public void Remove(Building building) { if (building == null) return; this.BuildingIndoors.Remove(building); this.Remove(building.indoors.Value); } /// Remove the given location. /// The location to remove. public void Remove(GameLocation location) { if (location == null) return; if (this.LocationDict.TryGetValue(location, out LocationTracker watcher)) { // track change this.Removed.Add(location); // remove this.LocationDict.Remove(location); watcher.Dispose(); if (location is BuildableGameLocation buildableLocation) this.Remove(buildableLocation.buildings); } } } }