using System;
using System.Collections.Generic;
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 {
///
/// A set of extensions for dealing with .
///
public static class NetExtensions {
///
/// Writes a to the given net buffer
///
/// The buffer to write to
/// The vector to write
[Obsolete("Lidgren.Network support is deprecated. Consider using LiteNetLib or a custom implementation instead.")]
public static void Write(this NetBuffer buffer, Vector2 vector) {
buffer.Write(vector.X);
buffer.Write(vector.Y);
}
///
/// Reads a from the given net buffer
///
/// The buffer to read from
/// The read vector
[Obsolete("Lidgren.Network support is deprecated. Consider using LiteNetLib or a custom implementation instead.")]
public static Vector2 ReadVector2(this NetBuffer buffer) {
return new Vector2(buffer.ReadFloat(), buffer.ReadFloat());
}
///
/// Writes a to the given net buffer
///
/// The buffer to write to
/// The guid to write
[Obsolete("Lidgren.Network support is deprecated. Consider using LiteNetLib or a custom implementation instead.")]
public static void Write(this NetBuffer buffer, Guid guid) {
buffer.Write(guid.ToByteArray());
}
///
/// Reads a from the given net buffer
///
/// The buffer to read from
/// The read guid
[Obsolete("Lidgren.Network support is deprecated. Consider using LiteNetLib or a custom implementation instead.")]
public static Guid ReadGuid(this NetBuffer buffer) {
return new Guid(buffer.ReadBytes(16));
}
///
/// Writes a to the given net buffer
///
/// The buffer to write to
/// The direction to write
[Obsolete("Lidgren.Network support is deprecated. Consider using LiteNetLib or a custom implementation instead.")]
public static void Write(this NetBuffer buffer, Direction2 direction) {
buffer.Write((short) direction);
}
///
/// Reads a from the given net buffer
///
/// The buffer to read from
/// The read direction
[Obsolete("Lidgren.Network support is deprecated. Consider using LiteNetLib or a custom implementation instead.")]
public static Direction2 ReadDirection(this NetBuffer buffer) {
return (Direction2) buffer.ReadInt16();
}
///
/// Writes a generic object to the given net buffer using a .
///
/// The buffer to write to
/// The object to write
/// The JSON serializer to use
/// The type of object written
[Obsolete("Lidgren.Network support is deprecated. Consider using LiteNetLib or a custom implementation instead.")]
public static void WriteObject(this NetBuffer buffer, T obj, JsonSerializer serializer) {
if (EqualityComparer.Default.Equals(obj, default)) {
buffer.Write(0);
return;
}
using (var memory = new MemoryStream()) {
using (var stream = new DeflateStream(memory, CompressionLevel.Fastest, true))
serializer.Serialize(new BsonDataWriter(stream), obj, typeof(T));
var arr = memory.ToArray();
buffer.Write(arr.Length);
buffer.Write(arr);
}
}
///
/// Reads a generic object from the given buffer using a .
///
/// The buffer to read from
/// The JSON serializer to use
/// The type of object read
/// The read object
[Obsolete("Lidgren.Network support is deprecated. Consider using LiteNetLib or a custom implementation instead.")]
public static T ReadObject(this NetBuffer buffer, JsonSerializer serializer) {
var length = buffer.ReadInt32();
if (length <= 0)
return default;
var arr = buffer.ReadBytes(length);
using (var memory = new MemoryStream(arr)) {
using (var stream = new DeflateStream(memory, CompressionMode.Decompress, true))
return serializer.Deserialize(new BsonDataReader(stream));
}
}
}
}