fix: Updates more logging to Serilog (#1105)

Adds more serilog logging and cleans up some server files.
This commit is contained in:
Kamron Batman 2022-07-01 11:57:08 -07:00 • committed by GitHub
parent 1cf4a43c2c
commit 38686c09b2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
30 changed files with 2261 additions and 2234 deletions

View file

@ -17,21 +17,20 @@ using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Server.Json
{
public class ClientVersionConverter : JsonConverter<ClientVersion>
{
public override ClientVersion Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
return new ClientVersion(reader.GetString());
}
namespace Server.Json;
throw new JsonException("Value must be a string");
public class ClientVersionConverter : JsonConverter<ClientVersion>
{
public override ClientVersion Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
return new ClientVersion(reader.GetString());
}
public override void Write(Utf8JsonWriter writer, ClientVersion value, JsonSerializerOptions options) =>
writer.WriteStringValue(value.ToString());
throw new JsonException("Value must be a string");
}
}
public override void Write(Utf8JsonWriter writer, ClientVersion value, JsonSerializerOptions options) =>
writer.WriteStringValue(value.ToString());
}

View file

@ -17,13 +17,12 @@ using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Server.Json
{
public class ClientVersionConverterFactory : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(ClientVersion);
namespace Server.Json;
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
new ClientVersionConverter();
}
}
public class ClientVersionConverterFactory : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(ClientVersion);
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
new ClientVersionConverter();
}

View file

