public void SubscribeToEvent <T>(AObject sender, NativeEventDelegate <T> eventDelegate) where T : NativeEventData { uint eventType = NativeEvents.GetEventID <T>(); if (eventType == 0) { throw new InvalidOperationException("AObject.SubscribeToEvent<T>(EventDelegate<T> eventDelegate) - Unknown native event id"); } // Move this NETCore.RegisterNETEventType(eventType); NativeEventHandlers[eventType] = (eventData) => { eventDelegate((T)eventData); }; if (sender != null) { NativeCore.SubscribeToEvent(this, sender, eventType); } else { NativeCore.SubscribeToEvent(this, eventType); } }
public static void EventDispatch(IntPtr sender, uint eventType, IntPtr eventData) { List <EventSubscription> eventReceivers; if (!eventReceiverLookup.TryGetValue(eventType, out eventReceivers)) { // This should not happen, as event NET objects are subscribed to are filtered throw new InvalidOperationException("NativeCore.EventDispatch - received unregistered event type"); } // iterate over copy of list so we can modify it while running ScriptVariantMap scriptMap = null; NativeEventData nativeEventData = null; AObject receiver; foreach (EventSubscription er in eventReceivers.ToList()) { // GC'd? if (!er.Receiver.TryGetTarget(out receiver)) { continue; } if (er.Sender != IntPtr.Zero && er.Sender != sender) { continue; } if (scriptMap == null) { if (svmDepth == svmMax) { throw new InvalidOperationException("NativeCore.EventDispatch - exceeded max svm"); } scriptMap = svm[svmDepth++]; scriptMap.CopyVariantMap(eventData); nativeEventData = NativeEvents.GetNativeEventData(eventType, scriptMap); nativeEventData.sourceEventData = eventData; } receiver.HandleEvent(eventType, scriptMap, nativeEventData); } if (scriptMap != null) { svmDepth--; if (nativeEventData != null) { NativeEvents.ReleaseNativeEventData(nativeEventData); } } }
// Native events public void UnsubscribeFromEvent <T>() where T : NativeEventData { uint eventType = NativeEvents.GetEventID <T>(); UnsubscribeFromEvent(eventType); }