Better saving for objects but need a way to delete them.

This commit is contained in:
Joshua Navarro 2019-01-10 23:24:55 -08:00
parent 77fec4be31
commit 08847c6bb1
3 changed files with 28 additions and 9 deletions

View File

@ -122,12 +122,12 @@ namespace Revitalize.Framework.Objects
//do same container creation logic in multitiled object //do same container creation logic in multitiled object
MultiTiledComponent self = Revitalize.ModCore.Serializer.Deserialize<MultiTiledComponent>(Path.Combine(Revitalize.ModCore.ModHelper.DirectoryPath, additionalSaveData["GUID"] + ".json")); MultiTiledComponent self = Revitalize.ModCore.Serializer.DeserializeGUID<MultiTiledComponent>( additionalSaveData["GUID"]);
if (!Revitalize.ModCore.ObjectGroups.ContainsKey(additionalSaveData["ParentGUID"])) if (!Revitalize.ModCore.ObjectGroups.ContainsKey(additionalSaveData["ParentGUID"]))
{ {
//Get new container //Get new container
MultiTiledObject obj = (MultiTiledObject)Revitalize.ModCore.Serializer.Deserialize<MultiTiledObject>(Path.Combine(Revitalize.ModCore.ModHelper.DirectoryPath, additionalSaveData["ParentGUID"] + ".json")); 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!!!!");
@ -178,7 +178,7 @@ namespace Revitalize.Framework.Objects
saveData.Add("ParentGUID", this.containerObject.guid.ToString()); saveData.Add("ParentGUID", this.containerObject.guid.ToString());
saveData.Add("GUID", this.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; return saveData;

View File

@ -212,9 +212,8 @@ namespace Revitalize.Framework.Objects
MultiTiledObject obj = (MultiTiledObject)Revitalize.ModCore.Serializer.Deserialize<MultiTiledObject>(Path.Combine(Revitalize.ModCore.ModHelper.DirectoryPath, additionalSaveData["GUID"] + ".json")); MultiTiledObject obj = (MultiTiledObject)Revitalize.ModCore.Serializer.DeserializeGUID<MultiTiledObject>(additionalSaveData["GUID"]);
Revitalize.ModCore.log("OK I SUPPOSE");
Dictionary<Vector2, Guid> guids = new Dictionary<Vector2, Guid>(); Dictionary<Vector2, Guid> guids = new Dictionary<Vector2, Guid>();
@ -226,8 +225,8 @@ namespace Revitalize.Framework.Objects
foreach(KeyValuePair<Vector2,Guid> pair in guids) foreach(KeyValuePair<Vector2,Guid> pair in guids)
{ {
obj.childrenGuids.Remove(pair.Key); obj.childrenGuids.Remove(pair.Key);
Revitalize.ModCore.log("DESERIALIZE: " + pair.Value.ToString()); //Revitalize.ModCore.log("DESERIALIZE: " + pair.Value.ToString());
MultiTiledComponent component= (MultiTiledComponent)Revitalize.ModCore.Serializer.Deserialize<MultiTiledComponent>(Path.Combine(Revitalize.ModCore.ModHelper.DirectoryPath, pair.Value + ".json")); MultiTiledComponent component= Revitalize.ModCore.Serializer.DeserializeGUID<MultiTiledComponent>(pair.Value.ToString());
component.InitNetFields(); component.InitNetFields();
obj.addComponent(pair.Key, component); obj.addComponent(pair.Key, component);
@ -254,7 +253,7 @@ namespace Revitalize.Framework.Objects
Dictionary<string,string> saveData= base.getAdditionalSaveData(); Dictionary<string,string> saveData= base.getAdditionalSaveData();
saveData.Add("GUID", this.guid.ToString()); 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; return saveData;
} }

View File

@ -6,6 +6,7 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Newtonsoft.Json; using Newtonsoft.Json;
using Revitalize.Framework.Utilities.Serialization.ContractResolvers; using Revitalize.Framework.Utilities.Serialization.ContractResolvers;
using StardewValley;
namespace Revitalize.Framework.Utilities namespace Revitalize.Framework.Utilities
{ {
@ -96,5 +97,24 @@ namespace Revitalize.Framework.Utilities
this.serializer.Serialize(writer, o); 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<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);
}
} }
} }