예제 #1
0
        static internal void UnsubscribeFromEvent(AObject receiver, RefCounted sender, uint eventType)
        {
            List <EventSubscription> eventReceivers;

            if (!eventReceiverLookup.TryGetValue(eventType, out eventReceivers))
            {
                return;
            }

            AObject obj;

            foreach (EventSubscription er in eventReceivers)
            {
                if (!er.Receiver.TryGetTarget(out obj))
                {
                    continue; // GC'd
                }
                if (obj == receiver &&
                    (sender == null || er.Sender == sender.nativeInstance))
                {
                    eventReceivers.Remove(er);
                    return;
                }
            }
        }
예제 #2
0
        static internal void SubscribeToEvent(AObject receiver, AObject sender, uint eventType)
        {
            List <EventSubscription> eventReceivers;

            if (!eventReceiverLookup.TryGetValue(eventType, out eventReceivers))
            {
                eventReceivers = eventReceiverLookup[eventType] = new List <EventSubscription>();
            }

            AObject obj;

            foreach (EventSubscription er in eventReceivers)
            {
                if (!er.Receiver.TryGetTarget(out obj))
                {
                    continue; // GC'd
                }
                // already on list?
                if (obj == receiver)
                {
                    return;
                }
            }

            WeakReference <RefCounted> w = null;

            if (!nativeLookup.TryGetValue(receiver.nativeInstance, out w))
            {
                throw new InvalidOperationException("NativeCore.SubscribeToEvent - unable to find native receiver instance");
            }

            eventReceivers.Add(new EventSubscription(receiver, sender));
        }
예제 #3
0
        public void UnsubscribeFromEvent(AObject sender, uint eventType)
        {
            NativeCore.UnsubscribeFromEvent(this, sender, eventType);
            var key = new SenderEventKey(eventType, sender.nativeInstance);

            SenderEventHandlers.Remove(key);
        }
예제 #4
0
        public static T GetSubsystem <T>() where T : AObject
        {
            AObject subSystem = null;

            subSystems.TryGetValue(typeof(T), out subSystem);
            return((T)subSystem);
        }
예제 #5
0
        public static T GetSubsystem <T>() where T : AObject
        {
            AObject subsystem = null;
            var     type      = typeof(T);

            // See if already registered (or a managed subsystem which will only be in the dictionary)
            if (subSystems.TryGetValue(type, out subsystem))
            {
                return((T)subsystem);
            }

            // If we're a managed type, throw error
            if (!NativeCore.IsNativeType(type))
            {
                throw new System.InvalidOperationException($"AtomicNET.GetSubsystem<T> - Attempting to get null subsystem: {type.Name}");
            }

            // Look up possible native subsystem
            subsystem = AtomicNET.Context.GetSubsystem(type.Name);

            // If we didn't find one, this is an error
            if (subsystem == null)
            {
                throw new System.InvalidOperationException($"AtomicNET.GetSubsystem<T> - Attempting to get null subsystem: {type.Name}");
            }

            // register the subsystem
            RegisterSubsystem(subsystem);

            return((T)subsystem);
        }
예제 #6
0
        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);
            }
        }
예제 #7
0
        static internal void SubscribeToEvent(AObject receiver, AObject sender, uint eventType)
        {
            List <EventSubscription> eventReceivers;

            if (!eventReceiverLookup.TryGetValue(eventType, out eventReceivers))
            {
                eventReceivers = eventReceiverLookup[eventType] = new List <EventSubscription>();
            }

            AObject obj;

            foreach (EventSubscription er in eventReceivers)
            {
                if (!er.Receiver.TryGetTarget(out obj))
                {
                    continue; // GC'd
                }
                // already on list?
                if (obj == receiver)
                {
                    return;
                }
            }

            eventReceivers.Add(new EventSubscription(receiver, sender));
        }
예제 #8
0
        public void SubscribeToEvent(AObject sender, uint eventType, EventDelegate eventDelegate)
        {
            if (sender == null)
            {
                throw new InvalidOperationException("AObject.SubscribeToEvent - trying to subscribe to events from a null object");
            }

            // Move this
            NETCore.RegisterNETEventType(eventType);
            NativeCore.SubscribeToEvent(this, sender, eventType);
        }
예제 #9
0
        public static void RegisterSubsystem(AObject instance)
        {
            var type = instance.GetType();

            if (subSystems.ContainsKey(type))
            {
                Log.Error($"AtomicNET.RegisterSubsystem - Attempting to reregister subsystem: {type.Name}");
                return;
            }
            subSystems[instance.GetType()] = instance;
        }
예제 #10
0
        static internal void UnsubscribeFromAllEvents(AObject receiver)
        {
            // TODO: Optimize

            AObject obj;

            foreach (var subList in eventReceiverLookup.Values)
            {
                subList.RemoveAll(item => item.Receiver.TryGetTarget(out obj) && obj == receiver);
            }
        }
예제 #11
0
        public void SubscribeToEvent(AObject sender, uint eventType, SenderEventDelegate eventDelegate)
        {
            if (sender == null)
            {
                throw new InvalidOperationException("AObject.SubscribeToEvent - trying to subscribe to events from a null object");
            }

            NETCore.RegisterNETEventType(eventType);
            var key = new SenderEventKey(eventType, sender.nativeInstance);

            SenderEventHandlers[key] = eventDelegate;
            NativeCore.SubscribeToEvent(this, sender, eventType);
        }
