add JSON converter for Vector2
This commit is contained in:
parent
c8191449a0
commit
70a1334f2c
|
@ -23,6 +23,7 @@
|
||||||
|
|
||||||
* For modders:
|
* For modders:
|
||||||
* Added support for loading `.tmx` map files.
|
* Added support for loading `.tmx` map files.
|
||||||
|
* Added JSON converter for `Vector2` values, so they work consistently crossplatform.
|
||||||
* Asset propagation for player sprites now affects other players' sprites, and updates recolor maps (e.g. sleeves).
|
* Asset propagation for player sprites now affects other players' sprites, and updates recolor maps (e.g. sleeves).
|
||||||
* Reworked the order that asset editors/loaders are called between multiple mods to support some framework mods like Content Patcher and Json Assets. Note that the order is undefined and should not be depended on.
|
* Reworked the order that asset editors/loaders are called between multiple mods to support some framework mods like Content Patcher and Json Assets. Note that the order is undefined and should not be depended on.
|
||||||
* Removed invalid-schedule validation which had false positives.
|
* Removed invalid-schedule validation which had false positives.
|
||||||
|
|
|
@ -222,6 +222,7 @@ namespace StardewModdingAPI.Framework
|
||||||
JsonConverter[] converters = {
|
JsonConverter[] converters = {
|
||||||
new ColorConverter(),
|
new ColorConverter(),
|
||||||
new PointConverter(),
|
new PointConverter(),
|
||||||
|
new Vector2Converter(),
|
||||||
new RectangleConverter()
|
new RectangleConverter()
|
||||||
};
|
};
|
||||||
foreach (JsonConverter converter in converters)
|
foreach (JsonConverter converter in converters)
|
||||||
|
|
|
@ -6,7 +6,7 @@ using StardewModdingAPI.Toolkit.Serialization.Converters;
|
||||||
|
|
||||||
namespace StardewModdingAPI.Framework.Serialization
|
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>
|
/// <remarks>
|
||||||
/// - Linux/Mac format: { "X": 1, "Y": 2 }
|
/// - Linux/Mac format: { "X": 1, "Y": 2 }
|
||||||
/// - Windows format: "1, 2"
|
/// - Windows format: "1, 2"
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue