Modules.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using System;
  2. using System.Collections.Generic;
  3. using IFramework.Coroutine;
  4. using IFramework.Inject;
  5. using IFramework.Message;
  6. using IFramework.Recorder;
  7. using IFramework.Timer;
  8. using IFramework.Queue;
  9. namespace IFramework
  10. {
  11. partial class Modules
  12. {
  13. #pragma warning disable CS1591 // 缺少对公共可见类型或成员的 XML 注释
  14. public ICoroutineModule coroutine { get { return GetModule<CoroutineModule>(); } }
  15. public IMessageModule message { get { return GetModule<MessageModule>(); } }
  16. public IStringMessageModule stringMessage { get { return GetModule<StringMessageModule>(); } }
  17. public IOperationRecorderModule recoder { get { return GetModule<OperationRecorderModule>(); } }
  18. public IInjectModule inject { get { return GetModule<InjectModule>(); } }
  19. public ITimerModule timer { get { return GetModule<TimerModule>(); } }
  20. #pragma warning restore CS1591 // 缺少对公共可见类型或成员的 XML 注释
  21. }
  22. /// <summary>
  23. /// 模块容器
  24. /// </summary>
  25. partial class Modules : Unit, IModules, IBelongToEnvironment
  26. {
  27. private IEnvironment _env;
  28. /// <summary>
  29. /// 环境
  30. /// </summary>
  31. public IEnvironment env { get { return _env; } }
  32. private object _lock = new object();
  33. private Dictionary<Type, Dictionary<string, Module>> _dic;
  34. private SimplePriorityQueue<Module, int> _queue;
  35. private List<UpdateModule> _updateModules;
  36. /// <summary>
  37. /// 创建一个模块,创建完了自动绑定
  38. /// </summary>
  39. /// <param name="type"></param>
  40. /// <param name="name"></param>
  41. /// <param name="priority"></param>
  42. /// <returns></returns>
  43. public Module CreateModule(Type type, string name = Module.defaultName, int priority = 0)
  44. {
  45. var mou = Module.CreatInstance(type, name, priority);
  46. mou.Bind(this);
  47. return mou;
  48. }
  49. /// <summary>
  50. /// 创建模块
  51. /// </summary>
  52. /// <typeparam name="T"></typeparam>
  53. /// <param name="name"></param>
  54. /// <param name="priority"></param>
  55. /// <returns></returns>
  56. public T CreateModule<T>(string name = Module.defaultName, int priority = 0) where T : Module
  57. {
  58. return CreateModule(typeof(T), name, priority) as T;
  59. }
  60. /// <summary>
  61. /// 查找模块
  62. /// </summary>
  63. /// <param name="type">模块类型</param>
  64. /// <param name="name">模块名称</param>
  65. /// <returns></returns>
  66. public Module FindModule(Type type, string name = Module.defaultName)
  67. {
  68. if (string.IsNullOrEmpty(name))
  69. name = type.Name;
  70. if (!_dic.ContainsKey(type)) return null;
  71. if (!_dic[type].ContainsKey(name)) return null;
  72. var module = _dic[type][name];
  73. return module;
  74. }
  75. /// <summary>
  76. /// 获取模块
  77. /// </summary>
  78. /// <param name="type"></param>
  79. /// <param name="name"></param>
  80. /// <param name="priority"></param>
  81. /// <returns></returns>
  82. public Module GetModule(Type type, string name = Module.defaultName, int priority = 0)
  83. {
  84. var tmp = FindModule(type, name);
  85. if (tmp == null)
  86. {
  87. tmp = CreateModule(type, name, priority);
  88. }
  89. return tmp;
  90. }
  91. /// <summary>
  92. /// 查找模块
  93. /// </summary>
  94. /// <typeparam name="T"></typeparam>
  95. /// <param name="name"></param>
  96. /// <returns></returns>
  97. public T FindModule<T>(string name = Module.defaultName) where T : Module
  98. {
  99. return FindModule(typeof(T), name) as T;
  100. }
  101. /// <summary>
  102. /// 获取模块
  103. /// </summary>
  104. /// <typeparam name="T"></typeparam>
  105. /// <param name="name"></param>
  106. /// <param name="priority"></param>
  107. /// <returns></returns>
  108. public T GetModule<T>(string name = Module.defaultName, int priority = 0) where T : Module
  109. {
  110. return GetModule(typeof(T), name, priority) as T;
  111. }
  112. /// <summary>
  113. /// Ctor
  114. /// </summary>
  115. /// <param name="env"></param>
  116. public Modules(Environment env)
  117. {
  118. this._env = env;
  119. _dic = new Dictionary<Type, Dictionary<string, Module>>();
  120. _queue = new SimplePriorityQueue<Module, int>();
  121. _updateModules = new List<UpdateModule>();
  122. }
  123. /// <summary>
  124. /// 绑定环境
  125. /// </summary>
  126. /// <summary>
  127. /// 释放
  128. /// </summary>
  129. protected override void OnDispose()
  130. {
  131. // using (new LockWait(ref _lock))
  132. {
  133. int count = _queue.count;
  134. Stack<Module> _modules = new Stack<Module>();
  135. for (int i = 0; i < count; i++)
  136. {
  137. var item = _queue.Dequeue();
  138. _modules.Push(item);
  139. }
  140. for (int i = 0; i < count; i++)
  141. {
  142. var item = _modules.Pop();
  143. item.Dispose();
  144. }
  145. _updateModules.Clear();
  146. _queue = null;
  147. _dic.Clear();
  148. _dic = null;
  149. }
  150. }
  151. internal void Update()
  152. {
  153. for (int i = 0; i < _updateModules.Count; i++)
  154. {
  155. _updateModules[i].Update();
  156. }
  157. }
  158. private void SyncUpdateList()
  159. {
  160. _updateModules.Clear();
  161. foreach (var item in _queue)
  162. {
  163. if (item is UpdateModule)
  164. {
  165. _updateModules.Add(item as UpdateModule);
  166. }
  167. }
  168. }
  169. internal bool SubscribeModule(Module moudle)
  170. {
  171. lock (_lock)
  172. {
  173. Type type = moudle.GetType();
  174. if (!_dic.ContainsKey(type))
  175. _dic.Add(type, new Dictionary<string, Module>());
  176. var list = _dic[type];
  177. if (list.ContainsKey(moudle.name))
  178. {
  179. Log.E(string.Format("Have Bind Module | Type {0} Name {1}", type, moudle.name));
  180. return false;
  181. }
  182. else
  183. {
  184. list.Add(moudle.name, moudle);
  185. _queue.Enqueue(moudle, moudle.priority);
  186. SyncUpdateList();
  187. return true;
  188. }
  189. }
  190. }
  191. internal bool UnSubscribeBindModule(Module moudle)
  192. {
  193. lock (_lock)
  194. {
  195. Type type = moudle.GetType();
  196. if (!_dic.ContainsKey(type))
  197. {
  198. Log.E(string.Format("01,Have Not Bind Module | Type {0} Name {1}", type, moudle.name));
  199. return false;
  200. }
  201. else
  202. {
  203. var list = _dic[type];
  204. if (!list.ContainsKey(moudle.name))
  205. {
  206. Log.E(string.Format("02,Have Not Bind Module | Type {0} Name {1}", type, moudle.name));
  207. return false;
  208. }
  209. else
  210. {
  211. _dic[type].Remove(moudle.name);
  212. if (_queue.Contains(moudle))
  213. {
  214. _queue.Remove(moudle);
  215. SyncUpdateList();
  216. }
  217. return true;
  218. }
  219. }
  220. }
  221. }
  222. }
  223. }