예제 #12
0
        public static void RegisterSubsystem(String name, AObject instance = null)
        {
            if (instance != null)
            {
                subSystems[instance.GetType()] = instance;
                return;
            }

            var subsystem = AtomicNET.Context.GetSubsystem(name);

            if (subsystem == null)
            {
                throw new System.InvalidOperationException("AtomicNET.RegisterSubsystem - Attempting to register null subsystem");
            }

            subSystems[subsystem.GetType()] = subsystem;
        }
예제 #13
0
        static internal void SubscribeToEvent(AObject receiver, uint eventType)
        {
            List <WeakReference <AObject> > eventReceivers;

            if (!eventReceiverLookup.TryGetValue(eventType, out eventReceivers))
            {
                eventReceivers = eventReceiverLookup[eventType] = new List <WeakReference <AObject> >();
            }

            AObject obj;

            foreach (WeakReference <AObject> wr in eventReceivers)
            {
                if (!wr.TryGetTarget(out obj))
                {
                    continue; // GC'd
                }
                // already on list?
                if (obj == receiver)
                {
                    return;
                }
            }

            WeakReference <RefCounted> w = null;

            if (!nativeLookup.TryGetValue(receiver.nativeInstance, out w))
            {
                throw new System.InvalidOperationException("NativeCore.SubscribeToEvent - unable to find native instance");
            }

            RefCounted refcounted;

            if (!w.TryGetTarget(out refcounted))
            {
                throw new System.InvalidOperationException("NativeCore.SubscribeToEvent - attempting to subscribe a GC'd AObject");
            }

            eventReceivers.Add(new WeakReference <AObject>(receiver));
        }
예제 #14
0
        public static void SubscribeToEvent(AObject subscriber, AObject sender, string eventType, AtomicEventDelegate function)
        {
            var eventTypeID = Atomic.StringToStringHash(eventType);

            Dictionary <uint, Subscription> subs;

            if (!subscriptions.TryGetValue(subscriber.RefID, out subs))
            {
                subs = new Dictionary <uint, Subscription> ();
                subscriptions [subscriber.RefID] = subs;
            }

            Subscription sub;

            if (!subs.TryGetValue(eventTypeID, out sub))
            {
                sub = new Subscription();
                subs [eventTypeID] = sub;
            }

            sub.SenderRefID = sender == null ? 0 : sender.RefID;
            sub.Handler     = function;

            List <uint> subscribers;

            if (!eventSubscribers.TryGetValue(eventTypeID, out subscribers))
            {
                subscribers = new List <uint> ();
                eventSubscribers [eventTypeID] = subscribers;
            }

            if (!subscribers.Contains(subscriber.RefID))
            {
                subscribers.Add(subscriber.RefID);
            }
        }
예제 #15
0
 static internal void SubscribeToEvent(AObject receiver, uint eventType)
 {
     SubscribeToEvent(receiver, null, eventType);
 }
예제 #16
0
        internal void HandleEvent(AObject sender, uint eventType, ScriptVariantMap eventData)
        {
            var key = new SenderEventKey(eventType, sender.nativeInstance);

            senderEventHandlers[key](sender, eventType, eventData);
        }
예제 #17
0
        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");
            }

            AObject managedSender = null;
            WeakReference <RefCounted> wr;

            if (sender != IntPtr.Zero &&
                nativeLookup.TryGetValue(sender, out wr))
            {
                RefCounted refCounted;
                if (wr.TryGetTarget(out refCounted))
                {
                    managedSender = refCounted as AObject;
                }
            }

            // iterate over copy of list so we can modify it while running
            ScriptVariantMap scriptMap = null;
            AObject          receiver;

            foreach (EventSubscription er in eventReceivers.ToList())
            {
                // GC'd?
                if (!er.Receiver.TryGetTarget(out receiver))
                {
                    continue;
                }

                if (scriptMap == null)
                {
                    if (svmDepth == svmMax)
                    {
                        throw new InvalidOperationException("NativeCore.EventDispatch - exceeded max svm");
                    }

                    scriptMap = svm[svmDepth++];
                    scriptMap.CopyVariantMap(eventData);
                }

                if (managedSender != null && er.Sender == sender)
                {
                    receiver.HandleEvent(managedSender, eventType, scriptMap);
                }
                else if (er.Sender == IntPtr.Zero)
                {
                    receiver.HandleEvent(eventType, scriptMap);
                }
            }

            if (scriptMap != null)
            {
                svmDepth--;
            }
        }
예제 #18
0
 public void SubscribeToEvent(AObject sender, string eventType, AtomicEventDelegate function)
 {
     EventCore.SubscribeToEvent(this, sender, eventType, function);
 }
예제 #19
0
 public EventSubscription(AObject obj, IntPtr source)
 {
     Receiver = new WeakReference <AObject>(obj);
     Sender   = source;
 }
예제 #20
0
 static internal void UnsubscribeFromEvent(AObject receiver, uint eventType)
 {
     UnsubscribeFromEvent(receiver, null, eventType);
 }
예제 #21
0
 public static void SendEvent(AObject sender, string eventType)
 {
     csb_Atomic_AObject_SendEvent(sender.nativeInstance, eventType);
 }
예제 #22
0
 public EventSubscription(AObject receiver, IntPtr sender)
 {
     Receiver = new WeakReference <AObject>(receiver);
     Sender   = sender;
 }
예제 #23
0
 public void SubscribeToEvent(AObject sender, string eventType, EventDelegate eventDelegate)
 {
     SubscribeToEvent(sender, AtomicNET.StringToStringHash(eventType), eventDelegate);
 }