Added in caching and looking through all aseemblies.
This commit is contained in:
parent
d4d62123b0
commit
77fec4be31
|
@ -11,12 +11,9 @@ 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
|
||||||
{
|
{
|
||||||
|
public static Dictionary<string, Type> AllTypes = new Dictionary<string, Type>();
|
||||||
|
|
||||||
JsonSerializerSettings settings;
|
JsonSerializerSettings settings;
|
||||||
public ItemCoverter()
|
public ItemCoverter()
|
||||||
|
@ -77,6 +74,12 @@ namespace Revitalize.Framework.Utilities.Serialization.Converters
|
||||||
|
|
||||||
string t = jo["Type"].Value<string>();
|
string t = jo["Type"].Value<string>();
|
||||||
|
|
||||||
|
//See if the type has already been cached and if so return it for deserialization.
|
||||||
|
if (AllTypes.ContainsKey(t))
|
||||||
|
{
|
||||||
|
|
||||||
|
return JsonConvert.DeserializeObject(jo["Item"].ToString(), AllTypes[t], this.settings);
|
||||||
|
}
|
||||||
|
|
||||||
Assembly asm = typeof(StardewValley.Object).Assembly;
|
Assembly asm = typeof(StardewValley.Object).Assembly;
|
||||||
Type type = null;
|
Type type = null;
|
||||||
|
@ -88,6 +91,25 @@ namespace Revitalize.Framework.Utilities.Serialization.Converters
|
||||||
asm = typeof(Revitalize.ModCore).Assembly;
|
asm = typeof(Revitalize.ModCore).Assembly;
|
||||||
type = asm.GetType(t);
|
type = asm.GetType(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (type == null)
|
||||||
|
{
|
||||||
|
foreach(Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
|
||||||
|
{
|
||||||
|
asm = assembly;
|
||||||
|
type = asm.GetType(t);
|
||||||
|
if (t != null) break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type == null)
|
||||||
|
{
|
||||||
|
throw new Exception("Unsupported type found when Deserializing Unsure what to do so we can;t deserialize this thing!: " + t);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Cache the newly found type.
|
||||||
|
AllTypes.Add(t, type);
|
||||||
|
|
||||||
return JsonConvert.DeserializeObject(jo["Item"].ToString(),type, this.settings);
|
return JsonConvert.DeserializeObject(jo["Item"].ToString(),type, this.settings);
|
||||||
/*
|
/*
|
||||||
if (t== typeof(StardewValley.Tools.Axe).FullName.ToString())
|
if (t== typeof(StardewValley.Tools.Axe).FullName.ToString())
|
||||||
|
|
|
@ -11,6 +11,8 @@ namespace Revitalize.Framework.Utilities
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Handles serialization of all objects in existence.
|
/// Handles serialization of all objects in existence.
|
||||||
|
///
|
||||||
|
/// TODO: Make JConvert that has same settings to implement a toJSon string obj
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class Serializer
|
public class Serializer
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue