using System.Collections.Generic;
using System.Linq;
using ConvenientChests.CategorizeChests.Framework.Persistence;
using Microsoft.Xna.Framework;
using StardewModdingAPI;
using StardewValley;
using StardewValley.Locations;
using StardewValley.Objects;
namespace ConvenientChests.CategorizeChests.Framework
{
///
/// The class responsible for producing data to be saved.
///
class Saver
{
private readonly IChestDataManager ChestDataManager;
public Saver(IChestDataManager chestDataManager)
{
ChestDataManager = chestDataManager;
}
///
/// Build save data for the current game state.
///
public SaveData GetSerializableData()
{
return new SaveData
{
ChestEntries = BuildChestEntries()
};
}
private IEnumerable BuildChestEntries()
{
foreach (var location in Game1.locations)
{
// chests
foreach (var pair in GetLocationChests(location))
yield return new ChestEntry(
ChestDataManager.GetChestData(pair.Value),
new ChestAddress(location.Name, pair.Key)
);
switch (location)
{
// buildings
case BuildableGameLocation buildableLocation:
foreach (var building in buildableLocation.buildings.Where(b => b.indoors.Value != null))
foreach (var pair in GetLocationChests(building.indoors.Value))
yield return new ChestEntry(
ChestDataManager.GetChestData(pair.Value),
new ChestAddress(location.Name, pair.Key, ChestLocationType.Building, building.nameOfIndoors)
);
break;
// fridges
case FarmHouse farmHouse when farmHouse.upgradeLevel >= 1:
yield return new ChestEntry(
ChestDataManager.GetChestData(farmHouse.fridge.Value),
new ChestAddress {LocationName = farmHouse.uniqueName?.Value ?? farmHouse.Name, LocationType = ChestLocationType.Refrigerator}
);
break;
}
}
}
///
/// Retrieve a collection of the chest objects present in the given
/// location, keyed by their tile location.
///
private static IDictionary GetLocationChests(GameLocation location) =>
location.Objects.Pairs
.Where(pair => pair.Value is Chest c && c.playerChest.Value)
.ToDictionary(
pair => pair.Key,
pair => (Chest) pair.Value
);
}
}