Skip to content

Commit ba0237b

Browse files
Merge branch 'develop-2.0.0' into fix/networkanimator-conditional-exit-2.0.0
2 parents 4fe966c + 9c9826a commit ba0237b

5 files changed

Lines changed: 57 additions & 14 deletions

File tree

‎com.unity.netcode.gameobjects/CHANGELOG.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ Additional documentation and release notes are available at [Multiplayer Documen
2323
### Fixed
2424

2525
- Issue where `NetworkAnimator` logged an error for a trigger-conditioned transition into an Exit node, and a client that joined during that transition, or during one into a sub-state machine, did not synchronize with it. Transitions that pass through Exit, Entry or sub-state machine nodes now resolve to the state they reach, and a late-joining client cross-fades from the matching originating state when several states transition into the same destination. (#4240)
26-
- Issue where a client promoted to session owner could throw an exception when synchronizing a newly joining client. (#4217)
26+
- Issue where `NetworkTime` reported a `TickOffset` of almost a whole tick, instead of zero, for some times that land on a tick (for example 0.3 at a tick rate of 10), which put `TickWithPartial` one tick ahead or, in long sessions, `Tick` one tick behind. Times between minus one tick and zero now report tick -1 with a positive `TickOffset`, as other negative times do. (#4227)
27+
- Issue where bits written with `BitWriter` were lost when a later `BitWriter.TryBeginWriteBits` call in the same bitwise context grew the `FastBufferWriter`. The bytes written earlier in that context were replaced with zeros or uninitialized memory. (#4225)
2728
- Issue where a growable `FastBufferWriter` created with a size of 0 never returned from its first write or `TryBeginWrite` call that needed the buffer to grow. (#4223)
2829
- Issue where a `FastBufferReader` created with an offset and the default length of -1 read past the end of its source. The `ArraySegment<byte>` overload that takes an offset now applies it from the start of the segment. (#4221)
30+
- Issue where a client promoted to session owner could throw an exception when synchronizing a newly joining client. (#4217)
2931
- Issue where a client synchronized while a `NetworkList<T>` still had unsent changes applied those changes twice, resulting in duplicate entries and index-based events landing on the wrong elements on that client. (#4215)
3032
- Issue where showing a `NetworkObject` with `NetworkShow` while its `NetworkVariable` updates were held back by `NetworkVariableUpdateTraits` stopped those pending changes from being sent until the variable was modified again. (#4215)
3133
- Issue where a rejected parent change, for example one made by a client without parenting authority, restored the `NetworkObject`'s parent but left it at the moved position when the change was made with `worldPositionStays` disabled. The original position, rotation and scale are now restored. (#4203)

‎com.unity.netcode.gameobjects/Runtime/Serialization/FastBufferWriter.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ internal unsafe void Grow(int additionalSizeRequired)
204204
#if DEBUG
205205
UnsafeUtility.MemSet(newBuffer, 0, newSize);
206206
#endif
207-
UnsafeUtility.MemCpy(newBuffer, Handle->BufferPointer, Length);
207+
// Not Length: bytes written in an open bitwise context are not committed to Position until it is disposed.
208+
UnsafeUtility.MemCpy(newBuffer, Handle->BufferPointer, Handle->Capacity);
208209
if (Handle->BufferGrew)
209210
{
210211
UnsafeUtility.Free(Handle->BufferPointer, Handle->Allocator);

‎com.unity.netcode.gameobjects/Runtime/Timing/NetworkTime.cs‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,20 +136,18 @@ public NetworkTime TimeTicksAgo(int ticks, float offset = 0.0f)
136136
private void UpdateCache()
137137
{
138138
double d = m_TimeSec / m_TickInterval;
139-
m_CachedTick = (int)d;
140-
// This check is needed due to double division imprecision of large numbers
141-
if ((d - m_CachedTick) >= 0.999999999999)
139+
// Floor keeps the offset positive for negative time.
140+
double tick = Math.Floor(d);
141+
double fraction = d - tick;
142+
// Double division imprecision can land a whole tick just below its value (0.3 / 0.1 = 2.9999999999999996),
143+
// and the error grows with the tick count.
144+
if (fraction >= 1d - Math.Max(1e-12, Math.Abs(d) * 1e-13))
142145
{
143-
m_CachedTick++;
144-
}
145-
m_CachedTickOffset = ((d - Math.Truncate(d)) * m_TickInterval);
146-
147-
// This handles negative time, decreases tick by 1 and makes offset positive.
148-
if (m_CachedTick < 0 && m_CachedTickOffset != 0d)
149-
{
150-
m_CachedTick--;
151-
m_CachedTickOffset = m_TickInterval + m_CachedTickOffset;
146+
tick++;
147+
fraction = 0d;
152148
}
149+
m_CachedTick = (int)tick;
150+
m_CachedTickOffset = fraction * m_TickInterval;
153151
}
154152

155153
/// <summary>

‎com.unity.netcode.gameobjects/Tests/Editor/Serialization/BitWriterTests.cs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,5 +329,30 @@ public unsafe void TestWritingBitsThrowsIfTryBeginWriteNotCalled()
329329

330330
}
331331
}
332+
333+
[Test]
334+
public void TestTryBeginWriteBitsGrowingBufferKeepsBitwiseWrites([Values] bool writeLeadingByte)
335+
{
336+
var writer = new FastBufferWriter(writeLeadingByte ? 2 : 1, Allocator.Temp, 16);
337+
using (writer)
338+
{
339+
if (writeLeadingByte)
340+
{
341+
writer.WriteByteSafe(0x11);
342+
}
343+
344+
using (var bitWriter = writer.EnterBitwiseContext())
345+
{
346+
Assert.IsTrue(bitWriter.TryBeginWriteBits(8));
347+
bitWriter.WriteBits(0xAB, 8);
348+
// Grows the buffer while 0xAB is still uncommitted to the writer.
349+
Assert.IsTrue(bitWriter.TryBeginWriteBits(16));
350+
bitWriter.WriteBits(0xCDEFUL, 16);
351+
}
352+
353+
var expected = writeLeadingByte ? new byte[] { 0x11, 0xAB, 0xEF, 0xCD } : new byte[] { 0xAB, 0xEF, 0xCD };
354+
CollectionAssert.AreEqual(expected, writer.ToArray());
355+
}
356+
}
332357
}
333358
}

‎com.unity.netcode.gameobjects/Tests/Editor/Timing/NetworkTimeTests.cs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@ public void NetworkTimeCreate(double time, double tickOffset, double epsilon = 0
6666
Assert.IsTrue(Approximately(networkTime.TickOffset, tickOffset));
6767
}
6868

69+
[Test]
70+
[TestCase(0.25d, 2, 0.05d)]
71+
[TestCase(0.3d, 3, 0d)]
72+
[TestCase(0.7d, 7, 0d)]
73+
[TestCase(-0.05d, -1, 0.05d)]
74+
[TestCase(-0.15d, -2, 0.05d)]
75+
[TestCase(-0.3d, -3, 0d)]
76+
[TestCase(16384.3d, 163843, 0d)]
77+
public void NetworkTimeTickAndOffset(double time, int expectedTick, double expectedTickOffset)
78+
{
79+
var networkTime = new NetworkTime(10, time);
80+
81+
Assert.AreEqual(expectedTick, networkTime.Tick);
82+
Assert.IsTrue(Approximately(networkTime.TickOffset, expectedTickOffset));
83+
Assert.IsTrue(Approximately(networkTime.FixedTime + networkTime.TickOffset, time));
84+
}
85+
6986
[Test]
7087
public void NetworkTimeDefault()
7188
{

0 commit comments

Comments
 (0)