Framework.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System;
  2. using System.Collections.Generic;
  3. namespace IFramework
  4. {
  5. /// <summary>
  6. /// 框架入口
  7. /// </summary>
  8. public static class Framework
  9. {
  10. #pragma warning disable CS1591 // 缺少对公共可见类型或成员的 XML 注释
  11. private static Dictionary<int, IEnvironment> envs = new Dictionary<int, IEnvironment>();
  12. private static object _lock = new object();
  13. public static IEnvironment current { get { return Environment.current; } }
  14. #pragma warning restore CS1591 // 缺少对公共可见类型或成员的 XML 注释
  15. /// <summary>
  16. /// 实例化环境
  17. /// </summary>
  18. /// <param name=" envType">环境类型</param>
  19. /// <returns>环境</returns>
  20. public static IEnvironment CreateEnv(EnvironmentType envType)
  21. {
  22. lock (_lock)
  23. {
  24. IEnvironment env;
  25. if (envs.TryGetValue((int)envType, out env))
  26. {
  27. throw new Exception(string.Format("The EnvironmentType {0} is not null ", envType));
  28. }
  29. else
  30. {
  31. env = new Environment(envType);
  32. envs.Add((int)envType, env);
  33. return env;
  34. }
  35. }
  36. }
  37. /// <summary>
  38. /// 根据序号获取环境
  39. /// </summary>
  40. /// <param name=" envType">环境类型</param>
  41. /// <returns></returns>
  42. public static IEnvironment GetEnv(EnvironmentType envType)
  43. {
  44. lock (_lock)
  45. {
  46. IEnvironment env;
  47. if (envs.TryGetValue((int)envType, out env))
  48. {
  49. return env;
  50. }
  51. else
  52. {
  53. throw new Exception(string.Format("The EnvironmentType {0} Error Not Find ,Please Check ", envType));
  54. }
  55. }
  56. }
  57. /// <summary>
  58. /// 释放环境
  59. /// </summary>
  60. /// <param name="envType"></param>
  61. public static void DisposeEnv(EnvironmentType envType)
  62. {
  63. var env = GetEnv(envType);
  64. if (env != null)
  65. {
  66. env.Dispose();
  67. lock (_lock)
  68. {
  69. envs.Remove((int)envType);
  70. }
  71. }
  72. }
  73. /// <summary>
  74. /// 绑顶 方法 到一个环境的 Update
  75. /// </summary>
  76. /// <param name="action">方法</param>
  77. /// <param name="env">环境</param>
  78. public static void BindEnvUpdate(this Action action, IEnvironment env)
  79. {
  80. env.BindUpdate(action);
  81. }
  82. /// <summary>
  83. /// 解除绑顶 方法 到一个环境的 Update
  84. /// </summary>
  85. /// <param name="action">方法</param>
  86. /// <param name="env">环境</param>
  87. public static void UnBindEnvUpdate(this Action action, IEnvironment env)
  88. {
  89. env.UnBindUpdate(action);
  90. }
  91. /// <summary>
  92. /// 绑顶 方法 到一个环境的 Dispose
  93. /// </summary>
  94. /// <param name="action">方法</param>
  95. /// <param name="env">环境</param>
  96. public static void BindEnvDispose(this Action action, IEnvironment env)
  97. {
  98. env.BindDispose(action);
  99. }
  100. /// <summary>
  101. /// 解除绑顶 方法 到一个环境的 Dispose
  102. /// </summary>
  103. /// <param name="action">方法</param>
  104. /// <param name="env">环境</param>
  105. public static void UnBindEnvDispose(this Action action, IEnvironment env)
  106. {
  107. env.UnBindDispose(action);
  108. }
  109. /// <summary>
  110. /// 绑顶 方法 到一个环境的 Update
  111. /// </summary>
  112. /// <param name="action">方法</param>
  113. /// <param name=" envType"></param>
  114. public static void BindEnvUpdate(this Action action, EnvironmentType envType)
  115. {
  116. action.BindEnvUpdate(GetEnv(envType));
  117. }
  118. /// <summary>
  119. /// 解除绑顶 方法 到一个环境的 Update
  120. /// </summary>
  121. /// <param name="action">方法</param>
  122. /// <param name=" envType"></param>
  123. public static void UnBindEnvUpdate(this Action action, EnvironmentType envType)
  124. {
  125. action.UnBindEnvUpdate(GetEnv(envType));
  126. }
  127. /// <summary>
  128. /// 绑顶 方法 到一个环境的 Dispose
  129. /// </summary>
  130. /// <param name="action">方法</param>
  131. /// <param name=" envType"></param>
  132. public static void BindEnvDispose(this Action action, EnvironmentType envType)
  133. {
  134. action.BindEnvDispose(GetEnv(envType));
  135. }
  136. /// <summary>
  137. /// 解除绑顶 方法 到一个环境的 Dispose
  138. /// </summary>
  139. /// <param name="action">方法</param>
  140. /// <param name=" envType"></param>
  141. public static void UnBindEnvDispose(this Action action, EnvironmentType envType)
  142. {
  143. action.UnBindEnvDispose(GetEnv(envType));
  144. }
  145. private class GlobalPool : BaseTypePool<object>
  146. {
  147. protected override IObjectPool CreatePool(Type type)
  148. {
  149. if (type.IsArray)
  150. {
  151. var poolType = typeof(ArrayPool<>).MakeGenericType(type.GetElementType());
  152. return Activator.CreateInstance(poolType) as IObjectPool;
  153. }
  154. return null;
  155. }
  156. }
  157. static private GlobalPool _globalPool = new GlobalPool();
  158. /// <summary>
  159. /// 获取全局对象池数量
  160. /// </summary>
  161. /// <typeparam name="T"></typeparam>
  162. public static int GetGlbalPoolCount<T>()
  163. {
  164. return _globalPool.GetPoolCount<T>();
  165. }
  166. /// <summary>
  167. /// 设置全局对象池
  168. /// </summary>
  169. /// <typeparam name="T"></typeparam>
  170. /// <param name="pool"></param>
  171. public static void SetGlbalPool<T>(ObjectPool<T> pool)
  172. {
  173. _globalPool.SetPool(pool);
  174. }
  175. /// <summary>
  176. /// 全局分配
  177. /// </summary>
  178. /// <typeparam name="T"></typeparam>
  179. /// <param name="arg"></param>
  180. /// <returns></returns>
  181. public static T GlobalAllocate<T>(IEventArgs arg = null)where T: class
  182. {
  183. return _globalPool.Get<T>(arg);
  184. }
  185. /// <summary>
  186. /// 全局回收
  187. /// </summary>
  188. /// <typeparam name="T"></typeparam>
  189. /// <param name="t"></param>
  190. /// <param name="arg"></param>
  191. public static void GlobalRecyle<T>(this T t, IEventArgs arg = null)where T :class
  192. {
  193. _globalPool.Set(t, arg);
  194. }
  195. /// <summary>
  196. /// 分配数组
  197. /// </summary>
  198. /// <typeparam name="T"></typeparam>
  199. /// <param name="length"></param>
  200. /// <returns></returns>
  201. public static T[] GlobalAllocateArray<T>(int length)
  202. {
  203. var result = GlobalAllocate<T[]>(new ArrayPoolArg(length));
  204. return result;
  205. }
  206. }
  207. }