@@ -25,54 +25,63 @@ private static bool SendAllRpcPrefix(NetworkBehaviour __instance, string functio
2525 return false ;
2626 }
2727
28- if ( __instance . netIdentity . observers == null || __instance . netIdentity . observers . Count == 0 )
29- return false ;
30-
3128 var rpcMessage = new RpcMessage
3229 {
3330 netId = __instance . netId ,
3431 componentIndex = __instance . ComponentIndex ,
35-
3632 functionHash = ( ushort ) functionHashCode ,
3733 payload = writer . ToArraySegment ( )
3834 } ;
3935
36+ if ( __instance . netIdentity . observers == null || __instance . netIdentity . observers . Count == 0 )
37+ return false ;
38+
4039 var players = ListPool < ExPlayer > . Shared . Rent ( ) ;
40+ var connections = ListPool < NetworkConnection > . Shared . Rent ( ) ;
4141
42- for ( var i = 0 ; i < ExPlayer . Players . Count ; i ++ )
42+ foreach ( var pair in __instance . netIdentity . observers )
4343 {
44- var player = ExPlayer . Players [ i ] ;
45-
46- if ( player ? . ReferenceHub == null || ! player . Connection . isReady )
47- continue ;
48-
49- if ( __instance . netIdentity . connectionToClient != null && __instance . netIdentity . connectionToClient == player . Connection && ! includeOwner )
50- continue ;
51-
52- if ( ! __instance . netIdentity . observers . ContainsKey ( player . ConnectionId ) )
53- continue ;
44+ if ( ExPlayer . TryGet ( pair . Value , out var player ) )
45+ {
46+ players . Add ( player ) ;
47+ }
48+ else
49+ {
50+ if ( __instance . connectionToClient == pair . Value && ! includeOwner )
51+ continue ;
5452
55- players . Add ( player ) ;
53+ connections . Add ( pair . Value ) ;
54+ }
5655 }
5756
58- if ( players . Count > 0 )
57+ if ( MirrorEvents . OnSendingRpc ( __instance , functionFullName , functionHashCode , writer , connections , players , ref rpcMessage ) )
5958 {
60- if ( MirrorEvents . OnSendingRpc ( __instance , functionFullName , functionHashCode , writer , players , ref rpcMessage )
61- && players . Count > 0 )
62- {
63- using var writer2 = NetworkWriterPool . Get ( ) ;
59+ using var writer2 = NetworkWriterPool . Get ( ) ;
6460
65- writer2 . Write ( rpcMessage ) ;
61+ writer2 . Write ( rpcMessage ) ;
6662
67- var segment = writer2 . ToArraySegment ( ) ;
63+ players . ForEach ( ply =>
64+ {
65+ if ( ply . Connection . isReady )
66+ {
67+ ply . Connection . Send ( rpcMessage , channelId ) ;
68+ }
69+ } ) ;
6870
69- players . ForEach ( ply => ply . ConnectionToClient . Send ( segment , channelId ) ) ;
71+ connections . ForEach ( conn =>
72+ {
73+ if ( conn . isReady )
74+ {
75+ conn . Send ( rpcMessage , channelId ) ;
76+ }
77+ } ) ;
7078
71- MirrorEvents . OnSentRpc ( __instance , functionFullName , functionHashCode , writer , players , ref rpcMessage ) ;
72- }
79+ MirrorEvents . OnSentRpc ( __instance , functionFullName , functionHashCode , writer , players , ref rpcMessage ) ;
7380 }
7481
7582 ListPool < ExPlayer > . Shared . Return ( players ) ;
83+ ListPool < NetworkConnection > . Shared . Return ( connections ) ;
84+
7685 return false ;
7786 }
7887
@@ -103,24 +112,21 @@ private static bool SendTargetRpcPrefix(NetworkBehaviour __instance, NetworkConn
103112 } ;
104113
105114 var players = ListPool < ExPlayer > . Shared . Rent ( ) ;
115+ var connections = ListPool < NetworkConnection > . Shared . Rent ( ) ;
106116
107117 players . Add ( player ) ;
118+ connections . Add ( conn ) ;
108119
109- if ( MirrorEvents . OnSendingRpc ( __instance , functionFullName , functionHashCode , writer , players , ref rpcMessage )
110- && players . Count > 0 )
120+ if ( MirrorEvents . OnSendingRpc ( __instance , functionFullName , functionHashCode , writer , connections , players , ref rpcMessage ) )
111121 {
112- using var writer2 = NetworkWriterPool . Get ( ) ;
113-
114- writer2 . Write ( rpcMessage ) ;
115-
116- var segment = writer2 . ToArraySegment ( ) ;
117-
118- player . ConnectionToClient . Send ( segment , channelId ) ;
122+ conn . Send ( rpcMessage , channelId ) ;
119123
120124 MirrorEvents . OnSentRpc ( __instance , functionFullName , functionHashCode , writer , players , ref rpcMessage ) ;
121125 }
122126
123127 ListPool < ExPlayer > . Shared . Return ( players ) ;
128+ ListPool < NetworkConnection > . Shared . Return ( connections ) ;
129+
124130 return false ;
125131 }
126132 }
0 commit comments