2016-03-27 06:27:07 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using Newtonsoft.Json.Serialization;
|
|
|
|
|
using StardewModdingAPI.Inheritance;
|
2016-03-27 13:09:09 +08:00
|
|
|
|
using Object = StardewValley.Object;
|
2016-03-27 06:27:07 +08:00
|
|
|
|
|
|
|
|
|
namespace StardewModdingAPI
|
|
|
|
|
{
|
2016-03-27 13:09:09 +08:00
|
|
|
|
internal class JsonResolver : DefaultContractResolver
|
2016-03-27 06:27:07 +08:00
|
|
|
|
{
|
|
|
|
|
protected override JsonContract CreateContract(Type objectType)
|
|
|
|
|
{
|
2016-03-27 13:09:09 +08:00
|
|
|
|
if (objectType == typeof (Rectangle) || objectType == typeof (Rectangle?))
|
2016-03-27 06:27:07 +08:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("FOUND A RECT");
|
2016-03-27 13:09:09 +08:00
|
|
|
|
JsonContract contract = CreateObjectContract(objectType);
|
2016-03-27 06:27:07 +08:00
|
|
|
|
contract.Converter = new RectangleConverter();
|
|
|
|
|
return contract;
|
|
|
|
|
}
|
2016-03-27 13:09:09 +08:00
|
|
|
|
if (objectType == typeof (Object))
|
2016-03-27 06:27:07 +08:00
|
|
|
|
{
|
2016-03-27 13:09:09 +08:00
|
|
|
|
Log.AsyncY("FOUND AN OBJECT");
|
|
|
|
|
JsonContract contract = CreateObjectContract(objectType);
|
2016-03-27 06:27:07 +08:00
|
|
|
|
contract.Converter = new ObjectConverter();
|
|
|
|
|
return contract;
|
|
|
|
|
}
|
|
|
|
|
return base.CreateContract(objectType);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ObjectConverter : JsonConverter
|
|
|
|
|
{
|
|
|
|
|
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
|
|
|
|
{
|
2016-03-27 13:09:09 +08:00
|
|
|
|
Log.AsyncY("TRYING TO WRITE");
|
|
|
|
|
var obj = (Object) value;
|
|
|
|
|
Log.AsyncY("TRYING TO WRITE");
|
2016-03-27 06:27:07 +08:00
|
|
|
|
|
|
|
|
|
var jObject = GetObject(obj);
|
2016-03-27 13:09:09 +08:00
|
|
|
|
Log.AsyncY("TRYING TO WRITE");
|
2016-03-27 06:27:07 +08:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-03-27 13:09:09 +08:00
|
|
|
|
Log.AsyncY(jObject.ToString());
|
2016-03-27 06:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2016-03-27 13:09:09 +08:00
|
|
|
|
Log.AsyncR(ex);
|
2016-03-27 06:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Console.ReadKey();
|
|
|
|
|
|
|
|
|
|
jObject.WriteTo(writer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
|
|
|
|
{
|
|
|
|
|
var jObject = JObject.Load(reader);
|
|
|
|
|
|
|
|
|
|
return GetObject(jObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool CanConvert(Type objectType)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-27 13:09:09 +08:00
|
|
|
|
protected static JObject GetObject(Object o)
|
2016-03-27 06:27:07 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var parentSheetIndex = o.parentSheetIndex;
|
|
|
|
|
var stack = o.stack;
|
|
|
|
|
var isRecipe = o.isRecipe;
|
|
|
|
|
var price = o.price;
|
|
|
|
|
var quality = o.quality;
|
|
|
|
|
|
|
|
|
|
var oo = new SBareObject(parentSheetIndex, stack, isRecipe, price, quality);
|
2016-03-27 13:09:09 +08:00
|
|
|
|
Log.AsyncG(JsonConvert.SerializeObject(oo));
|
2016-03-27 06:27:07 +08:00
|
|
|
|
return JObject.FromObject(oo);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2016-03-27 13:09:09 +08:00
|
|
|
|
Log.AsyncR(ex);
|
2016-03-27 06:27:07 +08:00
|
|
|
|
Console.ReadKey();
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-27 13:09:09 +08:00
|
|
|
|
protected static Object GetObject(JObject jObject)
|
2016-03-27 06:27:07 +08:00
|
|
|
|
{
|
2016-03-27 13:09:09 +08:00
|
|
|
|
var parentSheetIndex = GetTokenValue<object>(jObject, "parentSheetIndex") as int?;
|
|
|
|
|
var stack = GetTokenValue<object>(jObject, "parentSheetIndex") as int?;
|
|
|
|
|
var isRecipe = GetTokenValue<object>(jObject, "parentSheetIndex") as bool?;
|
|
|
|
|
var price = GetTokenValue<object>(jObject, "parentSheetIndex") as int?;
|
|
|
|
|
var quality = GetTokenValue<object>(jObject, "parentSheetIndex") as int?;
|
2016-03-27 06:27:07 +08:00
|
|
|
|
|
2016-03-27 13:09:09 +08:00
|
|
|
|
return new Object(parentSheetIndex ?? 0, stack ?? 0, isRecipe ?? false, price ?? -1, quality ?? 0);
|
2016-03-27 06:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-27 13:09:09 +08:00
|
|
|
|
protected static Object GetObject(JToken jToken)
|
2016-03-27 06:27:07 +08:00
|
|
|
|
{
|
|
|
|
|
var jObject = JObject.FromObject(jToken);
|
|
|
|
|
|
|
|
|
|
return GetObject(jObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected static T GetTokenValue<T>(JObject jObject, string tokenName) where T : class
|
|
|
|
|
{
|
|
|
|
|
JToken jToken;
|
|
|
|
|
jObject.TryGetValue(tokenName, StringComparison.InvariantCultureIgnoreCase, out jToken);
|
|
|
|
|
return jToken as T;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class RectangleConverter : JsonConverter
|
|
|
|
|
{
|
|
|
|
|
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
|
|
|
|
{
|
2016-03-27 13:09:09 +08:00
|
|
|
|
var rectangle = (Rectangle) value;
|
2016-03-27 06:27:07 +08:00
|
|
|
|
|
|
|
|
|
var jObject = GetObject(rectangle);
|
|
|
|
|
|
|
|
|
|
jObject.WriteTo(writer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(reader.ReadAsString());
|
|
|
|
|
var jObject = JObject.Load(reader);
|
|
|
|
|
|
|
|
|
|
return GetRectangle(jObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool CanConvert(Type objectType)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected static JObject GetObject(Rectangle rectangle)
|
|
|
|
|
{
|
|
|
|
|
var x = rectangle.X;
|
|
|
|
|
var y = rectangle.Y;
|
|
|
|
|
var width = rectangle.Width;
|
|
|
|
|
var height = rectangle.Height;
|
|
|
|
|
|
2016-03-27 13:09:09 +08:00
|
|
|
|
return JObject.FromObject(new {x, y, width, height});
|
2016-03-27 06:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected static Rectangle GetRectangle(JObject jObject)
|
|
|
|
|
{
|
|
|
|
|
var x = GetTokenValue(jObject, "x") ?? 0;
|
|
|
|
|
var y = GetTokenValue(jObject, "y") ?? 0;
|
|
|
|
|
var width = GetTokenValue(jObject, "width") ?? 0;
|
|
|
|
|
var height = GetTokenValue(jObject, "height") ?? 0;
|
|
|
|
|
|
|
|
|
|
return new Rectangle(x, y, width, height);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected static Rectangle GetRectangle(JToken jToken)
|
|
|
|
|
{
|
|
|
|
|
var jObject = JObject.FromObject(jToken);
|
|
|
|
|
|
|
|
|
|
return GetRectangle(jObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected static int? GetTokenValue(JObject jObject, string tokenName)
|
|
|
|
|
{
|
|
|
|
|
JToken jToken;
|
2016-03-27 13:09:09 +08:00
|
|
|
|
return jObject.TryGetValue(tokenName, StringComparison.InvariantCultureIgnoreCase, out jToken) ? (int) jToken : (int?) null;
|
2016-03-27 06:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class RectangleListConverter : RectangleConverter
|
|
|
|
|
{
|
|
|
|
|
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
|
|
|
|
{
|
2016-03-27 13:09:09 +08:00
|
|
|
|
var rectangleList = (IList<Rectangle>) value;
|
2016-03-27 06:27:07 +08:00
|
|
|
|
|
|
|
|
|
var jArray = new JArray();
|
|
|
|
|
|
|
|
|
|
foreach (var rectangle in rectangleList)
|
|
|
|
|
{
|
|
|
|
|
jArray.Add(GetObject(rectangle));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jArray.WriteTo(writer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
|
|
|
|
{
|
|
|
|
|
var rectangleList = new List<Rectangle>();
|
|
|
|
|
|
|
|
|
|
var jArray = JArray.Load(reader);
|
|
|
|
|
|
|
|
|
|
foreach (var jToken in jArray)
|
|
|
|
|
{
|
|
|
|
|
rectangleList.Add(GetRectangle(jToken));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rectangleList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool CanConvert(Type objectType)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-27 13:09:09 +08:00
|
|
|
|
}
|