using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
|
using System.Runtime.Serialization;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Net.NetworkInformation;
|
|
using System.Net.Sockets;
|
|
using UnityEngine;
|
|
|
|
namespace Networking
|
|
{
|
|
|
|
public static class Utility
|
|
{
|
|
public static byte[] ObjectToByteArray<T>(T obj)
|
|
{
|
|
if (obj == null)
|
|
return null;
|
|
BinaryFormatter bf = new BinaryFormatter();
|
|
|
|
SurrogateSelector surrogateSelector = new SurrogateSelector();
|
|
Vector3SerializationSurrogate vector3SS = new Vector3SerializationSurrogate();
|
|
ColorSerializationSurrogate colorSS = new ColorSerializationSurrogate();
|
|
surrogateSelector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), vector3SS);
|
|
surrogateSelector.AddSurrogate(typeof(Color), new StreamingContext(StreamingContextStates.All), colorSS);
|
|
|
|
bf.SurrogateSelector = surrogateSelector;
|
|
|
|
MemoryStream ms = new MemoryStream();
|
|
bf.Serialize(ms, obj);
|
|
return ms.ToArray();
|
|
}
|
|
|
|
public static T ByteArrayToObject<T>(byte[] arrBytes)
|
|
{
|
|
MemoryStream memStream = new MemoryStream();
|
|
BinaryFormatter bf = new BinaryFormatter();
|
|
|
|
SurrogateSelector surrogateSelector = new SurrogateSelector();
|
|
Vector3SerializationSurrogate vector3SS = new Vector3SerializationSurrogate();
|
|
ColorSerializationSurrogate colorSS = new ColorSerializationSurrogate();
|
|
surrogateSelector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), vector3SS);
|
|
surrogateSelector.AddSurrogate(typeof(Color), new StreamingContext(StreamingContextStates.All), colorSS);
|
|
|
|
bf.SurrogateSelector = surrogateSelector;
|
|
|
|
memStream.Write(arrBytes, 0, arrBytes.Length);
|
|
memStream.Seek(0, SeekOrigin.Begin);
|
|
T obj = (T)bf.Deserialize(memStream);
|
|
return obj;
|
|
}
|
|
|
|
public enum ADDRESSFAM
|
|
{
|
|
IPv4, IPv6
|
|
}
|
|
|
|
//https://stackoverflow.com/questions/51975799/how-to-get-ip-address-of-device-in-unity-2018
|
|
public static string GetIP(ADDRESSFAM Addfam)
|
|
{
|
|
//Return null if ADDRESSFAM is Ipv6 but Os does not support it
|
|
if (Addfam == ADDRESSFAM.IPv6 && !Socket.OSSupportsIPv6)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
string output = "";
|
|
|
|
foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
|
|
{
|
|
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
|
|
NetworkInterfaceType _type1 = NetworkInterfaceType.Wireless80211;
|
|
NetworkInterfaceType _type2 = NetworkInterfaceType.Ethernet;
|
|
|
|
if ((item.NetworkInterfaceType == _type1 || item.NetworkInterfaceType == _type2) && item.OperationalStatus == OperationalStatus.Up)
|
|
#endif
|
|
{
|
|
foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
|
|
{
|
|
//IPv4
|
|
if (Addfam == ADDRESSFAM.IPv4)
|
|
{
|
|
if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
|
|
{
|
|
output = ip.Address.ToString();
|
|
}
|
|
}
|
|
|
|
//IPv6
|
|
else if (Addfam == ADDRESSFAM.IPv6)
|
|
{
|
|
if (ip.Address.AddressFamily == AddressFamily.InterNetworkV6)
|
|
{
|
|
output = ip.Address.ToString();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return output;
|
|
}
|
|
|
|
}
|
|
}
|