Esempio n. 1
0
        private bool _DetachListenerAsync(int inEventKey, IGEventListener inListener)
        {
            bool ret = false;

            if (inListener == null)
            {
                Log.LMN.LogUtil.Error("MBLogicNode._DetachListenerAsync: inLogicNode is null");
                return(ret);
            }

            //置脏,导致这棵子树需要进入更新流程//
            IsListenerTreeNeedsUpdate = true;
            IGLogicNode parent = ParentNode;

            while (parent != null && !parent.IsListenerTreeNeedsUpdate)
            {
                parent.IsListenerTreeNeedsUpdate = true;
                parent = parent.ParentNode;
            }

            List <GEventListenerWrapper> listenerWrappers = null;

            if (!mEventListenerMap.TryGetValue(inEventKey, out listenerWrappers))
            {
                Log.LMN.LogUtil.Error("MBLogicNode._DetachListenerAsync: no event listener for -> {0} on {1}", inEventKey, inListener);
                return(ret);
            }

            //快速找出需要移除的节点位置//
            int priority = inListener.GetPriority(inEventKey);
            int index    = PrioritySortUtil.GetDecSeqRefArrayFirstIndex <GEventListenerWrapper>(priority, listenerWrappers);

            for (int i = index; i < listenerWrappers.Count && listenerWrappers[i].PriorityVal == priority; i++)
            {
                GEventListenerWrapper wrapper = listenerWrappers[i];
                object listenerObj            = wrapper.mEventListenerWeakRef.Target;
                if (listenerObj == inListener)
                {
                    inListener.ListenerStatus = AttachableStatus.Detaching;
                    ret = true;
                    break;
                }
            }

            if (ret)
            {
                EventListenerUpdateOperation updateOp = new EventListenerUpdateOperation();
                updateOp.mListener      = inListener;
                updateOp.mIsAddOrRemove = false;
                updateOp.mEventKey      = inEventKey;
                mAsyncListenerOps.Add(updateOp);
            }

            return(ret);
        }
Esempio n. 2
0
        private bool _DetachListenerSync(int inEventKey, IGEventListener inListener)
        {
            bool ret = false;

            if (inListener == null)
            {
                //请替换成你的log函数//
                Log.LMN.LogUtil.Error("ObjLogicNode._DetachListenerSync: inLogicNode is null");
                return(ret);
            }

            List <GEventListenerWrapper> listenerWrappers = null;

            if (!mEventListenerMap.TryGetValue(inEventKey, out listenerWrappers))
            {
                //请替换成你的log函数//
                Log.LMN.LogUtil.Error("ObjLogicNode._DetachListenerSync: no event listener for -> {0} on {1}", inEventKey, inListener);
                return(ret);
            }

            //快速找出需要移除的节点位置,注意这里其实兼顾了同一优先级节点的GC功能//
            int priority = inListener.GetPriority(inEventKey);
            int index    = PrioritySortUtil.GetDecSeqRefArrayFirstIndex <GEventListenerWrapper>(priority, listenerWrappers);
            List <GEventListenerWrapper> listenersToRemove = new List <GEventListenerWrapper>();

            for (int i = index; i < listenerWrappers.Count && listenerWrappers[i].PriorityVal == priority; i++)
            {
                GEventListenerWrapper wrapper = listenerWrappers[i];
                object listenerObj            = wrapper.mEventListenerWeakRef.Target;
                if (listenerObj == null || listenerObj == inListener)
                {
                    listenersToRemove.Add(wrapper);
                }
            }

            for (int i = 0; i < listenersToRemove.Count; i++)
            {
                listenerWrappers.Remove(listenersToRemove[i]);
            }

            inListener.ListenerStatus = AttachableStatus.Detached;

            ret = listenersToRemove.Count != 0;
            return(ret);
        }
Esempio n. 3
0
        private bool _AttachListenerSync(int inEventKey, IGEventListener inListener)
        {
            bool ret = false;

            if (inListener == null)
            {
                //请替换成你的log函数//
                Log.LMN.LogUtil.Error("ObjLogicNode._AttachListenerSync: inLogicNode is null");
                return(ret);
            }

            List <GEventListenerWrapper> eventListenerList = null;

            if (!mEventListenerMap.TryGetValue(inEventKey, out eventListenerList))
            {
                eventListenerList = new List <GEventListenerWrapper>();
                mEventListenerMap.Add(inEventKey, eventListenerList);
            }

#if DEBUG_G_LOGIC_NODE
            for (int i = 0; i < eventListenerList.Count; i++)
            {
                GEventListenerWrapper wrapper = eventListenerList[i];
                if (wrapper.mEventListenerWeakRef.IsAlive &&
                    wrapper.mEventListenerWeakRef.Target == inListener)
                {
                    //请替换成你的log函数//
                    Log.LMN.LogUtil.Error("ObjLogicNode._AttachListenerSync: duplicated -> " + inListener + ", in -> " + this + ", for -> " + inEventKey);
                    return(ret);
                }
            }
#endif

            int priority = inListener.GetPriority(inEventKey);
            GEventListenerWrapper listenerWrapper;
            listenerWrapper.mEventListenerWeakRef  = new WeakReference(inListener);
            listenerWrapper.mEventListenerPriority = priority;
            inListener.ListenerStatus = AttachableStatus.Attached;

            int index = PrioritySortUtil.GetDecSeqRefArrayInsertIndex <GEventListenerWrapper>(priority, eventListenerList);
            eventListenerList.Insert(index, listenerWrapper);

            ret = true;
            return(ret);
        }