public void Add(DLLType dllType, Assembly assembly) { this.assemblies[dllType] = assembly; this.types.Clear(); foreach (Assembly value in this.assemblies.Values) { foreach (Type type in value.GetTypes()) { object[] objects = type.GetCustomAttributes(typeof(BaseAttribute), false); if (objects.Length == 0) { continue; } BaseAttribute baseAttribute = (BaseAttribute)objects[0]; this.types.Add(baseAttribute.AttributeType, type); } } this.awakeSystems.Clear(); this.lateUpdateSystems.Clear(); this.updateSystems.Clear(); this.startSystems.Clear(); this.loadSystems.Clear(); this.changeSystems.Clear(); this.destroySystems.Clear(); this.deserializeSystems.Clear(); foreach (Type type in types[typeof(ObjectSystemAttribute)]) { object[] attrs = type.GetCustomAttributes(typeof(ObjectSystemAttribute), false); if (attrs.Length == 0) { continue; } object obj = Activator.CreateInstance(type); switch (obj) { case IAwakeSystem objectSystem: this.awakeSystems.Add(objectSystem.Type(), objectSystem); break; case IUpdateSystem updateSystem: this.updateSystems.Add(updateSystem.Type(), updateSystem); break; case ILateUpdateSystem lateUpdateSystem: this.lateUpdateSystems.Add(lateUpdateSystem.Type(), lateUpdateSystem); break; case IStartSystem startSystem: this.startSystems.Add(startSystem.Type(), startSystem); break; case IDestroySystem destroySystem: this.destroySystems.Add(destroySystem.Type(), destroySystem); break; case ILoadSystem loadSystem: this.loadSystems.Add(loadSystem.Type(), loadSystem); break; case IChangeSystem changeSystem: this.changeSystems.Add(changeSystem.Type(), changeSystem); break; case IDeserializeSystem deserializeSystem: this.deserializeSystems.Add(deserializeSystem.Type(), deserializeSystem); break; } } this.allEvents.Clear(); foreach (Type type in types[typeof(EventAttribute)]) { object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false); foreach (object attr in attrs) { EventAttribute aEventAttribute = (EventAttribute)attr; object obj = Activator.CreateInstance(type); IEvent iEvent = obj as IEvent; if (iEvent == null) { Log.Error($"{obj.GetType().Name} 没有继承IEvent"); } this.RegisterEvent(aEventAttribute.Type, iEvent); } } this.Load(); }
public void Add(DLLType dllType, Assembly assembly) { this.assemblies[dllType] = assembly; this.awakeEvents.Clear(); this.lateUpdateEvents.Clear(); this.updateEvents.Clear(); this.startEvents.Clear(); this.loadEvents.Clear(); Type[] types = DllHelper.GetMonoTypes(); foreach (Type type in types) { object[] attrs = type.GetCustomAttributes(typeof(ObjectSystemAttribute), false); if (attrs.Length == 0) { continue; } object obj = Activator.CreateInstance(type); AAwakeSystem objectSystem = obj as AAwakeSystem; if (objectSystem != null) { this.awakeEvents.Add(objectSystem.Type(), objectSystem); } AUpdateSystem aUpdateSystem = obj as AUpdateSystem; if (aUpdateSystem != null) { this.updateEvents.Add(aUpdateSystem.Type(), aUpdateSystem); } ALateUpdateSystem aLateUpdateSystem = obj as ALateUpdateSystem; if (aLateUpdateSystem != null) { this.lateUpdateEvents.Add(aLateUpdateSystem.Type(), aLateUpdateSystem); } AStartSystem aStartSystem = obj as AStartSystem; if (aStartSystem != null) { this.startEvents.Add(aStartSystem.Type(), aStartSystem); } ALoadSystem aLoadSystem = obj as ALoadSystem; if (aLoadSystem != null) { this.loadEvents.Add(aLoadSystem.Type(), aLoadSystem); } } this.allEvents.Clear(); foreach (Type type in types) { object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false); foreach (object attr in attrs) { EventAttribute aEventAttribute = (EventAttribute)attr; object obj = Activator.CreateInstance(type); IEvent iEvent = obj as IEvent; if (iEvent == null) { Log.Error($"{obj.GetType().Name} 没有继承IEvent"); } this.RegisterEvent(aEventAttribute.Type, iEvent); } } this.Load(); }
public void Add(DLLType dllType, Assembly assembly) { this.assemblies[dllType] = assembly; this.awakeSystems.Clear(); this.lateUpdateSystems.Clear(); this.updateSystems.Clear(); this.startSystems.Clear(); this.loadSystems.Clear(); this.changeSystems.Clear(); Type[] types = DllHelper.GetMonoTypes(); foreach (Type type in types) { object[] attrs = type.GetCustomAttributes(typeof(ObjectSystemAttribute), false); if (attrs.Length == 0) { continue; } object obj = Activator.CreateInstance(type); IAwakeSystem objectSystem = obj as IAwakeSystem; if (objectSystem != null) { this.awakeSystems.Add(objectSystem.Type(), objectSystem); } IUpdateSystem updateSystem = obj as IUpdateSystem; if (updateSystem != null) { this.updateSystems.Add(updateSystem.Type(), updateSystem); } ILateUpdateSystem lateUpdateSystem = obj as ILateUpdateSystem; if (lateUpdateSystem != null) { this.lateUpdateSystems.Add(lateUpdateSystem.Type(), lateUpdateSystem); } IStartSystem startSystem = obj as IStartSystem; if (startSystem != null) { this.startSystems.Add(startSystem.Type(), startSystem); } IDestroySystem destroySystem = obj as IDestroySystem; if (destroySystem != null) { this.destroySystems.Add(destroySystem.Type(), destroySystem); } ILoadSystem loadSystem = obj as ILoadSystem; if (loadSystem != null) { this.loadSystems.Add(loadSystem.Type(), loadSystem); } IChangeSystem changeSystem = obj as IChangeSystem; if (changeSystem != null) { this.changeSystems.Add(changeSystem.Type(), changeSystem); } } this.allEvents.Clear(); foreach (Type type in types) { object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false); foreach (object attr in attrs) { EventAttribute aEventAttribute = (EventAttribute)attr; object obj = Activator.CreateInstance(type); IEvent iEvent = obj as IEvent; if (iEvent == null) { Log.Error($"{obj.GetType().Name} 没有继承IEvent"); } this.RegisterEvent(aEventAttribute.Type, iEvent); } } this.Load(); }
private readonly Queue <long> starts = new Queue <long>(); //这个只需要一个 因为starts只会执行一次 /// <summary> /// 加载程序集 /// </summary> /// <param name="dllType"></param> /// <param name="assembly"></param> public void Add(DLLType dllType, Assembly assembly) { this.assemblies[dllType] = assembly; this.types.Clear(); //清空 foreach (Assembly value in this.assemblies.Values) { foreach (Type type in value.GetTypes()) { object[] objects = type.GetCustomAttributes(typeof(BaseAttribute), false); //有BaseAttribute特性的类 if (objects.Length == 0) { continue; } BaseAttribute baseAttribute = (BaseAttribute)objects[0]; this.types.Add(baseAttribute.AttributeType, type); //把类根据其特性的类型 加入到不同的字典中 } } this.awakeSystems.Clear(); this.lateUpdateSystems.Clear(); this.updateSystems.Clear(); this.startSystems.Clear(); this.loadSystems.Clear(); this.changeSystems.Clear(); foreach (Type type in types[typeof(ObjectSystemAttribute)]) //遍历有ObjectSystem特性的类 { object[] attrs = type.GetCustomAttributes(typeof(ObjectSystemAttribute), false); //再次检查 if (attrs.Length == 0) { continue; } object obj = Activator.CreateInstance(type); //实例化 //比如 public class TimerComponentUpdateSystem : UpdateSystem<TimerComponent> // 我们写的功能都在 TimerComponent 中 它里面有 start update 等方法 但是不用我们来调用 // TimerComponentUpdateSystem 类会自动帮我们调用 其中的 update 方法 // updateSystems 字典中会保存 TimerComponent类名 和 TimerComponentUpdateSystem 这个类 // 当 TimerComponent 这个类生成的时候 updateSystems 字典中检索到了 有这个类名 就会调用TimerComponentUpdateSystem // 而 TimerComponentUpdateSystem 的传参是 TimerComponent 他会自动调用 TimerComponent 中的方法 IAwakeSystem objectSystem = obj as IAwakeSystem; if (objectSystem != null) { this.awakeSystems.Add(objectSystem.Type(), objectSystem); //!!!这里的objectSystem.Type()其实就是需要执行的类的类型 所以他能收集对应的类的方法 } IUpdateSystem updateSystem = obj as IUpdateSystem; if (updateSystem != null) { this.updateSystems.Add(updateSystem.Type(), updateSystem); } ILateUpdateSystem lateUpdateSystem = obj as ILateUpdateSystem; if (lateUpdateSystem != null) { this.lateUpdateSystems.Add(lateUpdateSystem.Type(), lateUpdateSystem); } IStartSystem startSystem = obj as IStartSystem; if (startSystem != null) { this.startSystems.Add(startSystem.Type(), startSystem); } IDestroySystem destroySystem = obj as IDestroySystem; if (destroySystem != null) { this.destroySystems.Add(destroySystem.Type(), destroySystem); } ILoadSystem loadSystem = obj as ILoadSystem; if (loadSystem != null) { this.loadSystems.Add(loadSystem.Type(), loadSystem); } IChangeSystem changeSystem = obj as IChangeSystem; if (changeSystem != null) { this.changeSystems.Add(changeSystem.Type(), changeSystem); } } ///////////////////////////////////////////////////////////////////////////////////////// this.allEvents.Clear(); //清除事件 //遍历所有的有事件特性的类 foreach (Type type in types[typeof(EventAttribute)]) { object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false); //生成特性 foreach (object attr in attrs) { EventAttribute aEventAttribute = (EventAttribute)attr; object obj = Activator.CreateInstance(type); //反射出类 IEvent iEvent = obj as IEvent; if (iEvent == null) //判断类是否继承IEvent接口 { Log.Error($"{obj.GetType().Name} 没有继承IEvent"); } this.RegisterEvent(aEventAttribute.Type, iEvent); //加入字典中 } } this.Load(); }
public void Add(DLLType dllType, Assembly assembly) { //缓存到字典中 this.assemblies[dllType] = assembly; this.types.Clear(); //清空 //Log.Debug($"assemblies:{this.assemblies.Values.Count}"); //扫描字典中的所有dll 实际上就只有Unity.Model.dll foreach (Assembly value in this.assemblies.Values) { //遍历每个程序集下的类型 foreach (Type type in value.GetTypes()) { //获取定义的特性 object[] objects = type.GetCustomAttributes(typeof(BaseAttribute), false); if (objects.Length == 0) { continue; } BaseAttribute baseAttribute = (BaseAttribute)objects[0]; //根据特性进行缓存各种事件 this.types.Add(baseAttribute.AttributeType, type); //Log.Debug($"{baseAttribute.AttributeType.ToString()}/ {type.ToString()}"); } } //清空所有存储事件的字典 this.awakeSystems.Clear(); this.lateUpdateSystems.Clear(); this.updateSystems.Clear(); this.startSystems.Clear(); this.loadSystems.Clear(); this.changeSystems.Clear(); this.destroySystems.Clear(); this.deserializeSystems.Clear(); //遍历ObjectSystemAttribute类型的事件 foreach (Type type in types[typeof(ObjectSystemAttribute)]) { //从这个类型 获取特性列表 类型是ObjectSystemAttribute 并且非继承的 object[] attrs = type.GetCustomAttributes(typeof(ObjectSystemAttribute), false); if (attrs.Length == 0) { continue; } //创造该类型对应的实例 object obj = Activator.CreateInstance(type); //进行类型的匹配 然后缓存到各自的字典中 //以下这些都是接口interface,所以只要obj继承了以下某一个分支,那么也会满足以下该分支的条件判断 //说白话就是将继承自IAwakeSystem、IUpdateSystem...并且加了ObjectSystemAttribute特性的这些类,都缓存到各自的字典里去 switch (obj) { case IAwakeSystem objectSystem: this.awakeSystems.Add(objectSystem.Type(), objectSystem); break; case IUpdateSystem updateSystem: this.updateSystems.Add(updateSystem.Type(), updateSystem); break; case ILateUpdateSystem lateUpdateSystem: this.lateUpdateSystems.Add(lateUpdateSystem.Type(), lateUpdateSystem); break; case IStartSystem startSystem: this.startSystems.Add(startSystem.Type(), startSystem); break; case IDestroySystem destroySystem: this.destroySystems.Add(destroySystem.Type(), destroySystem); break; case ILoadSystem loadSystem: this.loadSystems.Add(loadSystem.Type(), loadSystem); Log.Debug($"{loadSystem.Type()}....................."); break; case IChangeSystem changeSystem: this.changeSystems.Add(changeSystem.Type(), changeSystem); break; case IDeserializeSystem deserializeSystem: this.deserializeSystems.Add(deserializeSystem.Type(), deserializeSystem); break; } } //allEvents是存储所有继承自IEvent的类 //先清除再缓存 this.allEvents.Clear(); //找到EventAttribute的class foreach (Type type in types[typeof(EventAttribute)]) { object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false); foreach (object attr in attrs) { EventAttribute aEventAttribute = (EventAttribute)attr; //创建class的实例 object obj = Activator.CreateInstance(type); //所有EventAttribute特性的类 都需要继承自IEvent IEvent iEvent = obj as IEvent; if (iEvent == null) { Log.Error($"{obj.GetType().Name} 没有继承IEvent"); } //注册事件,key是特性声明时候标注的字符串,value是class名称,带命名空间 //RegisterEvent方法内部是将所有实例缓存到allEvents字典中 this.RegisterEvent(aEventAttribute.Type, iEvent); } } //调用所有缓存的Load事件 this.Load(); }