quantum-space-buddies/QSB/Messaging/PlayerLeaveMessage.cs

40 lines
1.2 KiB
C#
Raw Normal View History

using System;
2020-09-02 10:17:04 +00:00
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine.Networking;
2020-03-06 18:03:35 +00:00
2020-08-09 11:38:35 +00:00
namespace QSB.Messaging
2020-03-06 18:03:35 +00:00
{
2020-08-09 11:19:51 +00:00
public class PlayerLeaveMessage : PlayerMessage
2020-03-06 18:03:35 +00:00
{
2020-09-02 10:17:04 +00:00
public NetworkInstanceId[] NetIds { get; set; }
public override void Deserialize(NetworkReader reader)
{
base.Deserialize(reader);
2020-09-02 10:17:04 +00:00
NetIds = DeserializeFromString<NetworkInstanceId[]>(reader.ReadString());
}
public override void Serialize(NetworkWriter writer)
{
base.Serialize(writer);
2020-09-02 10:17:04 +00:00
writer.Write(SerializeToString(NetIds));
}
public static string SerializeToString<T>(T value)
{
using (var stream = new MemoryStream())
{
(new BinaryFormatter()).Serialize(stream, value);
stream.Flush();
return Convert.ToBase64String(stream.ToArray());
}
}
public static T DeserializeFromString<T>(string data)
{
byte[] bytes = Convert.FromBase64String(data);
using (var stream = new MemoryStream(bytes))
return (T)(new BinaryFormatter()).Deserialize(stream);
}
2020-03-06 18:03:35 +00:00
}
2020-08-09 11:19:51 +00:00
}