private void SaveEvents_BeforeSave(object s, EventArgs e) { this.Monitor.Log("Packing custom objects...", LogLevel.Trace); ItemEvents.FireBeforeSerialize(); var data = new Dictionary <string, InstanceState>(); foreach (GameLocation loc in Game1.locations) { //TODO Testing. look into where Chests & Fridge store thier item locations /*[22:58:19 ERROR Entoarox Framework] This mod failed in the SaveEvents.AfterSave event. Technical details: * System.InvalidCastException: Unable to cast object of type 'StardewValley.Object' to type 'StardewValley.Objects.Chest'. * at Entoarox.Framework.Core.EntoaroxFrameworkMod.SaveEvents_AfterSave(Object s, EventArgs e) * at StardewModdingAPI.Framework.Events.ManagedEvent.Raise() in C:\source\_Stardew\SMAPI\src\SMAPI\Framework\Events\ManagedEvent.cs:line 126 */ foreach (SObject objects in loc.Objects.Values) { if (objects is Chest chest) { Serialize(data, chest.items); } } } Game1.player.Items = Serialize(data, Game1.player.Items); if (Game1.getLocationFromName("FarmHouse") is FarmHouse house && house.fridge.Value != null) { Serialize(data, house.fridge.Value.items); } string path = Path.Combine(Constants.CurrentSavePath, "Entoarox.Framework", "CustomItems.json"); this.Helper.WriteJsonFile(path, data); ItemEvents.FireAfterSerialize(); }
/// <summary>Raised before the game begins writes data to the save file (except the initial save creation).</summary> /// <param name="sender">The event sender.</param> /// <param name="e">The event arguments.</param> private void OnSaving(object sender, SavingEventArgs e) { if (Context.IsMainPlayer) { this.Monitor.Log("Packing custom objects...", LogLevel.Trace); ItemEvents.FireBeforeSerialize(); Dictionary <string, InstanceState> data = new Dictionary <string, InstanceState>(); foreach (GameLocation loc in Game1.locations) { foreach (Chest chest in loc.Objects.Values.OfType <Chest>()) { this.Serialize(data, chest.items); } } this.Serialize(data, Game1.player.Items); FarmHouse house = Game1.getLocationFromName("FarmHouse") as FarmHouse; if (house.fridge.Value != null) { this.Serialize(data, house.fridge.Value.items); } this.Helper.Data.WriteSaveData("custom-items", data); ItemEvents.FireAfterSerialize(); } }
private void SaveEvents_BeforeSave(object s, EventArgs e) { this.Monitor.Log("Packing custom objects...", LogLevel.Trace); ItemEvents.FireBeforeSerialize(); var data = new Dictionary <string, InstanceState>(); foreach (GameLocation loc in Game1.locations) { foreach (Chest chest in loc.objects.Where(a => a.Value is Chest).Select(a => (Chest)a.Value)) { chest.items = Serialize(data, chest.items); } } Game1.player.items = Serialize(data, Game1.player.items); var house = (Game1.getLocationFromName("FarmHouse") as StardewValley.Locations.FarmHouse); if (house.fridge != null) { house.fridge.items = Serialize(data, house.fridge.items); } string path = Path.Combine(Constants.CurrentSavePath, "Entoarox.Framework", "CustomItems.json"); this.Helper.WriteJsonFile(path, data); ItemEvents.FireAfterSerialize(); }
private void SaveEvents_BeforeSave(object s, EventArgs e) { this.Monitor.Log("Packing custom objects...", LogLevel.Trace); ItemEvents.FireBeforeSerialize(); Dictionary <string, InstanceState> data = new Dictionary <string, InstanceState>(); foreach (GameLocation loc in Game1.locations) { foreach (Chest chest in loc.Objects.Values.OfType <Chest>()) { this.Serialize(data, chest.items); } } this.Serialize(data, Game1.player.Items); FarmHouse house = Game1.getLocationFromName("FarmHouse") as FarmHouse; if (house.fridge.Value != null) { this.Serialize(data, house.fridge.Value.items); } string path = Path.Combine(Constants.CurrentSavePath, "Entoarox.Framework", "CustomItems.json"); this.Helper.WriteJsonFile(path, data); ItemEvents.FireAfterSerialize(); }
/// <summary>Raised before the game begins writes data to the save file (except the initial save creation).</summary> /// <param name="sender">The event sender.</param> /// <param name="e">The event arguments.</param> private void OnSaving(object sender, SavingEventArgs e) { if (Context.IsMainPlayer) { this.Monitor.Log("Packing custom objects...", LogLevel.Trace); ItemEvents.FireBeforeSerialize(); var data = new Dictionary <string, InstanceState>(); var features = new List <Tuple <string, Vector2, InstanceState> >(); var locations = new Dictionary <string, Tuple <bool, InstanceState> >(); foreach (var loc in this.GetAllLocations()) { foreach (Chest chest in loc.Objects.Values.OfType <Chest>()) { this.Serialize(data, chest.items); } this.Serialize(data, loc.Objects); if (loc.terrainFeatures != null) { this.Serialize(features, loc, loc.terrainFeatures); } } foreach (var location in Game1.locations.Where(a => a is ICustomItem).ToArray()) { Game1.locations.Remove(location); this.Serialize(locations, location); } this.Serialize(data, Game1.player.Items); FarmHouse house = Game1.getLocationFromName("FarmHouse") as FarmHouse; if (house.fridge.Value != null) { this.Serialize(data, house.fridge.Value.items); } this.Monitor.Log("Found and serialized [" + data.Count + "] Item instances", LogLevel.Trace); this.Monitor.Log("Found and serialized [" + features.Count + "] TerrainFeature instances", LogLevel.Trace); this.Monitor.Log("Found and serialized [" + locations.Count + "] GameLocation instances", LogLevel.Trace); string path = Path.Combine(Constants.CurrentSavePath, "Entoarox.Framework"); this.Helper.Data.WriteSaveData("custom-items", data); this.Helper.Data.WriteSaveData("custom-features", features); this.Helper.Data.WriteSaveData("custom-locations", locations); ItemEvents.FireAfterSerialize(); this.Monitor.Log("Packing complete", LogLevel.Trace); } }