@ -18,133 +18,132 @@ using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Server.Json
namespace Server.Json;
public class FlagsConverter<T> : JsonConverter<T> where T : struct, Enum
{
public class FlagsConverter<T> : JsonConverter<T> where T : struct, Enum
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
var flags = 0ul;
var underlyingType = Enum.GetUnderlyingType(typeof(T));
while (true)
{
var flags = 0ul;
var underlyingType = Enum.GetUnderlyingType(typeof(T));
while (true)
reader.Read();
if (reader.TokenType == JsonTokenType.EndObject)
{
reader.Read();
if (reader.TokenType == JsonTokenType.EndObject)
{
break;
}
if (reader.TokenType != JsonTokenType.PropertyName)
{
throw new JsonException("Invalid Json structure for Flag object");
}
var key = reader.GetString();
reader.Read();
if (!reader.GetBoolean() || !Enum.TryParse<T>(key, out var val))
{
continue;
}
flags |= ConvertToUInt64(underlyingType, val);
break;
}
switch (Type.GetTypeCode(underlyingType))
if (reader.TokenType != JsonTokenType.PropertyName)
{
case TypeCode.SByte:
{
var num = (sbyte)flags;
return Unsafe.As<sbyte, T>(ref num);
}
case TypeCode.Byte:
{
var num = (byte)flags;
return Unsafe.As<byte, T>(ref num);
}
case TypeCode.Int16:
{
var num = (short)flags;
return Unsafe.As<short, T>(ref num);
}
case TypeCode.UInt16:
{
var num = (ushort)flags;
return Unsafe.As<ushort, T>(ref num);
}
case TypeCode.UInt32:
{
var num = (uint)flags;
return Unsafe.As<uint, T>(ref num);
}
case TypeCode.Int64:
{
var num = (long)flags;
return Unsafe.As<long, T>(ref num);
}
case TypeCode.UInt64:
{
return Unsafe.As<ulong, T>(ref flags);
}
default:
{
var num = (int)flags;
return Unsafe.As<int, T>(ref num);
}
throw new JsonException("Invalid Json structure for Flag object");
}
var key = reader.GetString();
reader.Read();
if (!reader.GetBoolean() || !Enum.TryParse<T>(key, out var val))
{
continue;
}
flags |= ConvertToUInt64(underlyingType, val);
}
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
switch (Type.GetTypeCode(underlyingType))
{
writer.WriteStartObject();
var underlyingType = Enum.GetUnderlyingType(typeof(T));
var intValue = ConvertToUInt64(underlyingType, value);
foreach (var flagName in Enum.GetNames(typeof(T)))
{
var flagValue = Enum.Parse<T>(flagName, false);
var flag = ConvertToUInt64(underlyingType, flagValue);
// Do not write out multi-bit values. This is a custom behavior
if (flag > 0 && (flag & (flag - 1)) == 0)
case TypeCode.SByte:
{
writer.WriteBoolean(flagName, (intValue & flag) == flag);
var num = (sbyte)flags;
return Unsafe.As<sbyte, T>(ref num);
}
case TypeCode.Byte:
{
var num = (byte)flags;
return Unsafe.As<byte, T>(ref num);
}
case TypeCode.Int16:
{
var num = (short)flags;
return Unsafe.As<short, T>(ref num);
}
case TypeCode.UInt16:
{
var num = (ushort)flags;
return Unsafe.As<ushort, T>(ref num);
}
case TypeCode.UInt32:
{
var num = (uint)flags;
return Unsafe.As<uint, T>(ref num);
}
case TypeCode.Int64:
{
var num = (long)flags;
return Unsafe.As<long, T>(ref num);
}
case TypeCode.UInt64:
{
return Unsafe.As<ulong, T>(ref flags);
}
default:
{
var num = (int)flags;
return Unsafe.As<int, T>(ref num);
}
}
writer.WriteEndObject();
}
private static ulong ConvertToUInt64(Type underlyingType, object value) =>
Type.GetTypeCode(underlyingType) switch
{
TypeCode.SByte => (ulong)(sbyte)value,
TypeCode.Byte => (byte)value,
TypeCode.Int16 => (ulong)(short)value,
TypeCode.UInt16 => (ushort)value,
TypeCode.Int32 => (ulong)(int)value,
TypeCode.UInt32 => (uint)value,
TypeCode.Int64 => (ulong)(long)value,
TypeCode.UInt64 => (ulong)value,
_ => throw new InvalidOperationException()
};
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ulong GetUnderlyingTypeLength(TypeCode typeCode) =>
typeCode switch
{
TypeCode.Byte => 8,
TypeCode.SByte => 8,
TypeCode.Int16 => 16,
TypeCode.UInt16 => 16,
TypeCode.Char => 16,
TypeCode.Int32 => 32,
TypeCode.UInt32 => 32,
TypeCode.Int64 => 64,
TypeCode.UInt64 => 64,
_ => 64
};
}
}
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
writer.WriteStartObject();
var underlyingType = Enum.GetUnderlyingType(typeof(T));
var intValue = ConvertToUInt64(underlyingType, value);
foreach (var flagName in Enum.GetNames(typeof(T)))
{
var flagValue = Enum.Parse<T>(flagName, false);
var flag = ConvertToUInt64(underlyingType, flagValue);
// Do not write out multi-bit values. This is a custom behavior
if (flag > 0 && (flag & (flag - 1)) == 0)
{
writer.WriteBoolean(flagName, (intValue & flag) == flag);
}
}
writer.WriteEndObject();
}
private static ulong ConvertToUInt64(Type underlyingType, object value) =>
Type.GetTypeCode(underlyingType) switch
{
TypeCode.SByte => (ulong)(sbyte)value,
TypeCode.Byte => (byte)value,
TypeCode.Int16 => (ulong)(short)value,
TypeCode.UInt16 => (ushort)value,
TypeCode.Int32 => (ulong)(int)value,
TypeCode.UInt32 => (uint)value,
TypeCode.Int64 => (ulong)(long)value,
TypeCode.UInt64 => (ulong)value,
_ => throw new InvalidOperationException()
};
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ulong GetUnderlyingTypeLength(TypeCode typeCode) =>
typeCode switch
{
TypeCode.Byte => 8,
TypeCode.SByte => 8,
TypeCode.Int16 => 16,
TypeCode.UInt16 => 16,
TypeCode.Char => 16,
TypeCode.Int32 => 32,
TypeCode.UInt32 => 32,
TypeCode.Int64 => 64,
TypeCode.UInt64 => 64,
_ => 64
};
}

View file

@ -17,21 +17,20 @@ using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Server.Json
{
public class GuidConverter : JsonConverter<Guid>
{
public override Guid Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (Guid.TryParse(reader.GetString()!, out var guid))
{
return guid;
}
namespace Server.Json;
throw new JsonException("Guid must be in the correct format");
public class GuidConverter : JsonConverter<Guid>
{
public override Guid Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (Guid.TryParse(reader.GetString()!, out var guid))
{
return guid;
}
public override void Write(Utf8JsonWriter writer, Guid value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString());
throw new JsonException("Guid must be in the correct format");
}
}
public override void Write(Utf8JsonWriter writer, Guid value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString());
}

View file

@ -17,13 +17,12 @@ using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Server.Json
{
public class GuidConverterFactory : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(Guid);
namespace Server.Json;
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
new GuidConverter();
}
}
public class GuidConverterFactory : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(Guid);
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
new GuidConverter();
}

View file

@ -18,21 +18,20 @@ using System.Net;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Server.Json
{
public class IPEndPointConverter : JsonConverter<IPEndPoint>
{
public override IPEndPoint Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (IPEndPoint.TryParse(reader.GetString()!, out var ipep))
{
return ipep;
}
namespace Server.Json;
throw new JsonException("IPEndPoint must be in the correct format");
public class IPEndPointConverter : JsonConverter<IPEndPoint>
{
public override IPEndPoint Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (IPEndPoint.TryParse(reader.GetString()!, out var ipep))
{
return ipep;
}
public override void Write(Utf8JsonWriter writer, IPEndPoint value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString());
throw new JsonException("IPEndPoint must be in the correct format");
}
}
public override void Write(Utf8JsonWriter writer, IPEndPoint value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString());
}

