Normalize all the line endings

This commit is contained in:
Dean Herbert
2018-04-13 18:19:50 +09:00
parent f99503b60c
commit 32a74f95a5
1069 changed files with 95293 additions and 95293 deletions

View File

@ -1,100 +1,100 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace osu.Game.IO.Serialization.Converters
{
/// <summary>
/// A type of <see cref="JsonConverter"/> that serializes a <see cref="List{T}"/> alongside
/// a lookup table for the types contained. The lookup table is used in deserialization to
/// reconstruct the objects with their original types.
/// </summary>
/// <typeparam name="T">The type of objects contained in the <see cref="List{T}"/> this attribute is attached to.</typeparam>
public class TypedListConverter<T> : JsonConverter
{
private readonly bool requiresTypeVersion;
/// <summary>
/// Constructs a new <see cref="TypedListConverter{T}"/>.
/// </summary>
// ReSharper disable once UnusedMember.Global
public TypedListConverter()
{
}
/// <summary>
/// Constructs a new <see cref="TypedListConverter{T}"/>.
/// </summary>
/// <param name="requiresTypeVersion">Whether the version of the type should be serialized.</param>
// ReSharper disable once UnusedMember.Global (Used in Beatmap)
public TypedListConverter(bool requiresTypeVersion)
{
this.requiresTypeVersion = requiresTypeVersion;
}
public override bool CanConvert(Type objectType) => objectType == typeof(List<T>);
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var list = new List<T>();
var obj = JObject.Load(reader);
var lookupTable = serializer.Deserialize<List<string>>(obj["lookup_table"].CreateReader());
foreach (var tok in obj["items"])
{
var itemReader = tok.CreateReader();
var typeName = lookupTable[(int)tok["type"]];
var instance = (T)Activator.CreateInstance(Type.GetType(typeName));
serializer.Populate(itemReader, instance);
list.Add(instance);
}
return list;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var list = (List<T>)value;
var lookupTable = new List<string>();
var objects = new List<JObject>();
foreach (var item in list)
{
var type = item.GetType();
var assemblyName = type.Assembly.GetName();
var typeString = $"{type.FullName}, {assemblyName.Name}";
if (requiresTypeVersion)
typeString += $", {assemblyName.Version}";
int typeId = lookupTable.IndexOf(typeString);
if (typeId == -1)
{
lookupTable.Add(typeString);
typeId = lookupTable.Count - 1;
}
var itemObject = JObject.FromObject(item, serializer);
itemObject.AddFirst(new JProperty("type", typeId));
objects.Add(itemObject);
}
writer.WriteStartObject();
writer.WritePropertyName("lookup_table");
serializer.Serialize(writer, lookupTable);
writer.WritePropertyName("items");
serializer.Serialize(writer, objects);
writer.WriteEndObject();
}
}
}
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace osu.Game.IO.Serialization.Converters
{
/// <summary>
/// A type of <see cref="JsonConverter"/> that serializes a <see cref="List{T}"/> alongside
/// a lookup table for the types contained. The lookup table is used in deserialization to
/// reconstruct the objects with their original types.
/// </summary>
/// <typeparam name="T">The type of objects contained in the <see cref="List{T}"/> this attribute is attached to.</typeparam>
public class TypedListConverter<T> : JsonConverter
{
private readonly bool requiresTypeVersion;
/// <summary>
/// Constructs a new <see cref="TypedListConverter{T}"/>.
/// </summary>
// ReSharper disable once UnusedMember.Global
public TypedListConverter()
{
}
/// <summary>
/// Constructs a new <see cref="TypedListConverter{T}"/>.
/// </summary>
/// <param name="requiresTypeVersion">Whether the version of the type should be serialized.</param>
// ReSharper disable once UnusedMember.Global (Used in Beatmap)
public TypedListConverter(bool requiresTypeVersion)
{
this.requiresTypeVersion = requiresTypeVersion;
}
public override bool CanConvert(Type objectType) => objectType == typeof(List<T>);
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var list = new List<T>();
var obj = JObject.Load(reader);
var lookupTable = serializer.Deserialize<List<string>>(obj["lookup_table"].CreateReader());
foreach (var tok in obj["items"])
{
var itemReader = tok.CreateReader();
var typeName = lookupTable[(int)tok["type"]];
var instance = (T)Activator.CreateInstance(Type.GetType(typeName));
serializer.Populate(itemReader, instance);
list.Add(instance);
}
return list;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var list = (List<T>)value;
var lookupTable = new List<string>();
var objects = new List<JObject>();
foreach (var item in list)
{
var type = item.GetType();
var assemblyName = type.Assembly.GetName();
var typeString = $"{type.FullName}, {assemblyName.Name}";
if (requiresTypeVersion)
typeString += $", {assemblyName.Version}";
int typeId = lookupTable.IndexOf(typeString);
if (typeId == -1)
{
lookupTable.Add(typeString);
typeId = lookupTable.Count - 1;
}
var itemObject = JObject.FromObject(item, serializer);
itemObject.AddFirst(new JProperty("type", typeId));
objects.Add(itemObject);
}
writer.WriteStartObject();
writer.WritePropertyName("lookup_table");
serializer.Serialize(writer, lookupTable);
writer.WritePropertyName("items");
serializer.Serialize(writer, objects);
writer.WriteEndObject();
}
}
}

