2021-01-08 08:08:01 +00:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Drawing;
|
2022-10-24 04:13:40 +00:00
|
|
|
using System.Globalization;
|
2021-01-08 08:08:01 +00:00
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
|
|
namespace osu.Game.Tournament
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// We made a change from using SixLabors.ImageSharp.Point to System.Drawing.Point at some stage.
|
|
|
|
/// This handles converting to a standardised format on json serialize/deserialize operations.
|
|
|
|
/// </summary>
|
|
|
|
internal class JsonPointConverter : JsonConverter<Point>
|
|
|
|
{
|
|
|
|
public override void WriteJson(JsonWriter writer, Point value, JsonSerializer serializer)
|
|
|
|
{
|
|
|
|
// use the format of LaborSharp's Point since it is nicer.
|
2021-01-08 15:38:38 +00:00
|
|
|
serializer.Serialize(writer, new { value.X, value.Y });
|
2021-01-08 08:08:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override Point ReadJson(JsonReader reader, Type objectType, Point existingValue, bool hasExistingValue, JsonSerializer serializer)
|
|
|
|
{
|
|
|
|
if (reader.TokenType != JsonToken.StartObject)
|
|
|
|
{
|
|
|
|
// if there's no object present then this is using string representation (System.Drawing.Point serializes to "x,y")
|
2023-07-25 11:50:55 +00:00
|
|
|
string? str = (string?)reader.Value;
|
2021-01-08 08:08:01 +00:00
|
|
|
|
|
|
|
Debug.Assert(str != null);
|
|
|
|
|
2022-10-24 04:13:40 +00:00
|
|
|
// Null check suppression is required due to .NET standard expecting a non-null context.
|
|
|
|
// Seems to work fine at a runtime level (and the parameter is nullable in .NET 6+).
|
|
|
|
return new PointConverter().ConvertFromString(null!, CultureInfo.InvariantCulture, str) as Point? ?? new Point();
|
2021-01-08 08:08:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var point = new Point();
|
|
|
|
|
|
|
|
while (reader.Read())
|
|
|
|
{
|
|
|
|
if (reader.TokenType == JsonToken.EndObject) break;
|
|
|
|
|
|
|
|
if (reader.TokenType == JsonToken.PropertyName)
|
|
|
|
{
|
2023-07-25 11:50:55 +00:00
|
|
|
string? name = reader.Value?.ToString();
|
2021-01-08 08:08:01 +00:00
|
|
|
int? val = reader.ReadAsInt32();
|
|
|
|
|
2023-07-25 11:50:55 +00:00
|
|
|
if (name == null)
|
|
|
|
continue;
|
|
|
|
|
2021-01-08 08:08:01 +00:00
|
|
|
if (val == null)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch (name)
|
|
|
|
{
|
|
|
|
case "X":
|
|
|
|
point.X = val.Value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "Y":
|
|
|
|
point.Y = val.Value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return point;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|