View file

@ -18,13 +18,12 @@ using System.Net;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Server.Json
{
public class IPEndPointConverterFactory : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(IPEndPoint);
namespace Server.Json;
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
new IPEndPointConverter();
}
}
public class IPEndPointConverterFactory : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(IPEndPoint);
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
new IPEndPointConverter();
}

View file

@ -17,19 +17,18 @@ using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Server.Json
{
public class MapConverter : JsonConverter<Map>
{
public override Map Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
reader.TokenType switch
{
JsonTokenType.String => Map.Parse(reader.GetString()),
JsonTokenType.Number => Map.Maps[reader.GetInt32()],
_ => throw new JsonException("Value must be a number or string")
};
namespace Server.Json;
public override void Write(Utf8JsonWriter writer, Map value, JsonSerializerOptions options) =>
writer.WriteStringValue(value.Name);
}
}
public class MapConverter : JsonConverter<Map>
{
public override Map Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
reader.TokenType switch
{
JsonTokenType.String => Map.Parse(reader.GetString()),
JsonTokenType.Number => Map.Maps[reader.GetInt32()],
_ => throw new JsonException("Value must be a number or string")
};
public override void Write(Utf8JsonWriter writer, Map value, JsonSerializerOptions options) =>
writer.WriteStringValue(value.Name);
}

View file

@ -17,13 +17,12 @@ using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Server.Json
{
public class MapConverterFactory : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(Map);
namespace Server.Json;
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
new MapConverter();
}
}
public class MapConverterFactory : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(Map);
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
new MapConverter();
}

View file

@ -17,95 +17,94 @@ using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Server.Json
namespace Server.Json;
public class Point2DConverter : JsonConverter<Point2D>
{
public class Point2DConverter : JsonConverter<Point2D>
private Point2D DeserializeArray(ref Utf8JsonReader reader)
{
private Point2D DeserializeArray(ref Utf8JsonReader reader)
Span<int> data = stackalloc int[2];
var count = 0;
while (true)
{
Span<int> data = stackalloc int[2];
var count = 0;
while (true)
reader.Read();
if (reader.TokenType == JsonTokenType.EndArray)
{
reader.Read();
if (reader.TokenType == JsonTokenType.EndArray)
{
break;
}
if (reader.TokenType == JsonTokenType.Number)
{
if (count < 2)
{
data[count] = reader.GetInt32();
}
count++;
}
break;
}
if (count > 2)
if (reader.TokenType == JsonTokenType.Number)
{
throw new JsonException("Point2D must be an array of x, y");
}
if (count < 2)
{
data[count] = reader.GetInt32();
}
return new Point2D(data[0], data[1]);
count++;
}
}
private Point2D DeserializeObj(ref Utf8JsonReader reader)
if (count > 2)
{
Span<int> data = stackalloc int[2];
while (true)
{
reader.Read();
if (reader.TokenType == JsonTokenType.EndObject)
{
break;
}
if (reader.TokenType != JsonTokenType.PropertyName)
{
throw new JsonException("Invalid json structure for Point2D object");
}
var key = reader.GetString();
var i = key switch
{
"x" => 0,
"y" => 1,
_ => throw new JsonException($"Invalid property {key} for Point2D")
};
reader.Read();
if (reader.TokenType != JsonTokenType.Number)
{
throw new JsonException($"Value for {key} must be a number");
}
data[i] = reader.GetInt32();
}
return new Point2D(data[0], data[1]);
throw new JsonException("Point2D must be an array of x, y");
}
public override Point2D Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
reader.TokenType switch
return new Point2D(data[0], data[1]);
}
private Point2D DeserializeObj(ref Utf8JsonReader reader)
{
Span<int> data = stackalloc int[2];
while (true)
{
reader.Read();
if (reader.TokenType == JsonTokenType.EndObject)
{
JsonTokenType.StartArray => DeserializeArray(ref reader),
JsonTokenType.StartObject => DeserializeObj(ref reader),
_ => throw new JsonException("Invalid Json for Point3D")
break;
}
if (reader.TokenType != JsonTokenType.PropertyName)
{
throw new JsonException("Invalid json structure for Point2D object");
}
var key = reader.GetString();
var i = key switch
{
"x" => 0,
"y" => 1,
_ => throw new JsonException($"Invalid property {key} for Point2D")
};
public override void Write(Utf8JsonWriter writer, Point2D value, JsonSerializerOptions options)
{
writer.WriteStartArray();
writer.WriteNumberValue(value.X);
writer.WriteNumberValue(value.Y);
writer.WriteEndArray();
reader.Read();
if (reader.TokenType != JsonTokenType.Number)
{
throw new JsonException($"Value for {key} must be a number");
}
data[i] = reader.GetInt32();
}
return new Point2D(data[0], data[1]);
}
}
public override Point2D Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
reader.TokenType switch
{
JsonTokenType.StartArray => DeserializeArray(ref reader),
JsonTokenType.StartObject => DeserializeObj(ref reader),
_ => throw new JsonException("Invalid Json for Point3D")
};
public override void Write(Utf8JsonWriter writer, Point2D value, JsonSerializerOptions options)
{
writer.WriteStartArray();
writer.WriteNumberValue(value.X);
writer.WriteNumberValue(value.Y);
writer.WriteEndArray();
}
}

