public virtual void Load(ComponentNetworkOuter outer, ComponentNetworkInner inner) { networkOuter = outer; networkInner = inner; Handlers.Clear(); // 网络消息绑定数据结构 List <Type> types = AssemblyHelper.GetTypes(); foreach (Type type in types) { object[] attrs = type.GetCustomAttributes(typeof(MessageAttribute), false); if (attrs.Length == 0) { continue; } MessageAttribute messageAttribute = attrs[0] as MessageAttribute; if (messageAttribute == null) { continue; } Handlers.Add(messageAttribute.Opcode, messageAttribute.appType); } }
public override void Awake(JToken jd = null) { this.opcodeTypes.Clear(); this.typeMessages.Clear(); // 网络消息绑定数据结构 List <Type> types = AssemblyHelper.GetTypes(); foreach (Type type in types) { object[] attrs = type.GetCustomAttributes(typeof(MessageAttribute), false); if (attrs.Length == 0) { continue; } MessageAttribute messageAttribute = attrs[0] as MessageAttribute; if (messageAttribute == null) { continue; } this.opcodeTypes.Add(messageAttribute.Opcode, type); this.typeMessages.Add(messageAttribute.Opcode, Activator.CreateInstance(type)); } foreach (Type type in types) { object[] attrs = type.GetMethods(); if (attrs.Length == 0) { continue; } foreach (MethodInfo methodInfo in attrs) { object[] attrs2 = methodInfo.GetCustomAttributes(typeof(MessageMethodAttribute), false); if (attrs2.Length == 0) { continue; } MessageMethodAttribute attribute = attrs2[0] as MessageMethodAttribute; if (attribute == null) { continue; } if (!methodInfo.IsStatic) { Log.Debug($"MessageMethod {type.Name}.{methodInfo.Name} 不是静态函数,将被忽略!"); continue; } Action <Session, int, object> dd = (Action <Session, int, object>)Delegate.CreateDelegate(typeof(Action <Session, int, object>), null, methodInfo); this.registerMsg(attribute.Opcode, dd); } } }
static public void LoadComponent(JToken jd, Entity parent) { List <Type> list = AssemblyHelper.GetTypes(); foreach (JProperty jp in jd) //遍历数组 { Type item = list.Find(c => c.Name == jp.Name); //返回指定条件的元素 if (item != null && item.Name != "Entity") { Entity obj = parent; try { if (jd[item.Name]["Entity"] != null) { string name = jd[item.Name]["Entity"].ToString(); obj = Entity.Root.Find(name); if (obj == null) { obj = new Entity(name, Entity.Root); } } if (parent != null && parent != obj) { Log.Debug($"{parent.Name}.{obj.Name} + {item.Name}"); } else { Log.Debug($"{obj.Name} + {item.Name}"); } obj.AddComponent(item, jd[item.Name]); LoadComponent(jd[item.Name], obj); } catch (Exception e) { int HResult = e.HResult; Log.Error($"{obj.Name} + {item.Name} {e.ToString()}"); } } } }