Compare commits

..

No commits in common. "7fa63ef7e3a8078e9b77a6b20a91d8707e17d209" and "5c6c440db63fa87926f38c4c93cf675cc77ab1d9" have entirely different histories.

15 changed files with 280 additions and 513 deletions

View File

@ -10,7 +10,7 @@
<RootNamespace>DCFApixels.DragonECS</RootNamespace>
<Title>DragonECS</Title>
<Version>0.9.16</Version>
<Version>0.9.14</Version>
<Authors>DCFApixels</Authors>
<Description>ECS Framework for Game Engines with C# and .Net Platform</Description>
<Copyright>DCFApixels</Copyright>

View File

@ -8,7 +8,7 @@
"displayName": "DragonECS",
"description": "C# Entity Component System Framework",
"unity": "2020.3",
"version": "0.9.16",
"version": "0.9.14",
"repository": {
"type": "git",
"url": "https://github.com/DCFApixels/DragonECS.git"

View File

@ -30,7 +30,7 @@ namespace DCFApixels.DragonECS
}
public interface IEcsAspect
{
EcsMask Mask { get; }
EcsMask Mask { get; set; }
}
#region IEcsAspectExtensions tmp
@ -143,6 +143,7 @@ namespace DCFApixels.DragonECS
public EcsMask Mask
{
get { return _mask; }
set { }
}
public EcsWorld World
{

View File

@ -34,10 +34,10 @@ namespace DCFApixels.DragonECS
private int _freeNodesCount = 0;
private readonly Dictionary<string, LayerSystemsList> _layerLists = new Dictionary<string, LayerSystemsList>(8);
private readonly StructList<InitDeclaredRunner> _initDeclaredRunners = new StructList<InitDeclaredRunner>(4);
private readonly List<InitDeclaredRunner> _initDeclaredRunners = new List<InitDeclaredRunner>(4);
public readonly LayersMap Layers;
public readonly InitInjectionList Injections;
public readonly Injector.Builder Injector;
public readonly Configurator Configs;
private AddParams _defaultAddParams = new AddParams(BASIC_LAYER, 0, false);
@ -57,12 +57,11 @@ namespace DCFApixels.DragonECS
if (config == null) { config = new ConfigContainer(); }
Configs = new Configurator(config, this);
var injectorBuilder = new Injector.InjectionList();
Injections = new InitInjectionList(injectorBuilder, this);
Injections.AddNode<object>();
Injections.AddNode<EcsWorld>();
Injections.AddNode<EcsAspect>();
Injections.AddNode<EcsPipeline>();
Injector = new Injector.Builder(this);
Injector.AddNode<object>();
Injector.AddNode<EcsWorld>();
Injector.AddNode<EcsAspect>();
Injector.AddNode<EcsPipeline>();
var graph = new DependencyGraph<string>(BASIC_LAYER);
Layers = new LayersMap(graph, this, PRE_BEGIN_LAYER, BEGIN_LAYER, BASIC_LAYER, END_LAYER, POST_END_LAYER);
@ -202,7 +201,7 @@ namespace DCFApixels.DragonECS
_defaultAddParams = oldDefaultAddParams;
}
Injections.Inject(module);
Injector.Inject(module);
return this;
}
#endregion
@ -235,21 +234,22 @@ namespace DCFApixels.DragonECS
}
private void MergeWith(Builder other)
{
Injections.MergeWith(other.Injections);
foreach (var declaredRunners in other._initDeclaredRunners)
{
_initDeclaredRunners.Add(declaredRunners);
}
foreach (var config in other.Configs.Instance.GetAllConfigs())
{
Configs.Instance.Set(config.Key, config.Value);
}
Layers.MergeWith(other.Layers);
foreach (ref readonly SystemNode otherRecord in new LinkedListCountIterator<SystemNode>(_systemNodes, _systemNodesCount, _startIndex))
{
AddNode_Internal(otherRecord.system, otherRecord.layerName, otherRecord.sortOrder, otherRecord.isUnique);
}
throw new NotImplementedException();
//Injector.Add(other.Injector);
//foreach (var declaredRunners in other._initDeclaredRunners)
//{
// _initDeclaredRunners.Add(declaredRunners);
//}
//foreach (var config in other.Configs.Instance.GetAllConfigs())
//{
// Configs.Instance.Set(config.Key, config.Value);
//}
//Layers.MergeWith(other.Layers);
//
//foreach (ref readonly SystemNode otherRecord in new LinkedListCountIterator<SystemNode>(_systemNodes, _systemNodesCount, _startIndex))
//{
// AddNode_Internal(otherRecord.system, otherRecord.layerName, otherRecord.sortOrder, otherRecord.isUnique);
//}
}
#endregion
@ -368,7 +368,7 @@ namespace DCFApixels.DragonECS
}
}
EcsPipeline pipeline = new EcsPipeline((ReadOnlySpan<IEcsProcess>)allSystems, Configs.Instance.GetContainer(), Injections.Instance);
EcsPipeline pipeline = new EcsPipeline(Configs.Instance.GetContainer(), Injector, allSystems);
foreach (var item in _initDeclaredRunners)
{
item.Declare(pipeline);
@ -394,46 +394,8 @@ namespace DCFApixels.DragonECS
}
#endregion
#region InitInjector
public readonly struct InitInjectionList
{
private readonly Builder _pipelineBuilder;
public readonly Injector.InjectionList Instance;
public InitInjectionList(Injector.InjectionList instance, Builder pipelineBuilder)
{
Instance = instance;
_pipelineBuilder = pipelineBuilder;
}
public Builder AddNode<T>()
{
Instance.AddNode<T>();
return _pipelineBuilder;
}
public Builder Inject<T>(T obj)
{
Instance.Inject(obj);
return _pipelineBuilder;
}
public Builder Extract<T>(ref T obj)
{
Instance.Extract(ref obj);
return _pipelineBuilder;
}
public Builder Merge(Injector.InjectionList other)
{
Instance.MergeWith(other);
return _pipelineBuilder;
}
public Builder MergeWith(InitInjectionList other)
{
Instance.MergeWith(other.Instance);
return _pipelineBuilder;
}
}
#endregion
#region Configurator
public readonly struct Configurator
public class Configurator
{
private readonly IConfigContainerWriter _configs;
private readonly Builder _builder;
@ -602,8 +564,6 @@ namespace DCFApixels.DragonECS
#endregion
#region Obsolete
[Obsolete("Use " + nameof(Injections))]
public readonly InitInjectionList Injector;
[Obsolete("Use LayersMap")]
public class LayerList : LayersMap
{

View File

@ -78,25 +78,11 @@ namespace DCFApixels.DragonECS
#endregion
#region Constructors
public EcsPipeline(ReadOnlySpan<IEcsProcess> systems, IConfigContainer configs = null, Injector.InjectionList injectionList = null) :
this(systems.ToArray(), configs, injectionList)
{ }
public EcsPipeline(IEnumerable<IEcsProcess> systems, IConfigContainer configs = null, Injector.InjectionList injectionList = null) :
this(systems.ToArray(), configs, injectionList)
{ }
private EcsPipeline(IEcsProcess[] systems, IConfigContainer configs, Injector.InjectionList injectionList)
private EcsPipeline(IConfigContainer configs, Injector.Builder injectorBuilder, IEcsProcess[] systems)
{
if (configs == null)
{
configs = new ConfigContainer();
}
if (injectionList == null)
{
injectionList = Injector.InjectionList._Empty_Internal;
}
_configs = configs;
_allSystems = systems;
injectorBuilder.Inject(this);
var members = GetProcess<IEcsPipelineMember>();
for (int i = 0; i < members.Length; i++)
@ -104,8 +90,7 @@ namespace DCFApixels.DragonECS
members[i].Pipeline = this;
}
_injector = new Injector(this);
injectionList.InitInjectTo(_injector, this);
_injector = injectorBuilder.Build(this);
}
~EcsPipeline()
{

View File

@ -114,7 +114,7 @@ namespace DCFApixels.DragonECS
}
public ReadOnlySpan<WorldComponentPoolAbstract> GetAllWorldComponents()
{
return _worldComponentPools.AsReadOnlySpan();
return _worldComponentPools.ToReadOnlySpan();
}
public abstract class WorldComponentPoolAbstract
{

View File

@ -10,7 +10,7 @@ namespace DCFApixels.DragonECS
public static EcsPipeline.Builder Inject<T>(this EcsPipeline.Builder self, T data)
{
if (data == null) { Throw.ArgumentNull(); }
self.Injections.Inject(data);
self.Injector.Inject(data);
if (data is IEcsModule module)
{
self.AddModule(module);

View File

@ -8,8 +8,11 @@ namespace DCFApixels.DragonECS.Core.Internal
{
internal class InjectionBranch
{
private readonly Injector _source;
private readonly Type _type;
private StructList<InjectionNodeBase> _nodes = new StructList<InjectionNodeBase>(4);
private InjectionNodeBase[] _nodes = new InjectionNodeBase[4];
private int _nodesCount = 0;
public Type Type
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -18,15 +21,45 @@ namespace DCFApixels.DragonECS.Core.Internal
public ReadOnlySpan<InjectionNodeBase> Nodes
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _nodes.AsReadOnlySpan(); }
get { return new ReadOnlySpan<InjectionNodeBase>(_nodes, 0, _nodesCount); }
}
public InjectionBranch(Type type)
public InjectionBranch(Injector source, Type type)
{
_source = source;
_type = type;
}
public void Inject(object obj)
{
for (int i = 0; i < _nodesCount; i++)
{
_nodes[i].Inject(obj);
}
if (obj is IInjectionBlock block)
{
block.InjectTo(_source);
}
}
public void AddNode(InjectionNodeBase node)
{
_nodes.Add(node);
if (_nodesCount >= _nodes.Length)
{
Array.Resize(ref _nodes, (_nodes.Length << 1) + 1);
}
_nodes[_nodesCount++] = node;
}
public void Trim()
{
if (_nodesCount <= 0)
{
_nodes = Array.Empty<InjectionNodeBase>();
return;
}
InjectionNodeBase[] newNodes = new InjectionNodeBase[_nodesCount];
for (int i = 0; i < newNodes.Length; i++)
{
newNodes[i] = _nodes[i];
}
}
}
}

View File

@ -60,6 +60,8 @@ namespace DCFApixels.DragonECS.Core.Internal
}
private void ExtractTo_Internal(object target)
{
var type = target.GetType();
var intrfs = type.GetInterfaces();
if (target is IEcsInject<T> intrf)
{
intrf.Inject(_currentInjectedDependency);

View File

@ -11,59 +11,14 @@ namespace DCFApixels.DragonECS
{
public class Injector : IInjector
{
private readonly EcsPipeline _pipeline;
private readonly Dictionary<Type, InjectionBranch> _branches = new Dictionary<Type, InjectionBranch>(32);
private readonly Dictionary<Type, InjectionNodeBase> _nodes = new Dictionary<Type, InjectionNodeBase>(32);
private ReadOnlySpan<InjectionNodeBase> GetNodes(Type type)
{
if(_branches.TryGetValue(type, out InjectionBranch branch))
{
return branch.Nodes;
}
return Array.Empty<InjectionNodeBase>();
}
private EcsPipeline _pipeline;
private Dictionary<Type, InjectionBranch> _branches = new Dictionary<Type, InjectionBranch>(32);
private Dictionary<Type, InjectionNodeBase> _nodes = new Dictionary<Type, InjectionNodeBase>(32);
private bool _isInit = false;
#region InjectionTempHistory
private StructList<object> _injectionTempHistory = new StructList<object>(32);
private int _injectionTempHistoryReadersCount = 0;
private int StartReadHistory_Internal()
{
_injectionTempHistoryReadersCount++;
return _injectionTempHistory.Count;
}
private ReadOnlySpan<object> EndReadHistory_Internal(int startIndex)
{
_injectionTempHistoryReadersCount--;
if(_injectionTempHistoryReadersCount < 0)
{
Throw.OpeningClosingMethodsBalanceError();
}
var result = _injectionTempHistory.AsReadOnlySpan().Slice(startIndex);
if (_injectionTempHistoryReadersCount == 0)
{
_injectionTempHistory.Recreate();
}
return result;
}
public readonly struct InjectionHistorySpanReader
{
private readonly Injector _injector;
private readonly int _startIndex;
public InjectionHistorySpanReader(Injector injector)
{
_injector = injector;
_startIndex = _injector.StartReadHistory_Internal();
}
public ReadOnlySpan<object> StopReadAndGetHistorySpan()
{
return _injector.EndReadHistory_Internal(_startIndex);
}
}
public InjectionHistorySpanReader StartReadHistory()
{
return new InjectionHistorySpanReader(this);
}
#endregion
#if DEBUG
private HashSet<Type> _requiredInjectionTypes = new HashSet<Type>();
#endif
public EcsPipeline Pipelie
{
@ -71,14 +26,12 @@ namespace DCFApixels.DragonECS
get { return _pipeline; }
}
public Injector(EcsPipeline pipeline)
{
_pipeline = pipeline;
}
private Injector() { }
#region Inject/Extract/AddNode
public void Inject<T>(T obj)
{
object raw = obj;
Type tType = typeof(T);
Type objType = obj.GetType();
if (_branches.TryGetValue(objType, out InjectionBranch branch) == false)
@ -87,32 +40,31 @@ namespace DCFApixels.DragonECS
{
InitNode(new InjectionNode<T>());
}
bool hasObjTypeNode = _nodes.ContainsKey(objType);
if (hasObjTypeNode == false && obj is IInjectionUnit unit)
bool hasNode = _nodes.ContainsKey(objType);
if (hasNode == false && obj is IInjectionUnit unit)
{
unit.InitInjectionNode(new InjectionGraph(this));
hasObjTypeNode = _nodes.ContainsKey(objType);
hasNode = _nodes.ContainsKey(objType);
}
branch = new InjectionBranch(objType);
branch = new InjectionBranch(this, objType);
InitBranch(branch);
}
var branchNodes = branch.Nodes;
for (int i = 0; i < branchNodes.Length; i++)
#if DEBUG
foreach (var requiredInjectionType in _requiredInjectionTypes)
{
branchNodes[i].Inject(obj);
}
if (_injectionTempHistoryReadersCount > 0)
if (requiredInjectionType.IsAssignableFrom(objType))
{
_injectionTempHistory.Add(obj);
}
if (obj is IInjectionBlock block)
if (_nodes.ContainsKey(requiredInjectionType) == false)
{
block.InjectTo(this);
throw new InjectionException($"A systems in the pipeline implements IEcsInject<{requiredInjectionType.Name}> interface, but no suitable injection node was found in the Injector. To create a node, use Injector.AddNode<{requiredInjectionType.Name}>() or implement the IInjectionUnit interface for type {objType.Name}.");
}
}
}
#endif
}
branch.Inject(raw);
}
public void ExtractAllTo(object target)
{
if (target is IEcsInjectProcess == false) { return; }
@ -134,11 +86,12 @@ namespace DCFApixels.DragonECS
}
throw new InjectionException($"The injection graph is missing a node for {type.Name} type. To create a node, use the Injector.AddNode<{type.Name}>() method directly in the injector or in the implementation of the IInjectionUnit for {type.Name}.");
}
public bool AddNode<T>()
public void AddNode<T>()
{
if (_nodes.ContainsKey(typeof(T)) == false)
{
if (_nodes.ContainsKey(typeof(T))) { return false; }
InitNode(new InjectionNode<T>());
return true;
}
}
#endregion
@ -173,121 +126,71 @@ namespace DCFApixels.DragonECS
}
}
}
private bool IsCanInstantiated(Type type)
{
return !type.IsAbstract && !type.IsInterface;
}
#endregion
#region InjectionList
public class InjectionList : IInjector
#region Build
private void Init(EcsPipeline pipeline)
{
public static readonly InjectionList _Empty_Internal = new InjectionList();
if (_isInit) { Throw.Exception("Already initialized"); }
private StructList<InjectionBase> _injections = new StructList<InjectionBase>(32);
private StructList<NodeBase> _nodes = new StructList<NodeBase>(32);
private EcsWorld _monoWorld;
public void AddNode<T>()
_pipeline = pipeline;
foreach (var pair in _nodes)
{
_nodes.Add(new Node<T>());
pair.Value.Init(pipeline);
}
public void Inject<T>(T obj)
{
FindMonoWorld(obj);
_injections.Add(new Injection<T>(obj));
}
public void Extract<T>(ref T obj) // TODO проверить
{
Type type = typeof(T);
for (int i = _injections.Count - 1; i >= 0; i--)
{
var item = _injections[i];
if (type.IsAssignableFrom(item.Type))
{
obj = (T)item.Raw;
return;
}
}
Throw.UndefinedException();
}
public void MergeWith(InjectionList other)
{
foreach (var item in other._injections)
{
FindMonoWorld(item);
_injections.Add(item);
}
foreach (var item in other._nodes)
{
_nodes.Add(item);
}
}
public void InitInjectTo(Injector injector, EcsPipeline pipeline)
{
_isInit = true;
#if DEBUG
HashSet<Type> requiredInjectionTypes = new HashSet<Type>();
var systems = pipeline.AllSystems;
var systems = _pipeline.AllSystems;
var injectType = typeof(IEcsInject<>);
foreach (var system in systems)
{
var type = system.GetType();
foreach (var requiredInjectionType in type.GetInterfaces().Where(o => o.IsGenericType && o.GetGenericTypeDefinition() == injectType).Select(o => o.GenericTypeArguments[0]))
{
requiredInjectionTypes.Add(requiredInjectionType);
}
}
var reader = injector.StartReadHistory();
#endif
var initInjectionCallbacks = pipeline.GetProcess<IOnInitInjectionComplete>();
foreach (var system in initInjectionCallbacks)
{
system.OnBeforeInitInjection();
}
injector.Inject(pipeline);
injector.AddNode<object>();
InjectTo(injector, pipeline);
foreach (var system in initInjectionCallbacks)
{
system.OnInitInjectionComplete();
}
#if DEBUG
var injectionHistory = reader.StopReadAndGetHistorySpan();
foreach (var injection in injectionHistory)
{
foreach (var node in injector.GetNodes(injection.GetType()))
{
requiredInjectionTypes.Remove(node.Type);
}
}
if (requiredInjectionTypes.Count > 0)
{
foreach (var requiredInjectionType in requiredInjectionTypes)
{
throw new InjectionException($"A systems in the pipeline implements IEcsInject<{requiredInjectionType.Name}> interface, but no suitable injection node was found in the Injector. To create a node, use Injector.AddNode<{requiredInjectionType.Name}>() or implement the IInjectionUnit interface for the type being injected.");
_requiredInjectionTypes.Add(requiredInjectionType);
}
}
#endif
}
public void InjectTo(Injector injector, EcsPipeline pipeline)
private bool TryDeclare<T>()
{
var monoWorldProcess = pipeline.GetProcess<IMonoWorldInject>(); // TODO Проверить IMonoWorldInject
foreach (var monoWorldSystem in monoWorldProcess)
Type type = typeof(T);
if (_nodes.ContainsKey(type))
{
monoWorldSystem.World = _monoWorld;
return false;
}
foreach (var item in _nodes)
InitNode(new InjectionNode<T>());
#if !REFLECTION_DISABLED
if (IsCanInstantiated(type))
#endif
{
item.AddNodeTo(injector);
}
foreach (var item in _injections)
{
item.InjectTo(injector);
InitBranch(new InjectionBranch(this, type));
}
return true;
}
private void FindMonoWorld(object obj)
public class Builder : IInjector
{
private EcsPipeline.Builder _source;
private Injector _instance;
private List<InitInjectBase> _initInjections = new List<InitInjectBase>(16);
private EcsWorld _monoWorld;
internal Builder(EcsPipeline.Builder source)
{
_source = source;
_instance = new Injector();
}
public EcsPipeline.Builder AddNode<T>()
{
_instance.TryDeclare<T>();
return _source;
}
public EcsPipeline.Builder Inject<T>(T obj)
{
if (obj is EcsWorld objWorld)
{
@ -313,6 +216,55 @@ namespace DCFApixels.DragonECS
}
}
}
_initInjections.Add(new InitInject<T>(obj));
return _source;
}
public EcsPipeline.Builder Extract<T>(ref T obj) // TODO проверить
{
Type type = typeof(T);
for (int i = _initInjections.Count - 1; i >= 0; i--)
{
var item = _initInjections[i];
if (type.IsAssignableFrom(item.Type))
{
obj = (T)item.Raw;
return _source;
}
}
Throw.UndefinedException();
return default;
}
public Injector Build(EcsPipeline pipeline)
{
var monoWorldProcess = pipeline.GetProcess<IMonoWorldInject>(); // TODO Проверить IMonoWorldInject
foreach (var monoWorldSystem in monoWorldProcess)
{
monoWorldSystem.World = _monoWorld;
}
var initInjectionCallbacks = pipeline.GetProcess<IOnInitInjectionComplete>();
foreach (var system in initInjectionCallbacks)
{
system.OnBeforeInitInjection();
}
_instance.Init(pipeline);
foreach (var item in _initInjections)
{
item.InjectTo(_instance);
}
foreach (var system in initInjectionCallbacks)
{
system.OnInitInjectionComplete();
}
return _instance;
}
public void Add(Builder other)
{
foreach (var item in other._initInjections)
{
_initInjections.Add(item);
}
}
void IInjector.Inject<T>(T obj) { Inject(obj); }
@ -323,38 +275,24 @@ namespace DCFApixels.DragonECS
return result;
}
private abstract class NodeBase
{
public abstract Type Type { get; }
public abstract void AddNodeTo(Injector instance);
}
private sealed class Node<T> : NodeBase
{
public override Type Type { get { return typeof(T); } }
public override void AddNodeTo(Injector instance)
{
instance.AddNode<T>();
}
}
private abstract class InjectionBase
private abstract class InitInjectBase
{
public abstract Type Type { get; }
public abstract object Raw { get; }
public abstract void InjectTo(Injector instance);
}
private sealed class Injection<T> : InjectionBase
private sealed class InitInject<T> : InitInjectBase
{
private T _injectedData;
public override Type Type { get { return typeof(T); } }
public override object Raw { get { return _injectedData; } }
public Injection(T injectedData)
public InitInject(T injectedData)
{
_injectedData = injectedData;
}
public override void InjectTo(Injector instance)
{
instance.Inject(_injectedData);
instance.Inject<T>(_injectedData);
}
}
}

