fix: Unity 2020 support

This commit is contained in:
vis2k 2022-11-09 15:01:33 +01:00
parent 1dada3bb4b
commit 53a9717334
3 changed files with 17 additions and 4 deletions

View File

@ -178,14 +178,15 @@ internal virtual void Send(ArraySegment<byte> 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<int, Batcher> 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<byte> 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

View File

@ -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
}
}

View File

@ -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
}
}