2019-12-20 15:03:33 +01:00
|
|
|
using System;
|
2019-12-20 23:32:40 +01:00
|
|
|
using System.Collections.Generic;
|
2019-12-20 15:03:33 +01:00
|
|
|
using System.IO;
|
|
|
|
using System.IO.Compression;
|
|
|
|
using Lidgren.Network;
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using MLEM.Misc;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using Newtonsoft.Json.Bson;
|
|
|
|
|
|
|
|
namespace MLEM.Data {
|
2020-05-22 20:32:38 +02:00
|
|
|
/// <summary>
|
|
|
|
/// A set of extensions for dealing with <see cref="NetBuffer"/>.
|
|
|
|
/// </summary>
|
2019-12-20 15:03:33 +01:00
|
|
|
public static class NetExtensions {
|
|
|
|
|
2020-05-22 20:32:38 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Writes a <see cref="Vector2"/> to the given net buffer
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="buffer">The buffer to write to</param>
|
|
|
|
/// <param name="vector">The vector to write</param>
|
2021-12-03 12:52:26 +01:00
|
|
|
[Obsolete("Lidgren.Network support is deprecated. Consider using LiteNetLib or a custom implementation instead.")]
|
2019-12-20 15:03:33 +01:00
|
|
|
public static void Write(this NetBuffer buffer, Vector2 vector) {
|
|
|
|
buffer.Write(vector.X);
|
|
|
|
buffer.Write(vector.Y);
|
|
|
|
}
|
|
|
|
|
2020-05-22 20:32:38 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Reads a <see cref="Vector2"/> from the given net buffer
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="buffer">The buffer to read from</param>
|
|
|
|
/// <returns>The read vector</returns>
|
2021-12-03 12:52:26 +01:00
|
|
|
[Obsolete("Lidgren.Network support is deprecated. Consider using LiteNetLib or a custom implementation instead.")]
|
2019-12-20 15:03:33 +01:00
|
|
|
public static Vector2 ReadVector2(this NetBuffer buffer) {
|
|
|
|
return new Vector2(buffer.ReadFloat(), buffer.ReadFloat());
|
|
|
|
}
|
|
|
|
|
2020-05-22 20:32:38 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Writes a <see cref="Guid"/> to the given net buffer
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="buffer">The buffer to write to</param>
|
|
|
|
/// <param name="guid">The guid to write</param>
|
2021-12-03 12:52:26 +01:00
|
|
|
[Obsolete("Lidgren.Network support is deprecated. Consider using LiteNetLib or a custom implementation instead.")]
|
2019-12-20 15:03:33 +01:00
|
|
|
public static void Write(this NetBuffer buffer, Guid guid) {
|
|
|
|
buffer.Write(guid.ToByteArray());
|
|
|
|
}
|
|
|
|
|
2020-05-22 20:32:38 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Reads a <see cref="Guid"/> from the given net buffer
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="buffer">The buffer to read from</param>
|
|
|
|
/// <returns>The read guid</returns>
|
2021-12-03 12:52:26 +01:00
|
|
|
[Obsolete("Lidgren.Network support is deprecated. Consider using LiteNetLib or a custom implementation instead.")]
|
2019-12-20 15:03:33 +01:00
|
|
|
public static Guid ReadGuid(this NetBuffer buffer) {
|
|
|
|
return new Guid(buffer.ReadBytes(16));
|
|
|
|
}
|
|
|
|
|
2020-05-22 20:32:38 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Writes a <see cref="Direction2"/> to the given net buffer
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="buffer">The buffer to write to</param>
|
|
|
|
/// <param name="direction">The direction to write</param>
|
2021-12-03 12:52:26 +01:00
|
|
|
[Obsolete("Lidgren.Network support is deprecated. Consider using LiteNetLib or a custom implementation instead.")]
|
2019-12-20 15:03:33 +01:00
|
|
|
public static void Write(this NetBuffer buffer, Direction2 direction) {
|
|
|
|
buffer.Write((short) direction);
|
|
|
|
}
|
|
|
|
|
2020-05-22 20:32:38 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Reads a <see cref="Direction2"/> from the given net buffer
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="buffer">The buffer to read from</param>
|
|
|
|
/// <returns>The read direction</returns>
|
2021-12-03 12:52:26 +01:00
|
|
|
[Obsolete("Lidgren.Network support is deprecated. Consider using LiteNetLib or a custom implementation instead.")]
|
2019-12-20 15:03:33 +01:00
|
|
|
public static Direction2 ReadDirection(this NetBuffer buffer) {
|
|
|
|
return (Direction2) buffer.ReadInt16();
|
|
|
|
}
|
|
|
|
|
2020-05-22 20:32:38 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Writes a generic object to the given net buffer using a <see cref="JsonSerializer"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="buffer">The buffer to write to</param>
|
|
|
|
/// <param name="obj">The object to write</param>
|
|
|
|
/// <param name="serializer">The JSON serializer to use</param>
|
|
|
|
/// <typeparam name="T">The type of object written</typeparam>
|
2021-12-03 12:52:26 +01:00
|
|
|
[Obsolete("Lidgren.Network support is deprecated. Consider using LiteNetLib or a custom implementation instead.")]
|
2019-12-20 15:03:33 +01:00
|
|
|
public static void WriteObject<T>(this NetBuffer buffer, T obj, JsonSerializer serializer) {
|
2019-12-20 23:35:38 +01:00
|
|
|
if (EqualityComparer<T>.Default.Equals(obj, default)) {
|
2019-12-20 23:32:40 +01:00
|
|
|
buffer.Write(0);
|
2019-12-20 23:35:38 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-12-20 15:03:33 +01:00
|
|
|
using (var memory = new MemoryStream()) {
|
2020-01-27 22:31:09 +01:00
|
|
|
using (var stream = new DeflateStream(memory, CompressionLevel.Fastest, true))
|
|
|
|
serializer.Serialize(new BsonDataWriter(stream), obj, typeof(T));
|
2019-12-20 15:07:25 +01:00
|
|
|
var arr = memory.ToArray();
|
|
|
|
buffer.Write(arr.Length);
|
|
|
|
buffer.Write(arr);
|
2019-12-20 15:03:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-22 20:32:38 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Reads a generic object from the given buffer using a <see cref="JsonSerializer"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="buffer">The buffer to read from</param>
|
|
|
|
/// <param name="serializer">The JSON serializer to use</param>
|
|
|
|
/// <typeparam name="T">The type of object read</typeparam>
|
|
|
|
/// <returns>The read object</returns>
|
2021-12-03 12:52:26 +01:00
|
|
|
[Obsolete("Lidgren.Network support is deprecated. Consider using LiteNetLib or a custom implementation instead.")]
|
2019-12-20 15:03:33 +01:00
|
|
|
public static T ReadObject<T>(this NetBuffer buffer, JsonSerializer serializer) {
|
2019-12-20 15:07:25 +01:00
|
|
|
var length = buffer.ReadInt32();
|
2019-12-20 23:32:40 +01:00
|
|
|
if (length <= 0)
|
|
|
|
return default;
|
2019-12-20 15:07:25 +01:00
|
|
|
var arr = buffer.ReadBytes(length);
|
|
|
|
using (var memory = new MemoryStream(arr)) {
|
2020-01-27 22:31:09 +01:00
|
|
|
using (var stream = new DeflateStream(memory, CompressionMode.Decompress, true))
|
|
|
|
return serializer.Deserialize<T>(new BsonDataReader(stream));
|
2019-12-20 15:03:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|