DragonECS/src/EcsPipeline.cs

359 lines
14 KiB
C#
Raw Normal View History

2023-05-26 04:31:31 +08:00
using DCFApixels.DragonECS.Internal;
using DCFApixels.DragonECS.RunnersCore;
2023-04-23 17:55:01 +08:00
using System;
using System.Collections;
2023-03-26 11:19:03 +08:00
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
2023-03-26 11:19:03 +08:00
using System.Runtime.CompilerServices;
namespace DCFApixels.DragonECS
{
2023-03-30 05:32:43 +08:00
public sealed class EcsPipeline
2023-03-26 11:19:03 +08:00
{
private IEcsSystem[] _allSystems;
private Dictionary<Type, IEcsRunner> _runners;
private IEcsRunProcess _runRunnerCache;
2023-03-26 11:19:03 +08:00
private ReadOnlyCollection<IEcsSystem> _allSystemsSealed;
private ReadOnlyDictionary<Type, IEcsRunner> _allRunnersSealed;
private bool _isInit;
2023-03-26 11:19:03 +08:00
private bool _isDestoryed;
2023-04-01 20:45:37 +08:00
private bool _isEmptyDummy;
2023-03-26 11:19:03 +08:00
#region Properties
public ReadOnlyCollection<IEcsSystem> AllSystems => _allSystemsSealed;
public ReadOnlyDictionary<Type, IEcsRunner> AllRunners => _allRunnersSealed;
2023-04-01 20:45:37 +08:00
public bool IsInit => _isInit;
2023-03-26 11:19:03 +08:00
public bool IsDestoryed => _isDestoryed;
#endregion
#region Constructors
2023-03-30 05:32:43 +08:00
private EcsPipeline(IEcsSystem[] systems)
2023-03-26 11:19:03 +08:00
{
_allSystems = systems;
_runners = new Dictionary<Type, IEcsRunner>();
_allSystemsSealed = new ReadOnlyCollection<IEcsSystem>(_allSystems);
_allRunnersSealed = new ReadOnlyDictionary<Type, IEcsRunner>(_runners);
_isInit = false;
2023-04-01 20:45:37 +08:00
_isEmptyDummy = false;
2023-03-26 11:19:03 +08:00
_isDestoryed = false;
}
#endregion
#region Runners
public T GetRunner<T>() where T : IEcsSystem
{
Type type = typeof(T);
if (_runners.TryGetValue(type, out IEcsRunner result))
return (T)result;
2023-03-29 15:09:00 +08:00
result = (IEcsRunner)EcsRunner<T>.Instantiate(this);
2023-03-26 11:19:03 +08:00
_runners.Add(type, result);
return (T)result;
}
2023-03-30 05:32:43 +08:00
internal void OnRunnerDestroy(IEcsRunner runner)
{
_runners.Remove(runner.Interface);
}
2023-03-26 11:19:03 +08:00
#endregion
#region LifeCycle
public void Init()
{
2023-04-01 20:45:37 +08:00
if (_isEmptyDummy)
return;
if (_isInit == true)
{
2023-03-30 05:32:43 +08:00
EcsDebug.Print("[Warning]", $"This {nameof(EcsPipeline)} has already been initialized");
return;
}
_isInit = true;
2023-03-30 11:17:17 +08:00
var ecsPipelineInjectRunner = GetRunner<IEcsInject<EcsPipeline>>();
ecsPipelineInjectRunner.Inject(this);
EcsRunner.Destroy(ecsPipelineInjectRunner);
var preInitRunner = GetRunner<IEcsPreInitProcess>();
2023-03-30 11:17:17 +08:00
preInitRunner.PreInit(this);
EcsRunner.Destroy(preInitRunner);
var initRunner = GetRunner<IEcsInitProcess>();
2023-03-30 11:17:17 +08:00
initRunner.Init(this);
EcsRunner.Destroy(initRunner);
_runRunnerCache = GetRunner<IEcsRunProcess>();
}
2023-03-26 11:19:03 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Run()
{
#if (DEBUG && !DISABLE_DEBUG) || !DRAGONECS_NO_SANITIZE_CHECKS
CheckBeforeInitForMethod(nameof(Run));
2023-03-26 11:19:03 +08:00
CheckAfterDestroyForMethod(nameof(Run));
#endif
_runRunnerCache.Run(this);
}
public void Destroy()
{
2023-04-01 20:45:37 +08:00
if (_isEmptyDummy)
return;
#if (DEBUG && !DISABLE_DEBUG) || !DRAGONECS_NO_SANITIZE_CHECKS
CheckBeforeInitForMethod(nameof(Run));
2023-03-26 11:19:03 +08:00
#endif
if (_isDestoryed == true)
{
2023-03-30 05:32:43 +08:00
EcsDebug.Print("[Warning]", $"This {nameof(EcsPipeline)} has already been destroyed");
return;
}
2023-03-26 11:19:03 +08:00
_isDestoryed = true;
GetRunner<IEcsDestroyProcess>().Destroy(this);
2023-03-26 11:19:03 +08:00
}
#endregion
#region StateChecks
#if (DEBUG && !DISABLE_DEBUG) || !DRAGONECS_NO_SANITIZE_CHECKS
private void CheckBeforeInitForMethod(string methodName)
{
if (!_isInit)
2023-03-30 05:32:43 +08:00
throw new MethodAccessException($"It is forbidden to call {methodName}, before initialization {nameof(EcsPipeline)}");
}
private void CheckAfterInitForMethod(string methodName)
{
if (_isInit)
2023-03-30 05:32:43 +08:00
throw new MethodAccessException($"It is forbidden to call {methodName}, after initialization {nameof(EcsPipeline)}");
}
2023-03-26 11:19:03 +08:00
private void CheckAfterDestroyForMethod(string methodName)
{
if (_isDestoryed)
2023-03-30 05:32:43 +08:00
throw new MethodAccessException($"It is forbidden to call {methodName}, after destroying {nameof(EcsPipeline)}");
2023-03-26 11:19:03 +08:00
}
#endif
#endregion
#region Builder
public static Builder New()
{
return new Builder();
}
public class Builder
{
private const int KEYS_CAPACITY = 4;
2023-03-30 05:32:43 +08:00
private HashSet<Type> _uniqueTypes;
private readonly Dictionary<string, List<IEcsSystem>> _systems;
private readonly string _basicLayer;
public readonly LayerList Layers;
2023-03-26 11:19:03 +08:00
public Builder()
{
_basicLayer = EcsConsts.BASIC_LAYER;
Layers = new LayerList(this, _basicLayer);
Layers.Insert(EcsConsts.BASIC_LAYER, EcsConsts.PRE_BEGIN_LAYER, EcsConsts.BEGIN_LAYER);
Layers.InsertAfter(EcsConsts.BASIC_LAYER, EcsConsts.END_LAYER, EcsConsts.POST_END_LAYER);
_uniqueTypes = new HashSet<Type>();
_systems = new Dictionary<string, List<IEcsSystem>>(KEYS_CAPACITY);
2023-03-26 11:19:03 +08:00
}
public Builder Add(IEcsSystem system, string layerName = null)
2023-03-30 05:32:43 +08:00
{
AddInternal(system, layerName, false);
2023-03-30 05:32:43 +08:00
return this;
}
public Builder AddUnique(IEcsSystem system, string layerName = null)
2023-03-30 05:32:43 +08:00
{
AddInternal(system, layerName, true);
2023-03-30 05:32:43 +08:00
return this;
}
2023-05-23 01:47:28 +08:00
public Builder Remove<TSystem>()
{
_uniqueTypes.Remove(typeof(TSystem));
foreach (var list in _systems.Values)
list.RemoveAll(o => o is TSystem);
return this;
}
private void AddInternal(IEcsSystem system, string layerName, bool isUnique)
2023-03-26 11:19:03 +08:00
{
if (layerName == null) layerName = _basicLayer;
2023-03-26 11:19:03 +08:00
List<IEcsSystem> list;
if (!_systems.TryGetValue(layerName, out list))
2023-03-26 11:19:03 +08:00
{
2023-05-26 04:31:31 +08:00
list = new List<IEcsSystem> { new SystemsBlockMarkerSystem(layerName.ToString()) };
_systems.Add(layerName, list);
2023-03-26 11:19:03 +08:00
}
2023-03-30 05:32:43 +08:00
if ((_uniqueTypes.Add(system.GetType()) == false && isUnique))
return;
2023-03-26 11:19:03 +08:00
list.Add(system);
if (system is IEcsModule module)//если система одновременно явялется и системой и модулем то за один Add будет вызван Add и AddModule
AddModule(module);
2023-03-26 11:19:03 +08:00
}
public Builder AddModule(IEcsModule module)
2023-03-26 11:19:03 +08:00
{
module.ImportSystems(this);
return this;
}
public EcsPipeline Build()
2023-03-26 11:19:03 +08:00
{
Add(new DeleteEmptyEntitesSystem(), EcsConsts.POST_END_LAYER);
List<IEcsSystem> result = new List<IEcsSystem>(32);
List<IEcsSystem> basicBlockList = _systems[_basicLayer];
foreach (var item in _systems)
{
if (!Layers.Has(item.Key))
basicBlockList.AddRange(item.Value);
}
foreach (var item in Layers)
{
if(_systems.TryGetValue(item, out var list))
result.AddRange(list);
}
return new EcsPipeline(result.ToArray());
}
2023-03-26 11:19:03 +08:00
public class LayerList : IEnumerable<string>
2023-03-26 11:19:03 +08:00
{
private const string ADD_LAYER = nameof(ADD_LAYER); // автоматический слой нужный только для метода Add
private Builder _source;
private List<string> _layers;
private string _basicLayerName;
public LayerList(Builder source, string basicLayerName)
2023-03-26 11:19:03 +08:00
{
_source = source;
_layers = new List<string>(16) { basicLayerName, ADD_LAYER };
_basicLayerName = basicLayerName;
2023-03-26 11:19:03 +08:00
}
public Builder Add(string newLayer) => Insert(ADD_LAYER, newLayer);
public Builder Insert(string targetLayer, string newLayer)
{
if (Has(newLayer)) return _source;
2023-03-26 11:19:03 +08:00
int index = _layers.IndexOf(targetLayer);
if (index < 0)
throw new KeyNotFoundException($"Layer {targetLayer} not found");
_layers.Insert(index, newLayer);
return _source;
}
public Builder InsertAfter(string targetLayer, string newLayer)
{
if (Has(newLayer)) return _source;
2023-03-26 11:19:03 +08:00
if (targetLayer == _basicLayerName) // нужно чтобы метод Add работал правильно. _basicLayerName и ADD_LAYER считается одним слоем, поэтому Before = _basicLayerName After = ADD_LAYER
targetLayer = ADD_LAYER;
2023-03-26 11:19:03 +08:00
int index = _layers.IndexOf(targetLayer);
if (index < 0)
throw new KeyNotFoundException($"Layer {targetLayer} not found");
if (++index >= _layers.Count)
_layers.Add(newLayer);
else
_layers.Insert(index, newLayer);
return _source;
}
public Builder Move(string targetLayer, string movingLayer)
2023-03-26 11:19:03 +08:00
{
_layers.Remove(movingLayer);
return Insert(targetLayer, movingLayer);
2023-03-26 11:19:03 +08:00
}
public Builder MoveAfter(string targetLayer, string movingLayer)
2023-03-26 11:19:03 +08:00
{
if (targetLayer == _basicLayerName) // нужно чтобы метод Add работал правильно. _basicLayerName и ADD_LAYER считается одним слоем, поэтому Before = _basicLayerName After = ADD_LAYER
targetLayer = ADD_LAYER;
_layers.Remove(movingLayer);
return InsertAfter(targetLayer, movingLayer);
2023-03-26 11:19:03 +08:00
}
public Builder Add(params string[] newLayers) => Insert(ADD_LAYER, newLayers);
public Builder Insert(string targetLayer, params string[] newLayers)
{
int index = _layers.IndexOf(targetLayer);
if (index < 0)
throw new KeyNotFoundException($"Layer {targetLayer} not found");
_layers.InsertRange(index, newLayers.Where(o => !Has(o)));
return _source;
}
public Builder InsertAfter(string targetLayer, params string[] newLayers)
{
int index = _layers.IndexOf(targetLayer);
if (index < 0)
throw new KeyNotFoundException($"Layer {targetLayer} not found");
if (targetLayer == _basicLayerName) // нужно чтобы метод Add работал правильно. _basicLayerName и ADD_LAYER считается одним слоем, поэтому Before = _basicLayerName After = ADD_LAYER
targetLayer = ADD_LAYER;
if (++index >= _layers.Count)
_layers.AddRange(newLayers.Where(o => !Has(o)));
else
_layers.InsertRange(index, newLayers.Where(o => !Has(o)));
return _source;
}
public Builder Move(string targetLayer, params string[] movingLayers)
{
foreach (var movingLayer in movingLayers)
_layers.Remove(movingLayer);
return Insert(targetLayer, movingLayers);
}
public Builder MoveAfter(string targetLayer, params string[] movingLayers)
{
if (targetLayer == _basicLayerName) // нужно чтобы метод Add работал правильно. _basicLayerName и ADD_LAYER считается одним слоем, поэтому Before = _basicLayerName After = ADD_LAYER
targetLayer = ADD_LAYER;
foreach (var movingLayer in movingLayers)
_layers.Remove(movingLayer);
return InsertAfter(targetLayer, movingLayers);
}
public bool Has(string layer) => _layers.Contains(layer);
public List<string>.Enumerator GetEnumerator() => _layers.GetEnumerator();
IEnumerator<string> IEnumerable<string>.GetEnumerator() => _layers.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => _layers.GetEnumerator();
2023-03-26 11:19:03 +08:00
}
}
#endregion
}
public interface IEcsModule
{
2023-05-26 00:24:38 +08:00
void ImportSystems(EcsPipeline.Builder b);
2023-03-26 11:19:03 +08:00
}
2023-03-30 05:32:43 +08:00
#region Extensions
public static class EcsPipelineExtensions
2023-03-26 11:19:03 +08:00
{
2023-05-26 00:24:38 +08:00
public static bool IsNullOrDestroyed(this EcsPipeline self) => self == null || self.IsDestoryed;
public static EcsPipeline.Builder Add(this EcsPipeline.Builder self, IEnumerable<IEcsSystem> range, string layerName = null)
2023-03-26 11:19:03 +08:00
{
2023-05-26 00:24:38 +08:00
foreach (var item in range) self.Add(item, layerName);
2023-03-26 11:19:03 +08:00
return self;
}
public static EcsPipeline.Builder AddUnique(this EcsPipeline.Builder self, IEnumerable<IEcsSystem> range, string layerName = null)
2023-03-30 05:32:43 +08:00
{
2023-05-26 00:24:38 +08:00
foreach (var item in range) self.AddUnique(item, layerName);
2023-03-30 05:32:43 +08:00
return self;
}
public static EcsPipeline BuildAndInit(this EcsPipeline.Builder self)
{
2023-03-30 05:32:43 +08:00
EcsPipeline result = self.Build();
result.Init();
return result;
}
2023-03-26 11:19:03 +08:00
}
2023-03-30 05:32:43 +08:00
#endregion
2023-03-26 11:19:03 +08:00
}