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). ## [1.10.0] - 2024-07-22 ### Added - Added `NetworkBehaviour.OnNetworkPreSpawn` and `NetworkBehaviour.OnNetworkPostSpawn` methods that provide the ability to handle pre and post spawning actions during the `NetworkObject` spawn sequence. (#2906) - Added a client-side only `NetworkBehaviour.OnNetworkSessionSynchronized` convenience method that is invoked on all `NetworkBehaviour`s after a newly joined client has finished synchronizing with the network session in progress. (#2906) - Added `NetworkBehaviour.OnInSceneObjectsSpawned` convenience method that is invoked when all in-scene `NetworkObject`s have been spawned after a scene has been loaded or upon a host or server starting. (#2906) ### Fixed - Fixed issue where the realtime network stats monitor was not able to display RPC traffic in release builds due to those stats being only available in development builds or the editor. (#2980) - Fixed issue where `NetworkManager.ScenesLoaded` was not being updated if `PostSynchronizationSceneUnloading` was set and any loaded scenes not used during synchronization were unloaded.(#2977) - Fixed issue where internal delta serialization could not have a byte serializer defined when serializing deltas for other types. Added `[GenerateSerializationForType(typeof(byte))]` to both the `NetworkVariable` and `AnticipatedNetworkVariable` classes to assure a byte serializer is defined. (#2953) - Fixed issue with the client count not being correct on the host or server side when a client disconnects itself from a session. (#2941) - Fixed issue with the host trying to send itself a message that it has connected when first starting up. (#2941) - Fixed issue where in-scene placed NetworkObjects could be destroyed if a client disconnects early and/or before approval. (#2923) - Fixed issue where `NetworkDeltaPosition` would "jitter" periodically if both unreliable delta state updates and half-floats were used together. (#2922) - Fixed issue where `NetworkRigidbody2D` would not properly change body type based on the instance's authority when spawned. (#2916) - Fixed issue where a `NetworkObject` component's associated `NetworkBehaviour` components would not be detected if scene loading is disabled in the editor and the currently loaded scene has in-scene placed `NetworkObject`s. (#2906) - Fixed issue where an in-scene placed `NetworkObject` with `NetworkTransform` that is also parented under a `GameObject` would not properly synchronize when the parent `GameObject` had a world space position other than 0,0,0. (#2895) ### Changed
201 lines
7.6 KiB
C#
201 lines
7.6 KiB
C#
using System;
|
|
using Unity.Collections;
|
|
using UnityEngine;
|
|
|
|
namespace Unity.Netcode
|
|
{
|
|
internal static class RpcMessageHelpers
|
|
{
|
|
public static unsafe void Serialize(ref FastBufferWriter writer, ref RpcMetadata metadata, ref FastBufferWriter payload)
|
|
{
|
|
BytePacker.WriteValueBitPacked(writer, metadata.NetworkObjectId);
|
|
BytePacker.WriteValueBitPacked(writer, metadata.NetworkBehaviourId);
|
|
BytePacker.WriteValueBitPacked(writer, metadata.NetworkRpcMethodId);
|
|
writer.WriteBytesSafe(payload.GetUnsafePtr(), payload.Length);
|
|
}
|
|
|
|
public static unsafe bool Deserialize(ref FastBufferReader reader, ref NetworkContext context, ref RpcMetadata metadata, ref FastBufferReader payload)
|
|
{
|
|
ByteUnpacker.ReadValueBitPacked(reader, out metadata.NetworkObjectId);
|
|
ByteUnpacker.ReadValueBitPacked(reader, out metadata.NetworkBehaviourId);
|
|
ByteUnpacker.ReadValueBitPacked(reader, out metadata.NetworkRpcMethodId);
|
|
|
|
var networkManager = (NetworkManager)context.SystemOwner;
|
|
if (!networkManager.SpawnManager.SpawnedObjects.ContainsKey(metadata.NetworkObjectId))
|
|
{
|
|
networkManager.DeferredMessageManager.DeferMessage(IDeferredNetworkMessageManager.TriggerType.OnSpawn, metadata.NetworkObjectId, reader, ref context);
|
|
return false;
|
|
}
|
|
|
|
var networkObject = networkManager.SpawnManager.SpawnedObjects[metadata.NetworkObjectId];
|
|
var networkBehaviour = networkManager.SpawnManager.SpawnedObjects[metadata.NetworkObjectId].GetNetworkBehaviourAtOrderIndex(metadata.NetworkBehaviourId);
|
|
if (networkBehaviour == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (!NetworkBehaviour.__rpc_func_table[networkBehaviour.GetType()].ContainsKey(metadata.NetworkRpcMethodId))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
payload = new FastBufferReader(reader.GetUnsafePtrAtCurrentPosition(), Allocator.None, reader.Length - reader.Position);
|
|
|
|
#if DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
|
|
if (NetworkBehaviour.__rpc_name_table[networkBehaviour.GetType()].TryGetValue(metadata.NetworkRpcMethodId, out var rpcMethodName))
|
|
{
|
|
networkManager.NetworkMetrics.TrackRpcReceived(
|
|
context.SenderId,
|
|
networkObject,
|
|
rpcMethodName,
|
|
networkBehaviour.__getTypeName(),
|
|
reader.Length);
|
|
}
|
|
#endif
|
|
|
|
return true;
|
|
}
|
|
|
|
public static void Handle(ref NetworkContext context, ref RpcMetadata metadata, ref FastBufferReader payload, ref __RpcParams rpcParams)
|
|
{
|
|
var networkManager = (NetworkManager)context.SystemOwner;
|
|
if (!networkManager.SpawnManager.SpawnedObjects.TryGetValue(metadata.NetworkObjectId, out var networkObject))
|
|
{
|
|
throw new InvalidOperationException($"An RPC called on a {nameof(NetworkObject)} that is not in the spawned objects list. Please make sure the {nameof(NetworkObject)} is spawned before calling RPCs.");
|
|
}
|
|
var networkBehaviour = networkObject.GetNetworkBehaviourAtOrderIndex(metadata.NetworkBehaviourId);
|
|
|
|
try
|
|
{
|
|
NetworkBehaviour.__rpc_func_table[networkBehaviour.GetType()][metadata.NetworkRpcMethodId](networkBehaviour, payload, rpcParams);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.LogException(new Exception("Unhandled RPC exception!", ex));
|
|
if (networkManager.LogLevel == LogLevel.Developer)
|
|
{
|
|
Debug.Log($"RPC Table Contents");
|
|
foreach (var entry in NetworkBehaviour.__rpc_func_table[networkBehaviour.GetType()])
|
|
{
|
|
Debug.Log($"{entry.Key} | {entry.Value.Method.Name}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
internal struct RpcMetadata : INetworkSerializeByMemcpy
|
|
{
|
|
public ulong NetworkObjectId;
|
|
public ushort NetworkBehaviourId;
|
|
public uint NetworkRpcMethodId;
|
|
}
|
|
|
|
internal struct ServerRpcMessage : INetworkMessage
|
|
{
|
|
public int Version => 0;
|
|
|
|
public RpcMetadata Metadata;
|
|
|
|
public FastBufferWriter WriteBuffer;
|
|
public FastBufferReader ReadBuffer;
|
|
|
|
public unsafe void Serialize(FastBufferWriter writer, int targetVersion)
|
|
{
|
|
RpcMessageHelpers.Serialize(ref writer, ref Metadata, ref WriteBuffer);
|
|
}
|
|
|
|
public unsafe bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion)
|
|
{
|
|
return RpcMessageHelpers.Deserialize(ref reader, ref context, ref Metadata, ref ReadBuffer);
|
|
}
|
|
|
|
public void Handle(ref NetworkContext context)
|
|
{
|
|
var rpcParams = new __RpcParams
|
|
{
|
|
Server = new ServerRpcParams
|
|
{
|
|
Receive = new ServerRpcReceiveParams
|
|
{
|
|
SenderClientId = context.SenderId
|
|
}
|
|
}
|
|
};
|
|
RpcMessageHelpers.Handle(ref context, ref Metadata, ref ReadBuffer, ref rpcParams);
|
|
}
|
|
}
|
|
|
|
internal struct ClientRpcMessage : INetworkMessage
|
|
{
|
|
public int Version => 0;
|
|
|
|
public RpcMetadata Metadata;
|
|
|
|
public FastBufferWriter WriteBuffer;
|
|
public FastBufferReader ReadBuffer;
|
|
|
|
public void Serialize(FastBufferWriter writer, int targetVersion)
|
|
{
|
|
RpcMessageHelpers.Serialize(ref writer, ref Metadata, ref WriteBuffer);
|
|
}
|
|
|
|
public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion)
|
|
{
|
|
return RpcMessageHelpers.Deserialize(ref reader, ref context, ref Metadata, ref ReadBuffer);
|
|
}
|
|
|
|
public void Handle(ref NetworkContext context)
|
|
{
|
|
var rpcParams = new __RpcParams
|
|
{
|
|
Client = new ClientRpcParams
|
|
{
|
|
Receive = new ClientRpcReceiveParams
|
|
{
|
|
}
|
|
}
|
|
};
|
|
RpcMessageHelpers.Handle(ref context, ref Metadata, ref ReadBuffer, ref rpcParams);
|
|
}
|
|
}
|
|
|
|
internal struct RpcMessage : INetworkMessage
|
|
{
|
|
public int Version => 0;
|
|
|
|
public RpcMetadata Metadata;
|
|
public ulong SenderClientId;
|
|
|
|
public FastBufferWriter WriteBuffer;
|
|
public FastBufferReader ReadBuffer;
|
|
|
|
public unsafe void Serialize(FastBufferWriter writer, int targetVersion)
|
|
{
|
|
BytePacker.WriteValuePacked(writer, SenderClientId);
|
|
RpcMessageHelpers.Serialize(ref writer, ref Metadata, ref WriteBuffer);
|
|
}
|
|
|
|
public unsafe bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion)
|
|
{
|
|
ByteUnpacker.ReadValuePacked(reader, out SenderClientId);
|
|
return RpcMessageHelpers.Deserialize(ref reader, ref context, ref Metadata, ref ReadBuffer);
|
|
}
|
|
|
|
public void Handle(ref NetworkContext context)
|
|
{
|
|
var rpcParams = new __RpcParams
|
|
{
|
|
Ext = new RpcParams
|
|
{
|
|
Receive = new RpcReceiveParams
|
|
{
|
|
SenderClientId = SenderClientId
|
|
}
|
|
}
|
|
};
|
|
RpcMessageHelpers.Handle(ref context, ref Metadata, ref ReadBuffer, ref rpcParams);
|
|
}
|
|
}
|
|
}
|