11using System ;
22using System . Collections . Generic ;
3+ using System . Runtime . CompilerServices ;
34using Unity . Collections ;
45using UnityEngine ;
56
@@ -40,7 +41,7 @@ public abstract class NetworkBehaviour : MonoBehaviour
4041 internal static readonly Dictionary < Type , Dictionary < uint , RpcReceiveHandler > > __rpc_func_table = new Dictionary < Type , Dictionary < uint , RpcReceiveHandler > > ( ) ;
4142 internal static readonly Dictionary < Type , Dictionary < uint , RpcInvokePermission > > __rpc_permission_table = new Dictionary < Type , Dictionary < uint , RpcInvokePermission > > ( ) ;
4243
43- #if DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
44+ #if MULTIPLAYER_TOOLS && ( DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE )
4445 // RuntimeAccessModifiersILPP will make this `public`
4546 internal static readonly Dictionary < Type , Dictionary < uint , string > > __rpc_name_table = new Dictionary < Type , Dictionary < uint , string > > ( ) ;
4647#endif
@@ -142,16 +143,9 @@ internal void __endSendServerRpc(ref FastBufferWriter bufferWriter, uint rpcMeth
142143 }
143144
144145 bufferWriter . Dispose ( ) ;
145- #if DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
146- if ( __rpc_name_table [ GetType ( ) ] . TryGetValue ( rpcMethodId , out var rpcMethodName ) )
147- {
148- networkManager . NetworkMetrics . TrackRpcSent (
149- NetworkManager . ServerClientId ,
150- m_NetworkObject ,
151- rpcMethodName ,
152- __getTypeName ( ) ,
153- rpcWriteSize ) ;
154- }
146+
147+ #if MULTIPLAYER_TOOLS && ( DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE )
148+ TrackRpcMetricsSend ( ref serverRpcMessage , rpcMethodId , rpcWriteSize ) ;
155149#endif
156150 }
157151
@@ -275,7 +269,11 @@ internal void __endSendClientRpc(ref FastBufferWriter bufferWriter, uint rpcMeth
275269 }
276270
277271 bufferWriter . Dispose ( ) ;
278- #if DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
272+ #if MULTIPLAYER_TOOLS && ( DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE )
273+ if ( ! ValidateRpcMessageMetrics ( GetType ( ) ) )
274+ {
275+ return ;
276+ }
279277 if ( __rpc_name_table [ GetType ( ) ] . TryGetValue ( rpcMethodId , out var rpcMethodName ) )
280278 {
281279 if ( clientRpcParams . Send . TargetClientIds != null )
@@ -755,6 +753,11 @@ public virtual void OnNetworkPreDespawn() { }
755753
756754 internal virtual void InternalOnNetworkPreSpawn ( ref NetworkManager networkManager ) { }
757755
756+ /// <summary>
757+ /// Handles pre-spawn related initializations.
758+ /// Invokes any <see cref="InternalOnNetworkPreSpawn"/> subscriptions.
759+ /// Finally invokes <see cref="OnNetworkPreSpawn(ref NetworkManager)"/>.
760+ /// </summary>
758761 internal void NetworkPreSpawn ( ref NetworkManager networkManager , NetworkObject networkObject )
759762 {
760763 m_NetworkObject = networkObject ;
@@ -782,13 +785,28 @@ internal void NetworkPreSpawn(ref NetworkManager networkManager, NetworkObject n
782785 }
783786 }
784787
788+ /// <summary>
789+ /// Initializes the:
790+ /// - <see cref="IsSpawned"/> state.
791+ /// - <see cref="NetworkVariableBase"/> instances.
792+ /// - Spawned related properties are applied.
793+ /// !! Note !!:
794+ /// This also populates RPC related tables based on this <see cref="NetworkBehaviour"/>'s RPCs (if any).
795+ /// </summary>
785796 internal void InternalOnNetworkSpawn ( )
786797 {
787798 IsSpawned = true ;
788- // Initialize the NetworkVariables so they are accessible in OnNetworkSpawn;
799+ // Initialize the NetworkVariables and **RPC tables** so they are accessible in OnNetworkSpawn
789800 InitializeVariables ( ) ;
801+ // Apply the spawned state/properties to this instance
790802 UpdateNetworkProperties ( ) ;
803+ }
791804
805+ /// <summary>
806+ /// Handles invoking <see cref="OnNetworkSpawn"/>.
807+ /// </summary>
808+ internal void NetworkSpawn ( )
809+ {
792810 try
793811 {
794812 OnNetworkSpawn ( ) ;
@@ -799,6 +817,9 @@ internal void InternalOnNetworkSpawn()
799817 }
800818 }
801819
820+ /// <summary>
821+ /// Handles invoking <see cref="OnNetworkPostSpawn"/>.
822+ /// </summary>
802823 internal void NetworkPostSpawn ( )
803824 {
804825 try
@@ -819,6 +840,9 @@ internal void NetworkPostSpawn()
819840 }
820841 }
821842
843+ /// <summary>
844+ /// Handles invoking <see cref="OnNetworkSessionSynchronized"/>.
845+ /// </summary>
822846 internal void NetworkSessionSynchronized ( )
823847 {
824848 try
@@ -832,6 +856,9 @@ internal void NetworkSessionSynchronized()
832856 }
833857 }
834858
859+ /// <summary>
860+ /// Handles invoking <see cref="OnInSceneObjectsSpawned"/>.
861+ /// </summary>
835862 internal void InSceneNetworkObjectsSpawned ( )
836863 {
837864 try
@@ -844,6 +871,9 @@ internal void InSceneNetworkObjectsSpawned()
844871 }
845872 }
846873
874+ /// <summary>
875+ /// Handles invoking <see cref="OnNetworkPreDespawn"/>.
876+ /// </summary>
847877 internal void InternalOnNetworkPreDespawn ( )
848878 {
849879 try
@@ -863,6 +893,9 @@ internal void InternalOnNetworkPreDespawn()
863893 }
864894 }
865895
896+ /// <summary>
897+ /// Handles invoking <see cref="OnNetworkDespawn"/>.
898+ /// </summary>
866899 internal void InternalOnNetworkDespawn ( )
867900 {
868901 IsSpawned = false ;
@@ -965,13 +998,90 @@ internal virtual void __initializeRpcs()
965998 internal void __registerRpc ( uint hash , RpcReceiveHandler handler , string rpcMethodName , RpcInvokePermission permission )
966999#pragma warning restore IDE1006 // restore naming rule violation check
9671000 {
968- __rpc_func_table [ GetType ( ) ] [ hash ] = handler ;
969- __rpc_permission_table [ GetType ( ) ] [ hash ] = permission ;
970- #if DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
971- __rpc_name_table [ GetType ( ) ] [ hash ] = rpcMethodName ;
1001+ var rpcType = GetType ( ) ;
1002+ __rpc_func_table [ rpcType ] [ hash ] = handler ;
1003+ __rpc_permission_table [ rpcType ] [ hash ] = permission ;
1004+ #if MULTIPLAYER_TOOLS && ( DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE )
1005+ __rpc_name_table [ rpcType ] [ hash ] = rpcMethodName ;
9721006#endif
9731007 }
9741008
1009+ #if MULTIPLAYER_TOOLS && ( DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE )
1010+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
1011+ private bool ValidateRpcMessageMetrics ( Type type )
1012+ {
1013+ if ( m_NetworkManager == null )
1014+ {
1015+ Debug . LogError ( $ "[{ nameof ( ValidateRpcMessageMetrics ) } ][{ type . Name } ] { nameof ( NetworkBehaviour ) } is attempting to invoking an RPC before { nameof ( NetworkManager ) } has been initialized!") ;
1016+ return false ;
1017+ }
1018+
1019+ if ( ! __rpc_name_table . ContainsKey ( type ) )
1020+ {
1021+ Debug . LogError ( $ "[{ nameof ( ValidateRpcMessageMetrics ) } ][{ type . Name } ][{ nameof ( __rpc_name_table ) } ] RPC table initialization failure: Table does not contain an entry for { type . Name } !") ;
1022+ return false ;
1023+ }
1024+ return true ;
1025+ }
1026+
1027+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
1028+ internal void TrackRpcMetricsSend ( ref ServerRpcMessage message , uint rpcMethodId , int rpcWriteSize )
1029+ {
1030+ var type = GetType ( ) ;
1031+ if ( ! ValidateRpcMessageMetrics ( type ) )
1032+ {
1033+ return ;
1034+ }
1035+ if ( __rpc_name_table [ type ] . TryGetValue ( rpcMethodId , out var rpcMethodName ) )
1036+ {
1037+ m_NetworkManager . NetworkMetrics . TrackRpcSent (
1038+ NetworkManager . ServerClientId ,
1039+ m_NetworkObject ,
1040+ rpcMethodName ,
1041+ __getTypeName ( ) ,
1042+ rpcWriteSize ) ;
1043+ }
1044+ }
1045+
1046+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
1047+ internal void TrackRpcMetricsSend ( ulong clientId , ref RpcMessage message , int length )
1048+ {
1049+ var type = GetType ( ) ;
1050+ if ( ! ValidateRpcMessageMetrics ( type ) )
1051+ {
1052+ return ;
1053+ }
1054+ if ( __rpc_name_table [ type ] . TryGetValue ( message . Metadata . NetworkRpcMethodId , out var rpcMethodName ) )
1055+ {
1056+ m_NetworkManager . NetworkMetrics . TrackRpcSent (
1057+ m_NetworkManager . LocalClientId ,
1058+ NetworkObject ,
1059+ rpcMethodName ,
1060+ __getTypeName ( ) ,
1061+ length ) ;
1062+ }
1063+ }
1064+
1065+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
1066+ internal void TrackRpcMetricsReceive ( ref RpcMetadata metadata , ref NetworkContext context , int length )
1067+ {
1068+ var type = GetType ( ) ;
1069+ if ( ! ValidateRpcMessageMetrics ( type ) )
1070+ {
1071+ return ;
1072+ }
1073+ if ( __rpc_name_table [ type ] . TryGetValue ( metadata . NetworkRpcMethodId , out var rpcMethodName ) )
1074+ {
1075+ m_NetworkManager . NetworkMetrics . TrackRpcReceived (
1076+ context . SenderId ,
1077+ NetworkObject ,
1078+ rpcMethodName ,
1079+ __getTypeName ( ) ,
1080+ length ) ;
1081+ }
1082+ }
1083+ #endif
1084+
9751085#pragma warning disable IDE1006 // disable naming rule violation check
9761086 // RuntimeAccessModifiersILPP will make this `protected`
9771087 // Using this method here because ILPP doesn't seem to let us do visibility modification on properties.
@@ -1014,7 +1124,7 @@ internal void InitializeVariables()
10141124 {
10151125 __rpc_func_table [ GetType ( ) ] = new Dictionary < uint , RpcReceiveHandler > ( ) ;
10161126 __rpc_permission_table [ GetType ( ) ] = new Dictionary < uint , RpcInvokePermission > ( ) ;
1017- #if UNITY_EDITOR || DEVELOPMENT_BUILD || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
1127+ #if MULTIPLAYER_TOOLS && ( DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE )
10181128 __rpc_name_table [ GetType ( ) ] = new Dictionary < uint , string > ( ) ;
10191129#endif
10201130 __initializeRpcs ( ) ;
0 commit comments