Replaced ALL usage of BinaryWriter / BinaryReader with BitReader & BitWriter

This commit is contained in:
Albin Corén 2018-04-17 23:23:24 +02:00
parent 39c262e9e0
commit 2acb20e8ea
7 changed files with 679 additions and 847 deletions

View File

@ -7,7 +7,6 @@
{
Bool,
Byte,
Char,
Double,
Single,
Int,

View File

@ -4,7 +4,6 @@ using UnityEngine;
using System.Reflection;
using MLAPI.Attributes;
using System.Linq;
using System.IO;
using MLAPI.Data;
using MLAPI.NetworkingManagerComponents.Binary;
using MLAPI.NetworkingManagerComponents.Core;
@ -245,18 +244,6 @@ namespace MLAPI.MonoBehaviours.Core
HookMethod = method
});
}
else if (sortedFields[i].FieldType == typeof(char))
{
syncedVarFields.Add(new SyncedVarField()
{
Dirty = false,
Target = ((SyncedVar)sortedFields[i].GetCustomAttributes(typeof(SyncedVar), true)[0]).target,
FieldInfo = sortedFields[i],
FieldType = FieldType.Char,
FieldValue = sortedFields[i].GetValue(this),
HookMethod = method
});
}
else if (sortedFields[i].FieldType == typeof(double))
{
syncedVarFields.Add(new SyncedVarField()
@ -450,95 +437,88 @@ namespace MLAPI.MonoBehaviours.Core
if (syncedVarFields.Count == 0)
return;
using (MemoryStream stream = new MemoryStream())
using (BitWriter writer = new BitWriter())
{
using (BinaryWriter writer = new BinaryWriter(stream))
//Write all indexes
int syncCount = 0;
for (int i = 0; i < syncedVarFields.Count; i++)
{
//Write all indexes
int syncCount = 0;
for (int i = 0; i < syncedVarFields.Count; i++)
if (!syncedVarFields[i].Target)
syncCount++;
else if (syncedVarFields[i].Target && ownerClientId == clientId)
syncCount++;
}
if (syncCount == 0)
return;
writer.WriteByte((byte)syncCount);
writer.WriteUInt(networkId); //NetId
writer.WriteUShort(networkedObject.GetOrderIndex(this)); //Behaviour OrderIndex
for (byte i = 0; i < syncedVarFields.Count; i++)
{
if (syncedVarFields[i].Target && clientId != ownerClientId)
continue;
writer.WriteByte(i); //FieldIndex
switch (syncedVarFields[i].FieldType)
{
if (!syncedVarFields[i].Target)
syncCount++;
else if (syncedVarFields[i].Target && ownerClientId == clientId)
syncCount++;
}
if (syncCount == 0)
return;
writer.Write((byte)syncCount);
writer.Write(networkId); //NetId
writer.Write(networkedObject.GetOrderIndex(this)); //Behaviour OrderIndex
for (byte i = 0; i < syncedVarFields.Count; i++)
{
if (syncedVarFields[i].Target && clientId != ownerClientId)
continue;
writer.Write(i); //FieldIndex
switch (syncedVarFields[i].FieldType)
{
case FieldType.Bool:
writer.Write((bool)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Byte:
writer.Write((byte)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Char:
writer.Write((char)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Double:
writer.Write((double)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Single:
writer.Write((float)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Int:
writer.Write((int)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Long:
writer.Write((long)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.SByte:
writer.Write((sbyte)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Short:
writer.Write((short)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.UInt:
writer.Write((uint)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.ULong:
writer.Write((ulong)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.UShort:
writer.Write((ushort)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.String:
writer.Write((string)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Vector3:
Vector3 vector3 = (Vector3)syncedVarFields[i].FieldInfo.GetValue(this);
writer.Write(vector3.x);
writer.Write(vector3.y);
writer.Write(vector3.z);
break;
case FieldType.Vector2:
Vector2 vector2 = (Vector2)syncedVarFields[i].FieldInfo.GetValue(this);
writer.Write(vector2.x);
writer.Write(vector2.y);
break;
case FieldType.Quaternion:
Vector3 euler = ((Quaternion)syncedVarFields[i].FieldInfo.GetValue(this)).eulerAngles;
writer.Write(euler.x);
writer.Write(euler.y);
writer.Write(euler.z);
break;
case FieldType.ByteArray:
writer.Write((ushort)((byte[])syncedVarFields[i].FieldInfo.GetValue(this)).Length);
writer.Write((byte[])syncedVarFields[i].FieldInfo.GetValue(this));
break;
}
case FieldType.Bool:
writer.WriteBool((bool)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Byte:
writer.WriteByte((byte)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Double:
writer.WriteDouble((double)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Single:
writer.WriteFloat((float)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Int:
writer.WriteInt((int)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Long:
writer.WriteLong((long)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.SByte:
writer.WriteSByte((sbyte)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Short:
writer.WriteShort((short)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.UInt:
writer.WriteUInt((uint)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.ULong:
writer.WriteULong((ulong)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.UShort:
writer.WriteUShort((ushort)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.String:
writer.WriteString((string)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Vector3:
Vector3 vector3 = (Vector3)syncedVarFields[i].FieldInfo.GetValue(this);
writer.WriteFloat(vector3.x);
writer.WriteFloat(vector3.y);
writer.WriteFloat(vector3.z);
break;
case FieldType.Vector2:
Vector2 vector2 = (Vector2)syncedVarFields[i].FieldInfo.GetValue(this);
writer.WriteFloat(vector2.x);
writer.WriteFloat(vector2.y);
break;
case FieldType.Quaternion:
Vector3 euler = ((Quaternion)syncedVarFields[i].FieldInfo.GetValue(this)).eulerAngles;
writer.WriteFloat(euler.x);
writer.WriteFloat(euler.y);
writer.WriteFloat(euler.z);
break;
case FieldType.ByteArray:
writer.WriteByteArray((byte[])syncedVarFields[i].FieldInfo.GetValue(this));
break;
}
}
InternalMessageHandler.Send(clientId, "MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", stream.ToArray());
InternalMessageHandler.Send(clientId, "MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer.Finalize());
}
}
@ -571,177 +551,162 @@ namespace MLAPI.MonoBehaviours.Core
if (dirtyTargets == 0)
{
//It's sync time!
using (MemoryStream stream = new MemoryStream())
using (BitWriter writer = new BitWriter())
{
using (BinaryWriter writer = new BinaryWriter(stream))
//Write all indexes
writer.WriteByte(totalDirtyCount);
writer.WriteUInt(networkId); //NetId
writer.WriteUShort(networkedObject.GetOrderIndex(this)); //Behaviour OrderIndex
for (byte i = 0; i < syncedVarFields.Count; i++)
{
//Write all indexes
writer.Write(totalDirtyCount);
writer.Write(networkId); //NetId
writer.Write(networkedObject.GetOrderIndex(this)); //Behaviour OrderIndex
for (byte i = 0; i < syncedVarFields.Count; i++)
//Writes all the indexes of the dirty syncvars.
if (syncedVarFields[i].Dirty == true)
{
//Writes all the indexes of the dirty syncvars.
if (syncedVarFields[i].Dirty == true)
writer.WriteByte(i); //FieldIndex
switch (syncedVarFields[i].FieldType)
{
writer.Write(i); //FieldIndex
switch (syncedVarFields[i].FieldType)
{
case FieldType.Bool:
writer.Write((bool)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Byte:
writer.Write((byte)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Char:
writer.Write((char)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Double:
writer.Write((double)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Single:
writer.Write((float)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Int:
writer.Write((int)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Long:
writer.Write((long)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.SByte:
writer.Write((sbyte)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Short:
writer.Write((short)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.UInt:
writer.Write((uint)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.ULong:
writer.Write((ulong)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.UShort:
writer.Write((ushort)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.String:
writer.Write((string)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Vector3:
Vector3 vector3 = (Vector3)syncedVarFields[i].FieldInfo.GetValue(this);
writer.Write(vector3.x);
writer.Write(vector3.y);
writer.Write(vector3.z);
break;
case FieldType.Vector2:
Vector2 vector2 = (Vector2)syncedVarFields[i].FieldInfo.GetValue(this);
writer.Write(vector2.x);
writer.Write(vector2.y);
break;
case FieldType.Quaternion:
Vector3 euler = ((Quaternion)syncedVarFields[i].FieldInfo.GetValue(this)).eulerAngles;
writer.Write(euler.x);
writer.Write(euler.y);
writer.Write(euler.z);
break;
case FieldType.ByteArray:
writer.Write((ushort)((byte[])syncedVarFields[i].FieldInfo.GetValue(this)).Length);
writer.Write((byte[])syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Bool:
writer.WriteBool((bool)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Byte:
writer.WriteByte((byte)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Double:
writer.WriteDouble((double)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Single:
writer.WriteFloat((float)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Int:
writer.WriteInt((int)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Long:
writer.WriteLong((long)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.SByte:
writer.WriteSByte((sbyte)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Short:
writer.WriteShort((short)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.UInt:
writer.WriteUInt((uint)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.ULong:
writer.WriteULong((ulong)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.UShort:
writer.WriteUShort((ushort)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.String:
writer.WriteString((string)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Vector3:
Vector3 vector3 = (Vector3)syncedVarFields[i].FieldInfo.GetValue(this);
writer.WriteFloat(vector3.x);
writer.WriteFloat(vector3.y);
writer.WriteFloat(vector3.z);
break;
case FieldType.Vector2:
Vector2 vector2 = (Vector2)syncedVarFields[i].FieldInfo.GetValue(this);
writer.WriteFloat(vector2.x);
writer.WriteFloat(vector2.y);
break;
case FieldType.Quaternion:
Vector3 euler = ((Quaternion)syncedVarFields[i].FieldInfo.GetValue(this)).eulerAngles;
writer.WriteFloat(euler.x);
writer.WriteFloat(euler.y);
writer.WriteFloat(euler.z);
break;
case FieldType.ByteArray:
writer.WriteByteArray((byte[])syncedVarFields[i].FieldInfo.GetValue(this));
break;
}
syncedVarFields[i].FieldValue = syncedVarFields[i].FieldInfo.GetValue(this);
syncedVarFields[i].Dirty = false;
}
syncedVarFields[i].FieldValue = syncedVarFields[i].FieldInfo.GetValue(this);
syncedVarFields[i].Dirty = false;
}
}
InternalMessageHandler.Send("MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", stream.ToArray());
InternalMessageHandler.Send("MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer.Finalize());
}
}
else
{
//It's sync time. This is the target receivers packet.
using (MemoryStream stream = new MemoryStream())
using (BitWriter writer = new BitWriter())
{
using (BinaryWriter writer = new BinaryWriter(stream))
//Write all indexes
writer.WriteByte(totalDirtyCount);
writer.WriteUInt(networkId); //NetId
writer.WriteUShort(networkedObject.GetOrderIndex(this)); //Behaviour OrderIndex
for (byte i = 0; i < syncedVarFields.Count; i++)
{
//Write all indexes
writer.Write(totalDirtyCount);
writer.Write(networkId); //NetId
writer.Write(networkedObject.GetOrderIndex(this)); //Behaviour OrderIndex
for (byte i = 0; i < syncedVarFields.Count; i++)
//Writes all the indexes of the dirty syncvars.
if (syncedVarFields[i].Dirty == true)
{
//Writes all the indexes of the dirty syncvars.
if (syncedVarFields[i].Dirty == true)
writer.WriteByte(i); //FieldIndex
switch (syncedVarFields[i].FieldType)
{
writer.Write(i); //FieldIndex
switch (syncedVarFields[i].FieldType)
{
case FieldType.Bool:
writer.Write((bool)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Byte:
writer.Write((byte)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Char:
writer.Write((char)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Double:
writer.Write((double)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Single:
writer.Write((float)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Int:
writer.Write((int)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Long:
writer.Write((long)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.SByte:
writer.Write((sbyte)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Short:
writer.Write((short)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.UInt:
writer.Write((uint)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.ULong:
writer.Write((ulong)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.UShort:
writer.Write((ushort)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.String:
writer.Write((string)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Vector3:
Vector3 vector3 = (Vector3)syncedVarFields[i].FieldInfo.GetValue(this);
writer.Write(vector3.x);
writer.Write(vector3.y);
writer.Write(vector3.z);
break;
case FieldType.Vector2:
Vector2 vector2 = (Vector2)syncedVarFields[i].FieldInfo.GetValue(this);
writer.Write(vector2.x);
writer.Write(vector2.y);
break;
case FieldType.Quaternion:
Vector3 euler = ((Quaternion)syncedVarFields[i].FieldInfo.GetValue(this)).eulerAngles;
writer.Write(euler.x);
writer.Write(euler.y);
writer.Write(euler.z);
break;
case FieldType.ByteArray:
writer.Write((ushort)((byte[])syncedVarFields[i].FieldInfo.GetValue(this)).Length);
writer.Write((byte[])syncedVarFields[i].FieldInfo.GetValue(this));
break;
}
case FieldType.Bool:
writer.WriteBool((bool)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Byte:
writer.WriteByte((byte)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Double:
writer.WriteDouble((double)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Single:
writer.WriteFloat((float)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Int:
writer.WriteInt((int)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Long:
writer.WriteLong((long)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.SByte:
writer.WriteSByte((sbyte)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Short:
writer.WriteShort((short)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.UInt:
writer.WriteUInt((uint)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.ULong:
writer.WriteULong((ulong)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.UShort:
writer.WriteUShort((ushort)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.String:
writer.WriteString((string)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Vector3:
Vector3 vector3 = (Vector3)syncedVarFields[i].FieldInfo.GetValue(this);
writer.WriteFloat(vector3.x);
writer.WriteFloat(vector3.y);
writer.WriteFloat(vector3.z);
break;
case FieldType.Vector2:
Vector2 vector2 = (Vector2)syncedVarFields[i].FieldInfo.GetValue(this);
writer.WriteFloat(vector2.x);
writer.WriteFloat(vector2.y);
break;
case FieldType.Quaternion:
Vector3 euler = ((Quaternion)syncedVarFields[i].FieldInfo.GetValue(this)).eulerAngles;
writer.WriteFloat(euler.x);
writer.WriteFloat(euler.y);
writer.WriteFloat(euler.z);
break;
case FieldType.ByteArray:
writer.WriteByteArray((byte[])syncedVarFields[i].FieldInfo.GetValue(this));
break;
}
}
}
InternalMessageHandler.Send(ownerClientId, "MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", stream.ToArray()); //Send only to target
InternalMessageHandler.Send(ownerClientId, "MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer.Finalize()); //Send only to target
}
if (nonTargetDirtyCount == 0)
@ -756,81 +721,75 @@ namespace MLAPI.MonoBehaviours.Core
}
//It's sync time. This is the NON target receivers packet.
using (MemoryStream stream = new MemoryStream())
using (BitWriter writer = new BitWriter())
{
using (BinaryWriter writer = new BinaryWriter(stream))
{
//Write all indexes
writer.Write(nonTargetDirtyCount);
writer.Write(networkId); //NetId
writer.Write(networkedObject.GetOrderIndex(this)); //Behaviour OrderIndex
writer.WriteByte(nonTargetDirtyCount);
writer.WriteUInt(networkId); //NetId
writer.WriteUShort(networkedObject.GetOrderIndex(this)); //Behaviour OrderIndex
for (byte i = 0; i < syncedVarFields.Count; i++)
{
//Writes all the indexes of the dirty syncvars.
if (syncedVarFields[i].Dirty == true && !syncedVarFields[i].Target)
{
writer.Write(i); //FieldIndex
writer.WriteByte(i); //FieldIndex
switch (syncedVarFields[i].FieldType)
{
case FieldType.Bool:
writer.Write((bool)syncedVarFields[i].FieldInfo.GetValue(this));
writer.WriteBool((bool)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Byte:
writer.Write((byte)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Char:
writer.Write((char)syncedVarFields[i].FieldInfo.GetValue(this));
writer.WriteByte((byte)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Double:
writer.Write((double)syncedVarFields[i].FieldInfo.GetValue(this));
writer.WriteDouble((double)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Single:
writer.Write((float)syncedVarFields[i].FieldInfo.GetValue(this));
writer.WriteFloat((float)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Int:
writer.Write((int)syncedVarFields[i].FieldInfo.GetValue(this));
writer.WriteInt((int)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Long:
writer.Write((long)syncedVarFields[i].FieldInfo.GetValue(this));
writer.WriteLong((long)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.SByte:
writer.Write((sbyte)syncedVarFields[i].FieldInfo.GetValue(this));
writer.WriteSByte((sbyte)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Short:
writer.Write((short)syncedVarFields[i].FieldInfo.GetValue(this));
writer.WriteShort((short)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.UInt:
writer.Write((uint)syncedVarFields[i].FieldInfo.GetValue(this));
writer.WriteUInt((uint)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.ULong:
writer.Write((ulong)syncedVarFields[i].FieldInfo.GetValue(this));
writer.WriteULong((ulong)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.UShort:
writer.Write((ushort)syncedVarFields[i].FieldInfo.GetValue(this));
writer.WriteUShort((ushort)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.String:
writer.Write((string)syncedVarFields[i].FieldInfo.GetValue(this));
writer.WriteString((string)syncedVarFields[i].FieldInfo.GetValue(this));
break;
case FieldType.Vector3:
Vector3 vector3 = (Vector3)syncedVarFields[i].FieldInfo.GetValue(this);
writer.Write(vector3.x);
writer.Write(vector3.y);
writer.Write(vector3.z);
writer.WriteFloat(vector3.x);
writer.WriteFloat(vector3.y);
writer.WriteFloat(vector3.z);
break;
case FieldType.Vector2:
Vector2 vector2 = (Vector2)syncedVarFields[i].FieldInfo.GetValue(this);
writer.Write(vector2.x);
writer.Write(vector2.y);
writer.WriteFloat(vector2.x);
writer.WriteFloat(vector2.y);
break;
case FieldType.Quaternion:
Vector3 euler = ((Quaternion)syncedVarFields[i].FieldInfo.GetValue(this)).eulerAngles;
writer.Write(euler.x);
writer.Write(euler.y);
writer.Write(euler.z);
writer.WriteFloat(euler.x);
writer.WriteFloat(euler.y);
writer.WriteFloat(euler.z);
break;
case FieldType.ByteArray:
writer.Write((ushort)((byte[])syncedVarFields[i].FieldInfo.GetValue(this)).Length);
writer.Write((byte[])syncedVarFields[i].FieldInfo.GetValue(this));
writer.WriteByteArray((byte[])syncedVarFields[i].FieldInfo.GetValue(this));
break;
}
@ -838,8 +797,8 @@ namespace MLAPI.MonoBehaviours.Core
syncedVarFields[i].Dirty = false;
}
}
}
InternalMessageHandler.Send("MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", stream.ToArray(), ownerClientId); // Send to everyone except target.
InternalMessageHandler.Send("MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer.Finalize(), ownerClientId); // Send to everyone except target.
}
}
lastSyncTime = NetworkingManager.singleton.NetworkTime;
@ -866,12 +825,6 @@ namespace MLAPI.MonoBehaviours.Core
else
syncedVarFields[i].Dirty = false; //Up to date
break;
case FieldType.Char:
if ((char)syncedVarFields[i].FieldInfo.GetValue(this) != (char)syncedVarFields[i].FieldValue)
syncedVarFields[i].Dirty = true; //This fields value is out of sync!
else
syncedVarFields[i].Dirty = false; //Up to date
break;
case FieldType.Double:
if ((double)syncedVarFields[i].FieldInfo.GetValue(this) != (double)syncedVarFields[i].FieldValue)
syncedVarFields[i].Dirty = true; //This fields value is out of sync!

View File

@ -2,7 +2,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using System.Linq;
@ -745,29 +744,17 @@ namespace MLAPI.MonoBehaviours.Core
diffiePublic = clientDiffieHellman.GetPublicKey();
}
int sizeOfStream = 32;
if (NetworkConfig.ConnectionApproval)
sizeOfStream += 2 + NetworkConfig.ConnectionData.Length;
if (NetworkConfig.EnableEncryption)
sizeOfStream += 2 + diffiePublic.Length;
using (MemoryStream writeStream = new MemoryStream(sizeOfStream))
using (BitWriter writer = new BitWriter())
{
using (BinaryWriter writer = new BinaryWriter(writeStream))
{
writer.Write(NetworkConfig.GetConfig());
if (NetworkConfig.EnableEncryption)
{
writer.Write((ushort)diffiePublic.Length);
writer.Write(diffiePublic);
}
if (NetworkConfig.ConnectionApproval)
{
writer.Write((ushort)NetworkConfig.ConnectionData.Length);
writer.Write(NetworkConfig.ConnectionData);
}
}
InternalMessageHandler.Send(netId.GetClientId(), "MLAPI_CONNECTION_REQUEST", "MLAPI_INTERNAL", writeStream.GetBuffer(), null, null, true);
writer.WriteByteArray(NetworkConfig.GetConfig(), true);
if (NetworkConfig.EnableEncryption)
writer.WriteByteArray(diffiePublic);
if (NetworkConfig.ConnectionApproval)
writer.WriteByteArray(NetworkConfig.ConnectionData);
InternalMessageHandler.Send(netId.GetClientId(), "MLAPI_CONNECTION_REQUEST", "MLAPI_INTERNAL", writer.Finalize(), null, null, true);
}
}
break;
@ -1061,32 +1048,25 @@ namespace MLAPI.MonoBehaviours.Core
if (isServer)
{
using (MemoryStream stream = new MemoryStream(4))
using (BitWriter writer = new BitWriter())
{
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write(clientId);
}
InternalMessageHandler.Send("MLAPI_CLIENT_DISCONNECT", "MLAPI_INTERNAL", stream.GetBuffer(), clientId);
}
writer.WriteUInt(clientId);
InternalMessageHandler.Send("MLAPI_CLIENT_DISCONNECT", "MLAPI_INTERNAL", writer.Finalize(), clientId);
}
}
}
private void SyncTime()
{
using (MemoryStream stream = new MemoryStream(8))
using (BitWriter writer = new BitWriter())
{
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write(NetworkTime);
int timestamp = NetworkTransport.GetNetworkTimestamp();
writer.Write(timestamp);
}
writer.WriteFloat(NetworkTime);
int timestamp = NetworkTransport.GetNetworkTimestamp();
writer.WriteInt(timestamp);
byte[] buffer = writer.Finalize();
foreach (KeyValuePair<uint, NetworkedClient> pair in connectedClients)
{
InternalMessageHandler.Send("MLAPI_TIME_SYNC", "MLAPI_TIME_SYNC", stream.GetBuffer());
}
InternalMessageHandler.Send("MLAPI_TIME_SYNC", "MLAPI_TIME_SYNC", buffer);
}
}
@ -1134,86 +1114,57 @@ namespace MLAPI.MonoBehaviours.Core
GameObject go = SpawnManager.SpawnPlayerObject(clientId, networkId, position, rotation);
connectedClients[clientId].PlayerObject = go;
}
int sizeOfStream = 17 + ((connectedClients.Count - 1) * 4);
int amountOfObjectsToSend = SpawnManager.spawnedObjects.Values.Count();
if (NetworkConfig.HandleObjectSpawning)
using (BitWriter writer = new BitWriter())
{
sizeOfStream += 4;
sizeOfStream += 38 * amountOfObjectsToSend;
}
writer.WriteUInt(clientId);
if (NetworkConfig.EnableSceneSwitching)
writer.WriteUInt(NetworkSceneManager.CurrentSceneIndex);
if (NetworkConfig.EnableEncryption)
{
sizeOfStream += 2 + publicKey.Length;
if (NetworkConfig.SignKeyExchange)
if (NetworkConfig.EnableEncryption)
{
sizeOfStream += 2 + publicKeySignature.Length;
writer.WriteByteArray(publicKey);
if (NetworkConfig.SignKeyExchange)
writer.WriteByteArray(publicKeySignature);
}
}
if (NetworkConfig.EnableSceneSwitching)
{
sizeOfStream += 4;
}
writer.WriteFloat(NetworkTime);
writer.WriteInt(NetworkTransport.GetNetworkTimestamp());
using (MemoryStream writeStream = new MemoryStream(sizeOfStream))
{
using (BinaryWriter writer = new BinaryWriter(writeStream))
writer.WriteInt(connectedClients.Count - 1);
foreach (KeyValuePair<uint, NetworkedClient> item in connectedClients)
{
writer.Write(clientId);
if(NetworkConfig.EnableSceneSwitching)
//Our own ID. Already added as the first one above
if (item.Key == clientId)
continue;
writer.WriteUInt(item.Key); //ClientId
}
if (NetworkConfig.HandleObjectSpawning)
{
writer.WriteInt(amountOfObjectsToSend);
foreach (KeyValuePair<uint, NetworkedObject> pair in SpawnManager.spawnedObjects)
{
writer.Write(NetworkSceneManager.CurrentSceneIndex);
}
writer.WriteBool(pair.Value.isPlayerObject);
writer.WriteUInt(pair.Value.NetworkId);
writer.WriteUInt(pair.Value.OwnerClientId);
writer.WriteInt(NetworkConfig.NetworkPrefabIds[pair.Value.NetworkedPrefabName]);
writer.WriteBool(pair.Value.gameObject.activeInHierarchy);
writer.WriteBool(pair.Value.sceneObject == null ? true : pair.Value.sceneObject.Value);
if (NetworkConfig.EnableEncryption)
{
writer.Write((ushort)publicKey.Length);
writer.Write(publicKey);
if (NetworkConfig.SignKeyExchange)
{
writer.Write((ushort)publicKeySignature.Length);
writer.Write(publicKeySignature);
}
}
writer.WriteFloat(pair.Value.transform.position.x);
writer.WriteFloat(pair.Value.transform.position.y);
writer.WriteFloat(pair.Value.transform.position.z);
writer.Write(NetworkTime);
writer.Write(NetworkTransport.GetNetworkTimestamp());
writer.Write(connectedClients.Count - 1);
foreach (KeyValuePair<uint, NetworkedClient> item in connectedClients)
{
//Our own ID. Already added as the first one above
if (item.Key == clientId)
continue;
writer.Write(item.Key); //ClientId
}
if (NetworkConfig.HandleObjectSpawning)
{
writer.Write(amountOfObjectsToSend);
foreach (KeyValuePair<uint, NetworkedObject> pair in SpawnManager.spawnedObjects)
{
writer.Write(pair.Value.isPlayerObject);
writer.Write(pair.Value.NetworkId);
writer.Write(pair.Value.OwnerClientId);
writer.Write(NetworkConfig.NetworkPrefabIds[pair.Value.NetworkedPrefabName]);
writer.Write(pair.Value.gameObject.activeInHierarchy);
writer.Write(pair.Value.sceneObject == null ? true : pair.Value.sceneObject.Value);
writer.Write(pair.Value.transform.position.x);
writer.Write(pair.Value.transform.position.y);
writer.Write(pair.Value.transform.position.z);
writer.Write(pair.Value.transform.rotation.eulerAngles.x);
writer.Write(pair.Value.transform.rotation.eulerAngles.y);
writer.Write(pair.Value.transform.rotation.eulerAngles.z);
}
writer.WriteFloat(pair.Value.transform.rotation.eulerAngles.x);
writer.WriteFloat(pair.Value.transform.rotation.eulerAngles.y);
writer.WriteFloat(pair.Value.transform.rotation.eulerAngles.z);
}
}
InternalMessageHandler.Send(clientId, "MLAPI_CONNECTION_APPROVED", "MLAPI_INTERNAL", writeStream.GetBuffer(), null, null, true);
InternalMessageHandler.Send(clientId, "MLAPI_CONNECTION_APPROVED", "MLAPI_INTERNAL", writer.Finalize(), null, null, true);
if (OnClientConnectedCallback != null)
OnClientConnectedCallback.Invoke(clientId);
@ -1221,37 +1172,30 @@ namespace MLAPI.MonoBehaviours.Core
//Inform old clients of the new player
if(NetworkConfig.HandleObjectSpawning)
sizeOfStream = 38;
else
sizeOfStream = 4;
using (MemoryStream stream = new MemoryStream(sizeOfStream))
using (BitWriter writer = new BitWriter())
{
using (BinaryWriter writer = new BinaryWriter(stream))
if (NetworkConfig.HandleObjectSpawning)
{
if (NetworkConfig.HandleObjectSpawning)
{
writer.Write(true);
writer.Write(connectedClients[clientId].PlayerObject.GetComponent<NetworkedObject>().NetworkId);
writer.Write(clientId);
writer.Write(-1);
writer.Write(false);
writer.WriteBool(true);
writer.WriteUInt(connectedClients[clientId].PlayerObject.GetComponent<NetworkedObject>().NetworkId);
writer.WriteUInt(clientId);
writer.WriteInt(-1);
writer.WriteBool(false);
writer.Write(connectedClients[clientId].PlayerObject.transform.position.x);
writer.Write(connectedClients[clientId].PlayerObject.transform.position.y);
writer.Write(connectedClients[clientId].PlayerObject.transform.position.z);
writer.WriteFloat(connectedClients[clientId].PlayerObject.transform.position.x);
writer.WriteFloat(connectedClients[clientId].PlayerObject.transform.position.y);
writer.WriteFloat(connectedClients[clientId].PlayerObject.transform.position.z);
writer.Write(connectedClients[clientId].PlayerObject.transform.rotation.eulerAngles.x);
writer.Write(connectedClients[clientId].PlayerObject.transform.rotation.eulerAngles.y);
writer.Write(connectedClients[clientId].PlayerObject.transform.rotation.eulerAngles.z);
}
else
{
writer.Write(clientId);
}
writer.WriteFloat(connectedClients[clientId].PlayerObject.transform.rotation.eulerAngles.x);
writer.WriteFloat(connectedClients[clientId].PlayerObject.transform.rotation.eulerAngles.y);
writer.WriteFloat(connectedClients[clientId].PlayerObject.transform.rotation.eulerAngles.z);
}
InternalMessageHandler.Send("MLAPI_ADD_OBJECT", "MLAPI_INTERNAL", stream.GetBuffer(), clientId);
else
{
writer.WriteUInt(clientId);
}
InternalMessageHandler.Send("MLAPI_ADD_OBJECT", "MLAPI_INTERNAL", writer.Finalize(), clientId);
}
//Flush syncvars:
foreach (KeyValuePair<uint, NetworkedObject> networkedObject in SpawnManager.spawnedObjects)

View File

@ -1,8 +1,7 @@
using System;
using System.IO;
using System.Security.Cryptography;
using System.Security.Cryptography;
using MLAPI.Data;
using MLAPI.MonoBehaviours.Core;
using MLAPI.NetworkingManagerComponents.Binary;
using UnityEngine;
using UnityEngine.Networking;
@ -12,133 +11,121 @@ namespace MLAPI.NetworkingManagerComponents.Core
{
internal static void HandleConnectionRequest(uint clientId, byte[] incommingData, int channelId)
{
using (MemoryStream messageReadStream = new MemoryStream(incommingData))
{
using (BinaryReader messageReader = new BinaryReader(messageReadStream))
{
byte[] configHash = messageReader.ReadBytes(32);
if (!netManager.NetworkConfig.CompareConfig(configHash))
{
Debug.LogWarning("MLAPI: NetworkConfiguration missmatch. The configuration between the server and client does not match.");
netManager.DisconnectClient(clientId);
return;
}
byte[] aesKey = new byte[0];
if (netManager.NetworkConfig.EnableEncryption)
{
ushort diffiePublicSize = messageReader.ReadUInt16();
byte[] diffiePublic = messageReader.ReadBytes(diffiePublicSize);
netManager.diffieHellmanPublicKeys.Add(clientId, diffiePublic);
BitReader reader = new BitReader(incommingData);
}
if (netManager.NetworkConfig.ConnectionApproval)
{
ushort bufferSize = messageReader.ReadUInt16();
byte[] connectionBuffer = messageReader.ReadBytes(bufferSize);
netManager.ConnectionApprovalCallback(connectionBuffer, clientId, netManager.HandleApproval);
}
else
{
netManager.HandleApproval(clientId, true, Vector3.zero, Quaternion.identity);
}
}
byte[] configHash = reader.ReadByteArray(32);
if (!netManager.NetworkConfig.CompareConfig(configHash))
{
Debug.LogWarning("MLAPI: NetworkConfiguration missmatch. The configuration between the server and client does not match.");
netManager.DisconnectClient(clientId);
return;
}
if (netManager.NetworkConfig.EnableEncryption)
{
byte[] diffiePublic = reader.ReadByteArray();
netManager.diffieHellmanPublicKeys.Add(clientId, diffiePublic);
}
if (netManager.NetworkConfig.ConnectionApproval)
{
byte[] connectionBuffer = reader.ReadByteArray();
netManager.ConnectionApprovalCallback(connectionBuffer, clientId, netManager.HandleApproval);
}
else
{
netManager.HandleApproval(clientId, true, Vector3.zero, Quaternion.identity);
}
}
internal static void HandleConnectionApproved(uint clientId, byte[] incommingData, int channelId)
{
using (MemoryStream messageReadStream = new MemoryStream(incommingData))
BitReader reader = new BitReader(incommingData);
netManager.myClientId = reader.ReadUInt();
uint sceneIndex = 0;
if (netManager.NetworkConfig.EnableSceneSwitching)
sceneIndex = reader.ReadUInt();
if (netManager.NetworkConfig.EnableEncryption)
{
using (BinaryReader messageReader = new BinaryReader(messageReadStream))
byte[] serverPublicKey = reader.ReadByteArray();
netManager.clientAesKey = netManager.clientDiffieHellman.GetSharedSecret(serverPublicKey);
if (netManager.NetworkConfig.SignKeyExchange)
{
netManager.myClientId = messageReader.ReadUInt32();
uint sceneIndex = 0;
if (netManager.NetworkConfig.EnableSceneSwitching)
byte[] publicKeySignature = reader.ReadByteArray();
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
sceneIndex = messageReader.ReadUInt32();
}
if (netManager.NetworkConfig.EnableEncryption)
{
ushort keyLength = messageReader.ReadUInt16();
byte[] serverPublicKey = messageReader.ReadBytes(keyLength);
netManager.clientAesKey = netManager.clientDiffieHellman.GetSharedSecret(serverPublicKey);
if (netManager.NetworkConfig.SignKeyExchange)
rsa.PersistKeyInCsp = false;
rsa.FromXmlString(netManager.NetworkConfig.RSAPublicKey);
if (!rsa.VerifyData(serverPublicKey, new SHA512CryptoServiceProvider(), publicKeySignature))
{
ushort signatureLength = messageReader.ReadUInt16();
byte[] publicKeySignature = messageReader.ReadBytes(signatureLength);
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.PersistKeyInCsp = false;
rsa.FromXmlString(netManager.NetworkConfig.RSAPublicKey);
if (!rsa.VerifyData(serverPublicKey, new SHA512CryptoServiceProvider(), publicKeySignature))
{
//Man in the middle.
Debug.LogWarning("MLAPI: Signature doesnt match for the key exchange public part. Disconnecting");
netManager.StopClient();
return;
}
}
//Man in the middle.
Debug.LogWarning("MLAPI: Signature doesnt match for the key exchange public part. Disconnecting");
netManager.StopClient();
return;
}
}
float netTime = messageReader.ReadSingle();
int remoteStamp = messageReader.ReadInt32();
byte error;
NetId netId = new NetId(clientId);
int msDelay = NetworkTransport.GetRemoteDelayTimeMS(netId.HostId, netId.ConnectionId, remoteStamp, out error);
if ((NetworkError)error != NetworkError.Ok)
msDelay = 0;
netManager.networkTime = netTime + (msDelay / 1000f);
netManager.connectedClients.Add(clientId, new NetworkedClient() { ClientId = clientId });
int clientCount = messageReader.ReadInt32();
for (int i = 0; i < clientCount; i++)
{
uint _clientId = messageReader.ReadUInt32();
netManager.connectedClients.Add(_clientId, new NetworkedClient() { ClientId = _clientId });
}
if (netManager.NetworkConfig.HandleObjectSpawning)
{
SpawnManager.DestroySceneObjects();
int objectCount = messageReader.ReadInt32();
for (int i = 0; i < objectCount; i++)
{
bool isPlayerObject = messageReader.ReadBoolean();
uint networkId = messageReader.ReadUInt32();
uint ownerId = messageReader.ReadUInt32();
int prefabId = messageReader.ReadInt32();
bool isActive = messageReader.ReadBoolean();
bool sceneObject = messageReader.ReadBoolean();
float xPos = messageReader.ReadSingle();
float yPos = messageReader.ReadSingle();
float zPos = messageReader.ReadSingle();
float xRot = messageReader.ReadSingle();
float yRot = messageReader.ReadSingle();
float zRot = messageReader.ReadSingle();
if (isPlayerObject)
{
SpawnManager.SpawnPlayerObject(ownerId, networkId, new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
}
else
{
GameObject go = SpawnManager.SpawnPrefabIndexClient(prefabId, networkId, ownerId,
new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
go.GetComponent<NetworkedObject>().sceneObject = sceneObject;
go.SetActive(isActive);
}
}
}
if (netManager.NetworkConfig.EnableSceneSwitching)
{
NetworkSceneManager.OnSceneSwitch(sceneIndex);
}
}
}
float netTime = reader.ReadFloat();
int remoteStamp = reader.ReadInt();
byte error;
NetId netId = new NetId(clientId);
int msDelay = NetworkTransport.GetRemoteDelayTimeMS(netId.HostId, netId.ConnectionId, remoteStamp, out error);
if ((NetworkError)error != NetworkError.Ok)
msDelay = 0;
netManager.networkTime = netTime + (msDelay / 1000f);
netManager.connectedClients.Add(clientId, new NetworkedClient() { ClientId = clientId });
int clientCount = reader.ReadInt();
for (int i = 0; i < clientCount; i++)
{
uint _clientId = reader.ReadUInt();
netManager.connectedClients.Add(_clientId, new NetworkedClient() { ClientId = _clientId });
}
if (netManager.NetworkConfig.HandleObjectSpawning)
{
SpawnManager.DestroySceneObjects();
int objectCount = reader.ReadInt();
for (int i = 0; i < objectCount; i++)
{
bool isPlayerObject = reader.ReadBool();
uint networkId = reader.ReadUInt();
uint ownerId = reader.ReadUInt();
int prefabId = reader.ReadInt();
bool isActive = reader.ReadBool();
bool sceneObject = reader.ReadBool();
float xPos = reader.ReadFloat();
float yPos = reader.ReadFloat();
float zPos = reader.ReadFloat();
float xRot = reader.ReadFloat();
float yRot = reader.ReadFloat();
float zRot = reader.ReadFloat();
if (isPlayerObject)
{
SpawnManager.SpawnPlayerObject(ownerId, networkId, new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
}
else
{
GameObject go = SpawnManager.SpawnPrefabIndexClient(prefabId, networkId, ownerId,
new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
go.GetComponent<NetworkedObject>().sceneObject = sceneObject;
go.SetActive(isActive);
}
}
}
if (netManager.NetworkConfig.EnableSceneSwitching)
{
NetworkSceneManager.OnSceneSwitch(sceneIndex);
}
netManager._isClientConnected = true;
if (netManager.OnClientConnectedCallback != null)
netManager.OnClientConnectedCallback.Invoke(clientId);
@ -146,236 +133,201 @@ namespace MLAPI.NetworkingManagerComponents.Core
internal static void HandleAddObject(uint clientId, byte[] incommingData, int channelId)
{
using (MemoryStream messageReadStream = new MemoryStream(incommingData))
BitReader reader = new BitReader(incommingData);
if (netManager.NetworkConfig.HandleObjectSpawning)
{
using (BinaryReader messageReader = new BinaryReader(messageReadStream))
bool isPlayerObject = reader.ReadBool();
uint networkId = reader.ReadUInt();
uint ownerId = reader.ReadUInt();
int prefabId = reader.ReadInt();
bool sceneObject = reader.ReadBool();
float xPos = reader.ReadFloat();
float yPos = reader.ReadFloat();
float zPos = reader.ReadFloat();
float xRot = reader.ReadFloat();
float yRot = reader.ReadFloat();
float zRot = reader.ReadFloat();
if (isPlayerObject)
{
if (netManager.NetworkConfig.HandleObjectSpawning)
{
bool isPlayerObject = messageReader.ReadBoolean();
uint networkId = messageReader.ReadUInt32();
uint ownerId = messageReader.ReadUInt32();
int prefabId = messageReader.ReadInt32();
bool sceneObject = messageReader.ReadBoolean();
float xPos = messageReader.ReadSingle();
float yPos = messageReader.ReadSingle();
float zPos = messageReader.ReadSingle();
float xRot = messageReader.ReadSingle();
float yRot = messageReader.ReadSingle();
float zRot = messageReader.ReadSingle();
if (isPlayerObject)
{
netManager.connectedClients.Add(ownerId, new NetworkedClient() { ClientId = ownerId });
SpawnManager.SpawnPlayerObject(ownerId, networkId, new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
}
else
{
GameObject go = SpawnManager.SpawnPrefabIndexClient(prefabId, networkId, ownerId,
new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
go.GetComponent<NetworkedObject>().sceneObject = sceneObject;
}
}
else
{
uint ownerId = messageReader.ReadUInt32();
netManager.connectedClients.Add(ownerId, new NetworkedClient() { ClientId = ownerId });
}
netManager.connectedClients.Add(ownerId, new NetworkedClient() { ClientId = ownerId });
SpawnManager.SpawnPlayerObject(ownerId, networkId, new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
}
else
{
GameObject go = SpawnManager.SpawnPrefabIndexClient(prefabId, networkId, ownerId,
new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
go.GetComponent<NetworkedObject>().sceneObject = sceneObject;
}
}
else
{
uint ownerId = reader.ReadUInt();
netManager.connectedClients.Add(ownerId, new NetworkedClient() { ClientId = ownerId });
}
}
internal static void HandleClientDisconnect(uint clientId, byte[] incommingData, int channelId)
{
using (MemoryStream messageReadStream = new MemoryStream(incommingData))
{
using (BinaryReader messageReader = new BinaryReader(messageReadStream))
{
uint disconnectedClientId = messageReader.ReadUInt32();
netManager.OnClientDisconnect(disconnectedClientId);
}
}
BitReader reader = new BitReader(incommingData);
uint disconnectedClientId = reader.ReadUInt();
netManager.OnClientDisconnect(disconnectedClientId);
}
internal static void HandleDestroyObject(uint clientId, byte[] incommingData, int channelId)
{
using (MemoryStream messageReadStream = new MemoryStream(incommingData))
{
using (BinaryReader messageReader = new BinaryReader(messageReadStream))
{
uint netId = messageReader.ReadUInt32();
SpawnManager.OnDestroyObject(netId, true);
}
}
BitReader reader = new BitReader(incommingData);
uint netId = reader.ReadUInt();
SpawnManager.OnDestroyObject(netId, true);
}
internal static void HandleSwitchScene(uint clientId, byte[] incommingData, int channelId)
{
using (MemoryStream messageReadStream = new MemoryStream(incommingData))
{
using (BinaryReader messageReader = new BinaryReader(messageReadStream))
{
NetworkSceneManager.OnSceneSwitch(messageReader.ReadUInt32());
}
}
BitReader reader = new BitReader(incommingData);
NetworkSceneManager.OnSceneSwitch(reader.ReadUInt());
}
internal static void HandleSpawnPoolObject(uint clientId, byte[] incommingData, int channelId)
{
using (MemoryStream messageReadStream = new MemoryStream(incommingData))
{
using (BinaryReader messageReader = new BinaryReader(messageReadStream))
{
uint netId = messageReader.ReadUInt32();
BitReader reader = new BitReader(incommingData);
float xPos = messageReader.ReadSingle();
float yPos = messageReader.ReadSingle();
float zPos = messageReader.ReadSingle();
uint netId = reader.ReadUInt();
float xRot = messageReader.ReadSingle();
float yRot = messageReader.ReadSingle();
float zRot = messageReader.ReadSingle();
float xPos = reader.ReadFloat();
float yPos = reader.ReadFloat();
float zPos = reader.ReadFloat();
SpawnManager.spawnedObjects[netId].transform.position = new Vector3(xPos, yPos, zPos);
SpawnManager.spawnedObjects[netId].transform.rotation = Quaternion.Euler(xRot, yRot, zRot);
SpawnManager.spawnedObjects[netId].gameObject.SetActive(true);
}
}
float xRot = reader.ReadFloat();
float yRot = reader.ReadFloat();
float zRot = reader.ReadFloat();
SpawnManager.spawnedObjects[netId].transform.position = new Vector3(xPos, yPos, zPos);
SpawnManager.spawnedObjects[netId].transform.rotation = Quaternion.Euler(xRot, yRot, zRot);
SpawnManager.spawnedObjects[netId].gameObject.SetActive(true);
}
internal static void HandleDestroyPoolObject(uint clientId, byte[] incommingData, int channelId)
{
using (MemoryStream messageReadStream = new MemoryStream(incommingData))
{
using (BinaryReader messageReader = new BinaryReader(messageReadStream))
{
uint netId = messageReader.ReadUInt32();
SpawnManager.spawnedObjects[netId].gameObject.SetActive(false);
}
}
BitReader reader = new BitReader(incommingData);
uint netId = reader.ReadUInt();
SpawnManager.spawnedObjects[netId].gameObject.SetActive(false);
}
internal static void HandleChangeOwner(uint clientId, byte[] incommingData, int channelId)
{
using (MemoryStream messageReadStream = new MemoryStream(incommingData))
BitReader reader = new BitReader(incommingData);
uint netId = reader.ReadUInt();
uint ownerClientId = reader.ReadUInt();
if (SpawnManager.spawnedObjects[netId].OwnerClientId == netManager.MyClientId)
{
using (BinaryReader messageReader = new BinaryReader(messageReadStream))
{
uint netId = messageReader.ReadUInt32();
uint ownerClientId = messageReader.ReadUInt32();
if (SpawnManager.spawnedObjects[netId].OwnerClientId == netManager.MyClientId)
{
//We are current owner.
SpawnManager.spawnedObjects[netId].InvokeBehaviourOnLostOwnership();
}
if (ownerClientId == netManager.MyClientId)
{
//We are new owner.
SpawnManager.spawnedObjects[netId].InvokeBehaviourOnGainedOwnership();
}
SpawnManager.spawnedObjects[netId].ownerClientId = ownerClientId;
}
//We are current owner.
SpawnManager.spawnedObjects[netId].InvokeBehaviourOnLostOwnership();
}
if (ownerClientId == netManager.MyClientId)
{
//We are new owner.
SpawnManager.spawnedObjects[netId].InvokeBehaviourOnGainedOwnership();
}
SpawnManager.spawnedObjects[netId].ownerClientId = ownerClientId;
}
internal static void HandleSyncVarUpdate(uint clientId, byte[] incommingData, int channelId)
{
using (MemoryStream messageReadStream = new MemoryStream(incommingData))
BitReader reader = new BitReader(incommingData);
byte dirtyCount = reader.ReadByte();
uint netId = reader.ReadUInt();
ushort orderIndex = reader.ReadUShort();
if (dirtyCount > 0)
{
using (BinaryReader messageReader = new BinaryReader(messageReadStream))
for (int i = 0; i < dirtyCount; i++)
{
byte dirtyCount = messageReader.ReadByte();
uint netId = messageReader.ReadUInt32();
ushort orderIndex = messageReader.ReadUInt16();
if (dirtyCount > 0)
byte fieldIndex = reader.ReadByte();
if (!SpawnManager.spawnedObjects.ContainsKey(netId))
{
for (int i = 0; i < dirtyCount; i++)
{
byte fieldIndex = messageReader.ReadByte();
if (!SpawnManager.spawnedObjects.ContainsKey(netId))
{
Debug.LogWarning("MLAPI: Sync message recieved for a non existant object with id: " + netId);
return;
Debug.LogWarning("MLAPI: Sync message recieved for a non existant object with id: " + netId);
return;
}
else if (SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex) == null)
{
Debug.LogWarning("MLAPI: Sync message recieved for a non existant behaviour");
return;
}
else if (fieldIndex > (SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).syncedVarFields.Count - 1))
{
Debug.LogWarning("MLAPI: Sync message recieved for field out of bounds");
return;
}
FieldType type = SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).syncedVarFields[fieldIndex].FieldType;
switch (type)
{
case FieldType.Bool:
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(reader.ReadBool(), fieldIndex);
break;
case FieldType.Byte:
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(reader.ReadByte(), fieldIndex);
break;
case FieldType.Double:
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(reader.ReadDouble(), fieldIndex);
break;
case FieldType.Single:
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(reader.ReadFloat(), fieldIndex);
break;
case FieldType.Int:
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(reader.ReadInt(), fieldIndex);
break;
case FieldType.Long:
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(reader.ReadLong(), fieldIndex);
break;
case FieldType.SByte:
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(reader.ReadSByte(), fieldIndex);
break;
case FieldType.Short:
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(reader.ReadShort(), fieldIndex);
break;
case FieldType.UInt:
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(reader.ReadUInt(), fieldIndex);
break;
case FieldType.ULong:
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(reader.ReadULong(), fieldIndex);
break;
case FieldType.UShort:
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(reader.ReadUShort(), fieldIndex);
break;
case FieldType.String:
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(reader.ReadString(), fieldIndex);
break;
case FieldType.Vector3:
{ //Cases aren't their own scope. Therefor we create a scope for them as they share the X,Y,Z local variables otherwise.
float x = reader.ReadFloat();
float y = reader.ReadFloat();
float z = reader.ReadFloat();
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(new Vector3(x, y, z), fieldIndex);
}
else if (SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex) == null)
break;
case FieldType.Vector2:
{
Debug.LogWarning("MLAPI: Sync message recieved for a non existant behaviour");
return;
float x = reader.ReadFloat();
float y = reader.ReadFloat();
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(new Vector2(x, y), fieldIndex);
}
else if (fieldIndex > (SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).syncedVarFields.Count - 1))
break;
case FieldType.Quaternion:
{
Debug.LogWarning("MLAPI: Sync message recieved for field out of bounds");
return;
float x = reader.ReadFloat();
float y = reader.ReadFloat();
float z = reader.ReadFloat();
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(Quaternion.Euler(x, y, z), fieldIndex);
}
FieldType type = SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).syncedVarFields[fieldIndex].FieldType;
switch (type)
{
case FieldType.Bool:
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(messageReader.ReadBoolean(), fieldIndex);
break;
case FieldType.Byte:
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(messageReader.ReadByte(), fieldIndex);
break;
case FieldType.Char:
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(messageReader.ReadChar(), fieldIndex);
break;
case FieldType.Double:
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(messageReader.ReadDouble(), fieldIndex);
break;
case FieldType.Single:
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(messageReader.ReadSingle(), fieldIndex);
break;
case FieldType.Int:
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(messageReader.ReadInt32(), fieldIndex);
break;
case FieldType.Long:
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(messageReader.ReadInt64(), fieldIndex);
break;
case FieldType.SByte:
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(messageReader.ReadSByte(), fieldIndex);
break;
case FieldType.Short:
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(messageReader.ReadInt16(), fieldIndex);
break;
case FieldType.UInt:
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(messageReader.ReadUInt32(), fieldIndex);
break;
case FieldType.ULong:
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(messageReader.ReadUInt64(), fieldIndex);
break;
case FieldType.UShort:
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(messageReader.ReadUInt16(), fieldIndex);
break;
case FieldType.String:
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(messageReader.ReadString(), fieldIndex);
break;
case FieldType.Vector3:
{ //Cases aren't their own scope. Therefor we create a scope for them as they share the X,Y,Z local variables otherwise.
float x = messageReader.ReadSingle();
float y = messageReader.ReadSingle();
float z = messageReader.ReadSingle();
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(new Vector3(x, y, z), fieldIndex);
}
break;
case FieldType.Vector2:
{
float x = messageReader.ReadSingle();
float y = messageReader.ReadSingle();
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(new Vector2(x, y), fieldIndex);
}
break;
case FieldType.Quaternion:
{
float x = messageReader.ReadSingle();
float y = messageReader.ReadSingle();
float z = messageReader.ReadSingle();
SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).OnSyncVarUpdate(Quaternion.Euler(x, y, z), fieldIndex);
}
break;
}
}
break;
}
}
}
@ -383,41 +335,37 @@ namespace MLAPI.NetworkingManagerComponents.Core
internal static void HandleAddObjects(uint clientId, byte[] incommingData, int channelId)
{
using (MemoryStream messageReadStream = new MemoryStream(incommingData))
BitReader reader = new BitReader(incommingData);
if (netManager.NetworkConfig.HandleObjectSpawning)
{
using (BinaryReader messageReader = new BinaryReader(messageReadStream))
ushort objectCount = reader.ReadUShort();
for (int i = 0; i < objectCount; i++)
{
if (netManager.NetworkConfig.HandleObjectSpawning)
bool isPlayerObject = reader.ReadBool();
uint networkId = reader.ReadUInt();
uint ownerId = reader.ReadUInt();
int prefabId = reader.ReadInt();
bool sceneObject = reader.ReadBool();
float xPos = reader.ReadFloat();
float yPos = reader.ReadFloat();
float zPos = reader.ReadFloat();
float xRot = reader.ReadFloat();
float yRot = reader.ReadFloat();
float zRot = reader.ReadFloat();
if (isPlayerObject)
{
ushort objectCount = messageReader.ReadUInt16();
for (int i = 0; i < objectCount; i++)
{
bool isPlayerObject = messageReader.ReadBoolean();
uint networkId = messageReader.ReadUInt32();
uint ownerId = messageReader.ReadUInt32();
int prefabId = messageReader.ReadInt32();
bool sceneObject = messageReader.ReadBoolean();
float xPos = messageReader.ReadSingle();
float yPos = messageReader.ReadSingle();
float zPos = messageReader.ReadSingle();
float xRot = messageReader.ReadSingle();
float yRot = messageReader.ReadSingle();
float zRot = messageReader.ReadSingle();
if (isPlayerObject)
{
netManager.connectedClients.Add(ownerId, new NetworkedClient() { ClientId = ownerId });
SpawnManager.SpawnPlayerObject(ownerId, networkId, new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
}
else
{
GameObject go = SpawnManager.SpawnPrefabIndexClient(prefabId, networkId, ownerId,
new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
go.GetComponent<NetworkedObject>().sceneObject = sceneObject;
}
}
netManager.connectedClients.Add(ownerId, new NetworkedClient() { ClientId = ownerId });
SpawnManager.SpawnPlayerObject(ownerId, networkId, new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
}
else
{
GameObject go = SpawnManager.SpawnPrefabIndexClient(prefabId, networkId, ownerId,
new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
go.GetComponent<NetworkedObject>().sceneObject = sceneObject;
}
}
}
@ -425,21 +373,17 @@ namespace MLAPI.NetworkingManagerComponents.Core
internal static void HandleTimeSync(uint clientId, byte[] incommingData, int channelId)
{
using (MemoryStream messageReadStream = new MemoryStream(incommingData))
{
using (BinaryReader messageReader = new BinaryReader(messageReadStream))
{
float netTime = messageReader.ReadSingle();
int timestamp = messageReader.ReadInt32();
BitReader reader = new BitReader(incommingData);
NetId netId = new NetId(clientId);
byte error;
int msDelay = NetworkTransport.GetRemoteDelayTimeMS(netId.HostId, netId.ConnectionId, timestamp, out error);
if ((NetworkError)error != NetworkError.Ok)
msDelay = 0;
netManager.networkTime = netTime + (msDelay / 1000f);
}
}
}
float netTime = reader.ReadFloat();
int timestamp = reader.ReadInt();
NetId netId = new NetId(clientId);
byte error;
int msDelay = NetworkTransport.GetRemoteDelayTimeMS(netId.HostId, netId.ConnectionId, timestamp, out error);
if ((NetworkError)error != NetworkError.Ok)
msDelay = 0;
netManager.networkTime = netTime + (msDelay / 1000f);
}
}
}

View File

@ -1,5 +1,6 @@
using MLAPI.Data;
using MLAPI.MonoBehaviours.Core;
using MLAPI.NetworkingManagerComponents.Binary;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
@ -66,19 +67,19 @@ namespace MLAPI.NetworkingManagerComponents.Core
return null;
}
GameObject go = Pools[PoolNamesToIndexes[poolName]].SpawnObject(position, rotation);
using (MemoryStream stream = new MemoryStream(28))
using (BitWriter writer = new BitWriter())
{
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write(go.GetComponent<NetworkedObject>().NetworkId);
writer.Write(position.x);
writer.Write(position.y);
writer.Write(position.z);
writer.Write(rotation.eulerAngles.x);
writer.Write(rotation.eulerAngles.y);
writer.Write(rotation.eulerAngles.z);
}
InternalMessageHandler.Send("MLAPI_SPAWN_POOL_OBJECT", "MLAPI_INTERNAL", stream.GetBuffer());
writer.WriteUInt(go.GetComponent<NetworkedObject>().NetworkId);
writer.WriteFloat(position.x);
writer.WriteFloat(position.y);
writer.WriteFloat(position.z);
writer.WriteFloat(rotation.eulerAngles.x);
writer.WriteFloat(rotation.eulerAngles.y);
writer.WriteFloat(rotation.eulerAngles.z);
InternalMessageHandler.Send("MLAPI_SPAWN_POOL_OBJECT", "MLAPI_INTERNAL", writer.Finalize());
}
return go;
}
@ -95,13 +96,11 @@ namespace MLAPI.NetworkingManagerComponents.Core
return;
}
netObject.gameObject.SetActive(false);
using (MemoryStream stream = new MemoryStream(4))
using (BitWriter writer = new BitWriter())
{
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write(netObject.NetworkId);
}
InternalMessageHandler.Send("MLAPI_DESTROY_POOL_OBJECT", "MLAPI_INTERNAL", stream.GetBuffer());
writer.WriteUInt(netObject.NetworkId);
InternalMessageHandler.Send("MLAPI_DESTROY_POOL_OBJECT", "MLAPI_INTERNAL", writer.Finalize());
}
}
}

View File

@ -1,6 +1,6 @@
using MLAPI.MonoBehaviours.Core;
using MLAPI.NetworkingManagerComponents.Binary;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
@ -58,13 +58,11 @@ namespace MLAPI.NetworkingManagerComponents.Core
AsyncOperation sceneLoad = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
sceneLoad.completed += OnSceneLoaded;
using(MemoryStream stream = new MemoryStream(4))
using (BitWriter writer = new BitWriter())
{
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write(sceneNameToIndex[sceneName]);
}
InternalMessageHandler.Send("MLAPI_SWITCH_SCENE", "MLAPI_INTERNAL", stream.GetBuffer());
writer.WriteUInt(sceneNameToIndex[sceneName]);
InternalMessageHandler.Send("MLAPI_SWITCH_SCENE", "MLAPI_INTERNAL", writer.Finalize());
}
}

View File

@ -1,5 +1,6 @@
using MLAPI.Data;
using MLAPI.MonoBehaviours.Core;
using MLAPI.NetworkingManagerComponents.Binary;
using System;
using System.Collections.Generic;
using System.IO;
@ -38,14 +39,13 @@ namespace MLAPI.NetworkingManagerComponents.Core
NetworkedObject netObject = SpawnManager.spawnedObjects[netId];
NetworkingManager.singleton.connectedClients[netObject.OwnerClientId].OwnedObjects.RemoveAll(x => x.NetworkId == netId);
netObject.ownerClientId = new NetId(0, 0, false, true).GetClientId();
using (MemoryStream stream = new MemoryStream(8))
using (BitWriter writer = new BitWriter())
{
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write(netId);
writer.Write(netObject.ownerClientId);
}
InternalMessageHandler.Send("MLAPI_CHANGE_OWNER", "MLAPI_INTERNAL", stream.GetBuffer());
writer.WriteUInt(netId);
writer.WriteUInt(netObject.ownerClientId);
InternalMessageHandler.Send("MLAPI_CHANGE_OWNER", "MLAPI_INTERNAL", writer.Finalize());
}
}
@ -55,14 +55,13 @@ namespace MLAPI.NetworkingManagerComponents.Core
NetworkingManager.singleton.connectedClients[netObject.OwnerClientId].OwnedObjects.RemoveAll(x => x.NetworkId == netId);
NetworkingManager.singleton.connectedClients[clientId].OwnedObjects.Add(netObject);
netObject.ownerClientId = clientId;
using (MemoryStream stream = new MemoryStream(8))
using (BitWriter writer = new BitWriter())
{
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write(netId);
writer.Write(clientId);
}
InternalMessageHandler.Send("MLAPI_CHANGE_OWNER", "MLAPI_INTERNAL", stream.GetBuffer());
writer.WriteUInt(netId);
writer.WriteUInt(clientId);
InternalMessageHandler.Send("MLAPI_CHANGE_OWNER", "MLAPI_INTERNAL", writer.Finalize());
}
}
@ -109,29 +108,28 @@ namespace MLAPI.NetworkingManagerComponents.Core
if (pair.Value.sceneObject == null || pair.Value.sceneObject == true)
sceneObjectsToSync.Add(pair.Value);
}
int sizeOfStream = 2 + (38 * sceneObjectsToSync.Count); //The two is the base size, it's a ushort containing the amount of objects. Each object takes 38 bytes
using (MemoryStream stream = new MemoryStream(sizeOfStream))
using (BitWriter writer = new BitWriter())
{
using (BinaryWriter writer = new BinaryWriter(stream))
writer.WriteUShort((ushort)sceneObjectsToSync.Count);
for (int i = 0; i < sceneObjectsToSync.Count; i++)
{
writer.Write((ushort)sceneObjectsToSync.Count);
for (int i = 0; i < sceneObjectsToSync.Count; i++)
{
writer.Write(false); //isLocalPlayer
writer.Write(sceneObjectsToSync[i].NetworkId);
writer.Write(sceneObjectsToSync[i].OwnerClientId);
writer.Write(NetworkingManager.singleton.NetworkConfig.NetworkPrefabIds[sceneObjectsToSync[i].NetworkedPrefabName]);
writer.WriteBool(false); //isLocalPlayer
writer.WriteUInt(sceneObjectsToSync[i].NetworkId);
writer.WriteUInt(sceneObjectsToSync[i].OwnerClientId);
writer.WriteInt(NetworkingManager.singleton.NetworkConfig.NetworkPrefabIds[sceneObjectsToSync[i].NetworkedPrefabName]);
writer.WriteBool(sceneObjectsToSync[i].sceneObject == null ? true : sceneObjectsToSync[i].sceneObject.Value);
writer.Write(sceneObjectsToSync[i].transform.position.x);
writer.Write(sceneObjectsToSync[i].transform.position.y);
writer.Write(sceneObjectsToSync[i].transform.position.z);
writer.WriteFloat(sceneObjectsToSync[i].transform.position.x);
writer.WriteFloat(sceneObjectsToSync[i].transform.position.y);
writer.WriteFloat(sceneObjectsToSync[i].transform.position.z);
writer.Write(sceneObjectsToSync[i].transform.rotation.eulerAngles.x);
writer.Write(sceneObjectsToSync[i].transform.rotation.eulerAngles.y);
writer.Write(sceneObjectsToSync[i].transform.rotation.eulerAngles.z);
}
writer.WriteFloat(sceneObjectsToSync[i].transform.rotation.eulerAngles.x);
writer.WriteFloat(sceneObjectsToSync[i].transform.rotation.eulerAngles.y);
writer.WriteFloat(sceneObjectsToSync[i].transform.rotation.eulerAngles.z);
}
InternalMessageHandler.Send("MLAPI_ADD_OBJECTS", "MLAPI_INTERNAL", stream.GetBuffer());
InternalMessageHandler.Send("MLAPI_ADD_OBJECTS", "MLAPI_INTERNAL", writer.Finalize());
}
}
@ -195,25 +193,24 @@ namespace MLAPI.NetworkingManagerComponents.Core
netObject.ownerClientId = clientOwnerId.Value;
NetworkingManager.singleton.connectedClients[clientOwnerId.Value].OwnedObjects.Add(netObject);
}
using (MemoryStream stream = new MemoryStream(37))
{
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write(false);
writer.Write(netObject.NetworkId);
writer.Write(netObject.OwnerClientId);
writer.Write(netManager.NetworkConfig.NetworkPrefabIds[netObject.NetworkedPrefabName]);
using (BitWriter writer = new BitWriter())
{
writer.WriteBool(false);
writer.WriteUInt(netObject.NetworkId);
writer.WriteUInt(netObject.OwnerClientId);
writer.WriteInt(netManager.NetworkConfig.NetworkPrefabIds[netObject.NetworkedPrefabName]);
writer.WriteBool(netObject.sceneObject == null ? true : netObject.sceneObject.Value);
writer.Write(netObject.transform.position.x);
writer.Write(netObject.transform.position.y);
writer.Write(netObject.transform.position.z);
writer.WriteFloat(netObject.transform.position.x);
writer.WriteFloat(netObject.transform.position.y);
writer.WriteFloat(netObject.transform.position.z);
writer.Write(netObject.transform.rotation.eulerAngles.x);
writer.Write(netObject.transform.rotation.eulerAngles.y);
writer.Write(netObject.transform.rotation.eulerAngles.z);
}
writer.WriteFloat(netObject.transform.rotation.eulerAngles.x);
writer.WriteFloat(netObject.transform.rotation.eulerAngles.y);
writer.WriteFloat(netObject.transform.rotation.eulerAngles.z);
InternalMessageHandler.Send("MLAPI_ADD_OBJECT", "MLAPI_INTERNAL", stream.GetBuffer());
InternalMessageHandler.Send("MLAPI_ADD_OBJECT", "MLAPI_INTERNAL", writer.Finalize());
}
}
@ -263,13 +260,11 @@ namespace MLAPI.NetworkingManagerComponents.Core
releasedNetworkObjectIds.Push(networkId);
if (spawnedObjects[networkId] != null)
{
using (MemoryStream stream = new MemoryStream(4))
using (BitWriter writer = new BitWriter())
{
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write(networkId);
}
InternalMessageHandler.Send("MLAPI_DESTROY_OBJECT", "MLAPI_INTERNAL", stream.GetBuffer());
writer.WriteUInt(networkId);
InternalMessageHandler.Send("MLAPI_DESTROY_OBJECT", "MLAPI_INTERNAL", writer.Finalize());
}
}
}