2using System.Collections.Generic;
8 public enum RemoteCallType { Command, ClientRpc }
19 public Type componentType;
20 public RemoteCallType callType;
21 public RemoteCallDelegate
function;
22 public bool cmdRequiresAuthority;
24 public bool AreEqual(Type componentType, RemoteCallType remoteCallType, RemoteCallDelegate invokeFunction) =>
25 this.componentType == componentType &&
26 this.callType == remoteCallType &&
27 this.function == invokeFunction;
45 static readonly Dictionary<ushort, Invoker> remoteCallDelegates =
new Dictionary<ushort, Invoker>();
47 static bool CheckIfDelegateExists(Type componentType, RemoteCallType remoteCallType, RemoteCallDelegate func, ushort functionHash)
49 if (remoteCallDelegates.ContainsKey(functionHash))
53 Invoker oldInvoker = remoteCallDelegates[functionHash];
54 if (oldInvoker.AreEqual(componentType, remoteCallType, func))
61 Debug.LogError($
"Function {oldInvoker.componentType}.{oldInvoker.function.GetMethodName()} and {componentType}.{func.GetMethodName()} have the same hash. Please rename one of them");
68 internal static ushort RegisterDelegate(Type componentType,
string functionFullName, RemoteCallType remoteCallType, RemoteCallDelegate func,
bool cmdRequiresAuthority =
true)
71 ushort hash = (ushort)(functionFullName.GetStableHashCode() & 0xFFFF);
73 if (CheckIfDelegateExists(componentType, remoteCallType, func, hash))
76 remoteCallDelegates[hash] =
new Invoker
78 callType = remoteCallType,
79 componentType = componentType,
81 cmdRequiresAuthority = cmdRequiresAuthority
89 public static void RegisterCommand(Type componentType,
string functionFullName, RemoteCallDelegate func,
bool requiresAuthority) =>
90 RegisterDelegate(componentType, functionFullName, RemoteCallType.Command, func, requiresAuthority);
95 public static void RegisterRpc(Type componentType,
string functionFullName, RemoteCallDelegate func) =>
96 RegisterDelegate(componentType, functionFullName, RemoteCallType.ClientRpc, func);
99 internal static void RemoveDelegate(ushort hash) =>
100 remoteCallDelegates.Remove(hash);
105 static bool GetInvokerForHash(ushort functionHash, RemoteCallType remoteCallType, out
Invoker invoker) =>
106 remoteCallDelegates.TryGetValue(functionHash, out invoker) &&
108 invoker.callType == remoteCallType;
116 if (GetInvokerForHash(functionHash, remoteCallType, out
Invoker invoker) &&
117 invoker.componentType.IsInstanceOfType(component))
120 invoker.function(component, reader, senderConnection);
127 internal static bool CommandRequiresAuthority(ushort cmdHash) =>
128 GetInvokerForHash(cmdHash, RemoteCallType.Command, out
Invoker invoker) &&
129 invoker.cmdRequiresAuthority;
132 public static RemoteCallDelegate
GetDelegate(ushort functionHash) =>
133 remoteCallDelegates.TryGetValue(functionHash, out
Invoker invoker)
Base class for networked components.
Network Reader for most simple types like floats, ints, buffers, structs, etc. Use NetworkReaderPool....
Used to help manage remote calls for NetworkBehaviours
static RemoteCallDelegate GetDelegate(ushort functionHash)
Gets the handler function by hash. Useful for profilers and debuggers.