From 438f4fa9c248c125230ff29db4ec4f75fa3bf703 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albin=20Cor=C3=A9n?= <2108U9@gmail.com> Date: Thu, 19 Apr 2018 17:32:00 +0200 Subject: [PATCH] Cleaned up SyncedVar system --- MLAPI/Data/FieldType.cs | 135 ++++- .../MonoBehaviours/Core/NetworkedBehaviour.cs | 540 +----------------- 2 files changed, 150 insertions(+), 525 deletions(-) diff --git a/MLAPI/Data/FieldType.cs b/MLAPI/Data/FieldType.cs index 20c41d9..abd8c31 100644 --- a/MLAPI/Data/FieldType.cs +++ b/MLAPI/Data/FieldType.cs @@ -1,4 +1,6 @@ -using System; +using MLAPI.NetworkingManagerComponents.Binary; +using System; +using System.Reflection; using UnityEngine; namespace MLAPI.Data @@ -43,6 +45,137 @@ namespace MLAPI.Data internal static class FieldTypeHelper { + internal static void WriteFieldType(BitWriter writer, FieldInfo field, object fieldInstance, FieldType fieldType) + { + switch (fieldType) + { + case FieldType.Bool: + writer.WriteBool((bool)field.GetValue(fieldInstance)); + break; + case FieldType.Byte: + writer.WriteByte((byte)field.GetValue(fieldInstance)); + break; + case FieldType.Double: + writer.WriteDouble((double)field.GetValue(fieldInstance)); + break; + case FieldType.Single: + writer.WriteFloat((float)field.GetValue(fieldInstance)); + break; + case FieldType.Int: + writer.WriteInt((int)field.GetValue(fieldInstance)); + break; + case FieldType.Long: + writer.WriteLong((long)field.GetValue(fieldInstance)); + break; + case FieldType.SByte: + writer.WriteSByte((sbyte)field.GetValue(fieldInstance)); + break; + case FieldType.Short: + writer.WriteShort((short)field.GetValue(fieldInstance)); + break; + case FieldType.UInt: + writer.WriteUInt((uint)field.GetValue(fieldInstance)); + break; + case FieldType.ULong: + writer.WriteULong((ulong)field.GetValue(fieldInstance)); + break; + case FieldType.UShort: + writer.WriteUShort((ushort)field.GetValue(fieldInstance)); + break; + case FieldType.String: + writer.WriteString((string)field.GetValue(fieldInstance)); + break; + case FieldType.Vector3: + Vector3 vector3 = (Vector3)field.GetValue(fieldInstance); + writer.WriteFloat(vector3.x); + writer.WriteFloat(vector3.y); + writer.WriteFloat(vector3.z); + break; + case FieldType.Vector2: + Vector2 vector2 = (Vector2)field.GetValue(fieldInstance); + writer.WriteFloat(vector2.x); + writer.WriteFloat(vector2.y); + break; + case FieldType.Quaternion: + Vector3 euler = ((Quaternion)field.GetValue(fieldInstance)).eulerAngles; + writer.WriteFloat(euler.x); + writer.WriteFloat(euler.y); + writer.WriteFloat(euler.z); + break; + case FieldType.BoolArray: + bool[] bools = (bool[])field.GetValue(fieldInstance); + writer.WriteUShort((ushort)bools.Length); + for (int j = 0; j < bools.Length; j++) + writer.WriteBool(bools[j]); + break; + case FieldType.ByteArray: + writer.WriteByteArray((byte[])field.GetValue(fieldInstance)); + break; + case FieldType.DoubleArray: + writer.WriteDoubleArray((double[])field.GetValue(fieldInstance)); + break; + case FieldType.SingleArray: + writer.WriteFloatArray((float[])field.GetValue(fieldInstance)); + break; + case FieldType.IntArray: + writer.WriteIntArray((int[])field.GetValue(fieldInstance)); + break; + case FieldType.LongArray: + writer.WriteLongArray((long[])field.GetValue(fieldInstance)); + break; + case FieldType.SByteArray: + writer.WriteSByteArray((sbyte[])field.GetValue(fieldInstance)); + break; + case FieldType.ShortArray: + writer.WriteShortArray((short[])field.GetValue(fieldInstance)); + break; + case FieldType.UIntArray: + writer.WriteUIntArray((uint[])field.GetValue(fieldInstance)); + break; + case FieldType.ULongArray: + writer.WriteULongArray((ulong[])field.GetValue(fieldInstance)); + break; + case FieldType.UShortArray: + writer.WriteUShortArray((ushort[])field.GetValue(fieldInstance)); + break; + case FieldType.StringArray: + string[] strings = (string[])field.GetValue(fieldInstance); + writer.WriteUShort((ushort)strings.Length); + for (int j = 0; j < strings.Length; j++) + writer.WriteString(strings[j]); + break; + case FieldType.Vector3Array: + Vector3[] vector3s = (Vector3[])field.GetValue(fieldInstance); + writer.WriteUShort((ushort)vector3s.Length); + for (int j = 0; j < vector3s.Length; j++) + { + writer.WriteFloat(vector3s[j].x); + writer.WriteFloat(vector3s[j].y); + writer.WriteFloat(vector3s[j].z); + } + break; + case FieldType.Vector2Array: + Vector2[] vector2s = (Vector2[])field.GetValue(fieldInstance); + writer.WriteUShort((ushort)vector2s.Length); + for (int j = 0; j < vector2s.Length; j++) + { + writer.WriteFloat(vector2s[j].x); + writer.WriteFloat(vector2s[j].y); + } + break; + case FieldType.QuaternionArray: + Quaternion[] quaternions = (Quaternion[])field.GetValue(fieldInstance); + writer.WriteUShort((ushort)quaternions.Length); + for (int j = 0; j < quaternions.Length; j++) + { + writer.WriteFloat(quaternions[j].eulerAngles.x); + writer.WriteFloat(quaternions[j].eulerAngles.y); + writer.WriteFloat(quaternions[j].eulerAngles.z); + } + break; + } + } + internal static FieldType GetFieldType(Type type) { if (type == typeof(bool)) diff --git a/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs b/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs index e9fc905..2cb942e 100644 --- a/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs +++ b/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs @@ -279,134 +279,7 @@ namespace MLAPI.MonoBehaviours.Core if (syncedVarFields[i].Target && clientId != ownerClientId) continue; writer.WriteByte(i); //FieldIndex - switch (syncedVarFields[i].FieldType) - { - 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.BoolArray: - bool[] bools = (bool[])syncedVarFields[i].FieldInfo.GetValue(this); - writer.WriteUShort((ushort)bools.Length); - for (int j = 0; j < bools.Length; j++) - writer.WriteBool(bools[j]); - break; - case FieldType.ByteArray: - writer.WriteByteArray((byte[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.DoubleArray: - writer.WriteDoubleArray((double[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.SingleArray: - writer.WriteFloatArray((float[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.IntArray: - writer.WriteIntArray((int[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.LongArray: - writer.WriteLongArray((long[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.SByteArray: - writer.WriteSByteArray((sbyte[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.ShortArray: - writer.WriteShortArray((short[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.UIntArray: - writer.WriteUIntArray((uint[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.ULongArray: - writer.WriteULongArray((ulong[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.UShortArray: - writer.WriteUShortArray((ushort[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.StringArray: - string[] strings = (string[])syncedVarFields[i].FieldInfo.GetValue(this); - writer.WriteUShort((ushort)strings.Length); - for (int j = 0; j < strings.Length; j++) - writer.WriteString(strings[j]); - break; - case FieldType.Vector3Array: - Vector3[] vector3s = (Vector3[])syncedVarFields[i].FieldInfo.GetValue(this); - writer.WriteUShort((ushort)vector3s.Length); - for (int j = 0; j < vector3s.Length; j++) - { - writer.WriteFloat(vector3s[j].x); - writer.WriteFloat(vector3s[j].y); - writer.WriteFloat(vector3s[j].z); - } - break; - case FieldType.Vector2Array: - Vector2[] vector2s = (Vector2[])syncedVarFields[i].FieldInfo.GetValue(this); - writer.WriteUShort((ushort)vector2s.Length); - for (int j = 0; j < vector2s.Length; j++) - { - writer.WriteFloat(vector2s[j].x); - writer.WriteFloat(vector2s[j].y); - } - break; - case FieldType.QuaternionArray: - Quaternion[] quaternions = (Quaternion[])syncedVarFields[i].FieldInfo.GetValue(this); - writer.WriteUShort((ushort)quaternions.Length); - for (int j = 0; j < quaternions.Length; j++) - { - writer.WriteFloat(quaternions[j].eulerAngles.x); - writer.WriteFloat(quaternions[j].eulerAngles.y); - writer.WriteFloat(quaternions[j].eulerAngles.z); - } - break; - - } + FieldTypeHelper.WriteFieldType(writer, syncedVarFields[i].FieldInfo, this, syncedVarFields[i].FieldType); } InternalMessageHandler.Send(clientId, "MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer.Finalize()); } @@ -453,133 +326,7 @@ namespace MLAPI.MonoBehaviours.Core if (syncedVarFields[i].Dirty == true) { writer.WriteByte(i); //FieldIndex - switch (syncedVarFields[i].FieldType) - { - 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.BoolArray: - bool[] bools = (bool[])syncedVarFields[i].FieldInfo.GetValue(this); - writer.WriteUShort((ushort)bools.Length); - for (int j = 0; j < bools.Length; j++) - writer.WriteBool(bools[j]); - break; - case FieldType.ByteArray: - writer.WriteByteArray((byte[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.DoubleArray: - writer.WriteDoubleArray((double[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.SingleArray: - writer.WriteFloatArray((float[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.IntArray: - writer.WriteIntArray((int[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.LongArray: - writer.WriteLongArray((long[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.SByteArray: - writer.WriteSByteArray((sbyte[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.ShortArray: - writer.WriteShortArray((short[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.UIntArray: - writer.WriteUIntArray((uint[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.ULongArray: - writer.WriteULongArray((ulong[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.UShortArray: - writer.WriteUShortArray((ushort[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.StringArray: - string[] strings = (string[])syncedVarFields[i].FieldInfo.GetValue(this); - writer.WriteUShort((ushort)strings.Length); - for (int j = 0; j < strings.Length; j++) - writer.WriteString(strings[j]); - break; - case FieldType.Vector3Array: - Vector3[] vector3s = (Vector3[])syncedVarFields[i].FieldInfo.GetValue(this); - writer.WriteUShort((ushort)vector3s.Length); - for (int j = 0; j < vector3s.Length; j++) - { - writer.WriteFloat(vector3s[j].x); - writer.WriteFloat(vector3s[j].y); - writer.WriteFloat(vector3s[j].z); - } - break; - case FieldType.Vector2Array: - Vector2[] vector2s = (Vector2[])syncedVarFields[i].FieldInfo.GetValue(this); - writer.WriteUShort((ushort)vector2s.Length); - for (int j = 0; j < vector2s.Length; j++) - { - writer.WriteFloat(vector2s[j].x); - writer.WriteFloat(vector2s[j].y); - } - break; - case FieldType.QuaternionArray: - Quaternion[] quaternions = (Quaternion[])syncedVarFields[i].FieldInfo.GetValue(this); - writer.WriteUShort((ushort)quaternions.Length); - for (int j = 0; j < quaternions.Length; j++) - { - writer.WriteFloat(quaternions[j].eulerAngles.x); - writer.WriteFloat(quaternions[j].eulerAngles.y); - writer.WriteFloat(quaternions[j].eulerAngles.z); - } - break; - } + FieldTypeHelper.WriteFieldType(writer, syncedVarFields[i].FieldInfo, this, syncedVarFields[i].FieldType); syncedVarFields[i].FieldValue = syncedVarFields[i].FieldInfo.GetValue(this); syncedVarFields[i].Dirty = false; } @@ -604,134 +351,7 @@ namespace MLAPI.MonoBehaviours.Core if (syncedVarFields[i].Dirty == true) { writer.WriteByte(i); //FieldIndex - switch (syncedVarFields[i].FieldType) - { - 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.BoolArray: - bool[] bools = (bool[])syncedVarFields[i].FieldInfo.GetValue(this); - writer.WriteUShort((ushort)bools.Length); - for (int j = 0; j < bools.Length; j++) - writer.WriteBool(bools[j]); - break; - case FieldType.ByteArray: - writer.WriteByteArray((byte[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.DoubleArray: - writer.WriteDoubleArray((double[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.SingleArray: - writer.WriteFloatArray((float[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.IntArray: - writer.WriteIntArray((int[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.LongArray: - writer.WriteLongArray((long[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.SByteArray: - writer.WriteSByteArray((sbyte[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.ShortArray: - writer.WriteShortArray((short[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.UIntArray: - writer.WriteUIntArray((uint[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.ULongArray: - writer.WriteULongArray((ulong[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.UShortArray: - writer.WriteUShortArray((ushort[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.StringArray: - string[] strings = (string[])syncedVarFields[i].FieldInfo.GetValue(this); - writer.WriteUShort((ushort)strings.Length); - for (int j = 0; j < strings.Length; j++) - writer.WriteString(strings[j]); - break; - case FieldType.Vector3Array: - Vector3[] vector3s = (Vector3[])syncedVarFields[i].FieldInfo.GetValue(this); - writer.WriteUShort((ushort)vector3s.Length); - for (int j = 0; j < vector3s.Length; j++) - { - writer.WriteFloat(vector3s[j].x); - writer.WriteFloat(vector3s[j].y); - writer.WriteFloat(vector3s[j].z); - } - break; - case FieldType.Vector2Array: - Vector2[] vector2s = (Vector2[])syncedVarFields[i].FieldInfo.GetValue(this); - writer.WriteUShort((ushort)vector2s.Length); - for (int j = 0; j < vector2s.Length; j++) - { - writer.WriteFloat(vector2s[j].x); - writer.WriteFloat(vector2s[j].y); - } - break; - case FieldType.QuaternionArray: - Quaternion[] quaternions = (Quaternion[])syncedVarFields[i].FieldInfo.GetValue(this); - writer.WriteUShort((ushort)quaternions.Length); - for (int j = 0; j < quaternions.Length; j++) - { - writer.WriteFloat(quaternions[j].eulerAngles.x); - writer.WriteFloat(quaternions[j].eulerAngles.y); - writer.WriteFloat(quaternions[j].eulerAngles.z); - } - break; - } - + FieldTypeHelper.WriteFieldType(writer, syncedVarFields[i].FieldInfo, this, syncedVarFields[i].FieldType); if (nonTargetDirtyCount == 0) { //Only targeted SyncedVars were changed. Thus we need to set them as non dirty here since it wont be done by the next loop. @@ -750,149 +370,21 @@ namespace MLAPI.MonoBehaviours.Core //It's sync time. This is the NON target receivers packet. using (BitWriter writer = new BitWriter()) { - //Write all indexes - writer.WriteByte(nonTargetDirtyCount); - writer.WriteUInt(networkId); //NetId - writer.WriteUShort(networkedObject.GetOrderIndex(this)); //Behaviour OrderIndex - for (byte i = 0; i < syncedVarFields.Count; i++) + //Write all indexes + 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) { - //Writes all the indexes of the dirty syncvars. - if (syncedVarFields[i].Dirty == true && !syncedVarFields[i].Target) - { - writer.WriteByte(i); //FieldIndex - switch (syncedVarFields[i].FieldType) - { - 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.BoolArray: - bool[] bools = (bool[])syncedVarFields[i].FieldInfo.GetValue(this); - writer.WriteUShort((ushort)bools.Length); - for (int j = 0; j < bools.Length; j++) - writer.WriteBool(bools[j]); - break; - case FieldType.ByteArray: - writer.WriteByteArray((byte[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.DoubleArray: - writer.WriteDoubleArray((double[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.SingleArray: - writer.WriteFloatArray((float[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.IntArray: - writer.WriteIntArray((int[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.LongArray: - writer.WriteLongArray((long[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.SByteArray: - writer.WriteSByteArray((sbyte[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.ShortArray: - writer.WriteShortArray((short[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.UIntArray: - writer.WriteUIntArray((uint[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.ULongArray: - writer.WriteULongArray((ulong[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.UShortArray: - writer.WriteUShortArray((ushort[])syncedVarFields[i].FieldInfo.GetValue(this)); - break; - case FieldType.StringArray: - string[] strings = (string[])syncedVarFields[i].FieldInfo.GetValue(this); - writer.WriteUShort((ushort)strings.Length); - for (int j = 0; j < strings.Length; j++) - writer.WriteString(strings[j]); - break; - case FieldType.Vector3Array: - Vector3[] vector3s = (Vector3[])syncedVarFields[i].FieldInfo.GetValue(this); - writer.WriteUShort((ushort)vector3s.Length); - for (int j = 0; j < vector3s.Length; j++) - { - writer.WriteFloat(vector3s[j].x); - writer.WriteFloat(vector3s[j].y); - writer.WriteFloat(vector3s[j].z); - } - break; - case FieldType.Vector2Array: - Vector2[] vector2s = (Vector2[])syncedVarFields[i].FieldInfo.GetValue(this); - writer.WriteUShort((ushort)vector2s.Length); - for (int j = 0; j < vector2s.Length; j++) - { - writer.WriteFloat(vector2s[j].x); - writer.WriteFloat(vector2s[j].y); - } - break; - case FieldType.QuaternionArray: - Quaternion[] quaternions = (Quaternion[])syncedVarFields[i].FieldInfo.GetValue(this); - writer.WriteUShort((ushort)quaternions.Length); - for (int j = 0; j < quaternions.Length; j++) - { - writer.WriteFloat(quaternions[j].eulerAngles.x); - writer.WriteFloat(quaternions[j].eulerAngles.y); - writer.WriteFloat(quaternions[j].eulerAngles.z); - } - break; - - } - syncedVarFields[i].FieldValue = syncedVarFields[i].FieldInfo.GetValue(this); - syncedVarFields[i].Dirty = false; - } + writer.WriteByte(i); //FieldIndex + FieldTypeHelper.WriteFieldType(writer, syncedVarFields[i].FieldInfo, this, syncedVarFields[i].FieldType); + syncedVarFields[i].FieldValue = syncedVarFields[i].FieldInfo.GetValue(this); + syncedVarFields[i].Dirty = false; } - + } InternalMessageHandler.Send("MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer.Finalize(), ownerClientId); // Send to everyone except target. } }