View file

@ -17,14 +17,13 @@ using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Server.Json
{
public class Point2DConverterFactory : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert) =>
typeToConvert == typeof(Point2D) || typeToConvert == typeof(IPoint2D);
namespace Server.Json;
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
new Point2DConverter();
}
}
public class Point2DConverterFactory : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert) =>
typeToConvert == typeof(Point2D) || typeToConvert == typeof(IPoint2D);
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
new Point2DConverter();
}

View file

@ -17,97 +17,96 @@ using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Server.Json
namespace Server.Json;
public class Point3DConverter : JsonConverter<Point3D>
{
public class Point3DConverter : JsonConverter<Point3D>
private Point3D DeserializeArray(ref Utf8JsonReader reader)
{
private Point3D DeserializeArray(ref Utf8JsonReader reader)
Span<int> data = stackalloc int[3];
var count = 0;
while (true)
{
Span<int> data = stackalloc int[3];
var count = 0;
while (true)
reader.Read();
if (reader.TokenType == JsonTokenType.EndArray)
{
reader.Read();
if (reader.TokenType == JsonTokenType.EndArray)
{
break;
}
if (reader.TokenType == JsonTokenType.Number)
{
if (count < 3)
{
data[count] = reader.GetInt32();
}
count++;
}
break;
}
if (count > 3)
if (reader.TokenType == JsonTokenType.Number)
{
throw new JsonException("Point3D must be an array of x, y, z");
}
if (count < 3)
{
data[count] = reader.GetInt32();
}
return new Point3D(data[0], data[1], data[2]);
count++;
}
}
private Point3D DeserializeObj(ref Utf8JsonReader reader)
if (count > 3)
{
Span<int> data = stackalloc int[3];
while (true)
{
reader.Read();
if (reader.TokenType == JsonTokenType.EndObject)
{
break;
}
if (reader.TokenType != JsonTokenType.PropertyName)
{
throw new JsonException("Invalid json structure for Point3D object");
}
var key = reader.GetString();
var i = key switch
{
"x" => 0,
"y" => 1,
"z" => 2,
_ => throw new JsonException($"Invalid property {key} for Point3D")
};
reader.Read();
if (reader.TokenType != JsonTokenType.Number)
{
throw new JsonException($"Value for {key} must be a number");
}
data[i] = reader.GetInt32();
}
return new Point3D(data[0], data[1], data[2]);
throw new JsonException("Point3D must be an array of x, y, z");
}
public override Point3D Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
reader.TokenType switch
return new Point3D(data[0], data[1], data[2]);
}
private Point3D DeserializeObj(ref Utf8JsonReader reader)
{
Span<int> data = stackalloc int[3];
while (true)
{
reader.Read();
if (reader.TokenType == JsonTokenType.EndObject)
{
JsonTokenType.StartArray => DeserializeArray(ref reader),
JsonTokenType.StartObject => DeserializeObj(ref reader),
_ => throw new JsonException("Invalid Json for Point3D")
break;
}
if (reader.TokenType != JsonTokenType.PropertyName)
{
throw new JsonException("Invalid json structure for Point3D object");
}
var key = reader.GetString();
var i = key switch
{
"x" => 0,
"y" => 1,
"z" => 2,
_ => throw new JsonException($"Invalid property {key} for Point3D")
};
public override void Write(Utf8JsonWriter writer, Point3D value, JsonSerializerOptions options)
{
writer.WriteStartArray();
writer.WriteNumberValue(value.X);
writer.WriteNumberValue(value.Y);
writer.WriteNumberValue(value.Z);
writer.WriteEndArray();
reader.Read();
if (reader.TokenType != JsonTokenType.Number)
{
throw new JsonException($"Value for {key} must be a number");
}
data[i] = reader.GetInt32();
}
return new Point3D(data[0], data[1], data[2]);
}
}
public override Point3D Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
reader.TokenType switch
{
JsonTokenType.StartArray => DeserializeArray(ref reader),
JsonTokenType.StartObject => DeserializeObj(ref reader),
_ => throw new JsonException("Invalid Json for Point3D")
};
public override void Write(Utf8JsonWriter writer, Point3D value, JsonSerializerOptions options)
{
writer.WriteStartArray();
writer.WriteNumberValue(value.X);
writer.WriteNumberValue(value.Y);
writer.WriteNumberValue(value.Z);
writer.WriteEndArray();
}
}

