ObjectPool.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using UnityEngine;
  8. namespace ETModel
  9. {
  10. public class ComponentQueue: IDisposable
  11. {
  12. public long Id;
  13. #if !SERVER
  14. public static GameObject Global { get; } = GameObject.Find("/Global");
  15. public GameObject ViewGO { get; set; }
  16. #endif
  17. public string TypeName { get; }
  18. private readonly Queue<Entity> queue = new Queue<Entity>();
  19. public ComponentQueue(string typeName)
  20. {
  21. this.Id = IdGenerater.GenerateId();
  22. this.TypeName = typeName;
  23. #if !SERVER
  24. this.ViewGO = new GameObject();
  25. this.ViewGO.name = this.GetType().Name;
  26. this.ViewGO.layer = LayerNames.GetLayerInt(LayerNames.HIDDEN);
  27. this.ViewGO.transform.SetParent(Global.transform, false);
  28. this.ViewGO.AddComponent<ComponentView>().Component = this;
  29. #endif
  30. }
  31. public void Enqueue(Entity entity)
  32. {
  33. this.queue.Enqueue(entity);
  34. }
  35. public Entity Dequeue()
  36. {
  37. return this.queue.Dequeue();
  38. }
  39. public Entity Peek()
  40. {
  41. return this.queue.Peek();
  42. }
  43. public Queue<Entity> Queue
  44. {
  45. get
  46. {
  47. return this.queue;
  48. }
  49. }
  50. public int Count
  51. {
  52. get
  53. {
  54. return this.queue.Count;
  55. }
  56. }
  57. public void Dispose()
  58. {
  59. while (this.queue.Count > 0)
  60. {
  61. Entity component = this.queue.Dequeue();
  62. component.Dispose();
  63. }
  64. }
  65. }
  66. public class ObjectPool: IDisposable
  67. {
  68. #if !SERVER
  69. public static GameObject Global { get; } = GameObject.Find("/Global");
  70. public GameObject ViewGO { get; set; }
  71. #endif
  72. public string Name { get; set; }
  73. private readonly Dictionary<Type, ComponentQueue> dictionary = new Dictionary<Type, ComponentQueue>();
  74. public ObjectPool()
  75. {
  76. #if !SERVER
  77. this.ViewGO = new GameObject();
  78. this.ViewGO.name = this.GetType().Name;
  79. this.ViewGO.layer = LayerNames.GetLayerInt(LayerNames.HIDDEN);
  80. this.ViewGO.transform.SetParent(Global.transform, false);
  81. this.ViewGO.AddComponent<ComponentView>().Component = this;
  82. #endif
  83. }
  84. public Entity Fetch(Type type)
  85. {
  86. Entity obj;
  87. if (!this.dictionary.TryGetValue(type, out ComponentQueue queue))
  88. {
  89. obj = (Entity)Activator.CreateInstance(type);
  90. }
  91. else if (queue.Count == 0)
  92. {
  93. obj = (Entity)Activator.CreateInstance(type);
  94. }
  95. else
  96. {
  97. obj = queue.Dequeue();
  98. }
  99. obj.IsFromPool = true;
  100. return obj;
  101. }
  102. public T Fetch<T>() where T: Entity
  103. {
  104. T t = (T) this.Fetch(typeof(T));
  105. return t;
  106. }
  107. public void Recycle(Entity obj)
  108. {
  109. Type type = obj.GetType();
  110. ComponentQueue queue;
  111. if (!this.dictionary.TryGetValue(type, out queue))
  112. {
  113. queue = new ComponentQueue(type.Name);
  114. #if !SERVER
  115. if (queue.ViewGO != null)
  116. {
  117. queue.ViewGO.transform.SetParent(this.ViewGO.transform);
  118. queue.ViewGO.name = $"{type.Name}s";
  119. }
  120. #endif
  121. this.dictionary.Add(type, queue);
  122. }
  123. #if !SERVER
  124. if (obj.ViewGO != null)
  125. {
  126. obj.ViewGO.transform.SetParent(queue.ViewGO.transform);
  127. }
  128. #endif
  129. obj.Id = 0;
  130. queue.Enqueue(obj);
  131. }
  132. public void Dispose()
  133. {
  134. foreach (var kv in this.dictionary)
  135. {
  136. kv.Value.Dispose();
  137. }
  138. this.dictionary.Clear();
  139. }
  140. public override string ToString()
  141. {
  142. StringBuilder sb = new StringBuilder();
  143. Dictionary<Type, int> typeCount = new Dictionary<Type, int>();
  144. foreach (var kv in this.dictionary)
  145. {
  146. typeCount[kv.Key] = kv.Value.Count;
  147. }
  148. IOrderedEnumerable<KeyValuePair<Type, int>> orderByDescending = typeCount.OrderByDescending(s => s.Value);
  149. sb.AppendLine("ObjectPool Count: ");
  150. foreach (var kv in orderByDescending)
  151. {
  152. if (kv.Value == 1)
  153. {
  154. continue;
  155. }
  156. sb.AppendLine($"\t{kv.Key.Name}: {kv.Value}");
  157. }
  158. MultiMapSet<string, string> dict = Check();
  159. sb.Append("not reset field:\n");
  160. foreach (KeyValuePair<string,HashSet<string>> pair in dict.GetDictionary())
  161. {
  162. sb.Append(pair.Key + ": ");
  163. foreach (string value in pair.Value)
  164. {
  165. sb.Append(value + ", ");
  166. }
  167. sb.Append("\n");
  168. }
  169. return sb.ToString();
  170. }
  171. public void LogErrorCheckResult()
  172. {
  173. MultiMapSet<string, string> dict = Check();
  174. if (dict.Count == 0)
  175. {
  176. return;
  177. }
  178. StringBuilder sb = new StringBuilder();
  179. sb.Append("not reset field:\n");
  180. foreach (KeyValuePair<string,HashSet<string>> pair in dict.GetDictionary())
  181. {
  182. sb.Append(pair.Key + ": ");
  183. foreach (string value in pair.Value)
  184. {
  185. sb.Append(value + ", ");
  186. }
  187. sb.Append("\n");
  188. }
  189. Log.Error(sb.ToString());
  190. }
  191. public MultiMapSet<string, string> Check()
  192. {
  193. MultiMapSet<string, string> dict = new MultiMapSet<string, string>();
  194. foreach (ComponentQueue queue in this.dictionary.Values)
  195. {
  196. foreach (Entity entity in queue.Queue)
  197. {
  198. Type type = entity.GetType();
  199. FieldInfo[] fieldInfos = type.GetFields();
  200. foreach (FieldInfo fieldInfo in fieldInfos)
  201. {
  202. if (fieldInfo.IsLiteral)
  203. {
  204. continue;
  205. }
  206. if (fieldInfo.GetCustomAttributes(typeof (NoMemoryCheck)).Count() > 0)
  207. {
  208. continue;
  209. }
  210. Type fieldType = fieldInfo.FieldType;
  211. if (fieldType == typeof (int))
  212. {
  213. if ((int) fieldInfo.GetValue(entity) != 0)
  214. {
  215. dict.Add(type.Name, fieldInfo.Name);
  216. }
  217. continue;
  218. }
  219. if (fieldType == typeof (uint))
  220. {
  221. if ((uint) fieldInfo.GetValue(entity) != 0)
  222. {
  223. dict.Add(type.Name, fieldInfo.Name);
  224. }
  225. continue;
  226. }
  227. if (fieldType == typeof (long))
  228. {
  229. if ((long) fieldInfo.GetValue(entity) != 0)
  230. {
  231. dict.Add(type.Name, fieldInfo.Name);
  232. }
  233. continue;
  234. }
  235. if (fieldType == typeof (ulong))
  236. {
  237. if ((ulong) fieldInfo.GetValue(entity) != 0)
  238. {
  239. dict.Add(type.Name, fieldInfo.Name);
  240. }
  241. continue;
  242. }
  243. if (fieldType == typeof (short))
  244. {
  245. if ((short) fieldInfo.GetValue(entity) != 0)
  246. {
  247. dict.Add(type.Name, fieldInfo.Name);
  248. }
  249. continue;
  250. }
  251. if (fieldType == typeof (ushort))
  252. {
  253. if ((ushort) fieldInfo.GetValue(entity) != 0)
  254. {
  255. dict.Add(type.Name, fieldInfo.Name);
  256. }
  257. continue;
  258. }
  259. if (fieldType == typeof (float))
  260. {
  261. if (Math.Abs((float)fieldInfo.GetValue(entity)) > 0.0001)
  262. {
  263. dict.Add(type.Name, fieldInfo.Name);
  264. }
  265. continue;
  266. }
  267. if (fieldType == typeof (double))
  268. {
  269. if (Math.Abs((double)fieldInfo.GetValue(entity)) > 0.0001)
  270. {
  271. dict.Add(type.Name, fieldInfo.Name);
  272. }
  273. continue;
  274. }
  275. if (fieldType == typeof (bool))
  276. {
  277. if ((bool) fieldInfo.GetValue(entity) != false)
  278. {
  279. dict.Add(type.Name, fieldInfo.Name);
  280. }
  281. continue;
  282. }
  283. if (typeof(ICollection).IsAssignableFrom(fieldType))
  284. {
  285. object fieldValue = fieldInfo.GetValue(entity);
  286. if (fieldValue == null)
  287. {
  288. continue;
  289. }
  290. if (((ICollection)fieldValue).Count != 0)
  291. {
  292. dict.Add(type.Name, fieldInfo.Name);
  293. }
  294. continue;
  295. }
  296. PropertyInfo propertyInfo = fieldType.GetProperty("Count");
  297. if (propertyInfo != null)
  298. {
  299. if ((int) propertyInfo.GetValue(fieldInfo.GetValue(entity)) != 0)
  300. {
  301. dict.Add(type.Name, fieldInfo.Name);
  302. }
  303. continue;
  304. }
  305. if (fieldType.IsClass)
  306. {
  307. if (fieldInfo.GetValue(entity) != null)
  308. {
  309. dict.Add(type.Name, fieldInfo.Name);
  310. }
  311. continue;
  312. }
  313. }
  314. }
  315. }
  316. return dict;
  317. }
  318. }
  319. }