diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2016-10-31 17:15:27 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2016-10-31 17:15:27 -0400 |
commit | b6568f8d9dffca29b088b3840366ede623c142b4 (patch) | |
tree | e664482640ac48eae8b4af43517353494cbf0978 | |
parent | 7d0cd22f1ec0465c666cf0c69cbfa4da5fd3fe1a (diff) | |
download | SMAPI-b6568f8d9dffca29b088b3840366ede623c142b4.tar.gz SMAPI-b6568f8d9dffca29b088b3840366ede623c142b4.tar.bz2 SMAPI-b6568f8d9dffca29b088b3840366ede623c142b4.zip |
remove unused deserialisation logic per discussion
-rw-r--r-- | src/StardewModdingAPI/Config.cs | 17 | ||||
-rw-r--r-- | src/StardewModdingAPI/Inheritance/SBareObject.cs | 20 | ||||
-rw-r--r-- | src/StardewModdingAPI/JsonResolver.cs | 212 | ||||
-rw-r--r-- | src/StardewModdingAPI/StardewModdingAPI.csproj | 2 |
4 files changed, 7 insertions, 244 deletions
diff --git a/src/StardewModdingAPI/Config.cs b/src/StardewModdingAPI/Config.cs index c5b7beca..f3f4f22a 100644 --- a/src/StardewModdingAPI/Config.cs +++ b/src/StardewModdingAPI/Config.cs @@ -47,14 +47,11 @@ namespace StardewModdingAPI try { //try to load the config from a json blob on disk - var c = JsonConvert.DeserializeObject<T>(File.ReadAllText(ConfigLocation), new JsonSerializerSettings {ContractResolver = new JsonResolver()}); - - c.ConfigLocation = ConfigLocation; + T config = JsonConvert.DeserializeObject<T>(File.ReadAllText(this.ConfigLocation)); + config.ConfigLocation = this.ConfigLocation; //update the config with default values if needed - ret = c.UpdateConfig<T>(); - - c = null; + ret = config.UpdateConfig<T>(); } catch (Exception ex) { @@ -85,10 +82,10 @@ namespace StardewModdingAPI try { //default config - var b = JObject.FromObject(Instance<T>().GenerateDefaultConfig<T>(), new JsonSerializer {ContractResolver = new JsonResolver()}); + var b = JObject.FromObject(this.Instance<T>().GenerateDefaultConfig<T>()); //user config - var u = JObject.FromObject(this, new JsonSerializer {ContractResolver = new JsonResolver()}); + var u = JObject.FromObject(this); //overwrite default values with user values b.Merge(u, new JsonMergeSettings {MergeArrayHandling = MergeArrayHandling.Replace}); @@ -97,7 +94,7 @@ namespace StardewModdingAPI var c = b.ToObject<T>(); //re-write the location on disk to the object - c.ConfigLocation = ConfigLocation; + c.ConfigLocation = this.ConfigLocation; return c; } @@ -155,7 +152,7 @@ namespace StardewModdingAPI return; } - var s = JsonConvert.SerializeObject(baseConfig, typeof(T), Formatting.Indented, new JsonSerializerSettings {ContractResolver = new JsonResolver()}); + var s = JsonConvert.SerializeObject(baseConfig, Formatting.Indented); if (!Directory.Exists(baseConfig.ConfigDir)) Directory.CreateDirectory(baseConfig.ConfigDir); diff --git a/src/StardewModdingAPI/Inheritance/SBareObject.cs b/src/StardewModdingAPI/Inheritance/SBareObject.cs deleted file mode 100644 index 5bef7b3e..00000000 --- a/src/StardewModdingAPI/Inheritance/SBareObject.cs +++ /dev/null @@ -1,20 +0,0 @@ -namespace StardewModdingAPI.Inheritance -{ - public struct SBareObject - { - public int parentSheetIndex { get; set; } - public int stack { get; set; } - public bool isRecipe { get; set; } - public int price { get; set; } - public int quality { get; set; } - - public SBareObject(int psi, int sta, bool ir, int pri, int qua) - { - parentSheetIndex = psi; - stack = sta; - isRecipe = ir; - price = pri; - quality = qua; - } - } -}
\ No newline at end of file diff --git a/src/StardewModdingAPI/JsonResolver.cs b/src/StardewModdingAPI/JsonResolver.cs deleted file mode 100644 index b6e763f7..00000000 --- a/src/StardewModdingAPI/JsonResolver.cs +++ /dev/null @@ -1,212 +0,0 @@ -using System; -using System.Collections.Generic; -using Microsoft.Xna.Framework; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; -using Newtonsoft.Json.Serialization; -using StardewModdingAPI.Inheritance; -using Object = StardewValley.Object; - -namespace StardewModdingAPI -{ - internal class JsonResolver : DefaultContractResolver - { - protected override JsonContract CreateContract(Type objectType) - { - if (objectType == typeof(Rectangle) || objectType == typeof(Rectangle?)) - { - Console.WriteLine("FOUND A RECT"); - JsonContract contract = CreateObjectContract(objectType); - contract.Converter = new RectangleConverter(); - return contract; - } - if (objectType == typeof(Object)) - { - Log.AsyncY("FOUND AN OBJECT"); - JsonContract contract = CreateObjectContract(objectType); - contract.Converter = new ObjectConverter(); - return contract; - } - return base.CreateContract(objectType); - } - } - - public class ObjectConverter : JsonConverter - { - public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) - { - Log.AsyncY("TRYING TO WRITE"); - var obj = (Object) value; - Log.AsyncY("TRYING TO WRITE"); - - var jObject = GetObject(obj); - Log.AsyncY("TRYING TO WRITE"); - - try - { - Log.AsyncY(jObject.ToString()); - } - catch (Exception ex) - { - Log.AsyncR(ex); - } - - 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(); - } - - protected static JObject GetObject(Object o) - { - 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); - Log.AsyncG(JsonConvert.SerializeObject(oo)); - return JObject.FromObject(oo); - } - catch (Exception ex) - { - Log.AsyncR(ex); - Console.ReadKey(); - } - return null; - } - - protected static Object GetObject(JObject jObject) - { - 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?; - - return new Object(parentSheetIndex ?? 0, stack ?? 0, isRecipe ?? false, price ?? -1, quality ?? 0); - } - - protected static Object GetObject(JToken jToken) - { - 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) - { - var rectangle = (Rectangle) value; - - 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; - - return JObject.FromObject(new {x, y, width, height}); - } - - 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; - return jObject.TryGetValue(tokenName, StringComparison.InvariantCultureIgnoreCase, out jToken) ? (int) jToken : (int?) null; - } - } - - public class RectangleListConverter : RectangleConverter - { - public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) - { - var rectangleList = (IList<Rectangle>) value; - - 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(); - } - } -}
\ No newline at end of file diff --git a/src/StardewModdingAPI/StardewModdingAPI.csproj b/src/StardewModdingAPI/StardewModdingAPI.csproj index 90c4dc68..81fd213e 100644 --- a/src/StardewModdingAPI/StardewModdingAPI.csproj +++ b/src/StardewModdingAPI/StardewModdingAPI.csproj @@ -198,8 +198,6 @@ <Compile Include="Inheritance\Menus\SBobberBar.cs" />
<Compile Include="Inheritance\Menus\SGameMenu.cs" />
<Compile Include="Inheritance\Menus\SInventoryPage.cs" />
- <Compile Include="Inheritance\SBareObject.cs" />
- <Compile Include="JsonResolver.cs" />
<Compile Include="Log.cs" />
<Compile Include="Logger.cs" />
<Compile Include="LogWriter.cs" />
|