This repository has been archived on 2025-04-22. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Unity Technologies 8fe07bbad2 com.unity.netcode.gameobjects@2.2.0
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

Additional documentation and release notes are available at [Multiplayer Documentation](https://docs-multiplayer.unity3d.com).

## [2.2.0] - 2024-12-12

### Added

- Added `NetworkObject.OwnershipStatus.SessionOwner` to allow Network Objects to be distributable and only owned by the Session Owner. This flag will override all other `OwnershipStatus` flags. (#3175)
- Added `UnityTransport.GetEndpoint` method to provide a way to obtain `NetworkEndpoint` information of a connection via client identifier. (#3130)
- Added `NetworkTransport.OnEarlyUpdate` and `NetworkTransport.OnPostLateUpdate` methods to provide more control over handling transport related events at the start and end of each frame. (#3113)

### Fixed

- Fixed issue where the server, host, or session owner would not populate the in-scene place `NetworkObject` table if the scene was loaded prior to starting the `NetworkManager`. (#3177)
- Fixed issue where the `NetworkObjectIdHash` value could be incorrect when entering play mode while still in prefab edit mode with pending changes and using MPPM. (#3162)
- Fixed issue where a sever only `NetworkManager` instance would spawn the actual `NetworkPrefab`'s `GameObject` as opposed to creating an instance of it. (#3160)
- Fixed issue where only the session owner (as opposed to all clients) would handle spawning prefab overrides properly when using a distributed authority network topology. (#3160)
- Fixed issue where an exception was thrown when calling `NetworkManager.Shutdown` after calling `UnityTransport.Shutdown`. (#3118)
- Fixed issue where `NetworkList` properties on in-scene placed `NetworkObject`s could cause small memory leaks when entering playmode. (#3147)
- Fixed in-scene `NertworkObject` synchronization issue when loading a scene with currently connected clients connected to a session created by a `NetworkManager` started as a server (i.e. not as a host). (#3133)
- Fixed issue where a `NetworkManager` started as a server would not add itself as an observer to in-scene placed `NetworkObject`s instantiated and spawned by a scene loading event. (#3133)
- Fixed issue where spawning a player using `NetworkObject.InstantiateAndSpawn` or `NetworkSpawnManager.InstantiateAndSpawn` would not update the `NetworkSpawnManager.PlayerObjects` or assign the newly spawned player to the `NetworkClient.PlayerObject`. (#3122)
- Fixed issue where queued UnitTransport (NetworkTransport) message batches were being sent on the next frame. They are now sent at the end of the frame during `PostLateUpdate`.  (#3113)
- Fixed issue where `NotOwnerRpcTarget` or `OwnerRpcTarget` were not using their replacements `NotAuthorityRpcTarget` and `AuthorityRpcTarget` which would invoke a warning. (#3111)
- Fixed issue where client is removed as an observer from spawned objects when their player instance is despawned. (#3110)
- Fixed issue where `NetworkAnimator` would statically allocate write buffer space for `Animator` parameters that could cause a write error if the number of parameters exceeded the space allocated. (#3108)

### Changed

- In-scene placed `NetworkObject`s have been made distributable when balancing object distribution after a connection event. (#3175)
- Optimised `NetworkVariable` and `NetworkTransform` related packets when in Distributed Authority mode.
- The Debug Simulator section of the Unity Transport component was removed. This section was not functional anymore and users are now recommended to use the more featureful [Network Simulator](https://docs-multiplayer.unity3d.com/tools/current/tools-network-simulator/) tool from the Multiplayer Tools package instead. (#3121)
2024-12-12 00:00:00 +00:00

214 lines
8.1 KiB
C#

using System;
using System.Runtime.CompilerServices;
using Unity.Collections.LowLevel.Unsafe;
namespace Unity.Netcode
{
/// <summary>
/// Helper class for doing bitwise writes for a FastBufferWriter.
/// Ensures all bitwise writes end on proper byte alignment so FastBufferWriter doesn't have to be concerned
/// with misaligned writes.
/// </summary>
public ref struct BitWriter
{
private FastBufferWriter m_Writer;
private unsafe byte* m_BufferPointer;
private readonly int m_Position;
private int m_BitPosition;
#if DEVELOPMENT_BUILD || UNITY_EDITOR
private int m_AllowedBitwiseWriteMark;
#endif
private const int k_BitsPerByte = 8;
/// <summary>
/// Whether or not the current BitPosition is evenly divisible by 8. I.e. whether or not the BitPosition is at a byte boundary.
/// </summary>
public bool BitAligned
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => (m_BitPosition & 7) == 0;
}
private int BytePosition => m_BitPosition >> 3;
internal unsafe BitWriter(FastBufferWriter writer)
{
m_Writer = writer;
m_BufferPointer = writer.Handle->BufferPointer + writer.Handle->Position;
m_Position = writer.Handle->Position;
m_BitPosition = 0;
#if DEVELOPMENT_BUILD || UNITY_EDITOR
m_AllowedBitwiseWriteMark = (m_Writer.Handle->AllowedWriteMark - m_Writer.Handle->Position) * k_BitsPerByte;
#endif
}
/// <summary>
/// Pads the written bit count to byte alignment and commits the write back to the writer
/// </summary>
public void Dispose()
{
var bytesWritten = m_BitPosition >> 3;
if (!BitAligned)
{
// Accounting for the partial write
++bytesWritten;
}
m_Writer.CommitBitwiseWrites(bytesWritten);
}
/// <summary>
/// Allows faster serialization by batching bounds checking.
/// When you know you will be writing multiple fields back-to-back and you know the total size,
/// you can call TryBeginWriteBits() once on the total size, and then follow it with calls to
/// WriteBit() or WriteBits().
///
/// Bitwise write operations will throw OverflowException in editor and development builds if you
/// go past the point you've marked using TryBeginWriteBits(). In release builds, OverflowException will not be thrown
/// for performance reasons, since the point of using TryBeginWrite is to avoid bounds checking in the following
/// operations in release builds. Instead, attempting to write past the marked position in release builds
/// will write to random memory and cause undefined behavior, likely including instability and crashes.
/// </summary>
/// <param name="bitCount">Number of bits you want to write, in total</param>
/// <returns>True if you can write, false if that would exceed buffer bounds</returns>
public unsafe bool TryBeginWriteBits(int bitCount)
{
var newBitPosition = m_BitPosition + bitCount;
var totalBytesWrittenInBitwiseContext = newBitPosition >> 3;
if ((newBitPosition & 7) != 0)
{
// Accounting for the partial write
++totalBytesWrittenInBitwiseContext;
}
if (m_Position + totalBytesWrittenInBitwiseContext > m_Writer.Handle->Capacity)
{
if (m_Position + totalBytesWrittenInBitwiseContext > m_Writer.Handle->MaxCapacity)
{
return false;
}
if (m_Writer.Handle->Capacity < m_Writer.Handle->MaxCapacity)
{
m_Writer.Grow(totalBytesWrittenInBitwiseContext);
m_BufferPointer = m_Writer.Handle->BufferPointer + m_Writer.Handle->Position;
}
else
{
return false;
}
}
#if DEVELOPMENT_BUILD || UNITY_EDITOR
m_AllowedBitwiseWriteMark = newBitPosition;
#endif
return true;
}
/// <summary>
/// Write s certain amount of bits to the stream.
/// </summary>
/// <param name="value">Value to get bits from.</param>
/// <param name="bitCount">Amount of bits to write</param>
public unsafe void WriteBits(ulong value, uint bitCount)
{
#if DEVELOPMENT_BUILD || UNITY_EDITOR
if (bitCount > 64)
{
throw new ArgumentOutOfRangeException(nameof(bitCount), "Cannot write more than 64 bits from a 64-bit value!");
}
int checkPos = (int)(m_BitPosition + bitCount);
if (checkPos > m_AllowedBitwiseWriteMark)
{
throw new OverflowException($"Attempted to write without first calling {nameof(TryBeginWriteBits)}()");
}
#endif
int wholeBytes = (int)bitCount / k_BitsPerByte;
byte* asBytes = (byte*)&value;
if (BitAligned)
{
if (wholeBytes != 0)
{
WritePartialValue(value, wholeBytes);
}
}
else
{
for (var i = 0; i < wholeBytes; ++i)
{
WriteMisaligned(asBytes[i]);
}
}
for (var count = wholeBytes * k_BitsPerByte; count < bitCount; ++count)
{
WriteBit((value & (1UL << count)) != 0);
}
}
/// <summary>
/// Write bits to stream.
/// </summary>
/// <param name="value">Value to get bits from.</param>
/// <param name="bitCount">Amount of bits to write.</param>
public void WriteBits(byte value, uint bitCount)
{
#if DEVELOPMENT_BUILD || UNITY_EDITOR
int checkPos = (int)(m_BitPosition + bitCount);
if (checkPos > m_AllowedBitwiseWriteMark)
{
throw new OverflowException($"Attempted to write without first calling {nameof(TryBeginWriteBits)}()");
}
#endif
for (int i = 0; i < bitCount; ++i)
{
WriteBit(((value >> i) & 1) != 0);
}
}
/// <summary>
/// Write a single bit to the buffer
/// </summary>
/// <param name="bit">Value of the bit. True represents 1, False represents 0</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe void WriteBit(bool bit)
{
#if DEVELOPMENT_BUILD || UNITY_EDITOR
int checkPos = (m_BitPosition + 1);
if (checkPos > m_AllowedBitwiseWriteMark)
{
throw new OverflowException($"Attempted to write without first calling {nameof(TryBeginWriteBits)}()");
}
#endif
int offset = m_BitPosition & 7;
int pos = BytePosition;
++m_BitPosition;
m_BufferPointer[pos] = (byte)(bit ? (m_BufferPointer[pos] & ~(1 << offset)) | (1 << offset) : (m_BufferPointer[pos] & ~(1 << offset)));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe void WritePartialValue<T>(T value, int bytesToWrite, int offsetBytes = 0) where T : unmanaged
{
byte* ptr = ((byte*)&value) + offsetBytes;
byte* bufferPointer = m_BufferPointer + BytePosition;
UnsafeUtility.MemCpy(bufferPointer, ptr, bytesToWrite);
m_BitPosition += bytesToWrite * k_BitsPerByte;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe void WriteMisaligned(byte value)
{
int off = m_BitPosition & 7;
int pos = m_BitPosition >> 3;
int shift1 = 8 - off;
m_BufferPointer[pos + 1] = (byte)((m_BufferPointer[pos + 1] & (0xFF << off)) | (value >> shift1));
m_BufferPointer[pos] = (byte)((m_BufferPointer[pos] & (0xFF >> shift1)) | (value << off));
m_BitPosition += 8;
}
}
}