summaryrefslogtreecommitdiff
path: root/src/SMAPI
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2020-02-01 01:08:29 -0500
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2020-02-01 01:08:29 -0500
commit70a1334f2c50cff279344b9b9d52d71c847516a7 (patch)
tree73b690ae1c725940664b7b172e5a2adf9e1bdd92 /src/SMAPI
parentc8191449a00e3db08214e3b1146e17f89f0245c5 (diff)
downloadSMAPI-70a1334f2c50cff279344b9b9d52d71c847516a7.tar.gz
SMAPI-70a1334f2c50cff279344b9b9d52d71c847516a7.tar.bz2
SMAPI-70a1334f2c50cff279344b9b9d52d71c847516a7.zip
add JSON converter for Vector2
Diffstat (limited to 'src/SMAPI')
-rw-r--r--src/SMAPI/Framework/SCore.cs1
-rw-r--r--src/SMAPI/Framework/Serialization/PointConverter.cs2
-rw-r--r--src/SMAPI/Framework/Serialization/Vector2Converter.cs43
3 files changed, 45 insertions, 1 deletions
diff --git a/src/SMAPI/Framework/SCore.cs b/src/SMAPI/Framework/SCore.cs
index 77c2fab8..50e6ea1c 100644
--- a/src/SMAPI/Framework/SCore.cs
+++ b/src/SMAPI/Framework/SCore.cs
@@ -222,6 +222,7 @@ namespace StardewModdingAPI.Framework
JsonConverter[] converters = {
new ColorConverter(),
new PointConverter(),
+ new Vector2Converter(),
new RectangleConverter()
};
foreach (JsonConverter converter in converters)
diff --git a/src/SMAPI/Framework/Serialization/PointConverter.cs b/src/SMAPI/Framework/Serialization/PointConverter.cs
index 8c2f3396..3481c9b2 100644
--- a/src/SMAPI/Framework/Serialization/PointConverter.cs
+++ b/src/SMAPI/Framework/Serialization/PointConverter.cs
@@ -6,7 +6,7 @@ using StardewModdingAPI.Toolkit.Serialization.Converters;
namespace StardewModdingAPI.Framework.Serialization
{
- /// <summary>Handles deserialization of <see cref="PointConverter"/> for crossplatform compatibility.</summary>
+ /// <summary>Handles deserialization of <see cref="Point"/> for crossplatform compatibility.</summary>
/// <remarks>
/// - Linux/Mac format: { "X": 1, "Y": 2 }
/// - Windows format: "1, 2"
diff --git a/src/SMAPI/Framework/Serialization/Vector2Converter.cs b/src/SMAPI/Framework/Serialization/Vector2Converter.cs
new file mode 100644
index 00000000..1d9b08e0
--- /dev/null
+++ b/src/SMAPI/Framework/Serialization/Vector2Converter.cs
@@ -0,0 +1,43 @@
+using System;
+using Microsoft.Xna.Framework;
+using Newtonsoft.Json.Linq;
+using StardewModdingAPI.Toolkit.Serialization;
+using StardewModdingAPI.Toolkit.Serialization.Converters;
+
+namespace StardewModdingAPI.Framework.Serialization
+{
+ /// <summary>Handles deserialization of <see cref="Vector2"/> for crossplatform compatibility.</summary>
+ /// <remarks>
+ /// - Linux/Mac format: { "X": 1, "Y": 2 }
+ /// - Windows format: "1, 2"
+ /// </remarks>
+ internal class Vector2Converter : SimpleReadOnlyConverter<Vector2>
+ {
+ /*********
+ ** Protected methods
+ *********/
+ /// <summary>Read a JSON object.</summary>
+ /// <param name="obj">The JSON object to read.</param>
+ /// <param name="path">The path to the current JSON node.</param>
+ protected override Vector2 ReadObject(JObject obj, string path)
+ {
+ float x = obj.ValueIgnoreCase<float>(nameof(Vector2.X));
+ float y = obj.ValueIgnoreCase<float>(nameof(Vector2.Y));
+ return new Vector2(x, y);
+ }
+
+ /// <summary>Read a JSON string.</summary>
+ /// <param name="str">The JSON string value.</param>
+ /// <param name="path">The path to the current JSON node.</param>
+ protected override Vector2 ReadString(string str, string path)
+ {
+ string[] parts = str.Split(',');
+ if (parts.Length != 2)
+ throw new SParseException($"Can't parse {typeof(Vector2).Name} from invalid value '{str}' (path: {path}).");
+
+ float x = Convert.ToSingle(parts[0]);
+ float y = Convert.ToSingle(parts[1]);
+ return new Vector2(x, y);
+ }
+ }
+}