View file

@ -17,14 +17,13 @@ using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Server.Json
{
public class Point3DConverterFactory : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert) =>
typeToConvert == typeof(Point3D) || typeToConvert == typeof(IPoint3D);
namespace Server.Json;
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
new Point3DConverter();
}
}
public class Point3DConverterFactory : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert) =>
typeToConvert == typeof(Point3D) || typeToConvert == typeof(IPoint3D);
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
new Point3DConverter();
}

View file

@ -17,168 +17,167 @@ using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Server.Json
namespace Server.Json;
public class Rectangle3DConverter : JsonConverter<Rectangle3D>
{
public class Rectangle3DConverter : JsonConverter<Rectangle3D>
private Rectangle3D DeserializeArray(ref Utf8JsonReader reader)
{
private Rectangle3D DeserializeArray(ref Utf8JsonReader reader)
Span<int> data = stackalloc int[6];
var count = 0;
while (true)
{
Span<int> data = stackalloc int[6];
var count = 0;
while (true)
reader.Read();
if (reader.TokenType == JsonTokenType.EndArray)
{
reader.Read();
if (reader.TokenType == JsonTokenType.EndArray)
{
break;
}
if (reader.TokenType == JsonTokenType.Number)
{
if (count < 6)
{
data[count] = reader.GetInt32();
}
count++;
}
break;
}
if (count > 6)
if (reader.TokenType == JsonTokenType.Number)
{
throw new JsonException("Rectangle3D must be an array of x, y, z, h, w, d");
}
if (count < 6)
{
data[count] = reader.GetInt32();
}
return new Rectangle3D(data[0], data[1], data[2], data[3], data[4], data[5]);
count++;
}
}
private Rectangle3D DeserializeObj(ref Utf8JsonReader reader, JsonSerializerOptions options)
if (count > 6)
{
Span<int> data = stackalloc int[6];
throw new JsonException("Rectangle3D must be an array of x, y, z, h, w, d");
}
// 0 - xyzwhd, 1 - x1y1z1x2y2z2, 2 - start/end
var objType = -1;
var hasZ = false;
return new Rectangle3D(data[0], data[1], data[2], data[3], data[4], data[5]);
}
while (true)
private Rectangle3D DeserializeObj(ref Utf8JsonReader reader, JsonSerializerOptions options)
{
Span<int> data = stackalloc int[6];
// 0 - xyzwhd, 1 - x1y1z1x2y2z2, 2 - start/end
var objType = -1;
var hasZ = false;
while (true)
{
reader.Read();
if (reader.TokenType == JsonTokenType.EndObject)
{
reader.Read();
if (reader.TokenType == JsonTokenType.EndObject)
{
break;
}
break;
}
if (reader.TokenType != JsonTokenType.PropertyName)
{
throw new JsonException("Invalid json structure for Rectangle3D object");
}
if (reader.TokenType != JsonTokenType.PropertyName)
{
throw new JsonException("Invalid json structure for Rectangle3D object");
}
var key = reader.GetString();
var key = reader.GetString();
reader.Read();
reader.Read();
if (key is "start" or "end")
{
if (objType > -1 && objType != 2)
{
throw new JsonException("Rectangle3D must have a start/end, or x/y/z/w/h/d, but not both.");
}
objType = 2;
var point3D = reader.ToObject<Point3D>(options);
var offset = key == "end" ? 3 : 0;
data[0 + offset] = point3D.X;
data[1 + offset] = point3D.Y;
// We can't do implicit z-level. Abandon using Point3D deserialization?
data[2 + offset] = point3D.Z;
continue;
}
var i = key switch
{
"x" => 0,
"y" => 1,
"z" => 2,
"w" => 3,
"width" => 3,
"h" => 4,
"height" => 4,
"d" => 5,
"depth" => 5,
"x1" => 10,
"y1" => 11,
"z1" => 12,
"x2" => 13,
"y2" => 14,
"z2" => 15,
_ => throw new JsonException($"Invalid property {key} for Rectangle3D")
};
if (i < 10)
{
if (objType > -1 && objType != 0)
{
throw new JsonException("Rectangle3D must have a start/end, or x/y/z/w/h/d, but not both.");
}
objType = 0;
data[i] = reader.GetInt32();
if (i == 2)
{
hasZ = true;
}
continue;
}
if (objType > -1 && objType != 1)
if (key is "start" or "end")
{
if (objType > -1 && objType != 2)
{
throw new JsonException("Rectangle3D must have a start/end, or x/y/z/w/h/d, but not both.");
}
objType = 1;
data[i - 10] = reader.GetInt32();
if (i is 12 or 15)
objType = 2;
var point3D = reader.ToObject<Point3D>(options);
var offset = key == "end" ? 3 : 0;
data[0 + offset] = point3D.X;
data[1 + offset] = point3D.Y;
// We can't do implicit z-level. Abandon using Point3D deserialization?
data[2 + offset] = point3D.Z;
continue;
}
var i = key switch
{
"x" => 0,
"y" => 1,
"z" => 2,
"w" => 3,
"width" => 3,
"h" => 4,
"height" => 4,
"d" => 5,
"depth" => 5,
"x1" => 10,
"y1" => 11,
"z1" => 12,
"x2" => 13,
"y2" => 14,
"z2" => 15,
_ => throw new JsonException($"Invalid property {key} for Rectangle3D")
};
if (i < 10)
{
if (objType > -1 && objType != 0)
{
throw new JsonException("Rectangle3D must have a start/end, or x/y/z/w/h/d, but not both.");
}
objType = 0;
data[i] = reader.GetInt32();
if (i == 2)
{
hasZ = true;
}
continue;
}
if (!hasZ)
if (objType > -1 && objType != 1)
{
// Bottom to top?
data[2] = -128;
data[5] = 127;
throw new JsonException("Rectangle3D must have a start/end, or x/y/z/w/h/d, but not both.");
}
return objType == 0
? new Rectangle3D(data[0], data[1], data[2], data[3], data[4], data[5])
: new Rectangle3D(
new Point3D(data[0], data[1], data[2]),
new Point3D(data[3], data[4], data[5])
);
objType = 1;
data[i - 10] = reader.GetInt32();
if (i is 12 or 15)
{
hasZ = true;
}
}
public override Rectangle3D Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
reader.TokenType switch
{
JsonTokenType.StartArray => DeserializeArray(ref reader),
JsonTokenType.StartObject => DeserializeObj(ref reader, options),
_ => throw new JsonException("Invalid Json for Point3D")
};
public override void Write(Utf8JsonWriter writer, Rectangle3D value, JsonSerializerOptions options)
if (!hasZ)
{
writer.WriteStartArray();
writer.WriteNumberValue(value.Start.X);
writer.WriteNumberValue(value.Start.Y);
writer.WriteNumberValue(value.Start.Z);
writer.WriteNumberValue(value.Width);
writer.WriteNumberValue(value.Height);
writer.WriteNumberValue(value.Depth);
writer.WriteEndArray();
// Bottom to top?
data[2] = -128;
data[5] = 127;
}
return objType == 0
? new Rectangle3D(data[0], data[1], data[2], data[3], data[4], data[5])
: new Rectangle3D(
new Point3D(data[0], data[1], data[2]),
new Point3D(data[3], data[4], data[5])
);
}
}
public override Rectangle3D Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
reader.TokenType switch
{
JsonTokenType.StartArray => DeserializeArray(ref reader),
JsonTokenType.StartObject => DeserializeObj(ref reader, options),
_ => throw new JsonException("Invalid Json for Point3D")
};
public override void Write(Utf8JsonWriter writer, Rectangle3D value, JsonSerializerOptions options)
{
writer.WriteStartArray();
writer.WriteNumberValue(value.Start.X);
writer.WriteNumberValue(value.Start.Y);
writer.WriteNumberValue(value.Start.Z);
writer.WriteNumberValue(value.Width);
writer.WriteNumberValue(value.Height);
writer.WriteNumberValue(value.Depth);
writer.WriteEndArray();
}
}

