From 53a97173347e86dcf3baf8f3b833c89031f3be59 Mon Sep 17 00:00:00 2001 From: vis2k Date: Wed, 9 Nov 2022 15:01:33 +0100 Subject: [PATCH] fix: Unity 2020 support --- Assets/Mirror/Core/NetworkConnection.cs | 9 +++++---- Assets/Mirror/Core/Tools/Vector3Long.cs | 8 ++++++++ Assets/Mirror/Tests/Editor/Vector3LongTests.cs | 4 ++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/Assets/Mirror/Core/NetworkConnection.cs b/Assets/Mirror/Core/NetworkConnection.cs index 01c940174..fffb624e6 100644 --- a/Assets/Mirror/Core/NetworkConnection.cs +++ b/Assets/Mirror/Core/NetworkConnection.cs @@ -178,14 +178,15 @@ internal virtual void Send(ArraySegment segment, int channelId = Channels. internal virtual void Update() { // go through batches for all channels - foreach ((int key, Batcher batcher) in batches) + // foreach ((int key, Batcher batcher) in batches) // Unity 2020 doesn't support deconstruct yet + foreach (KeyValuePair kvp in batches) { // make and send as many batches as necessary from the stored // messages. using (NetworkWriterPooled writer = NetworkWriterPool.Get()) { // make a batch with our local time (double precision) - while (batcher.GetBatch(writer)) + while (kvp.Value.GetBatch(writer)) { // validate packet before handing the batch to the // transport. this guarantees that we always stay @@ -193,10 +194,10 @@ internal virtual void Update() // => just in case transport forgets to check it // => just in case mirror miscalulated it etc. ArraySegment segment = writer.ToArraySegment(); - if (ValidatePacketSize(segment, key)) + if (ValidatePacketSize(segment, kvp.Key)) { // send to transport - SendToTransport(segment, key); + SendToTransport(segment, kvp.Key); //UnityEngine.Debug.Log($"sending batch of {writer.Position} bytes for channel={kvp.Key} connId={connectionId}"); // reset writer for each new batch diff --git a/Assets/Mirror/Core/Tools/Vector3Long.cs b/Assets/Mirror/Core/Tools/Vector3Long.cs index 945649594..f7b86ebdc 100644 --- a/Assets/Mirror/Core/Tools/Vector3Long.cs +++ b/Assets/Mirror/Core/Tools/Vector3Long.cs @@ -48,6 +48,9 @@ public Vector3Long(long x, long y, long z) public static Vector3Long operator *(long n, Vector3Long a) => new Vector3Long(a.x * n, a.y * n, a.z * n); +// Unity 2019/2020 don't have HashCode.Combine yet. +// this is only to avoid reflection. without defining, it works too. +#if UNITY_2021_3_OR_NEWER // == returns true if approximately equal (with epsilon). [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator ==(Vector3Long a, Vector3Long b) => @@ -57,6 +60,7 @@ public Vector3Long(long x, long y, long z) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator !=(Vector3Long a, Vector3Long b) => !(a == b); +#endif // NO IMPLICIT System.Numerics.Vector3Long conversion because double<->float // would silently lose precision in large worlds. @@ -94,6 +98,9 @@ public long this[int index] [MethodImpl(MethodImplOptions.AggressiveInlining)] public override string ToString() => $"({x} {y} {z})"; +// Unity 2019/2020 don't have HashCode.Combine yet. +// this is only to avoid reflection. without defining, it works too. +#if UNITY_2021_3_OR_NEWER // equality //////////////////////////////////////////////////////////// // implement Equals & HashCode explicitly for performance. // calling .Equals (instead of "==") checks for exact equality. @@ -110,5 +117,6 @@ public override bool Equals(object other) => // default generated by rider [MethodImpl(MethodImplOptions.AggressiveInlining)] public override int GetHashCode() => HashCode.Combine(x, y, z); +#endif } } diff --git a/Assets/Mirror/Tests/Editor/Vector3LongTests.cs b/Assets/Mirror/Tests/Editor/Vector3LongTests.cs index 74b32a6ec..91d95518b 100644 --- a/Assets/Mirror/Tests/Editor/Vector3LongTests.cs +++ b/Assets/Mirror/Tests/Editor/Vector3LongTests.cs @@ -55,6 +55,7 @@ public void OperatorMultiply() Assert.That(2 * a, Is.EqualTo(new Vector3Long(2, 4, 6))); } +#if UNITY_2021_3_OR_NEWER [Test] public void OperatorEquals() { @@ -78,6 +79,7 @@ public void OperatorNotEquals() // two vectors which are definitely not the same Assert.That(a != Vector3Long.one, Is.True); } +#endif [Test] public void OperatorIndexer() @@ -114,6 +116,7 @@ public void ToStringTest() Assert.That(v.ToString(), Is.EqualTo("(-10 0 42)")); } +#if UNITY_2021_3_OR_NEWER [Test] public void EqualsVector3Long() { @@ -142,5 +145,6 @@ public void GetHashCodeTest() // should be different for different vectors Assert.That(Vector3Long.zero.GetHashCode(), !Is.EqualTo(Vector3Long.one.GetHashCode())); } +#endif } }