public static void Load(this ActorMessageDispatcherComponent self) { self.ActorMessageHandlers.Clear(); var types = Game.EventSystem.GetTypes(typeof(ActorMessageHandlerAttribute)); foreach (Type type in types) { object obj = Activator.CreateInstance(type); IMActorHandler imHandler = obj as IMActorHandler; if (imHandler == null) { throw new Exception($"message handler not inherit IMActorHandler abstract class: {obj.GetType().FullName}"); } object[] attrs = type.GetCustomAttributes(typeof(ActorMessageHandlerAttribute), false); foreach (object attr in attrs) { ActorMessageHandlerAttribute actorMessageHandlerAttribute = attr as ActorMessageHandlerAttribute; Type messageType = imHandler.GetRequestType(); Type handleResponseType = imHandler.GetResponseType(); if (handleResponseType != null) { Type responseType = OpcodeTypeComponent.Instance.GetResponseType(messageType); if (handleResponseType != responseType) { throw new Exception($"message handler response type error: {messageType.FullName}"); } } ActorMessageDispatcherInfo actorMessageDispatcherInfo = new ActorMessageDispatcherInfo(actorMessageHandlerAttribute.SceneType, imHandler); self.RegisterHandler(messageType, actorMessageDispatcherInfo); } } }
private static void RegisterHandler(this ActorMessageDispatcherComponent self, Type type, ActorMessageDispatcherInfo handler) { if (!self.ActorMessageHandlers.ContainsKey(type)) { self.ActorMessageHandlers.Add(type, new List <ActorMessageDispatcherInfo>()); } self.ActorMessageHandlers[type].Add(handler); }