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.0.0-pre.2] - 2024-06-17 ### Added - Added `AnticipatedNetworkVariable<T>`, which adds support for client anticipation of `NetworkVariable` values, allowing for more responsive gameplay. (#2957) - Added `AnticipatedNetworkTransform`, which adds support for client anticipation of NetworkTransforms. (#2957) - Added `NetworkVariableBase.ExceedsDirtinessThreshold` to allow network variables to throttle updates by only sending updates when the difference between the current and previous values exceeds a threshold. (This is exposed in `NetworkVariable<T>` with the callback `NetworkVariable<T>.CheckExceedsDirtinessThreshold`). (#2957) - Added `NetworkVariableUpdateTraits`, which add additional throttling support: `MinSecondsBetweenUpdates` will prevent the `NetworkVariable` from sending updates more often than the specified time period (even if it exceeds the dirtiness threshold), while `MaxSecondsBetweenUpdates` will force a dirty `NetworkVariable` to send an update after the specified time period even if it has not yet exceeded the dirtiness threshold. (#2957) - Added virtual method `NetworkVariableBase.OnInitialize` which can be used by `NetworkVariable` subclasses to add initialization code. (#2957) - Added `NetworkTime.TickWithPartial`, which represents the current tick as a double that includes the fractional/partial tick value. (#2957) - Added `NetworkTickSystem.AnticipationTick`, which can be helpful with implementation of client anticipation. This value represents the tick the current local client was at at the beginning of the most recent network round trip, which enables it to correlate server update ticks with the client tick that may have triggered them. (#2957) - Added event `NetworkManager.OnSessionOwnerPromoted` that is invoked when a new session owner promotion occurs. (#2948) - Added `NetworkRigidBodyBase.GetLinearVelocity` and `NetworkRigidBodyBase.SetLinearVelocity` convenience/helper methods. (#2948) - Added `NetworkRigidBodyBase.GetAngularVelocity` and `NetworkRigidBodyBase.SetAngularVelocity` convenience/helper methods. (#2948) ### Fixed - Fixed issue when `NetworkTransform` half float precision is enabled and ownership changes the current base position was not being synchronized. (#2948) - Fixed issue where `OnClientConnected` not being invoked on the session owner when connecting to a new distributed authority session. (#2948) - Fixed issue where Rigidbody micro-motion (i.e. relatively small velocities) would result in non-authority instances slightly stuttering as the body would come to a rest (i.e. no motion). Now, the threshold value can increase at higher velocities and can decrease slightly below the provided threshold to account for this. (#2948) ### Changed - Changed `NetworkAnimator` no longer requires the `Animator` component to exist on the same `GameObject`. (#2957) - Changed `NetworkObjectReference` and `NetworkBehaviourReference` to allow null references when constructing and serializing. (#2957) - Changed the client's owned objects is now returned (`NetworkClient` and `NetworkSpawnManager`) as an array as opposed to a list for performance purposes. (#2948) - Changed `NetworkTransfrom.TryCommitTransformToServer` to be internal as it will be removed by the final 2.0.0 release. (#2948) - Changed `NetworkTransformEditor.OnEnable` to a virtual method to be able to customize a `NetworkTransform` derived class by creating a derived editor control from `NetworkTransformEditor`. (#2948)
153 lines
7.6 KiB
C#
153 lines
7.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
#endif
|
|
|
|
namespace Unity.Netcode
|
|
{
|
|
internal struct ILPPMessageProvider : INetworkMessageProvider
|
|
{
|
|
#pragma warning disable IDE1006 // disable naming rule violation check
|
|
// This is NOT modified by RuntimeAccessModifiersILPP right now, but is populated by ILPP.
|
|
internal static readonly List<NetworkMessageManager.MessageWithHandler> __network_message_types = new List<NetworkMessageManager.MessageWithHandler>();
|
|
#pragma warning restore IDE1006 // restore naming rule violation check
|
|
|
|
/// <summary>
|
|
/// Enum representing the different types of messages that can be sent over the network.
|
|
/// The values cannot be changed, as they are used to serialize and deserialize messages.
|
|
/// Adding new messages should be done by adding new values to the end of the enum
|
|
/// using the next free value.
|
|
/// </summary>
|
|
/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
/// Add any new Message types to this table at the END with incremented index value
|
|
/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
internal enum NetworkMessageTypes : uint
|
|
{
|
|
ConnectionApproved = 0,
|
|
ConnectionRequest = 1,
|
|
ChangeOwnership = 2,
|
|
ClientConnected = 3,
|
|
ClientDisconnected = 4,
|
|
ClientRpc = 5,
|
|
CreateObject = 6,
|
|
DestroyObject = 7,
|
|
DisconnectReason = 8,
|
|
ForwardClientRpc = 9,
|
|
ForwardServerRpc = 10,
|
|
NamedMessage = 11,
|
|
NetworkTransformMessage = 12,
|
|
NetworkVariableDelta = 13,
|
|
ParentSync = 14,
|
|
Proxy = 15,
|
|
Rpc = 16,
|
|
SceneEvent = 17,
|
|
ServerLog = 18,
|
|
ServerRpc = 19,
|
|
SessionOwner = 20,
|
|
TimeSync = 21,
|
|
Unnamed = 22,
|
|
AnticipationCounterSyncPingMessage = 23,
|
|
AnticipationCounterSyncPongMessage = 24,
|
|
}
|
|
|
|
|
|
// Enable this for integration tests that need no message types defined
|
|
internal static bool IntegrationTestNoMessages;
|
|
|
|
public List<NetworkMessageManager.MessageWithHandler> GetMessages()
|
|
{
|
|
// return no message types when defined for integration tests
|
|
if (IntegrationTestNoMessages)
|
|
{
|
|
return new List<NetworkMessageManager.MessageWithHandler>();
|
|
}
|
|
var messageTypeCount = Enum.GetValues(typeof(NetworkMessageTypes)).Length;
|
|
// Assure the allowed types count is the same as our NetworkMessageType enum count
|
|
if (__network_message_types.Count != messageTypeCount)
|
|
{
|
|
throw new Exception($"Allowed types is not equal to the number of message type indices! Allowed Count: {__network_message_types.Count} | Index Count: {messageTypeCount}");
|
|
}
|
|
|
|
// Populate with blanks to be replaced later
|
|
var adjustedMessageTypes = new List<NetworkMessageManager.MessageWithHandler>();
|
|
var blank = new NetworkMessageManager.MessageWithHandler();
|
|
for (int i = 0; i < messageTypeCount; i++)
|
|
{
|
|
adjustedMessageTypes.Add(blank);
|
|
}
|
|
|
|
// Create a type to enum index lookup table
|
|
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
// Add new Message types to this table paired with its new NetworkMessageTypes enum
|
|
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
var messageTypes = new Dictionary<Type, NetworkMessageTypes>
|
|
{
|
|
{ typeof(ConnectionApprovedMessage), NetworkMessageTypes.ConnectionApproved }, // This MUST be first
|
|
{ typeof(ConnectionRequestMessage), NetworkMessageTypes.ConnectionRequest }, // This MUST be second
|
|
{ typeof(ChangeOwnershipMessage), NetworkMessageTypes.ChangeOwnership },
|
|
{ typeof(ClientConnectedMessage), NetworkMessageTypes.ClientConnected },
|
|
{ typeof(ClientDisconnectedMessage), NetworkMessageTypes.ClientDisconnected },
|
|
{ typeof(ClientRpcMessage), NetworkMessageTypes.ClientRpc },
|
|
{ typeof(CreateObjectMessage), NetworkMessageTypes.CreateObject },
|
|
{ typeof(DestroyObjectMessage), NetworkMessageTypes.DestroyObject },
|
|
{ typeof(DisconnectReasonMessage), NetworkMessageTypes.DisconnectReason },
|
|
{ typeof(ForwardClientRpcMessage), NetworkMessageTypes.ForwardClientRpc },
|
|
{ typeof(ForwardServerRpcMessage), NetworkMessageTypes.ForwardServerRpc },
|
|
{ typeof(NamedMessage), NetworkMessageTypes.NamedMessage },
|
|
{ typeof(NetworkTransformMessage), NetworkMessageTypes.NetworkTransformMessage },
|
|
{ typeof(NetworkVariableDeltaMessage), NetworkMessageTypes.NetworkVariableDelta },
|
|
{ typeof(ParentSyncMessage), NetworkMessageTypes.ParentSync },
|
|
{ typeof(ProxyMessage), NetworkMessageTypes.Proxy },
|
|
{ typeof(RpcMessage), NetworkMessageTypes.Rpc },
|
|
{ typeof(SceneEventMessage), NetworkMessageTypes.SceneEvent },
|
|
{ typeof(ServerLogMessage), NetworkMessageTypes.ServerLog },
|
|
{ typeof(ServerRpcMessage), NetworkMessageTypes.ServerRpc },
|
|
{ typeof(TimeSyncMessage), NetworkMessageTypes.TimeSync },
|
|
{ typeof(UnnamedMessage), NetworkMessageTypes.Unnamed },
|
|
{ typeof(SessionOwnerMessage), NetworkMessageTypes.SessionOwner },
|
|
{ typeof(AnticipationCounterSyncPingMessage), NetworkMessageTypes.AnticipationCounterSyncPingMessage},
|
|
{ typeof(AnticipationCounterSyncPongMessage), NetworkMessageTypes.AnticipationCounterSyncPongMessage},
|
|
};
|
|
|
|
// Assure the type to lookup table count and NetworkMessageType enum count matches (i.e. to catch human error when adding new messages)
|
|
if (messageTypes.Count != messageTypeCount)
|
|
{
|
|
throw new Exception($"Message type to Message type index count mistmatch! Table Count: {messageTypes.Count} | Index Count: {messageTypeCount}");
|
|
}
|
|
|
|
// Now order the allowed types list based on the order of the NetworkMessageType enum
|
|
foreach (var messageHandler in __network_message_types)
|
|
{
|
|
if (!messageTypes.ContainsKey(messageHandler.MessageType))
|
|
{
|
|
throw new Exception($"Missing message type from lookup table: {messageHandler.MessageType}");
|
|
}
|
|
adjustedMessageTypes[(int)messageTypes[messageHandler.MessageType]] = messageHandler;
|
|
}
|
|
|
|
// return the NetworkMessageType enum ordered list
|
|
return adjustedMessageTypes;
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
[InitializeOnLoadMethod]
|
|
public static void NotifyOnPlayStateChange()
|
|
{
|
|
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
|
|
}
|
|
|
|
public static void OnPlayModeStateChanged(PlayModeStateChange change)
|
|
{
|
|
if (change == PlayModeStateChange.ExitingPlayMode)
|
|
{
|
|
// Clear out the network message types, because ILPP-generated RuntimeInitializeOnLoad code will
|
|
// run again and add more messages to it.
|
|
__network_message_types.Clear();
|
|
}
|
|
}
|
|
|
|
#endif
|
|
}
|
|
}
|