View file

@ -17,13 +17,12 @@ using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Server.Json
{
public class Rectangle3DConverterFactory : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(Rectangle3D);
namespace Server.Json;
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
new Rectangle3DConverter();
}
}
public class Rectangle3DConverterFactory : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(Rectangle3D);
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
new Rectangle3DConverter();
}

View file

@ -17,14 +17,13 @@ using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Server.Json
{
public class TimeSpanConverter : JsonConverter<TimeSpan>
{
public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> TimeSpan.Parse(reader.GetString()!);
namespace Server.Json;
public override void Write(Utf8JsonWriter writer, TimeSpan value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString());
}
}
public class TimeSpanConverter : JsonConverter<TimeSpan>
{
public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> TimeSpan.Parse(reader.GetString()!);
public override void Write(Utf8JsonWriter writer, TimeSpan value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString());
}

View file

@ -17,13 +17,12 @@ using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Server.Json
{
public class TimeSpanConverterFactory : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(TimeSpan);
namespace Server.Json;
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
new TimeSpanConverter();
}
}
public class TimeSpanConverterFactory : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(TimeSpan);
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
new TimeSpanConverter();
}

View file

@ -16,29 +16,31 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using Server.Logging;
namespace Server.Json
namespace Server.Json;
public class TypeConverter : JsonConverter<Type>
{
public class TypeConverter : JsonConverter<Type>
private static readonly ILogger logger = LogFactory.GetLogger(typeof(TypeConverter));
public override Type Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
public override Type Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
if (reader.TokenType != JsonTokenType.String)
{
if (reader.TokenType != JsonTokenType.String)
{
throw new JsonException("The JSON value could not be converted to System.Type");
}
var typeName = reader.GetString();
var type = AssemblyHandler.FindTypeByName(typeName);
if (type == null)
{
Console.WriteLine("Invalid type {0} deserialized", typeName);
}
return type;
throw new JsonException("The JSON value could not be converted to System.Type");
}
public override void Write(Utf8JsonWriter writer, Type value, JsonSerializerOptions options) =>
writer.WriteStringValue(value.FullName);
var typeName = reader.GetString();
var type = AssemblyHandler.FindTypeByName(typeName);
if (type == null)
{
logger.Warning("Attempted to deserialize type {Type} which does not exist.", typeName);
}
return type;
}
public override void Write(Utf8JsonWriter writer, Type value, JsonSerializerOptions options) =>
writer.WriteStringValue(value.FullName);
}

