Environment.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using IFramework;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Runtime.CompilerServices;
  7. namespace IFramework
  8. {
  9. /// <summary>
  10. /// 框架运行环境
  11. /// </summary>
  12. class Environment : Unit, IEnvironment
  13. {
  14. private bool _inited;
  15. private Modules _modules;
  16. private EnvironmentType _envType;
  17. private LoomModule _loom;
  18. private event Action update;
  19. private event Action onDispose;
  20. private static object _envsetlock = new object();
  21. private static Environment _current;
  22. private TimeCalculator _time;
  23. /// <summary>
  24. /// 环境是否已经初始化
  25. /// </summary>
  26. public bool inited { get { return _inited; } }
  27. /// <summary>
  28. /// 环境下自带的模块容器
  29. /// </summary>
  30. public IModules modules { get { return _modules; } }
  31. /// <summary>
  32. /// 环境类型
  33. /// </summary>
  34. public EnvironmentType envType { get { return _envType; } }
  35. /// <summary>
  36. /// 当前环境
  37. /// </summary>
  38. public static Environment current
  39. {
  40. get { return _current; }
  41. private set
  42. {
  43. lock (_envsetlock)
  44. {
  45. _current = value;
  46. }
  47. }
  48. }
  49. public ITimeCalculator time { get { return _time; } }
  50. /// <summary>
  51. /// ctor
  52. /// </summary>
  53. /// <param name="envType"></param>
  54. public Environment(EnvironmentType envType)
  55. {
  56. this._envType = envType;
  57. }
  58. /// <summary>
  59. /// 初始化环境,3.5 使用
  60. /// </summary>
  61. /// <param name="types">需要初始化调用的静态类</param>
  62. public void Init(IEnumerable<Type> types)
  63. {
  64. if (_inited) return;
  65. current = this;
  66. _modules = new Modules(this);
  67. //cycleCollection = new RecyclableObjectCollection();
  68. if (types != null)
  69. {
  70. foreach (var type in types)
  71. {
  72. TypeAttributes ta = type.Attributes;
  73. if ((ta & TypeAttributes.Abstract) != 0 && ((ta & TypeAttributes.Sealed) != 0))
  74. RuntimeHelpers.RunClassConstructor(type.TypeHandle);
  75. }
  76. }
  77. _loom = LoomModule.CreatInstance<LoomModule>("");
  78. _time = new TimeCalculator();
  79. _inited = true;
  80. }
  81. /// <summary>
  82. /// 初始化环境,4.X 使用
  83. /// </summary>
  84. public void InitWithAttribute()
  85. {
  86. var types = AppDomain.CurrentDomain.GetAssemblies()
  87. .SelectMany(item => item.GetTypes())
  88. .Where(item => item.IsDefined(typeof(OnEnvironmentInitAttribute), false))
  89. .Select((type) =>
  90. {
  91. var attr = type.GetCustomAttributes(typeof(OnEnvironmentInitAttribute), false).First() as OnEnvironmentInitAttribute;
  92. var _type = attr.type;
  93. if ((_type & this.envType) != 0 || (_type & EnvironmentType.None) != 0)
  94. return type;
  95. return null;
  96. }).ToList();
  97. types.RemoveAll((type) => { return type == null; });
  98. Init(types);
  99. }
  100. /// <summary>
  101. /// 释放
  102. /// </summary>
  103. protected override void OnDispose()
  104. {
  105. if (disposed || !inited) return;
  106. if (onDispose != null) onDispose();
  107. _modules.Dispose();
  108. _loom.Dispose();
  109. _time.Dispose();
  110. _modules = null;
  111. update = null;
  112. onDispose = null;
  113. }
  114. /// <summary>
  115. /// 刷新环境
  116. /// </summary>
  117. public void Update()
  118. {
  119. if (disposed) return;
  120. current = this;
  121. _time.BeginDelta();
  122. _loom.Update();
  123. _modules.Update();
  124. if (update != null) update();
  125. _time.EndDelta();
  126. }
  127. /// <summary>
  128. /// 等待 环境的 update,即等到该环境的线程来临
  129. /// </summary>
  130. /// <param name="action"></param>
  131. public void WaitEnvironmentFrame(Action action)
  132. {
  133. if (disposed) return;
  134. _loom.RunDelay(action);
  135. }
  136. /// <summary>
  137. /// 绑定帧
  138. /// </summary>
  139. /// <param name="action"></param>
  140. public void BindUpdate(Action action)
  141. {
  142. if (disposed) return;
  143. update += action;
  144. }
  145. /// <summary>
  146. /// 移除绑定帧
  147. /// </summary>
  148. /// <param name="action"></param>
  149. public void UnBindUpdate(Action action)
  150. {
  151. if (disposed) return;
  152. update -= action;
  153. }
  154. /// <summary>
  155. /// 绑定dispose
  156. /// </summary>
  157. /// <param name="action"></param>
  158. public void BindDispose(Action action)
  159. {
  160. if (disposed) return;
  161. onDispose += action;
  162. }
  163. /// <summary>
  164. /// 移除绑定dispose
  165. /// </summary>
  166. /// <param name="action"></param>
  167. public void UnBindDispose(Action action)
  168. {
  169. if (disposed) return;
  170. onDispose -= action;
  171. }
  172. }
  173. }