View File

@ -31,9 +31,5 @@ namespace DCFApixels.DragonECS.Core.Internal
{
return self.GetCustomAttribute<T>(inherit) != null;
}
public static bool IsCanInstantiated(this Type type)
{
return !type.IsAbstract && !type.IsInterface;
}
}
}

View File

@ -12,12 +12,8 @@ namespace DCFApixels.DragonECS.Core.Internal
[DebuggerDisplay("Count: {Count}")]
internal struct StructList<T>
{
internal readonly static bool _IsManaged = RuntimeHelpers.IsReferenceOrContainsReferences<T>();
internal T[] _items;
internal int _count;
#region Properties
public int Count
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -34,11 +30,6 @@ namespace DCFApixels.DragonECS.Core.Internal
Array.Resize(ref _items, value);
}
}
public bool IsNull
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _items == null; }
}
public T this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -58,16 +49,13 @@ namespace DCFApixels.DragonECS.Core.Internal
_items[index] = value;
}
}
#endregion
#region Constructors
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public StructList(int capacity)
{
_items = new T[ArrayUtility.NextPow2(capacity)];
_count = 0;
}
#endregion
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Add(T item)
@ -76,11 +64,6 @@ namespace DCFApixels.DragonECS.Core.Internal
{
Array.Resize(ref _items, _items.Length << 1);
}
AddFixed(item);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddFixed(T item)
{
_items[_count++] = item;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -89,11 +72,6 @@ namespace DCFApixels.DragonECS.Core.Internal
return Array.IndexOf(_items, item, 0, _count);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Contains(T item)
{
return _count != 0 && IndexOf(item) >= 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SwapAt(int idnex1, int idnex2)
{
T tmp = _items[idnex1];
@ -115,11 +93,8 @@ namespace DCFApixels.DragonECS.Core.Internal
if (index < 0 || index >= _count) { Throw.ArgumentOutOfRange(); }
#endif
_items[index] = _items[--_count];
if (_IsManaged)
{
_items[_count] = default;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void RemoveAtWithOrder(int index)
{
@ -153,56 +128,6 @@ namespace DCFApixels.DragonECS.Core.Internal
}
return false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T Peek()
{
#if DEBUG
if (_count <= 0) { Throw.EmptyStack(); }
#endif
return _items[_count - 1];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryPeek(out T result)
{
if (_count <= 0)
{
result = default;
return false;
}
result = _items[_count - 1];
return true;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T Pop()
{
#if DEBUG
if (_count <= 0) { Throw.EmptyStack(); }
#endif
T result = _items[--_count];
if (_IsManaged)
{
_items[_count] = default;
}
return result;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryPop(out T result)
{
if (_count <= 0)
{
result = default;
return false;
}
result = _items[--_count];
if (_IsManaged)
{
_items[_count] = default;
}
return true;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FastClear()
{
@ -211,31 +136,19 @@ namespace DCFApixels.DragonECS.Core.Internal
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Clear()
{
if (_IsManaged)
for (int i = 0; i < _count; i++)
{
Array.Clear(_items, 0, _count);
_items[i] = default;
}
_count = 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Recreate()
{
_items = new T[_items.Length];
_count = 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Recreate(int newSize)
{
_items = new T[ArrayUtility.NextPow2(newSize)];
_count = 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ReadOnlySpan<T>.Enumerator GetEnumerator()
{
return new ReadOnlySpan<T>(_items, 0, _count).GetEnumerator();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ReadOnlySpan<T> AsReadOnlySpan()
public ReadOnlySpan<T> ToReadOnlySpan()
{
return new ReadOnlySpan<T>(_items, 0, _count);
}
@ -244,13 +157,5 @@ namespace DCFApixels.DragonECS.Core.Internal
{
return _items.Take(_count);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T[] ToArray()
{
if (_count <= 0) { return Array.Empty<T>(); }
T[] result = new T[_count];
Array.Copy(_items, result, _count);
return result;
}
}
}

View File

@ -141,17 +141,8 @@ namespace DCFApixels.DragonECS.Core.Internal
{
throw new ArgumentException("The groups belong to different worlds.");
}
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void ArgumentDifferentWorldsException()
{
throw new ArgumentException("The groups belong to different worlds.");
}
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void EmptyStack()
{
throw new InvalidOperationException("Invalid Operation Empty Stack.");
}
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void ArgumentNull()
{

View File

@ -19,7 +19,7 @@ namespace DCFApixels.DragonECS.Core
private readonly IDependencyGraph<string> _graph;
private readonly EcsPipeline.Builder _pipelineBuilder;
//private readonly string _preBeginLayer;
private readonly string _preBeginLayer;
#region Properties
public EcsPipeline.Builder Back
@ -209,21 +209,21 @@ namespace DCFApixels.DragonECS.Core
{
var enumerator = other.GetEnumerator();
string prev = null;
//if (_preBeginLayer != null)
//{
// while (enumerator.MoveNext())
// {
// var layer = enumerator.Current;
// if (layer == _preBeginLayer) { break; }
//
// Add(layer);
// if (prev != null)
// {
// Move(prev).Before(layer);
// }
// prev = layer;
// }
//}
if (_preBeginLayer != null)
{
while (enumerator.MoveNext())
{
var layer = enumerator.Current;
if (layer == _preBeginLayer) { break; }
Add(layer);
if (prev != null)
{
Move(prev).Before(layer);
}
prev = layer;
}
}
while (enumerator.MoveNext())
{
var layer = enumerator.Current;

View File

@ -136,7 +136,7 @@ namespace DCFApixels.DragonECS
}
#endregion
#region Unpacking Try
#region Unpacking
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryGetID(out int id)
{
@ -156,90 +156,6 @@ namespace DCFApixels.DragonECS
return IsAlive;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryUnpack(out int id)
{
id = _id;
return IsAlive;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryUnpack(out int id, out EcsWorld world)
{
world = GetWorld_Internal();
id = _id;
return IsAlive;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryUnpack(out int id, out short gen, out EcsWorld world)
{
world = GetWorld_Internal();
gen = _gen;
id = _id;
return IsAlive;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryUnpack(out int id, out short worldID)
{
worldID = _world;
id = _id;
return IsAlive;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryUnpack(out int id, out short gen, out short worldID)
{
worldID = _world;
gen = _gen;
id = _id;
return IsAlive;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryUnpack(EcsWorld world, out int id)
{
if (world.ID != _world) { Throw.ArgumentDifferentWorldsException(); }
id = _id;
return world.IsAlive(_id, _gen);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryUnpack(EcsWorld world, out int id, out short gen)
{
if (world.ID != _world) { Throw.ArgumentDifferentWorldsException(); }
gen = _gen;
id = _id;
return world.IsAlive(_id, _gen);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryUnpack(EcsMask mask, out int id)
{
if (mask.WorldID != _world) { Throw.ArgumentDifferentWorldsException(); }
id = _id;
return mask.World.IsAlive(_id, _gen) && mask.World.IsMatchesMask(mask, _id);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryUnpack(EcsMask mask, out int id, out short gen)
{
if (mask.WorldID != _world) { Throw.ArgumentDifferentWorldsException(); }
gen = _gen;
id = _id;
return mask.World.IsAlive(_id, _gen) && mask.World.IsMatchesMask(mask, _id);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryUnpack(EcsAspect aspect, out int id)
{
if (aspect.World.ID != _world) { Throw.ArgumentDifferentWorldsException(); }
id = _id;
return aspect.World.IsAlive(_id, _gen) && aspect.IsMatches(_id);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryUnpack(EcsAspect aspect, out int id, out short gen)
{
if (aspect.World.ID != _world) { Throw.ArgumentDifferentWorldsException(); }
gen = _gen;
id = _id;
return aspect.World.IsAlive(_id, _gen) && aspect.IsMatches(_id);
}
#endregion
#region Unpacking/Deconstruct
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Unpack(out int id)
{
@ -322,13 +238,46 @@ namespace DCFApixels.DragonECS
gen = _gen;
id = _id;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Deconstruct(out int id, out short gen, out short worldID) { Unpack(out id, out gen, out worldID); }
public bool TryUnpack(out int id)
{
id = _id;
return IsAlive;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Deconstruct(out int id, out EcsWorld world) { Unpack(out id, out world); }
public bool TryUnpack(out int id, out EcsWorld world)
{
world = GetWorld_Internal();
id = _id;
return IsAlive;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryUnpack(out int id, out short gen, out EcsWorld world)
{
world = GetWorld_Internal();
gen = _gen;
id = _id;
return IsAlive;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryUnpack(out int id, out short worldID)
{
worldID = _world;
id = _id;
return IsAlive;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryUnpack(out int id, out short gen, out short worldID)
{
worldID = _world;
gen = _gen;
id = _id;
return IsAlive;
}
#endregion
#region Unpacking Unchecked
#region Unpacking
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int GetIDUnchecked()
{
@ -403,6 +352,13 @@ namespace DCFApixels.DragonECS
public static explicit operator int(entlong a) { return a.ID; }
#endregion
#region Deconstruct
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Deconstruct(out int id, out short gen, out short worldID) { Unpack(out id, out gen, out worldID); }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Deconstruct(out int id, out EcsWorld world) { Unpack(out id, out world); }
#endregion
#region Other
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private EcsWorld GetWorld_Internal()