using System; using System.Collections.Generic; namespace Fort23.Core { public class ComponentQueue : CObject { public string TypeName { get; } private readonly Queue _queue = new Queue(); public ComponentQueue(string typeName) { this.TypeName = typeName; } public async void Enqueue(CObject entity) { if (entity == null) { return; } if (!entity.isActive) { return; } await entity.AwaitTask(); if (!entity.isActive) { return; } entity.isActive = false; this._queue.Enqueue(entity); entity.DormancyObj(); } public CObject Dequeue() { return this._queue.Dequeue(); } public CObject Peek() { return this._queue.Peek(); } public Queue Queue => this._queue; public int Count => this._queue.Count; public override void Dispose() { while (this._queue.Count > 0) { CObject component = this._queue.Dequeue(); component.Dispose(); } } public override void ActiveObj() { } public override void DormancyObj() { } } public class CObjectPool : CObject { private static CObjectPool _instance; public static CObjectPool Instance { get { if (_instance == null) { _instance = new CObjectPool(); } return _instance; } } private readonly Dictionary _dictionary = new Dictionary(); public CObject Fetch(Type type) { CObject obj; if (!this._dictionary.TryGetValue(type, out ComponentQueue queue)) { obj = (CObject)Activator.CreateInstance(type); } else if (queue.Count == 0) { obj = (CObject)Activator.CreateInstance(type); } else { obj = queue.Dequeue(); } obj.isActive = true; obj.ActiveObj(); return obj; } public T Fetch() where T : CObject { T t = (T)this.Fetch(typeof(T)); return t; } public void Recycle(CObject obj) { if (obj == null) { return; } Type type = obj.GetType(); ComponentQueue queue; if (!this._dictionary.TryGetValue(type, out queue)) { queue = new ComponentQueue(type.Name); this._dictionary.Add(type, queue); } queue.Enqueue(obj); } public void Clear() { foreach (KeyValuePair kv in this._dictionary) { kv.Value.Dispose(); } this._dictionary.Clear(); } public override void Dispose() { foreach (KeyValuePair kv in this._dictionary) { kv.Value.Dispose(); } this._dictionary.Clear(); _instance = null; } public override void ActiveObj() { } public override void DormancyObj() { } } }