View file

@ -17,13 +17,12 @@ using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Server.Json
{
public class TypeConverterFactory : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(Type);
namespace Server.Json;
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
new TypeConverter();
}
public class TypeConverterFactory : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(Type);
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
new TypeConverter();
}

View file

@ -17,173 +17,172 @@ using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Server.Json
namespace Server.Json;
public class WorldLocationConverter : JsonConverter<WorldLocation>
{
public class WorldLocationConverter : JsonConverter<WorldLocation>
private static Point3DConverter _point3DConverter;
private static MapConverter _mapConverter;
private WorldLocation DeserializeArray(ref Utf8JsonReader reader)
{
private static Point3DConverter _point3DConverter;
private static MapConverter _mapConverter;
Span<int> data = stackalloc int[3];
var count = 0;
var hasMap = false;
Map map = null;
private WorldLocation DeserializeArray(ref Utf8JsonReader reader)
while (true)
{
Span<int> data = stackalloc int[3];
var count = 0;
var hasMap = false;
Map map = null;
while (true)
reader.Read();
if (reader.TokenType == JsonTokenType.EndArray)
{
reader.Read();
if (reader.TokenType == JsonTokenType.EndArray)
break;
}
if (reader.TokenType == JsonTokenType.Number)
{
if (count < 3)
{
break;
data[count] = reader.GetInt32();
}
if (reader.TokenType == JsonTokenType.Number)
else if (count == 3)
{
if (count < 3)
{
data[count] = reader.GetInt32();
}
else if (count == 3)
{
map = Map.Maps[reader.GetInt32()];
hasMap = true;
}
count++;
}
if (reader.TokenType == JsonTokenType.String)
{
var key = reader.GetString();
if (count != 3 || hasMap)
{
throw new JsonException($"Value {key} is not valid for this element.");
}
map = Map.Parse(key);
map = Map.Maps[reader.GetInt32()];
hasMap = true;
break;
}
count++;
}
if (!hasMap || count != 3)
if (reader.TokenType == JsonTokenType.String)
{
throw new JsonException("WorldLocation must be an array of x, y, z, and map");
}
return new WorldLocation(data[0], data[1], data[2], map);
}
private WorldLocation DeserializeObj(ref Utf8JsonReader reader, JsonSerializerOptions options)
{
Span<int> data = stackalloc int[3];
var count = 0;
var hasLoc = false;
var hasXYZ = false;
var hasMap = false;
Map map = null;
while (true)
{
reader.Read();
if (reader.TokenType == JsonTokenType.EndObject)
{
break;
}
if (reader.TokenType != JsonTokenType.PropertyName)
{
throw new JsonException("Invalid Json structure for WorldLocation object");
}
var key = reader.GetString();
var i = key switch
if (count != 3 || hasMap)
{
"x" => 0,
"y" => 1,
"z" => 2,
"loc" => 3,
"map" => 4,
_ => 5
};
if (i == 5)
{
continue;
throw new JsonException($"Value {key} is not valid for this element.");
}
reader.Read();
if (i < 3)
{
if (hasLoc)
{
throw new JsonException("WorldLocation must have loc or x, y, z, but not both");
}
if (reader.TokenType != JsonTokenType.Number)
{
throw new JsonException($"Value for {key} must be a number");
}
hasXYZ = true;
data[i] = reader.GetInt32();
continue;
}
if (i == 3)
{
if (hasXYZ)
{
throw new JsonException("WorldLocation must have loc or x, y, z, but not both");
}
hasLoc = true;
_point3DConverter ??= new Point3DConverter();
var loc = _point3DConverter.Read(ref reader, typeof(Point3D), options);
data[0] = loc.X;
data[1] = loc.Y;
data[2] = loc.Z;
count = 3;
continue;
}
_mapConverter ??= new MapConverter();
map = _mapConverter.Read(ref reader, typeof(Map), options);
map = Map.Parse(key);
hasMap = true;
break;
}
if (!hasMap || count != 3)
{
throw new JsonException("WorldLocation must have an x, y, z, and map properties");
}
return new WorldLocation(data[0], data[1], data[2], map);
}
public override WorldLocation Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
reader.TokenType switch
if (!hasMap || count != 3)
{
throw new JsonException("WorldLocation must be an array of x, y, z, and map");
}
return new WorldLocation(data[0], data[1], data[2], map);
}
private WorldLocation DeserializeObj(ref Utf8JsonReader reader, JsonSerializerOptions options)
{
Span<int> data = stackalloc int[3];
var count = 0;
var hasLoc = false;
var hasXYZ = false;
var hasMap = false;
Map map = null;
while (true)
{
reader.Read();
if (reader.TokenType == JsonTokenType.EndObject)
{
JsonTokenType.StartArray => DeserializeArray(ref reader),
JsonTokenType.StartObject => DeserializeObj(ref reader, options),
_ => throw new JsonException("Invalid Json for Point3D")
break;
}
if (reader.TokenType != JsonTokenType.PropertyName)
{
throw new JsonException("Invalid Json structure for WorldLocation object");
}
var key = reader.GetString();
var i = key switch
{
"x" => 0,
"y" => 1,
"z" => 2,
"loc" => 3,
"map" => 4,
_ => 5
};
public override void Write(Utf8JsonWriter writer, WorldLocation value, JsonSerializerOptions options)
{
writer.WriteStartArray();
writer.WriteNumberValue(value.X);
writer.WriteNumberValue(value.Y);
writer.WriteNumberValue(value.Z);
writer.WriteStringValue(value.Map.ToString());
writer.WriteEndArray();
if (i == 5)
{
continue;
}
reader.Read();
if (i < 3)
{
if (hasLoc)
{
throw new JsonException("WorldLocation must have loc or x, y, z, but not both");
}
if (reader.TokenType != JsonTokenType.Number)
{
throw new JsonException($"Value for {key} must be a number");
}
hasXYZ = true;
data[i] = reader.GetInt32();
continue;
}
if (i == 3)
{
if (hasXYZ)
{
throw new JsonException("WorldLocation must have loc or x, y, z, but not both");
}
hasLoc = true;
_point3DConverter ??= new Point3DConverter();
var loc = _point3DConverter.Read(ref reader, typeof(Point3D), options);
data[0] = loc.X;
data[1] = loc.Y;
data[2] = loc.Z;
count = 3;
continue;
}
_mapConverter ??= new MapConverter();
map = _mapConverter.Read(ref reader, typeof(Map), options);
hasMap = true;
}
if (!hasMap || count != 3)
{
throw new JsonException("WorldLocation must have an x, y, z, and map properties");
}
return new WorldLocation(data[0], data[1], data[2], map);
}
}
public override WorldLocation Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
reader.TokenType switch
{
JsonTokenType.StartArray => DeserializeArray(ref reader),
JsonTokenType.StartObject => DeserializeObj(ref reader, options),
_ => throw new JsonException("Invalid Json for Point3D")
};
public override void Write(Utf8JsonWriter writer, WorldLocation value, JsonSerializerOptions options)
{
writer.WriteStartArray();
writer.WriteNumberValue(value.X);
writer.WriteNumberValue(value.Y);
writer.WriteNumberValue(value.Z);
writer.WriteStringValue(value.Map.ToString());
writer.WriteEndArray();
}
}

View file

@ -17,13 +17,12 @@ using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Server.Json
{
public class WorldLocationConverterFactory : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(WorldLocation);
namespace Server.Json;
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
new WorldLocationConverter();
}
}
public class WorldLocationConverterFactory : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(WorldLocation);
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
new WorldLocationConverter();
}