From 08847c6bb1c4c3abf645f3de3c638e0deb3977bf Mon Sep 17 00:00:00 2001 From: Joshua Navarro Date: Thu, 10 Jan 2019 23:24:55 -0800 Subject: [PATCH] Better saving for objects but need a way to delete them. --- .../Framework/Objects/MultiTiledComponent.cs | 6 +++--- .../Framework/Objects/MultiTiledObject.cs | 11 +++++----- .../Utilities/Serialization/Serialization.cs | 20 +++++++++++++++++++ 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs b/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs index 07a4f579..db569573 100644 --- a/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs +++ b/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs @@ -122,12 +122,12 @@ namespace Revitalize.Framework.Objects //do same container creation logic in multitiled object - MultiTiledComponent self = Revitalize.ModCore.Serializer.Deserialize(Path.Combine(Revitalize.ModCore.ModHelper.DirectoryPath, additionalSaveData["GUID"] + ".json")); + MultiTiledComponent self = Revitalize.ModCore.Serializer.DeserializeGUID( additionalSaveData["GUID"]); if (!Revitalize.ModCore.ObjectGroups.ContainsKey(additionalSaveData["ParentGUID"])) { //Get new container - MultiTiledObject obj = (MultiTiledObject)Revitalize.ModCore.Serializer.Deserialize(Path.Combine(Revitalize.ModCore.ModHelper.DirectoryPath, additionalSaveData["ParentGUID"] + ".json")); + MultiTiledObject obj = (MultiTiledObject)Revitalize.ModCore.Serializer.DeserializeGUID(additionalSaveData["ParentGUID"]); self.containerObject = obj; obj.addComponent(offsetKey, self); Revitalize.ModCore.log("ADD IN AN OBJECT!!!!"); @@ -178,7 +178,7 @@ namespace Revitalize.Framework.Objects saveData.Add("ParentGUID", this.containerObject.guid.ToString()); saveData.Add("GUID", this.guid.ToString()); - Revitalize.ModCore.Serializer.Serialize(Path.Combine(Revitalize.ModCore.ModHelper.DirectoryPath, this.containerObject.childrenGuids[this.offsetKey].ToString() + ".json"),this); + Revitalize.ModCore.Serializer.SerializeGUID(this.containerObject.childrenGuids[this.offsetKey].ToString(),this); return saveData; diff --git a/GeneralMods/Revitalize/Framework/Objects/MultiTiledObject.cs b/GeneralMods/Revitalize/Framework/Objects/MultiTiledObject.cs index ad9b2404..6a7f6a37 100644 --- a/GeneralMods/Revitalize/Framework/Objects/MultiTiledObject.cs +++ b/GeneralMods/Revitalize/Framework/Objects/MultiTiledObject.cs @@ -212,9 +212,8 @@ namespace Revitalize.Framework.Objects - MultiTiledObject obj = (MultiTiledObject)Revitalize.ModCore.Serializer.Deserialize(Path.Combine(Revitalize.ModCore.ModHelper.DirectoryPath, additionalSaveData["GUID"] + ".json")); - - Revitalize.ModCore.log("OK I SUPPOSE"); + MultiTiledObject obj = (MultiTiledObject)Revitalize.ModCore.Serializer.DeserializeGUID(additionalSaveData["GUID"]); + Dictionary guids = new Dictionary(); @@ -226,8 +225,8 @@ namespace Revitalize.Framework.Objects foreach(KeyValuePair pair in guids) { obj.childrenGuids.Remove(pair.Key); - Revitalize.ModCore.log("DESERIALIZE: " + pair.Value.ToString()); - MultiTiledComponent component= (MultiTiledComponent)Revitalize.ModCore.Serializer.Deserialize(Path.Combine(Revitalize.ModCore.ModHelper.DirectoryPath, pair.Value + ".json")); + //Revitalize.ModCore.log("DESERIALIZE: " + pair.Value.ToString()); + MultiTiledComponent component= Revitalize.ModCore.Serializer.DeserializeGUID(pair.Value.ToString()); component.InitNetFields(); obj.addComponent(pair.Key, component); @@ -254,7 +253,7 @@ namespace Revitalize.Framework.Objects Dictionary saveData= base.getAdditionalSaveData(); saveData.Add("GUID", this.guid.ToString()); - Revitalize.ModCore.Serializer.Serialize(Path.Combine(Revitalize.ModCore.ModHelper.DirectoryPath, this.guid.ToString() + ".json"), this); + Revitalize.ModCore.Serializer.SerializeGUID(this.guid.ToString(), this); return saveData; } diff --git a/GeneralMods/Revitalize/Framework/Utilities/Serialization/Serialization.cs b/GeneralMods/Revitalize/Framework/Utilities/Serialization/Serialization.cs index 7742cdcb..6cf6d0ac 100644 --- a/GeneralMods/Revitalize/Framework/Utilities/Serialization/Serialization.cs +++ b/GeneralMods/Revitalize/Framework/Utilities/Serialization/Serialization.cs @@ -6,6 +6,7 @@ using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; using Revitalize.Framework.Utilities.Serialization.ContractResolvers; +using StardewValley; namespace Revitalize.Framework.Utilities { @@ -96,5 +97,24 @@ namespace Revitalize.Framework.Utilities this.serializer.Serialize(writer, o); } } + + public void SerializeGUID(string fileName,object obj) + { + string path = Path.Combine(Revitalize.ModCore.ModHelper.DirectoryPath, "SaveData", Game1.player.name + "_" + Game1.player.uniqueMultiplayerID, "SavedObjectInformation", fileName + ".json"); + Directory.CreateDirectory(Path.GetDirectoryName(path)); + Serialize(path, obj); + } + + public object DeserializeGUID(string fileName,Type T) + { + string path=Path.Combine(Revitalize.ModCore.ModHelper.DirectoryPath, "SaveData", Game1.player.name + "_" + Game1.player.uniqueMultiplayerID, "SavedObjectInformation", fileName + ".json"); + return Deserialize(path, T); + } + + public T DeserializeGUID(string fileName) + { + string path = Path.Combine(Revitalize.ModCore.ModHelper.DirectoryPath, "SaveData", Game1.player.name + "_" + Game1.player.uniqueMultiplayerID, "SavedObjectInformation", fileName + ".json"); + return Deserialize(path); + } } }