View File

@ -1,38 +1,38 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OpenTK;
namespace osu.Game.IO.Serialization.Converters
{
/// <summary>
/// A type of <see cref="JsonConverter"/> that serializes only the X and Y coordinates of a <see cref="Vector2"/>.
/// </summary>
public class Vector2Converter : JsonConverter
{
public override bool CanConvert(Type objectType) => objectType == typeof(Vector2);
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var obj = JObject.Load(reader);
return new Vector2((float)obj["x"], (float)obj["y"]);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var vector = (Vector2)value;
writer.WriteStartObject();
writer.WritePropertyName("x");
writer.WriteValue(vector.X);
writer.WritePropertyName("y");
writer.WriteValue(vector.Y);
writer.WriteEndObject();
}
}
}
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OpenTK;
namespace osu.Game.IO.Serialization.Converters
{
/// <summary>
/// A type of <see cref="JsonConverter"/> that serializes only the X and Y coordinates of a <see cref="Vector2"/>.
/// </summary>
public class Vector2Converter : JsonConverter
{
public override bool CanConvert(Type objectType) => objectType == typeof(Vector2);
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var obj = JObject.Load(reader);
return new Vector2((float)obj["x"], (float)obj["y"]);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var vector = (Vector2)value;
writer.WriteStartObject();
writer.WritePropertyName("x");
writer.WriteValue(vector.X);
writer.WritePropertyName("y");
writer.WriteValue(vector.Y);
writer.WriteEndObject();
}
}
}

View File

@ -1,37 +1,37 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using Newtonsoft.Json;
using osu.Game.IO.Serialization.Converters;
namespace osu.Game.IO.Serialization
{
public interface IJsonSerializable
{
}
public static class JsonSerializableExtensions
{
public static string Serialize(this IJsonSerializable obj) => JsonConvert.SerializeObject(obj, CreateGlobalSettings());
public static T Deserialize<T>(this string objString) => JsonConvert.DeserializeObject<T>(objString, CreateGlobalSettings());
public static void DeserializeInto<T>(this string objString, T target) => JsonConvert.PopulateObject(objString, target, CreateGlobalSettings());
public static T DeepClone<T>(this T obj) where T : IJsonSerializable => Deserialize<T>(Serialize(obj));
/// <summary>
/// Creates the default <see cref="JsonSerializerSettings"/> that should be used for all <see cref="IJsonSerializable"/>s.
/// </summary>
/// <returns></returns>
public static JsonSerializerSettings CreateGlobalSettings() => new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
Formatting = Formatting.Indented,
ObjectCreationHandling = ObjectCreationHandling.Replace,
DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate,
Converters = new JsonConverter[] { new Vector2Converter() },
ContractResolver = new KeyContractResolver()
};
}
}
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using Newtonsoft.Json;
using osu.Game.IO.Serialization.Converters;
namespace osu.Game.IO.Serialization
{
public interface IJsonSerializable
{
}
public static class JsonSerializableExtensions
{
public static string Serialize(this IJsonSerializable obj) => JsonConvert.SerializeObject(obj, CreateGlobalSettings());
public static T Deserialize<T>(this string objString) => JsonConvert.DeserializeObject<T>(objString, CreateGlobalSettings());
public static void DeserializeInto<T>(this string objString, T target) => JsonConvert.PopulateObject(objString, target, CreateGlobalSettings());
public static T DeepClone<T>(this T obj) where T : IJsonSerializable => Deserialize<T>(Serialize(obj));
/// <summary>
/// Creates the default <see cref="JsonSerializerSettings"/> that should be used for all <see cref="IJsonSerializable"/>s.
/// </summary>
/// <returns></returns>
public static JsonSerializerSettings CreateGlobalSettings() => new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
Formatting = Formatting.Indented,
ObjectCreationHandling = ObjectCreationHandling.Replace,
DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate,
Converters = new JsonConverter[] { new Vector2Converter() },
ContractResolver = new KeyContractResolver()
};
}
}

View File

@ -1,16 +1,16 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using Humanizer;
using Newtonsoft.Json.Serialization;
namespace osu.Game.IO.Serialization
{
public class KeyContractResolver : DefaultContractResolver
{
protected override string ResolvePropertyName(string propertyName)
{
return propertyName.Underscore();
}
}
}
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using Humanizer;
using Newtonsoft.Json.Serialization;
namespace osu.Game.IO.Serialization
{
public class KeyContractResolver : DefaultContractResolver
{
protected override string ResolvePropertyName(string propertyName)
{
return propertyName.Underscore();
}
}
}