From 50577ea889719b1c7cf14542636554c0ef14b1a4 Mon Sep 17 00:00:00 2001 From: Joshua Navarro Date: Sat, 12 Jan 2019 10:40:47 -0800 Subject: [PATCH] Updated Serializer.cs to delete old files. --- .../Framework/Objects/MultiTiledComponent.cs | 13 +- .../Framework/Objects/MultiTiledObject.cs | 2 +- .../Converters/Texture2DConverter.cs | 2 +- .../Utilities/Serialization/Serialization.cs | 128 +++++++++++++++++- GeneralMods/Revitalize/ModCore.cs | 13 +- 5 files changed, 139 insertions(+), 19 deletions(-) diff --git a/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs b/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs index db569573..191847a7 100644 --- a/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs +++ b/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs @@ -42,7 +42,7 @@ namespace Revitalize.Framework.Objects public override bool clicked(Farmer who) { - ModCore.log("Clicked a multiTiledComponent!"); + //ModCore.log("Clicked a multiTiledComponent!"); this.containerObject.pickUp(); return true; //return base.clicked(who); @@ -117,12 +117,11 @@ namespace Revitalize.Framework.Objects Vector2 offsetKey = new Vector2(Convert.ToInt32(additionalSaveData["offsetKeyX"]), Convert.ToInt32(additionalSaveData["offsetKeyY"])); - Revitalize.ModCore.log("HELLO WORLD!"); - //do same container creation logic in multitiled object MultiTiledComponent self = Revitalize.ModCore.Serializer.DeserializeGUID( additionalSaveData["GUID"]); + if (self == null) return null; if (!Revitalize.ModCore.ObjectGroups.ContainsKey(additionalSaveData["ParentGUID"])) { @@ -130,14 +129,14 @@ namespace Revitalize.Framework.Objects MultiTiledObject obj = (MultiTiledObject)Revitalize.ModCore.Serializer.DeserializeGUID(additionalSaveData["ParentGUID"]); self.containerObject = obj; obj.addComponent(offsetKey, self); - Revitalize.ModCore.log("ADD IN AN OBJECT!!!!"); - Revitalize.ModCore.ObjectGroups.Add(additionalSaveData["GUID"], (MultiTiledObject)obj); + //Revitalize.ModCore.log("ADD IN AN OBJECT!!!!"); + Revitalize.ModCore.ObjectGroups.Add(additionalSaveData["ParentGUID"], (MultiTiledObject)obj); } else { - self.containerObject = Revitalize.ModCore.ObjectGroups[additionalSaveData["GUID"]]; + self.containerObject = Revitalize.ModCore.ObjectGroups[additionalSaveData["ParentGUID"]]; Revitalize.ModCore.ObjectGroups[additionalSaveData["GUID"]].addComponent(offsetKey, self); - Revitalize.ModCore.log("READD AN OBJECT!!!!"); + //Revitalize.ModCore.log("READD AN OBJECT!!!!"); } return (ICustomObject)self; diff --git a/GeneralMods/Revitalize/Framework/Objects/MultiTiledObject.cs b/GeneralMods/Revitalize/Framework/Objects/MultiTiledObject.cs index 6a7f6a37..9670abeb 100644 --- a/GeneralMods/Revitalize/Framework/Objects/MultiTiledObject.cs +++ b/GeneralMods/Revitalize/Framework/Objects/MultiTiledObject.cs @@ -213,7 +213,7 @@ namespace Revitalize.Framework.Objects MultiTiledObject obj = (MultiTiledObject)Revitalize.ModCore.Serializer.DeserializeGUID(additionalSaveData["GUID"]); - + if (obj == null) return null; Dictionary guids = new Dictionary(); diff --git a/GeneralMods/Revitalize/Framework/Utilities/Serialization/Converters/Texture2DConverter.cs b/GeneralMods/Revitalize/Framework/Utilities/Serialization/Converters/Texture2DConverter.cs index 7b711f16..06da37c4 100644 --- a/GeneralMods/Revitalize/Framework/Utilities/Serialization/Converters/Texture2DConverter.cs +++ b/GeneralMods/Revitalize/Framework/Utilities/Serialization/Converters/Texture2DConverter.cs @@ -24,7 +24,7 @@ namespace Revitalize.Framework.Utilities.Serialization.Converters public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { string textureName = reader.Value as string; - ModCore.log(textureName); + //ModCore.log(textureName); string[] names = textureName.Split('.'); if (names.Length == 0) return null; diff --git a/GeneralMods/Revitalize/Framework/Utilities/Serialization/Serialization.cs b/GeneralMods/Revitalize/Framework/Utilities/Serialization/Serialization.cs index 6cf6d0ac..6e4744c7 100644 --- a/GeneralMods/Revitalize/Framework/Utilities/Serialization/Serialization.cs +++ b/GeneralMods/Revitalize/Framework/Utilities/Serialization/Serialization.cs @@ -19,6 +19,14 @@ namespace Revitalize.Framework.Utilities { private JsonSerializer serializer; + /// + /// All files to be cleaned up after loading. + /// + private Dictionary> filesToDelete = new Dictionary>(); + + + + /// /// Constructor. /// @@ -36,12 +44,86 @@ namespace Revitalize.Framework.Utilities this.addConverter(new Framework.Utilities.Serialization.Converters.ItemCoverter()); //this.addConverter(new Framework.Utilities.Serialization.Converters.NetFieldConverter()); //this.addConverter(new Framework.Utilities.Serialization.Converters.Vector2Converter()); + + gatherAllFilesForCleanup(); + + } + + /// + /// Process all the save data for objects to be deleted by this mod. + /// + private void gatherAllFilesForCleanup() + { + + string[] directories = Directory.GetDirectories(Path.Combine(Revitalize.ModCore.ModHelper.DirectoryPath, "SaveData")); + foreach (string playerData in directories) + { + string objectPath = Path.Combine(playerData, "SavedObjectInformation"); + string[] objectFiles = Directory.GetFiles(objectPath); + foreach (string file in objectFiles) + { + string playerName = new DirectoryInfo(objectPath).Parent.Name; + if (this.filesToDelete.ContainsKey(playerName)){ + this.filesToDelete[playerName].Add(file); + //Revitalize.ModCore.log("Added File: " + file); + } + else + { + this.filesToDelete.Add(playerName, new List()); + //Revitalize.ModCore.log("Added Player Key: " + playerName); + this.filesToDelete[playerName].Add(file); + //Revitalize.ModCore.log("Added File: " + file); + } + + } + } + } + + /// + /// Called after load to deal with internal file cleanUp + /// + public void afterLoad() + { + deleteAllUnusedFiles(); + } + + /// + /// Removes the file from all files that will be deleted. + /// + /// + /// + private void removeFileFromDeletion(string playerDirectory, string fileName) + { + if (this.filesToDelete.ContainsKey(playerDirectory)) + { + //Revitalize.ModCore.log("Removing from deletion: " + fileName); + this.filesToDelete[playerDirectory].Remove(fileName); + } + else + { + //Revitalize.ModCore.log("Found key: " + playerDirectory); + //Revitalize.ModCore.log("Found file: " + fileName); + } + } + + /// + /// Deletes unused object data. + /// + private void deleteAllUnusedFiles() + { + foreach(KeyValuePair> pair in this.filesToDelete) + { + foreach (string file in pair.Value) + { + File.Delete(file); + } + } } /// /// Adds a new converter to the json serializer. /// - /// + /// The type of json converter to add to the Serializer. public void addConverter(JsonConverter converter) { this.serializer.Converters.Add(converter); @@ -51,9 +133,9 @@ namespace Revitalize.Framework.Utilities /// /// Deserializes an object from a .json file. /// - /// - /// - /// + /// The type of object to deserialize into. + /// The path to the file. + /// An object of specified type T. public T Deserialize(string p) { string json = ""; @@ -69,6 +151,12 @@ namespace Revitalize.Framework.Utilities } } + /// + /// Deserializes an object from a .json file. + /// + /// The type of object to deserialize into. + /// The path to the file. + /// An object of specified type T. public object Deserialize(string p,Type T) { string json = ""; @@ -98,6 +186,11 @@ namespace Revitalize.Framework.Utilities } } + /// + /// Serialize a data structure into an file. + /// + /// + /// 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"); @@ -105,16 +198,41 @@ namespace Revitalize.Framework.Utilities Serialize(path, obj); } + /// + /// Deserialze a file into it's proper data structure. + /// + /// The type of data structure to deserialze to. + /// The name of the file to deserialize from. + /// A data structure object deserialize from a json string in a file. 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"); + removeFileFromDeletion((Game1.player.name + "_" + Game1.player.uniqueMultiplayerID), path); return Deserialize(path, T); } + /// + /// Deserialze a file into it's proper data structure. + /// + /// The type of data structure to deserialze to. + /// The name of the file to deserialize from. + /// A data structure object deserialize from a json string in a file. 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); + removeFileFromDeletion((Game1.player.name + "_" + Game1.player.uniqueMultiplayerID),path); + if (File.Exists(path)) + { + + return Deserialize(path); + } + else + { + return default(T); + } } + + + } } diff --git a/GeneralMods/Revitalize/ModCore.cs b/GeneralMods/Revitalize/ModCore.cs index ee7f1deb..d14bd4d1 100644 --- a/GeneralMods/Revitalize/ModCore.cs +++ b/GeneralMods/Revitalize/ModCore.cs @@ -20,8 +20,9 @@ using StardewValley.Objects; namespace Revitalize { // TODO: - //Need to find a way to recreate objects again. - //Make guid object list to keep track of container objects on rebuild. Container objects have guid, on getAdditionalSaveData, store it. On rebuild keep a list, get a reference to container object, clear those objects, and reset them as we are rebuilting multiTiledComponents. + // Find a way to delete unnecessary files in SaveData/Player/SaveObjectInformation + // + // // // -Multiple Lights On Object // -Illumination Colors @@ -275,18 +276,20 @@ namespace Revitalize private void GameLoop_SaveLoaded(object sender, StardewModdingAPI.Events.SaveLoadedEventArgs e) { + Serializer.afterLoad(); if (Game1.IsServer || Game1.IsMultiplayer || Game1.IsClient) { - //throw new Exception("Can't run Revitalize in multiplayer due to lack of current support!"); + throw new Exception("Can't run Revitalize in multiplayer due to lack of current support!"); } Game1.player.addItemToInventory(customObjects["Omegasis.BigTiledTest"]); + /* StardewValley.Tools.Axe axe = new StardewValley.Tools.Axe(); Serializer.Serialize(Path.Combine(this.Helper.DirectoryPath, "AXE.json"), axe); axe =(StardewValley.Tools.Axe)Serializer.Deserialize(Path.Combine(this.Helper.DirectoryPath, "AXE.json"),typeof(StardewValley.Tools.Axe)); - Game1.player.addItemToInventory(axe); - + //Game1.player.addItemToInventory(axe); + */ }