public void Start() { // 收集所有网络协议的类型 List <Type> types = ILRManager.Instance.HotfixAssemblyTypes; for (int i = 0; i < types.Count; i++) { System.Type type = types[i]; // 判断属性标签 if (Attribute.IsDefined(type, typeof(NetMessageAttribute))) { var attributeArray = type.GetCustomAttributes(typeof(NetMessageAttribute), false); NetMessageAttribute attribute = attributeArray[0] as NetMessageAttribute; // 判断是否重复 if (_msgTypes.ContainsKey(attribute.MsgType)) { throw new Exception($"Message {type} has same value : {attribute.MsgType}"); } // 添加到集合 _msgTypes.Add(attribute.MsgType, type); } } // 注册消息接收回调 NetManager.Instance.HotfixProtoCallback += OnHandleHotfixMsg; }
// 收集所有网络协议的类型 private void CollectTypes() { List <Type> types = ILRManager.Instance.HotfixAssemblyTypes; for (int i = 0; i < types.Count; i++) { System.Type type = types[i]; // 判断属性标签 if (Attribute.IsDefined(type, typeof(NetworkMessageAttribute))) { // 判断是否重复 NetworkMessageAttribute attribute = HotfixTypeHelper.GetAttribute <NetworkMessageAttribute>(type); if (_types.ContainsKey(attribute.MsgID)) { throw new Exception($"Message {type} has same value : {attribute.MsgID}"); } // 添加到集合 _types.Add(attribute.MsgID, type); } } }
public static void Init() { Type[] tmpTypes = HotFixHelper.GetHotfixTypes(); Type tmpCommandAttributeType = typeof(CommandAttribute); Type tmpCommandHandlerAttributeType = typeof(CommandHandlerAttribute); Type tmpCommandHandleAttributeType = typeof(CommandHandleAttribute); foreach (Type typeElem in tmpTypes) { //协议信息 var tmpCommandAttributes = typeElem.GetCustomAttributes(tmpCommandAttributeType, false); CommandAttribute tmpCommand = tmpCommandAttributes.Length > 0 ? tmpCommandAttributes[0] as CommandAttribute : null; if (null != tmpCommand) { if (!sCommandOpcode2TypeDict.ContainsKey(tmpCommand.Opcode)) { sCommandOpcode2TypeDict.Add(tmpCommand.Opcode, typeElem); } if (!sCommandType2FieldInfoDict.ContainsKey(typeElem)) { FieldInfo tmpFieldInfo = typeElem.GetField("Data"); if (null == tmpFieldInfo) { Logger.LogError("消息中不包含data数据"); } else { sCommandType2FieldInfoDict.Add(typeElem, tmpFieldInfo); } } } //协议观察者信息 var tmpCommandHandlerAttris = typeElem.GetCustomAttributes(tmpCommandHandlerAttributeType, false); CommandHandlerAttribute tmpCommandHandler = null; if (tmpCommandHandlerAttris.Length > 0) { tmpCommandHandler = tmpCommandHandlerAttris[0] as CommandHandlerAttribute; } if (null == tmpCommandHandler) { continue; } MethodInfo[] tmpMethods = typeElem.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly); foreach (MethodInfo methodInfoElem in tmpMethods) { var tmpCommandHandleAttris = methodInfoElem.GetCustomAttributes(tmpCommandHandleAttributeType, false); CommandHandleAttribute tmpCommandHandle = null; if (tmpCommandHandleAttris.Length > 0) { tmpCommandHandle = tmpCommandHandleAttris[0] as CommandHandleAttribute; } if (null == tmpCommandHandle) { continue; } //ILRT暂不支持GetParameters //ParameterInfo[] tmpParameters = methodInfoElem.GetParameters(); //if (tmpParameters.Length > 0 && tmpParameters[0].ParameterType != tmpCommandHandle.CommandType) //{ // Logger.LogError("消息处理方法形参类型错误 " + typeElem + " -> " + methodInfoElem.Name); // continue; //} List <MethodInfo> tmpMethodInfoList = null; if (!sOpcode2MethodInfosDict.TryGetValue(tmpCommandHandle.Opcode, out tmpMethodInfoList)) { tmpMethodInfoList = new List <MethodInfo>(); sOpcode2MethodInfosDict.Add(tmpCommandHandle.Opcode, tmpMethodInfoList); } tmpMethodInfoList.Add(methodInfoElem); } } }