1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 20:58:34 +01:00
MLEM/MLEM.Data/NetExtensions.cs

112 lines
4.4 KiB
C#
Raw Normal View History

2019-12-20 15:03:33 +01:00
using System;
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 {
/// <summary>
/// A set of extensions for dealing with <see cref="NetBuffer"/>.
/// </summary>
2019-12-20 15:03:33 +01:00
public static class NetExtensions {
/// <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>
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);
}
/// <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>
2019-12-20 15:03:33 +01:00
public static Vector2 ReadVector2(this NetBuffer buffer) {
return new Vector2(buffer.ReadFloat(), buffer.ReadFloat());
}
/// <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>
2019-12-20 15:03:33 +01:00
public static void Write(this NetBuffer buffer, Guid guid) {
buffer.Write(guid.ToByteArray());
}
/// <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>
2019-12-20 15:03:33 +01:00
public static Guid ReadGuid(this NetBuffer buffer) {
return new Guid(buffer.ReadBytes(16));
}
/// <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>
2019-12-20 15:03:33 +01:00
public static void Write(this NetBuffer buffer, Direction2 direction) {
buffer.Write((short) direction);
}
/// <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>
2019-12-20 15:03:33 +01:00
public static Direction2 ReadDirection(this NetBuffer buffer) {
return (Direction2) buffer.ReadInt16();
}
/// <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>
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)) {
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
}
}
/// <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>
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();
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
}
}
}
}