/// <summary> /// Remove a callback from listening for payloads of a given type. /// </summary> /// <typeparam name="T">The payload type being listened for</typeparam> /// <param name="callback">The delegate to remove</param> public void RemoveCallback <T>(FlarePayloadCallback <T> callback) where T : INetworkPayload { if (isInvoking) { registrationQueue.Enqueue(() => Remove(callback)); } else { Remove(callback); } void Remove <P>(FlarePayloadCallback <P> c) where P : INetworkPayload { var tag = NetworkTagAttribute.GetTag(typeof(P)); // Remove the callback from the dictionary if (tag != null) { if (payloadCallbacks.TryGetValue(tag.Value, out var value)) { value.Remove(c); } else { NetworkLogger.Log($"Payload type [{typeof(P).Name}] has no callbacks to remove!", LogCategory.PayloadCallbacks, LogLevel.Warning); } } else { NetworkLogger.Log("Cannot remove callbacks for types with no NetworkTag!", LogCategory.PayloadCallbacks, LogLevel.Warning); } } }
/// <summary> /// Add a callback to listen for payloads of a given type. /// </summary> /// <typeparam name="T">The payload type to listen for</typeparam> /// <param name="callback">The delegate to call</param> public void AddCallback <T>(FlarePayloadCallback <T> callback) where T : INetworkPayload { if (isInvoking) { registrationQueue.Enqueue(() => Add(callback)); } else { Add(callback); } void Add <P>(FlarePayloadCallback <P> c) where P : INetworkPayload { var type = typeof(P); var tag = NetworkTagAttribute.GetTag(type); if (tag != null) { // Add the callback to the dictionary if (!payloadCallbacks.TryGetValue(tag.Value, out var value)) { // Create callback entry if it doesn't exist value = new Callback(type); payloadCallbacks.Add(tag.Value, value); } value.Add(c); } else { NetworkLogger.Log("Cannot add callbacks for types with no NetworkTag!", LogCategory.PayloadCallbacks, LogLevel.Message); } } }
internal void Add <T>(FlarePayloadCallback <T> callback) where T : INetworkPayload { int key = callback.GetHashCode(); if (!Values.ContainsKey(key)) { // This cast needs benchmarking Values.Add(key, x => { if (x is T p) { callback(p); } }); if (Type.IsVisible) { NetworkLogger.Log($"Added delegate [{callback.Method.Name}] for payload type [{Name}]", LogCategory.PayloadCallbacks); } } else { NetworkLogger.Log($"Delegate [{callback.Method.Name}] is already added to listen for payload type [{Name}]", LogCategory.PayloadCallbacks, LogLevel.Warning); } }
internal void Remove <T>(FlarePayloadCallback <T> callback) where T : INetworkPayload { int key = callback.GetHashCode(); if (Values.ContainsKey(key)) { Values.Remove(key); if (Type.IsVisible) { NetworkLogger.Log($"Removed delegate [{callback.Method.Name}] for payload type [{Name}]", LogCategory.PayloadCallbacks); } } else { NetworkLogger.Log($"Delegate [{callback.Method.Name}] is not listening for payload type [{Name}]", LogCategory.PayloadCallbacks, LogLevel.Warning); } }
/// <summary> /// Remove a function from being invoked when a payload of a certain type is received. /// </summary> /// <typeparam name="T">The payload type being listened for</typeparam> /// <param name="c">The method to remove</param> public void RemoveCallback <T>(FlarePayloadCallback <T> c) where T : INetworkPayload => PayloadHandler.RemoveCallback(c);