Exemplo n.º 1
0
 public async Task ExecuteAsync(Update message)
 {
     if (message.Type == UpdateType.Message)
     {
         await _messageHandler.ExecuteMessage(message);
     }
     if (message.Type == UpdateType.CallbackQuery)
     {
         await _callbackHandler.ExecuteCallback(message);
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// 分发Message的实现
        /// </summary>
        /// <param name="message"></param>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        /// <param name="messageParams"></param>
        private static void ExecuteMessage(string message, object sender, MessageArgs args, params object[] messageParams)
        {
            if (string.IsNullOrEmpty(message))
            {
                Debug.LogError("[MessageCenter] Argument named 'message' is null or empty.");
                return;
            }

            if (sender == null)
            {
                Debug.LogError("[MessageCenter] Argument named 'sender' is null");
                return;
            }

            // 分发给Controller
            Type type;

            if (s_ControllerDict.TryGetValue(message, out type))
            {
                MessageControllerBase controller = Activator.CreateInstance(type) as MessageControllerBase;
                if (controller != null)
                {
                    controller.ExecuteMessage(message, sender, args, messageParams);
                }
            }

            /*
             * ///// 使用已经创建好的ScriptableObject,Dictionary<string, MessageControllerBase> s_ControllerDict;
             * // MessageControllerBase controller;
             * // if (s_ControllerDict.TryGetValue(message, out controller))
             * // {
             * //     controller.ExecuteMessage(message, sender, args, messageParams);
             * // }
             */

            /*
             * ///// 使用动态创建的ScriptableObject
             * // Type assetType;
             * // if (s_ControllerDict.TryGetValue(message, out assetType))
             * // {
             * //     MessageControllerBase controller = ScriptableObject.CreateInstance(assetType) as MessageControllerBase;
             * //     controller.ExecuteMessage(message, sender, args, messageParams);
             * //     ScriptableObject.Destroy(controller);
             * // }
             */

            // 分发给Handler
            if (s_Handlers.Count > 0)
            {
                for (int i = 0; i < s_Handlers.Count; i++)
                {
                    IMessageHandler handler = s_Handlers[i];
                    if (handler == null)
                    {
                        s_Handlers.RemoveAt(i);
                        i--;
                    }
                    else
                    {
                        handler.ExecuteMessage(message, sender, args, messageParams);
                    }
                }
            }

            // 分发给Listener
            List <MessageListener> listeners;

            if (s_ListenerDict.TryGetValue(message, out listeners))
            {
                for (int i = 0; i < listeners.Count; i++)
                {
                    MessageListener listener = listeners[i];
                    if (listener == null)
                    {
                        listeners.RemoveAt(i);
                        i--;
                        if (listeners.Count == 0)
                        {
                            s_ListenerDict.Remove(message);
                            break;
                        }
                    }
                    else
                    {
                        listener(message, sender, args, messageParams);
                    }
                }
            }
        }