mirror of
https://github.com/ppy/osu.git
synced 2025-02-16 01:03:21 +08:00
Merge pull request #11444 from peppy/fix-tournament-serialization-changes
Add custom handling of Point serialization to fix startup crashes of tournament client
This commit is contained in:
commit
ef5ea82d8c
65
osu.Game.Tournament/JsonPointConverter.cs
Normal file
65
osu.Game.Tournament/JsonPointConverter.cs
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
// 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;
|
||||||
|
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.
|
||||||
|
serializer.Serialize(writer, new { value.X, value.Y });
|
||||||
|
}
|
||||||
|
|
||||||
|
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")
|
||||||
|
string str = (string)reader.Value;
|
||||||
|
|
||||||
|
Debug.Assert(str != null);
|
||||||
|
|
||||||
|
return new PointConverter().ConvertFromString(str) as Point? ?? new Point();
|
||||||
|
}
|
||||||
|
|
||||||
|
var point = new Point();
|
||||||
|
|
||||||
|
while (reader.Read())
|
||||||
|
{
|
||||||
|
if (reader.TokenType == JsonToken.EndObject) break;
|
||||||
|
|
||||||
|
if (reader.TokenType == JsonToken.PropertyName)
|
||||||
|
{
|
||||||
|
var name = reader.Value?.ToString();
|
||||||
|
int? val = reader.ReadAsInt32();
|
||||||
|
|
||||||
|
if (val == null)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
switch (name)
|
||||||
|
{
|
||||||
|
case "X":
|
||||||
|
point.X = val.Value;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "Y":
|
||||||
|
point.Y = val.Value;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return point;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -8,12 +8,12 @@ using Newtonsoft.Json;
|
|||||||
using osu.Framework.Allocation;
|
using osu.Framework.Allocation;
|
||||||
using osu.Framework.Graphics.Textures;
|
using osu.Framework.Graphics.Textures;
|
||||||
using osu.Framework.Input;
|
using osu.Framework.Input;
|
||||||
using osu.Framework.Platform;
|
|
||||||
using osu.Framework.IO.Stores;
|
using osu.Framework.IO.Stores;
|
||||||
|
using osu.Framework.Platform;
|
||||||
using osu.Game.Beatmaps;
|
using osu.Game.Beatmaps;
|
||||||
using osu.Game.Online.API.Requests;
|
using osu.Game.Online.API.Requests;
|
||||||
using osu.Game.Tournament.IPC;
|
|
||||||
using osu.Game.Tournament.IO;
|
using osu.Game.Tournament.IO;
|
||||||
|
using osu.Game.Tournament.IPC;
|
||||||
using osu.Game.Tournament.Models;
|
using osu.Game.Tournament.Models;
|
||||||
using osu.Game.Users;
|
using osu.Game.Users;
|
||||||
using osuTK.Input;
|
using osuTK.Input;
|
||||||
@ -60,7 +60,7 @@ namespace osu.Game.Tournament
|
|||||||
{
|
{
|
||||||
using (Stream stream = storage.GetStream(bracket_filename, FileAccess.Read, FileMode.Open))
|
using (Stream stream = storage.GetStream(bracket_filename, FileAccess.Read, FileMode.Open))
|
||||||
using (var sr = new StreamReader(stream))
|
using (var sr = new StreamReader(stream))
|
||||||
ladder = JsonConvert.DeserializeObject<LadderInfo>(sr.ReadToEnd());
|
ladder = JsonConvert.DeserializeObject<LadderInfo>(sr.ReadToEnd(), new JsonPointConverter());
|
||||||
}
|
}
|
||||||
|
|
||||||
ladder ??= new LadderInfo();
|
ladder ??= new LadderInfo();
|
||||||
@ -251,6 +251,7 @@ namespace osu.Game.Tournament
|
|||||||
Formatting = Formatting.Indented,
|
Formatting = Formatting.Indented,
|
||||||
NullValueHandling = NullValueHandling.Ignore,
|
NullValueHandling = NullValueHandling.Ignore,
|
||||||
DefaultValueHandling = DefaultValueHandling.Ignore,
|
DefaultValueHandling = DefaultValueHandling.Ignore,
|
||||||
|
Converters = new JsonConverter[] { new JsonPointConverter() }
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user