From 72b344b3dda4d6e8fd5264d29b8e4cb5792c27b8 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Sun, 28 May 2023 06:29:04 +0800 Subject: [PATCH] refactoring --- src/Debug/Attributes/DebugColorAttribute.cs | 34 +++------- .../Attributes/DebugDescriptionAttribute.cs | 5 +- src/Debug/Attributes/DebugNameAttribute.cs | 5 +- src/EcsGroup.cs | 67 ++++++++++--------- src/EcsWorld.cs | 8 ++- 5 files changed, 54 insertions(+), 65 deletions(-) diff --git a/src/Debug/Attributes/DebugColorAttribute.cs b/src/Debug/Attributes/DebugColorAttribute.cs index 253f64f..378a02b 100644 --- a/src/Debug/Attributes/DebugColorAttribute.cs +++ b/src/Debug/Attributes/DebugColorAttribute.cs @@ -10,45 +10,31 @@ namespace DCFApixels.DragonECS public byte r => color.r; public byte g => color.g; public byte b => color.b; - + /// normalized R channel public float rn => color.r / 255f; + /// normalized G channel public float gn => color.g / 255f; + /// normalized B channel public float bn => color.b / 255f; - - public DebugColorAttribute(byte r, byte g, byte b) - { - color = new ColorRecord(r, g, b); - } - public DebugColorAttribute(DebugColor color) - { - this.color = new ColorRecord((int)color); - } + public DebugColorAttribute(byte r, byte g, byte b) => color = new ColorRecord(r, g, b); + public DebugColorAttribute(DebugColor color) => this.color = new ColorRecord((int)color); [StructLayout(LayoutKind.Explicit, Pack = 1, Size = 4)] private readonly struct ColorRecord // Union { - [FieldOffset(0)] - public readonly int full; - [FieldOffset(3)] - public readonly byte r; - [FieldOffset(2)] - public readonly byte g; - [FieldOffset(1)] - public readonly byte b; - + [FieldOffset(0)] public readonly int full; + [FieldOffset(3)] public readonly byte r; + [FieldOffset(2)] public readonly byte g; + [FieldOffset(1)] public readonly byte b; public ColorRecord(byte r, byte g, byte b) : this() { this.r = r; this.g = g; this.b = b; } - public ColorRecord(int full) : this() - { - this.full = full; - } + public ColorRecord(int full) : this() => this.full = full; } } - public enum DebugColor { /// Red. RGB is (255, 0, 0) diff --git a/src/Debug/Attributes/DebugDescriptionAttribute.cs b/src/Debug/Attributes/DebugDescriptionAttribute.cs index 88a50c5..1a7aacd 100644 --- a/src/Debug/Attributes/DebugDescriptionAttribute.cs +++ b/src/Debug/Attributes/DebugDescriptionAttribute.cs @@ -6,9 +6,6 @@ namespace DCFApixels.DragonECS public sealed class DebugDescriptionAttribute : Attribute { public readonly string description; - public DebugDescriptionAttribute(string description) - { - this.description = description; - } + public DebugDescriptionAttribute(string description) => this.description = description; } } diff --git a/src/Debug/Attributes/DebugNameAttribute.cs b/src/Debug/Attributes/DebugNameAttribute.cs index 30c4d57..01f6fda 100644 --- a/src/Debug/Attributes/DebugNameAttribute.cs +++ b/src/Debug/Attributes/DebugNameAttribute.cs @@ -6,9 +6,6 @@ namespace DCFApixels.DragonECS public sealed class DebugNameAttribute : Attribute { public readonly string name; - public DebugNameAttribute(string name) - { - this.name = name; - } + public DebugNameAttribute(string name) => this.name = name; } } diff --git a/src/EcsGroup.cs b/src/EcsGroup.cs index 2bb9460..2f0ee31 100644 --- a/src/EcsGroup.cs +++ b/src/EcsGroup.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; #if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS @@ -8,6 +9,7 @@ using static DCFApixels.DragonECS.EcsGroup.ThrowHalper; namespace DCFApixels.DragonECS { [StructLayout(LayoutKind.Sequential, Pack = 0, Size = 8)] + [DebuggerTypeProxy(typeof(DebuggerProxy))] public readonly ref struct EcsReadonlyGroup { private readonly EcsGroup _source; @@ -65,12 +67,7 @@ namespace DCFApixels.DragonECS #endregion #region Object - public override string ToString() - { - if (_source != null) - return _source.ToString(); - return "NULL"; - } + public override string ToString() => _source != null ? _source.ToString() : "NULL"; public override int GetHashCode() => _source.GetHashCode(); public override bool Equals(object obj) => obj is EcsGroup group && group == this; public bool Equals(EcsReadonlyGroup other) => _source == other._source; @@ -88,15 +85,23 @@ namespace DCFApixels.DragonECS public static bool operator !=(EcsReadonlyGroup a, EcsReadonlyGroup b) => !a.Equals(b); public static bool operator !=(EcsReadonlyGroup a, EcsGroup b) => !a.Equals(b); #endregion + + #region DebuggerProxy + internal class DebuggerProxy : EcsGroup.DebuggerProxy + { + public DebuggerProxy(EcsReadonlyGroup group) : base(group._source) { } + } + #endregion } + [DebuggerTypeProxy(typeof(DebuggerProxy))] public unsafe class EcsGroup : IDisposable, IEquatable { private EcsWorld _source; private int[] _dense; private int[] _sparse; private int _count; - private bool _isReleased = true; + internal bool _isReleased = true; #region Properties public EcsWorld World => _source; @@ -391,10 +396,7 @@ namespace DCFApixels.DragonECS #endregion #region Object - public override string ToString() - { - return string.Join(", ", _dense.AsSpan(1, _count).ToArray()); - } + public override string ToString() => string.Join(", ", _dense.AsSpan(1, _count).ToArray()); public override bool Equals(object obj) => obj is EcsGroup group && Equals(group); public bool Equals(EcsReadonlyGroup other) => Equals(other.GetGroupInternal()); public bool Equals(EcsGroup other) @@ -448,13 +450,9 @@ namespace DCFApixels.DragonECS #endregion #region IDisposable/Release - public void Dispose() - { - Release(); - } + public void Dispose() => Release(); public void Release() { - _isReleased = true; _source.ReleaseGroup(this); } #endregion @@ -472,21 +470,30 @@ namespace DCFApixels.DragonECS } #endif #endregion - } - #region Extensions - public static class EcsGroupExtensions - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Normalize(this EcsGroup self, ref T[] array) + #region DebuggerProxy + internal class DebuggerProxy { - if (array.Length < self.CapacityDense) Array.Resize(ref array, self.CapacityDense); - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Normalize(this EcsReadonlyGroup self, ref T[] array) - { - if (array.Length < self.CapacityDense) Array.Resize(ref array, self.CapacityDense); + private EcsGroup _group; + public EcsWorld World => _group.World; + public bool IsReleased => _group.IsReleased; + public entlong[] Entities + { + get + { + entlong[] result = new entlong[_group.Count]; + int i = 0; + foreach (var e in _group) + result[i++] = _group.World.GetEntityLong(e); + return result; + } + } + public int Count => _group.Count; + public int CapacityDense => _group.CapacityDense; + public int CapacitySparce => _group.CapacitySparce; + + public DebuggerProxy(EcsGroup group) => _group = group; } + #endregion } - #endregion -} +} \ No newline at end of file diff --git a/src/EcsWorld.cs b/src/EcsWorld.cs index 42d4aab..1d88b3c 100644 --- a/src/EcsWorld.cs +++ b/src/EcsWorld.cs @@ -322,15 +322,16 @@ namespace DCFApixels.DragonECS } #endregion - #region Groups + #region Groups Pool internal void RegisterGroup(EcsGroup group) { _groups.Add(new WeakReference(group)); } internal EcsGroup GetGroupFromPool() { - if (_groupsPool.Count <= 0) return new EcsGroup(this); - return _groupsPool.Pop(); + EcsGroup result = _groupsPool.Count <= 0 ? new EcsGroup(this) : _groupsPool.Pop(); + result._isReleased = false; + return result; } internal void ReleaseGroup(EcsGroup group) { @@ -338,6 +339,7 @@ namespace DCFApixels.DragonECS if (group.World != this) throw new ArgumentException("groupFilter.WorldIndex != this"); #endif + group._isReleased = true; group.Clear(); _groupsPool.Push(group); }