Test for Serializer working on StardewValley items works. Work can continue.

This commit is contained in:
Joshua Navarro 2019-01-10 18:32:35 -08:00
parent 1e9d253281
commit d4d62123b0
3 changed files with 41 additions and 2 deletions

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -10,6 +11,10 @@ using StardewValley;
namespace Revitalize.Framework.Utilities.Serialization.Converters namespace Revitalize.Framework.Utilities.Serialization.Converters
{ {
/// <summary>
/// TODO:
/// Add support for all vanilla SDV objects in deserialization.
/// </summary>
public class ItemCoverter:Newtonsoft.Json.JsonConverter public class ItemCoverter:Newtonsoft.Json.JsonConverter
{ {
@ -72,7 +77,20 @@ namespace Revitalize.Framework.Utilities.Serialization.Converters
string t = jo["Type"].Value<string>(); string t = jo["Type"].Value<string>();
if(t== typeof(StardewValley.Tools.Axe).FullName.ToString())
Assembly asm = typeof(StardewValley.Object).Assembly;
Type type = null;
type = asm.GetType(t);
if (type == null)
{
asm = typeof(Revitalize.ModCore).Assembly;
type = asm.GetType(t);
}
return JsonConvert.DeserializeObject(jo["Item"].ToString(),type, this.settings);
/*
if (t== typeof(StardewValley.Tools.Axe).FullName.ToString())
{ {
Revitalize.ModCore.log("DESERIALIZE AXE!!!"); Revitalize.ModCore.log("DESERIALIZE AXE!!!");
//return jo["Item"].Value<StardewValley.Tools.Axe>(); //return jo["Item"].Value<StardewValley.Tools.Axe>();
@ -100,6 +118,8 @@ namespace Revitalize.Framework.Utilities.Serialization.Converters
throw new NotImplementedException("CANT DESERIALIZE: " + t.ToString()); throw new NotImplementedException("CANT DESERIALIZE: " + t.ToString());
} }
*/
} }

View File

@ -66,6 +66,21 @@ namespace Revitalize.Framework.Utilities
} }
} }
public object Deserialize(string p,Type T)
{
string json = "";
foreach (string line in File.ReadLines(p))
{
json += line;
}
using (StreamReader sw = new StreamReader(p))
using (JsonReader reader = new JsonTextReader(sw))
{
object obj = this.serializer.Deserialize(reader,T);
return obj;
}
}
/// <summary> /// <summary>
/// Serializes an object to a .json file. /// Serializes an object to a .json file.
/// </summary> /// </summary>

View File

@ -282,6 +282,10 @@ namespace Revitalize
} }
Game1.player.addItemToInventory(customObjects["Omegasis.BigTiledTest"]); 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);
} }