From afc45981f339f633633507fee3014d31e2f84d58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albin=20Cor=C3=A9n?= <2108U9@gmail.com> Date: Thu, 19 Apr 2018 17:21:22 +0200 Subject: [PATCH] Added support for more array types to SyncedVars --- MLAPI/Data/FieldType.cs | 42 +++ .../MonoBehaviours/Core/NetworkedBehaviour.cs | 282 +++++++++++++++++- 2 files changed, 319 insertions(+), 5 deletions(-) diff --git a/MLAPI/Data/FieldType.cs b/MLAPI/Data/FieldType.cs index 0da5c11..20c41d9 100644 --- a/MLAPI/Data/FieldType.cs +++ b/MLAPI/Data/FieldType.cs @@ -23,7 +23,21 @@ namespace MLAPI.Data Vector3, Vector2, Quaternion, + BoolArray, ByteArray, + DoubleArray, + SingleArray, + IntArray, + LongArray, + SByteArray, + ShortArray, + UIntArray, + ULongArray, + UShortArray, + StringArray, + Vector3Array, + Vector2Array, + QuaternionArray, Invalid } @@ -61,8 +75,36 @@ namespace MLAPI.Data return FieldType.Vector2; else if (type == typeof(Quaternion)) return FieldType.Quaternion; + else if (type == typeof(bool[])) + return FieldType.BoolArray; else if (type == typeof(byte[])) return FieldType.ByteArray; + else if (type == typeof(double[])) + return FieldType.DoubleArray; + else if (type == typeof(float[])) + return FieldType.SingleArray; + else if (type == typeof(int[])) + return FieldType.IntArray; + else if (type == typeof(long[])) + return FieldType.LongArray; + else if (type == typeof(sbyte[])) + return FieldType.SByteArray; + else if (type == typeof(short[])) + return FieldType.ShortArray; + else if (type == typeof(uint[])) + return FieldType.UIntArray; + else if (type == typeof(ulong[])) + return FieldType.ULongArray; + else if (type == typeof(ushort[])) + return FieldType.UShortArray; + else if (type == typeof(string[])) + return FieldType.StringArray; + else if (type == typeof(Vector3[])) + return FieldType.Vector3Array; + else if (type == typeof(Vector2[])) + return FieldType.Vector2Array; + else if (type == typeof(Quaternion[])) + return FieldType.QuaternionArray; else return FieldType.Invalid; } diff --git a/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs b/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs index 56593ac..e9fc905 100644 --- a/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs +++ b/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs @@ -334,9 +334,78 @@ namespace MLAPI.MonoBehaviours.Core 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; + } } InternalMessageHandler.Send(clientId, "MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer.Finalize()); @@ -439,10 +508,77 @@ namespace MLAPI.MonoBehaviours.Core 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; @@ -523,9 +659,77 @@ namespace MLAPI.MonoBehaviours.Core 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; } if (nonTargetDirtyCount == 0) @@ -611,11 +815,79 @@ namespace MLAPI.MonoBehaviours.Core writer.WriteFloat(euler.y); writer.WriteFloat(euler.z); break; - case FieldType.ByteArray: - writer.WriteByteArray((byte[])syncedVarFields[i].FieldInfo.GetValue(this)); - 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; }