Updated Serializer.cs to delete old files.

This commit is contained in:
Joshua Navarro 2019-01-12 10:40:47 -08:00
parent 08847c6bb1
commit 50577ea889
5 changed files with 139 additions and 19 deletions

View File

@ -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<MultiTiledComponent>( 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<MultiTiledObject>(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;

View File

@ -213,7 +213,7 @@ namespace Revitalize.Framework.Objects
MultiTiledObject obj = (MultiTiledObject)Revitalize.ModCore.Serializer.DeserializeGUID<MultiTiledObject>(additionalSaveData["GUID"]);
if (obj == null) return null;
Dictionary<Vector2, Guid> guids = new Dictionary<Vector2, Guid>();

View File

@ -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;

View File

@ -19,6 +19,14 @@ namespace Revitalize.Framework.Utilities
{
private JsonSerializer serializer;
/// <summary>
/// All files to be cleaned up after loading.
/// </summary>
private Dictionary<string, List<string>> filesToDelete = new Dictionary<string, List<string>>();
/// <summary>
/// Constructor.
/// </summary>
@ -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();
}
/// <summary>
/// Process all the save data for objects to be deleted by this mod.
/// </summary>
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<string>());
//Revitalize.ModCore.log("Added Player Key: " + playerName);
this.filesToDelete[playerName].Add(file);
//Revitalize.ModCore.log("Added File: " + file);
}
}
}
}
/// <summary>
/// Called after load to deal with internal file cleanUp
/// </summary>
public void afterLoad()
{
deleteAllUnusedFiles();
}
/// <summary>
/// Removes the file from all files that will be deleted.
/// </summary>
/// <param name="playerDirectory"></param>
/// <param name="fileName"></param>
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);
}
}
/// <summary>
/// Deletes unused object data.
/// </summary>
private void deleteAllUnusedFiles()
{
foreach(KeyValuePair<string,List<string>> pair in this.filesToDelete)
{
foreach (string file in pair.Value)
{
File.Delete(file);
}
}
}
/// <summary>
/// Adds a new converter to the json serializer.
/// </summary>
/// <param name="converter"></param>
/// <param name="converter">The type of json converter to add to the Serializer.</param>
public void addConverter(JsonConverter converter)
{
this.serializer.Converters.Add(converter);
@ -51,9 +133,9 @@ namespace Revitalize.Framework.Utilities
/// <summary>
/// Deserializes an object from a .json file.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="p"></param>
/// <returns></returns>
/// <typeparam name="T">The type of object to deserialize into.</typeparam>
/// <param name="p">The path to the file.</param>
/// <returns>An object of specified type T.</returns>
public T Deserialize<T>(string p)
{
string json = "";
@ -69,6 +151,12 @@ namespace Revitalize.Framework.Utilities
}
}
/// <summary>
/// Deserializes an object from a .json file.
/// </summary>
/// <typeparam name="T">The type of object to deserialize into.</typeparam>
/// <param name="p">The path to the file.</param>
/// <returns>An object of specified type T.</returns>
public object Deserialize(string p,Type T)
{
string json = "";
@ -98,6 +186,11 @@ namespace Revitalize.Framework.Utilities
}
}
/// <summary>
/// Serialize a data structure into an file.
/// </summary>
/// <param name="fileName"></param>
/// <param name="obj"></param>
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);
}
/// <summary>
/// Deserialze a file into it's proper data structure.
/// </summary>
/// <typeparam name="T">The type of data structure to deserialze to.</typeparam>
/// <param name="fileName">The name of the file to deserialize from.</param>
/// <returns>A data structure object deserialize from a json string in a file.</returns>
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);
}
/// <summary>
/// Deserialze a file into it's proper data structure.
/// </summary>
/// <typeparam name="T">The type of data structure to deserialze to.</typeparam>
/// <param name="fileName">The name of the file to deserialize from.</param>
/// <returns>A data structure object deserialize from a json string in a file.</returns>
public T DeserializeGUID<T>(string fileName)
{
string path = Path.Combine(Revitalize.ModCore.ModHelper.DirectoryPath, "SaveData", Game1.player.name + "_" + Game1.player.uniqueMultiplayerID, "SavedObjectInformation", fileName + ".json");
return Deserialize<T>(path);
removeFileFromDeletion((Game1.player.name + "_" + Game1.player.uniqueMultiplayerID),path);
if (File.Exists(path))
{
return Deserialize<T>(path);
}
else
{
return default(T);
}
}
}
}

View File

@ -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);
*/
}