Updated Serializer.cs to delete old files.
This commit is contained in:
parent
08847c6bb1
commit
50577ea889
|
@ -42,7 +42,7 @@ namespace Revitalize.Framework.Objects
|
||||||
|
|
||||||
public override bool clicked(Farmer who)
|
public override bool clicked(Farmer who)
|
||||||
{
|
{
|
||||||
ModCore.log("Clicked a multiTiledComponent!");
|
//ModCore.log("Clicked a multiTiledComponent!");
|
||||||
this.containerObject.pickUp();
|
this.containerObject.pickUp();
|
||||||
return true;
|
return true;
|
||||||
//return base.clicked(who);
|
//return base.clicked(who);
|
||||||
|
@ -117,12 +117,11 @@ namespace Revitalize.Framework.Objects
|
||||||
Vector2 offsetKey = new Vector2(Convert.ToInt32(additionalSaveData["offsetKeyX"]), Convert.ToInt32(additionalSaveData["offsetKeyY"]));
|
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
|
//do same container creation logic in multitiled object
|
||||||
|
|
||||||
|
|
||||||
MultiTiledComponent self = Revitalize.ModCore.Serializer.DeserializeGUID<MultiTiledComponent>( additionalSaveData["GUID"]);
|
MultiTiledComponent self = Revitalize.ModCore.Serializer.DeserializeGUID<MultiTiledComponent>( additionalSaveData["GUID"]);
|
||||||
|
if (self == null) return null;
|
||||||
|
|
||||||
if (!Revitalize.ModCore.ObjectGroups.ContainsKey(additionalSaveData["ParentGUID"]))
|
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"]);
|
MultiTiledObject obj = (MultiTiledObject)Revitalize.ModCore.Serializer.DeserializeGUID<MultiTiledObject>(additionalSaveData["ParentGUID"]);
|
||||||
self.containerObject = obj;
|
self.containerObject = obj;
|
||||||
obj.addComponent(offsetKey, self);
|
obj.addComponent(offsetKey, self);
|
||||||
Revitalize.ModCore.log("ADD IN AN OBJECT!!!!");
|
//Revitalize.ModCore.log("ADD IN AN OBJECT!!!!");
|
||||||
Revitalize.ModCore.ObjectGroups.Add(additionalSaveData["GUID"], (MultiTiledObject)obj);
|
Revitalize.ModCore.ObjectGroups.Add(additionalSaveData["ParentGUID"], (MultiTiledObject)obj);
|
||||||
}
|
}
|
||||||
else
|
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.ObjectGroups[additionalSaveData["GUID"]].addComponent(offsetKey, self);
|
||||||
Revitalize.ModCore.log("READD AN OBJECT!!!!");
|
//Revitalize.ModCore.log("READD AN OBJECT!!!!");
|
||||||
}
|
}
|
||||||
|
|
||||||
return (ICustomObject)self;
|
return (ICustomObject)self;
|
||||||
|
|
|
@ -213,7 +213,7 @@ namespace Revitalize.Framework.Objects
|
||||||
|
|
||||||
|
|
||||||
MultiTiledObject obj = (MultiTiledObject)Revitalize.ModCore.Serializer.DeserializeGUID<MultiTiledObject>(additionalSaveData["GUID"]);
|
MultiTiledObject obj = (MultiTiledObject)Revitalize.ModCore.Serializer.DeserializeGUID<MultiTiledObject>(additionalSaveData["GUID"]);
|
||||||
|
if (obj == null) return null;
|
||||||
|
|
||||||
Dictionary<Vector2, Guid> guids = new Dictionary<Vector2, Guid>();
|
Dictionary<Vector2, Guid> guids = new Dictionary<Vector2, Guid>();
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,7 @@ namespace Revitalize.Framework.Utilities.Serialization.Converters
|
||||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||||
{
|
{
|
||||||
string textureName = reader.Value as string;
|
string textureName = reader.Value as string;
|
||||||
ModCore.log(textureName);
|
//ModCore.log(textureName);
|
||||||
string[] names = textureName.Split('.');
|
string[] names = textureName.Split('.');
|
||||||
if (names.Length == 0) return null;
|
if (names.Length == 0) return null;
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,14 @@ namespace Revitalize.Framework.Utilities
|
||||||
{
|
{
|
||||||
private JsonSerializer serializer;
|
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>
|
/// <summary>
|
||||||
/// Constructor.
|
/// Constructor.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -36,12 +44,86 @@ namespace Revitalize.Framework.Utilities
|
||||||
this.addConverter(new Framework.Utilities.Serialization.Converters.ItemCoverter());
|
this.addConverter(new Framework.Utilities.Serialization.Converters.ItemCoverter());
|
||||||
//this.addConverter(new Framework.Utilities.Serialization.Converters.NetFieldConverter());
|
//this.addConverter(new Framework.Utilities.Serialization.Converters.NetFieldConverter());
|
||||||
//this.addConverter(new Framework.Utilities.Serialization.Converters.Vector2Converter());
|
//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>
|
/// <summary>
|
||||||
/// Adds a new converter to the json serializer.
|
/// Adds a new converter to the json serializer.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="converter"></param>
|
/// <param name="converter">The type of json converter to add to the Serializer.</param>
|
||||||
public void addConverter(JsonConverter converter)
|
public void addConverter(JsonConverter converter)
|
||||||
{
|
{
|
||||||
this.serializer.Converters.Add(converter);
|
this.serializer.Converters.Add(converter);
|
||||||
|
@ -51,9 +133,9 @@ namespace Revitalize.Framework.Utilities
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Deserializes an object from a .json file.
|
/// Deserializes an object from a .json file.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T">The type of object to deserialize into.</typeparam>
|
||||||
/// <param name="p"></param>
|
/// <param name="p">The path to the file.</param>
|
||||||
/// <returns></returns>
|
/// <returns>An object of specified type T.</returns>
|
||||||
public T Deserialize<T>(string p)
|
public T Deserialize<T>(string p)
|
||||||
{
|
{
|
||||||
string json = "";
|
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)
|
public object Deserialize(string p,Type T)
|
||||||
{
|
{
|
||||||
string json = "";
|
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)
|
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");
|
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);
|
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)
|
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");
|
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);
|
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)
|
public T DeserializeGUID<T>(string fileName)
|
||||||
{
|
{
|
||||||
string path = Path.Combine(Revitalize.ModCore.ModHelper.DirectoryPath, "SaveData", Game1.player.name + "_" + Game1.player.uniqueMultiplayerID, "SavedObjectInformation", fileName + ".json");
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,8 +20,9 @@ using StardewValley.Objects;
|
||||||
namespace Revitalize
|
namespace Revitalize
|
||||||
{
|
{
|
||||||
// TODO:
|
// TODO:
|
||||||
//Need to find a way to recreate objects again.
|
// Find a way to delete unnecessary files in SaveData/Player/SaveObjectInformation
|
||||||
//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.
|
//
|
||||||
|
//
|
||||||
//
|
//
|
||||||
// -Multiple Lights On Object
|
// -Multiple Lights On Object
|
||||||
// -Illumination Colors
|
// -Illumination Colors
|
||||||
|
@ -275,18 +276,20 @@ namespace Revitalize
|
||||||
|
|
||||||
private void GameLoop_SaveLoaded(object sender, StardewModdingAPI.Events.SaveLoadedEventArgs e)
|
private void GameLoop_SaveLoaded(object sender, StardewModdingAPI.Events.SaveLoadedEventArgs e)
|
||||||
{
|
{
|
||||||
|
Serializer.afterLoad();
|
||||||
|
|
||||||
if (Game1.IsServer || Game1.IsMultiplayer || Game1.IsClient)
|
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"]);
|
Game1.player.addItemToInventory(customObjects["Omegasis.BigTiledTest"]);
|
||||||
|
|
||||||
|
/*
|
||||||
StardewValley.Tools.Axe axe = new StardewValley.Tools.Axe();
|
StardewValley.Tools.Axe axe = new StardewValley.Tools.Axe();
|
||||||
Serializer.Serialize(Path.Combine(this.Helper.DirectoryPath, "AXE.json"), 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));
|
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);
|
||||||
|
*/
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue