CObjectPool.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Fort23.Core
  4. {
  5. public class ComponentQueue : CObject
  6. {
  7. public string TypeName { get; }
  8. private readonly Queue<CObject> _queue = new Queue<CObject>();
  9. public ComponentQueue(string typeName)
  10. {
  11. this.TypeName = typeName;
  12. }
  13. public async void Enqueue(CObject entity)
  14. {
  15. if (entity == null)
  16. {
  17. return;
  18. }
  19. if (!entity.isActive)
  20. {
  21. return;
  22. }
  23. await entity.AwaitTask();
  24. if (!entity.isActive)
  25. {
  26. return;
  27. }
  28. entity.isActive = false;
  29. this._queue.Enqueue(entity);
  30. entity.DormancyObj();
  31. }
  32. public CObject Dequeue()
  33. {
  34. return this._queue.Dequeue();
  35. }
  36. public CObject Peek()
  37. {
  38. return this._queue.Peek();
  39. }
  40. public Queue<CObject> Queue => this._queue;
  41. public int Count => this._queue.Count;
  42. public override void Dispose()
  43. {
  44. while (this._queue.Count > 0)
  45. {
  46. CObject component = this._queue.Dequeue();
  47. component.Dispose();
  48. }
  49. }
  50. public override void ActiveObj()
  51. {
  52. }
  53. public override void DormancyObj()
  54. {
  55. }
  56. }
  57. public class CObjectPool : CObject
  58. {
  59. private static CObjectPool _instance;
  60. public static CObjectPool Instance
  61. {
  62. get
  63. {
  64. if (_instance == null)
  65. {
  66. _instance = new CObjectPool();
  67. }
  68. return _instance;
  69. }
  70. }
  71. private readonly Dictionary<Type, ComponentQueue> _dictionary = new Dictionary<Type, ComponentQueue>();
  72. public CObject Fetch(Type type)
  73. {
  74. CObject obj;
  75. if (!this._dictionary.TryGetValue(type, out ComponentQueue queue))
  76. {
  77. obj = (CObject)Activator.CreateInstance(type);
  78. }
  79. else if (queue.Count == 0)
  80. {
  81. obj = (CObject)Activator.CreateInstance(type);
  82. }
  83. else
  84. {
  85. obj = queue.Dequeue();
  86. }
  87. obj.isActive = true;
  88. obj.ActiveObj();
  89. return obj;
  90. }
  91. public T Fetch<T>() where T : CObject
  92. {
  93. T t = (T)this.Fetch(typeof(T));
  94. return t;
  95. }
  96. public void Recycle(CObject obj)
  97. {
  98. if (obj == null)
  99. {
  100. return;
  101. }
  102. Type type = obj.GetType();
  103. ComponentQueue queue;
  104. if (!this._dictionary.TryGetValue(type, out queue))
  105. {
  106. queue = new ComponentQueue(type.Name);
  107. this._dictionary.Add(type, queue);
  108. }
  109. queue.Enqueue(obj);
  110. }
  111. public void Clear()
  112. {
  113. foreach (KeyValuePair<Type, ComponentQueue> kv in this._dictionary)
  114. {
  115. kv.Value.Dispose();
  116. }
  117. this._dictionary.Clear();
  118. }
  119. public override void Dispose()
  120. {
  121. foreach (KeyValuePair<Type, ComponentQueue> kv in this._dictionary)
  122. {
  123. kv.Value.Dispose();
  124. }
  125. this._dictionary.Clear();
  126. _instance = null;
  127. }
  128. public override void ActiveObj()
  129. {
  130. }
  131. public override void DormancyObj()
  132. {
  133. }
  134. }
  135. }