From 9d3be07daa74462cd8608ea1e3f52910c0f55faf Mon Sep 17 00:00:00 2001 From: JoshuaNavarro Date: Wed, 21 Aug 2019 20:17:27 -0700 Subject: [PATCH] Got items to be loaded back into player's inventory upon loading a save file. --- .../Utilities/Serialization/Serialization.cs | 108 +++++++++++++++++- GeneralMods/Revitalize/ModCore.cs | 76 ++---------- 2 files changed, 114 insertions(+), 70 deletions(-) diff --git a/GeneralMods/Revitalize/Framework/Utilities/Serialization/Serialization.cs b/GeneralMods/Revitalize/Framework/Utilities/Serialization/Serialization.cs index 55d0b80e..67734c99 100644 --- a/GeneralMods/Revitalize/Framework/Utilities/Serialization/Serialization.cs +++ b/GeneralMods/Revitalize/Framework/Utilities/Serialization/Serialization.cs @@ -5,6 +5,8 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; +using Revitalize.Framework.Objects; +using Revitalize.Framework.Objects.Furniture; using Revitalize.Framework.Utilities.Serialization.ContractResolvers; using StardewValley; using StardewValley.Objects; @@ -119,7 +121,111 @@ namespace Revitalize.Framework.Utilities public void afterLoad() { this.deleteAllUnusedFiles(); - this.removeNullObjects(); + //this.removeNullObjects(); + this.restoreModObjects(); + } + + public void restoreModObjects() + { + + foreach (var v in ModCore.ObjectGroups) + { + foreach (var obj in v.Value.objects.Values) + { + (obj as CustomObject).replaceAfterLoad(); + } + } + + foreach (GameLocation loc in Game1.locations) + { + foreach (StardewValley.Object c in loc.Objects.Values) + { + if (c is Chest) + { + List toRemove = new List(); + List toAdd = new List(); + foreach (Item o in (c as Chest).items) + { + if (o == null) continue; + if (o is Chest && o.Name != "Chest") + { + ModCore.log("Found a custom object in a chest!"); + string jsonString = o.Name; + string guidName = jsonString.Split(new string[] { "GUID=" }, StringSplitOptions.None)[1]; + ModCore.log(jsonString); + string type = jsonString.Split('|')[2]; + Item I = (Item)ModCore.Serializer.DeserializeGUID(guidName, Type.GetType(type)); + + if (I is MultiTiledObject) + { + (I as MultiTiledObject).recreate(); + } + + toAdd.Add(I); + toRemove.Add(o); + //Item i = Serializer.DeserializeFromJSONString(jsonString); + //ModCore.log("Deserialized item is: "+i.Name); + } + } + + foreach (Item i in toRemove) + { + (c as Chest).items.Remove(i); + } + foreach (Item I in toAdd) + { + (c as Chest).items.Add(I); + } + } + else if (c is StorageFurnitureTile) + { + foreach (Item o in (c as StorageFurnitureTile).info.inventory.items) + { + if (o is Chest && o.Name != "Chest") + { + ModCore.log("Found a custom object in a chest!"); + } + } + } + } + } + List toAdd2 = new List(); + List toRemove2 = new List(); + foreach (Item I in Game1.player.Items) + { + if (I == null) continue; + else + { + if (I is Chest && I.Name != "Chest") + { + ModCore.log("Found a custom object in a chest!"); + string jsonString = I.Name; + string guidName = jsonString.Split(new string[] { "GUID=" }, StringSplitOptions.None)[1]; + ModCore.log(jsonString); + string type = jsonString.Split('|')[2]; + Item ret = (Item)ModCore.Serializer.DeserializeGUID(guidName, Type.GetType(type)); + + if (ret is MultiTiledObject) + { + (ret as MultiTiledObject).recreate(); + } + + toAdd2.Add(ret); + toRemove2.Add(I); + //Item i = Serializer.DeserializeFromJSONString(jsonString); + //ModCore.log("Deserialized item is: "+i.Name); + } + + } + } + foreach (Item i in toRemove2) + { + Game1.player.Items.Remove(i); + } + foreach (Item I in toAdd2) + { + Game1.player.addItemToInventory(I); + } } public void returnToTitle() diff --git a/GeneralMods/Revitalize/ModCore.cs b/GeneralMods/Revitalize/ModCore.cs index 007a3d09..2d2271db 100644 --- a/GeneralMods/Revitalize/ModCore.cs +++ b/GeneralMods/Revitalize/ModCore.cs @@ -210,7 +210,7 @@ namespace Revitalize playerInfo = new PlayerInfo(); TextureManager.AddTextureManager(Manifest, "Furniture"); - TextureManager.GetTextureManager(Manifest, "Furniture").searchForTextures(ModHelper, this.ModManifest, Path.Combine("Content", "Graphics", "Objects","Furniture")); + TextureManager.GetTextureManager(Manifest, "Furniture").searchForTextures(ModHelper, this.ModManifest, Path.Combine("Content", "Graphics", "Objects", "Furniture")); TextureManager.AddTextureManager(Manifest, "InventoryMenu"); TextureManager.GetTextureManager(Manifest, "InventoryMenu").searchForTextures(ModHelper, this.ModManifest, Path.Combine("Content", "Graphics", "Menus", "InventoryMenu")); TextureManager.AddTextureManager(Manifest, "Resources.Ore"); @@ -232,7 +232,7 @@ namespace Revitalize ObjectGroups = new Dictionary(); ObjectManager = new ObjectManager(Manifest); - + ObjectsToDraw = new Dictionary(); ModHelper.Events.GameLoop.SaveLoaded += this.GameLoop_SaveLoaded; @@ -380,74 +380,12 @@ namespace Revitalize { this.loadContent(); - - Serializer.afterLoad(); - if (Game1.IsServer || Game1.IsMultiplayer || Game1.IsClient) { throw new Exception("Can't run Revitalize in multiplayer due to lack of current support!"); } + Serializer.afterLoad(); - foreach(var v in ObjectGroups) - { - foreach(var obj in v.Value.objects.Values) - { - (obj as CustomObject).replaceAfterLoad(); - } - } - - foreach(GameLocation loc in Game1.locations) - { - foreach(StardewValley.Object c in loc.Objects.Values) - { - if (c is Chest) - { - List toRemove=new List(); - List toAdd=new List(); - foreach (Item o in (c as Chest).items) - { - if (o is Chest && o.Name != "Chest") - { - ModCore.log("Found a custom object in a chest!"); - string jsonString = o.Name; - string guidName = jsonString.Split(new string[] { "GUID=" },StringSplitOptions.None)[1]; - ModCore.log(jsonString); - string type = jsonString.Split('|')[2]; - Item I=(Item)Serializer.DeserializeGUID(guidName, Type.GetType(type)); - - if(I is MultiTiledObject) - { - (I as MultiTiledObject).recreate(); - } - - toAdd.Add(I); - toRemove.Add(o); - //Item i = Serializer.DeserializeFromJSONString(jsonString); - //ModCore.log("Deserialized item is: "+i.Name); - } - } - - foreach(Item i in toRemove) - { - (c as Chest).items.Remove(i); - } - foreach(Item I in toAdd) - { - (c as Chest).items.Add(I); - } - } - else if(c is StorageFurnitureTile) - { - foreach (Item o in (c as StorageFurnitureTile).info.inventory.items) - { - if (o is Chest&& o.Name != "Chest") - { - ModCore.log("Found a custom object in a chest!"); - } - } - } - } - } // Game1.player.addItemToInventory(GetObjectFromPool("Omegasis.BigTiledTest")); Game1.player.addItemToInventory(ObjectManager.getChair("Omegasis.Revitalize.Furniture.Chairs.OakChair")); @@ -465,11 +403,11 @@ namespace Revitalize */ //Game1.player.addItemToInventory(ObjectManager.resources.ores["Test"].getOne()); - - Game1.player.addItemToInventory(ObjectManager.resources.getOre("Tin",19)); + + Game1.player.addItemToInventory(ObjectManager.resources.getOre("Tin", 19)); //Ore tin = ObjectManager.resources.getOre("Tin", 19); - + //ModCore.log("Tin sells for: " + tin.sellToStorePrice()); //ObjectManager.resources.spawnOreVein("Omegasis.Revitalize.Resources.Ore.Test", new Vector2(8, 7)); @@ -496,7 +434,7 @@ namespace Revitalize /// public static void log(object message) { - ModMonitor.Log(message.ToString()+" "+getFileDebugInfo()); + ModMonitor.Log(message.ToString() + " " + getFileDebugInfo()); } public static string getFileDebugInfo()