From e84a26af02b7834a74c71003469a3a2a25c0edad Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Sat, 2 Mar 2024 21:45:09 +0800 Subject: [PATCH] refacotring --- src/Collections/EcsGroup.cs | 2 +- src/EcsAspect.cs | 51 ++++++++++++++------------ src/EcsMask.cs | 73 +++++++++++++++++++------------------ src/Injections/Injector.cs | 3 +- 4 files changed, 66 insertions(+), 63 deletions(-) diff --git a/src/Collections/EcsGroup.cs b/src/Collections/EcsGroup.cs index ac6d673..356953d 100644 --- a/src/Collections/EcsGroup.cs +++ b/src/Collections/EcsGroup.cs @@ -508,7 +508,7 @@ namespace DCFApixels.DragonECS #region Inverse public void Inverse() { - if(_count == 0) + if (_count == 0) { foreach (var entityID in _source.Entities) { diff --git a/src/EcsAspect.cs b/src/EcsAspect.cs index 4d1162d..4e013cd 100644 --- a/src/EcsAspect.cs +++ b/src/EcsAspect.cs @@ -10,7 +10,7 @@ namespace DCFApixels.DragonECS { internal EcsWorld _source; internal EcsMask _mask; - private bool _isInit; + private bool _isInit = false; private UnsafeArray _sortIncBuffer; private UnsafeArray _sortExcBuffer; @@ -18,9 +18,18 @@ namespace DCFApixels.DragonECS private UnsafeArray _sortExcChunckBuffer; #region Properties - public EcsMask Mask => _mask; - public EcsWorld World => _source; - public bool IsInit => _isInit; + public EcsMask Mask + { + get { return _mask; } + } + public EcsWorld World + { + get { return _source; } + } + public bool IsInit + { + get { return _isInit; } + } #endregion #region Methods @@ -148,7 +157,7 @@ namespace DCFApixels.DragonECS } #endregion - #region Destructor + #region Finalizator unsafe ~EcsAspect() { _sortIncBuffer.Dispose(); @@ -200,34 +209,31 @@ namespace DCFApixels.DragonECS group.Clear(); var enumerator = GetEnumerator(); while (enumerator.MoveNext()) + { group.Add_Internal(enumerator.Current); + } } public int CopyTo(ref int[] array) { - var enumerator = GetEnumerator(); int count = 0; + var enumerator = GetEnumerator(); while (enumerator.MoveNext()) { if (array.Length <= count) + { Array.Resize(ref array, array.Length << 1); + } array[count++] = enumerator.Current; } return count; } public EcsSpan CopyToSpan(ref int[] array) { - var enumerator = GetEnumerator(); - int count = 0; - while (enumerator.MoveNext()) - { - if (array.Length <= count) - Array.Resize(ref array, array.Length << 1); - array[count++] = enumerator.Current; - } + int count = CopyTo(ref array); return new EcsSpan(worldID, array, count); } - #region object + #region Other public override string ToString() { List ints = new List(); @@ -241,7 +247,7 @@ namespace DCFApixels.DragonECS #region Enumerator [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Enumerator GetEnumerator() => new Enumerator(_span, aspect); + public Enumerator GetEnumerator() { return new Enumerator(_span, aspect); } public unsafe ref struct Enumerator { @@ -282,11 +288,9 @@ namespace DCFApixels.DragonECS private UnsafeArray _sortExcChunckBuffer; private readonly int _entityComponentMaskLength; - //private EcsAspect aspect; public unsafe Enumerator(EcsSpan span, EcsAspect aspect) { - //this.aspect = aspect; _span = span.GetEnumerator(); _entityComponentMasks = aspect.World._entityComponentMasks; _sortIncChunckBuffer = aspect._sortIncChunckBuffer; @@ -368,13 +372,8 @@ namespace DCFApixels.DragonECS public int Current { [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => _span.Current; + get { return _span.Current; } } - //public entlong CurrentLong - //{ - // [MethodImpl(MethodImplOptions.AggressiveInlining)] - // get => aspect.World.GetEntityLong(_span.Current); - //} [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool MoveNext() { @@ -385,13 +384,17 @@ namespace DCFApixels.DragonECS { var bit = _sortIncChunckBuffer.ptr[i]; if ((_entityComponentMasks[e * _entityComponentMaskLength + bit.chankIndex] & bit.mask) != bit.mask) + { goto skip; + } } for (int i = 0; i < _sortExcChunckBuffer.Length; i++) { var bit = _sortExcChunckBuffer.ptr[i]; if ((_entityComponentMasks[e * _entityComponentMaskLength + bit.chankIndex] & bit.mask) > 0) + { goto skip; + } } return true; skip: continue; diff --git a/src/EcsMask.cs b/src/EcsMask.cs index f5b20a1..dc18122 100644 --- a/src/EcsMask.cs +++ b/src/EcsMask.cs @@ -18,13 +18,28 @@ namespace DCFApixels.DragonECS internal readonly int[] exc; #region Properties - public int ID => id; - public int WorldID => worldID; - public EcsWorld World => EcsWorld.GetWorld(worldID); + public int ID + { + get { return id; } + } + public int WorldID + { + get { return worldID; } + } + public EcsWorld World + { + get { return EcsWorld.GetWorld(worldID); } + } /// Including constraints - public ReadOnlySpan Inc => inc; + public ReadOnlySpan Inc + { + get { return inc; } + } /// Excluding constraints - public ReadOnlySpan Exc => exc; + public ReadOnlySpan Exc + { + get { return exc; } + } #endregion #region Constructors @@ -118,30 +133,10 @@ namespace DCFApixels.DragonECS } private static bool IsSubarray(int[] super, int[] sub) { - //if (super.Length > sub.Length) - //{ - // return false; - //} - //for (int superI = 0, subI = 0; - // subI < sub.Length; - // superI++, subI++) - //{ - // while (super[superI] < sub[subI]) - // { - // superI++; - // } - // if (super[superI] != sub[subI]) - // { - // return false; - // } - //} - //return true; - if (super.Length < sub.Length) { return false; } - int superI = 0; int subI = 0; @@ -153,7 +148,6 @@ namespace DCFApixels.DragonECS } subI++; } - return subI == sub.Length; } @@ -163,7 +157,6 @@ namespace DCFApixels.DragonECS { return false; } - int superI = 0; int subI = 0; @@ -175,13 +168,15 @@ namespace DCFApixels.DragonECS } superI++; } - return subI == sub.Length; } #endregion #region Object - public override string ToString() => CreateLogString(worldID, inc, exc); + public override string ToString() + { + return CreateLogString(worldID, inc, exc); + } public bool Equals(EcsMask mask) { return id == mask.id && worldID == mask.worldID; @@ -203,11 +198,11 @@ namespace DCFApixels.DragonECS { lock (_dummyHashSet) { - if (CheckRepeats(inc)) throw new EcsFrameworkException("The values in the Include constraints are repeated."); - if (CheckRepeats(exc)) throw new EcsFrameworkException("The values in the Exclude constraints are repeated."); + if (CheckRepeats(inc)) { throw new EcsFrameworkException("The values in the Include constraints are repeated."); } + if (CheckRepeats(exc)) { throw new EcsFrameworkException("The values in the Exclude constraints are repeated."); } _dummyHashSet.Clear(); _dummyHashSet.UnionWith(inc); - if (_dummyHashSet.Overlaps(exc)) throw new EcsFrameworkException("Conflicting Include and Exclude constraints."); + if (_dummyHashSet.Overlaps(exc)) { throw new EcsFrameworkException("Conflicting Include and Exclude constraints."); } } } private bool CheckRepeats(int[] array) @@ -215,7 +210,10 @@ namespace DCFApixels.DragonECS _dummyHashSet.Clear(); foreach (var item in array) { - if (_dummyHashSet.Contains(item)) return true; + if (_dummyHashSet.Contains(item)) + { + return true; + } _dummyHashSet.Add(item); } return false; @@ -224,7 +222,7 @@ namespace DCFApixels.DragonECS private static string CreateLogString(int worldID, int[] inc, int[] exc) { #if (DEBUG && !DISABLE_DEBUG) - string converter(int o) => EcsDebugUtility.GetGenericTypeName(EcsWorld.GetWorld(worldID).AllPools[o].ComponentType, 1); + string converter(int o) { return EcsDebugUtility.GetGenericTypeName(EcsWorld.GetWorld(worldID).AllPools[o].ComponentType, 1); } return $"Inc({string.Join(", ", inc.Select(converter))}) Exc({string.Join(", ", exc.Select(converter))})"; #else return $"Inc({string.Join(", ", inc)}) Exc({string.Join(", ", exc)})"; // Release optimization @@ -252,11 +250,14 @@ namespace DCFApixels.DragonECS excludedChunkMasks = mask.excChunckMasks; included = mask.inc; excluded = mask.exc; - Type converter(int o) => world.GetComponentType(o); + Type converter(int o) { return world.GetComponentType(o); } includedTypes = included.Select(converter).ToArray(); excludedTypes = excluded.Select(converter).ToArray(); } - public override string ToString() => CreateLogString(_worldID, included, excluded); + public override string ToString() + { + return CreateLogString(_worldID, included, excluded); + } } #endregion diff --git a/src/Injections/Injector.cs b/src/Injections/Injector.cs index 0d7029b..248fbae 100644 --- a/src/Injections/Injector.cs +++ b/src/Injections/Injector.cs @@ -1,5 +1,4 @@ -using DCFApixels.DragonECS.Internal; -using System; +using System; using System.Collections.Generic; namespace DCFApixels.DragonECS