refactoring

This commit is contained in:
DCFApixels 2025-03-24 14:26:40 +08:00
parent dd8b126c27
commit 615d201424
9 changed files with 86 additions and 43 deletions

View File

@ -1,16 +1,18 @@
#if DISABLE_DEBUG #if DISABLE_DEBUG
#undef DEBUG #undef DEBUG
#endif #endif
using DCFApixels.DragonECS.Core;
using DCFApixels.DragonECS.Internal; using DCFApixels.DragonECS.Internal;
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using static DCFApixels.DragonECS.EcsConsts;
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
{ {
using static EcsConsts; #region EcsProfilerMarker
public readonly struct EcsProfilerMarker public readonly struct EcsProfilerMarker
{ {
#if DEBUG || DRAGONECS_ENABLE_DEBUG_SERVICE #if DEBUG || DRAGONECS_ENABLE_DEBUG_SERVICE
@ -75,6 +77,7 @@ namespace DCFApixels.DragonECS
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator EcsProfilerMarker(string markerName) { return new EcsProfilerMarker(markerName); } public static explicit operator EcsProfilerMarker(string markerName) { return new EcsProfilerMarker(markerName); }
} }
#endregion
[MetaColor(MetaColor.DragonRose)] [MetaColor(MetaColor.DragonRose)]
[MetaGroup(PACK_GROUP, DEBUG_GROUP)] [MetaGroup(PACK_GROUP, DEBUG_GROUP)]
@ -82,6 +85,7 @@ namespace DCFApixels.DragonECS
[MetaID("DragonECS_10A4587C92013B55820D8604D718A1C3")] [MetaID("DragonECS_10A4587C92013B55820D8604D718A1C3")]
public static class EcsDebug public static class EcsDebug
{ {
#region Set
public static void Set<T>() where T : DebugService, new() public static void Set<T>() where T : DebugService, new()
{ {
DebugService.Set<T>(); DebugService.Set<T>();
@ -90,7 +94,9 @@ namespace DCFApixels.DragonECS
{ {
DebugService.Set(service); DebugService.Set(service);
} }
#endregion
#region Print
#if UNITY_2021_3_OR_NEWER #if UNITY_2021_3_OR_NEWER
[UnityEngine.HideInCallstack] [UnityEngine.HideInCallstack]
#endif #endif
@ -168,6 +174,9 @@ namespace DCFApixels.DragonECS
DebugService.CurrentThreadInstance.Print(tag, v); DebugService.CurrentThreadInstance.Print(tag, v);
#endif #endif
} }
#endregion
#region Other
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Break() public static void Break()
{ {
@ -175,9 +184,12 @@ namespace DCFApixels.DragonECS
DebugService.CurrentThreadInstance.Break(); DebugService.CurrentThreadInstance.Break();
#endif #endif
} }
#endregion
#region Events
public static OnPrintHandler OnPrint = delegate { }; public static OnPrintHandler OnPrint = delegate { };
public delegate void OnPrintHandler(string tag, object v); public delegate void OnPrintHandler(string tag, object v);
#endregion
} }
//------------------------------------------------------------------------------------------------------------// //------------------------------------------------------------------------------------------------------------//
@ -185,19 +197,23 @@ namespace DCFApixels.DragonECS
public abstract class DebugService public abstract class DebugService
{ {
private static DebugService _instance; private static DebugService _instance;
private static object _lock = new object(); private readonly static object _lock = new object();
private static HashSet<DebugService> _threadServiceClonesSet = new HashSet<DebugService>(); private readonly static HashSet<DebugService> _threadServiceClonesSet = new HashSet<DebugService>();
[ThreadStatic] [ThreadStatic]
private static DebugService _currentThreadInstanceClone; private static DebugService _currentThreadInstanceClone;
[ThreadStatic] [ThreadStatic]
private static DebugService _currentThreadInstance; // для сравнения private static DebugService _currentThreadInstance; // для сравнения
private static IdDispenser _idDispenser = new IdDispenser(16, 0); private readonly static IdDispenser _idDispenser = new IdDispenser(16, 0);
private static Dictionary<string, int> _nameIdTable = new Dictionary<string, int>(); private readonly static Dictionary<string, int> _nameIdTable = new Dictionary<string, int>();
#region Properties #region Properties
public static bool IsNullOrDefault
{
get { return _instance == null || _instance is NullDebugService || _instance is DefaultDebugService; }
}
public static DebugService Instance public static DebugService Instance
{ {
get { return _instance; } get { return _instance; }
@ -231,7 +247,11 @@ namespace DCFApixels.DragonECS
#region Static Constructor #region Static Constructor
static DebugService() static DebugService()
{ {
#if !UNITY_5_3_OR_NEWER
Set(new NullDebugService());
#else
Set(new DefaultDebugService()); Set(new DefaultDebugService());
#endif
} }
#endregion #endregion
@ -357,6 +377,11 @@ namespace DCFApixels.DragonECS
public delegate void OnServiceChangedHandler(DebugService service); public delegate void OnServiceChangedHandler(DebugService service);
#endregion #endregion
} }
}
namespace DCFApixels.DragonECS.Core
{
#region DebugServiceExtensions
public static class DebugServiceExtensions public static class DebugServiceExtensions
{ {
public static void PrintWarning(this DebugService self, object v) public static void PrintWarning(this DebugService self, object v)
@ -386,7 +411,9 @@ namespace DCFApixels.DragonECS
} }
//TODO PrintJson возможно будет добавлено когда-то //TODO PrintJson возможно будет добавлено когда-то
} }
#endregion
#region DefaultServices
//------------------------------------------------------------------------------------------------------------// //------------------------------------------------------------------------------------------------------------//
public sealed class NullDebugService : DebugService public sealed class NullDebugService : DebugService
@ -404,7 +431,6 @@ namespace DCFApixels.DragonECS
public sealed class DefaultDebugService : DebugService public sealed class DefaultDebugService : DebugService
{ {
#if !UNITY_5_3_OR_NEWER
private const string PROFILER_MARKER = "ProfilerMark"; private const string PROFILER_MARKER = "ProfilerMark";
private const string PROFILER_MARKER_CACHE = "[" + PROFILER_MARKER + "] "; private const string PROFILER_MARKER_CACHE = "[" + PROFILER_MARKER + "] ";
@ -573,14 +599,6 @@ namespace DCFApixels.DragonECS
return this.AutoToString(); return this.AutoToString();
} }
} }
#else
protected sealed override DebugService CreateThreadInstance() { return this; }
public sealed override void Break() { }
public sealed override void Print(string tag, object v) { }
public sealed override void ProfilerMarkBegin(int id) { }
public sealed override void ProfilerMarkEnd(int id) { }
protected sealed override void OnDelProfilerMark(int id) { }
protected sealed override void OnNewProfilerMark(int id, string name) { }
#endif
} }
#endregion
} }

View File

@ -45,7 +45,7 @@ namespace DCFApixels.DragonECS
private const string NULL_NAME = "NULL"; private const string NULL_NAME = "NULL";
public static readonly TypeMeta NullTypeMeta; public static readonly TypeMeta NullTypeMeta;
private static object _lock = new object(); private static readonly object _lock = new object();
private static readonly Dictionary<Type, TypeMeta> _metaCache = new Dictionary<Type, TypeMeta>(); private static readonly Dictionary<Type, TypeMeta> _metaCache = new Dictionary<Type, TypeMeta>();
private static int _increment = 1; private static int _increment = 1;
@ -84,7 +84,7 @@ namespace DCFApixels.DragonECS
_name = NULL_NAME, _name = NULL_NAME,
_typeName = NULL_NAME, _typeName = NULL_NAME,
_color = new MetaColor(MetaColor.Black), _color = MetaColor.Black,
_description = new MetaDescription("", NULL_NAME), _description = new MetaDescription("", NULL_NAME),
_group = MetaGroup.Empty, _group = MetaGroup.Empty,
_tags = Array.Empty<string>(), _tags = Array.Empty<string>(),

View File

@ -125,7 +125,7 @@ namespace DCFApixels.DragonECS
#endregion #endregion
//Инициализация аспектов проходит в синхронизированном состоянии, поэтому использование _staticMaskCache потоко безопасно. //Инициализация аспектов проходит в синхронизированном состоянии, поэтому использование _staticMaskCache потоко безопасно.
private static Dictionary<Type, EcsStaticMask> _staticMaskCache = new Dictionary<Type, EcsStaticMask>(); private readonly static Dictionary<Type, EcsStaticMask> _staticMaskCache = new Dictionary<Type, EcsStaticMask>();
internal EcsWorld _source; internal EcsWorld _source;
internal EcsMask _mask; internal EcsMask _mask;

View File

@ -114,20 +114,18 @@ namespace DCFApixels.DragonECS
private static IEcsProcess[] _buffer; private static IEcsProcess[] _buffer;
private T[] CreateProcess<T>() where T : IEcsProcess private T[] CreateProcess<T>() where T : IEcsProcess
{ {
if (_buffer == null || _buffer.Length < _allSystems.Length) ArrayUtility.UpsizeWithoutCopy(ref _buffer, _allSystems.Length);
{ int bufferLength = 0;
Array.Resize(ref _buffer, _allSystems.Length);
}
int l = 0;
for (int i = 0, iMax = _allSystems.Length; i < iMax; i++) for (int i = 0, iMax = _allSystems.Length; i < iMax; i++)
{ {
if (_allSystems[i] is T) if (_allSystems[i] is T)
{ {
_buffer[l++] = _allSystems[i]; _buffer[bufferLength++] = _allSystems[i];
} }
} }
T[] result = new T[l]; T[] result = new T[bufferLength];
Array.Copy(_buffer, result, l); Array.Copy(_buffer, result, bufferLength);
Array.Clear(_buffer, 0, bufferLength);
return result; return result;
} }
#endregion #endregion

View File

@ -955,10 +955,8 @@ namespace DCFApixels.DragonECS
public ReadOnlySpan<object> GetComponentsFor(int entityID) public ReadOnlySpan<object> GetComponentsFor(int entityID)
{ {
int count = GetComponentTypeIDsFor_Internal(entityID, ref _componentIDsBuffer); int count = GetComponentTypeIDsFor_Internal(entityID, ref _componentIDsBuffer);
if (_componentsBuffer == null || _componentsBuffer.Length < count) ArrayUtility.UpsizeWithoutCopy(ref _componentIDsBuffer, count);
{
_componentsBuffer = new object[count];
}
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
_componentsBuffer[i] = _pools[_componentIDsBuffer[i]].GetRaw(entityID); _componentsBuffer[i] = _pools[_componentIDsBuffer[i]].GetRaw(entityID);
@ -986,14 +984,7 @@ namespace DCFApixels.DragonECS
private unsafe int GetComponentTypeIDsFor_Internal(int entityID, ref int[] componentIDs) private unsafe int GetComponentTypeIDsFor_Internal(int entityID, ref int[] componentIDs)
{ {
var itemsCount = GetComponentsCount(entityID); var itemsCount = GetComponentsCount(entityID);
if (componentIDs == null) ArrayUtility.UpsizeWithoutCopy(ref componentIDs, itemsCount);
{
componentIDs = new int[itemsCount];
}
if (componentIDs.Length < itemsCount)
{
Array.Resize(ref componentIDs, itemsCount);
}
if (itemsCount <= 0) { return 0; } if (itemsCount <= 0) { return 0; }
@ -1170,6 +1161,7 @@ namespace DCFApixels.DragonECS
public long Version { get { return _world.Version; } } public long Version { get { return _world.Version; } }
public IEcsPool[] Pools { get { return _world._pools; } } public IEcsPool[] Pools { get { return _world._pools; } }
public short ID { get { return _world.ID; } } public short ID { get { return _world.ID; } }
public bool IsDestroyed { get { return _world._isDestroyed; } }
public List<MaskQueryExecutor> MaskQueries { get { return _queries; } } public List<MaskQueryExecutor> MaskQueries { get { return _queries; } }
public DebuggerProxy(EcsWorld world) public DebuggerProxy(EcsWorld world)
{ {

View File

@ -26,11 +26,11 @@ namespace DCFApixels.DragonECS
private static EcsWorld[] _worlds = Array.Empty<EcsWorld>(); private static EcsWorld[] _worlds = Array.Empty<EcsWorld>();
private static readonly IdDispenser _worldIdDispenser = new IdDispenser(4, 0, n => Array.Resize(ref _worlds, n)); private static readonly IdDispenser _worldIdDispenser = new IdDispenser(4, 0, n => Array.Resize(ref _worlds, n));
private static StructList<WorldComponentPoolAbstract> _allWorldComponentPools = new StructList<WorldComponentPoolAbstract>(64); private static StructList<WorldComponentPoolAbstract> _allWorldComponentPools = new StructList<WorldComponentPoolAbstract>(64);
private static readonly object _worldLock = new object();
private StructList<WorldComponentPoolAbstract> _worldComponentPools; private StructList<WorldComponentPoolAbstract> _worldComponentPools;
private int _builtinWorldComponentsCount = 0; private int _builtinWorldComponentsCount = 0;
private static readonly object _worldLock = new object();
static EcsWorld() static EcsWorld()
{ {
@ -92,6 +92,7 @@ namespace DCFApixels.DragonECS
} }
world = null; world = null;
} }
_worlds = Array.Empty<EcsWorld>();
_worldIdDispenser.ReleaseAll(); _worldIdDispenser.ReleaseAll();
} }
@ -137,8 +138,8 @@ namespace DCFApixels.DragonECS
private static short _count; private static short _count;
private static short[] _recycledItems = new short[4]; private static short[] _recycledItems = new short[4];
private static short _recycledItemsCount; private static short _recycledItemsCount;
private static IEcsWorldComponent<T> _interface = EcsWorldComponentHandler<T>.instance; private static readonly IEcsWorldComponent<T> _interface = EcsWorldComponentHandler<T>.instance;
private static Abstract _controller = new Abstract(); private static readonly Abstract _controller = new Abstract();
static WorldComponentPool() static WorldComponentPool()
{ {
_allWorldComponentPools.Add(_controller); _allWorldComponentPools.Add(_controller);

View File

@ -134,6 +134,39 @@ namespace DCFApixels.DragonECS.Internal
array[i] = value; array[i] = value;
} }
} }
public static void UpsizeWithoutCopy<T>(ref T[] array, int minSize)
{
if (array == null || minSize > array.Length)
{
array = new T[minSize];
}
}
public static void Upsize<T>(ref T[] array, int minSize)
{
if (array == null)
{
array = new T[minSize];
}
else if (minSize > array.Length)
{
Array.Resize(ref array, minSize);
}
}
public static void UpsizeToNextPow2<T>(ref T[] array, int minSize)
{
if (array == null)
{
minSize = NextPow2(minSize);
array = new T[minSize];
}
else if (minSize > array.Length)
{
minSize = NextPow2(minSize);
Array.Resize(ref array, minSize);
}
}
} }
internal readonly struct EnumerableInt : IEnumerable<int> internal readonly struct EnumerableInt : IEnumerable<int>
{ {

View File

@ -22,7 +22,7 @@ namespace DCFApixels.DragonECS.Internal
{ {
private static readonly Dictionary<EcsTypeCodeKey, EcsTypeCode> _codes = new Dictionary<EcsTypeCodeKey, EcsTypeCode>(); private static readonly Dictionary<EcsTypeCodeKey, EcsTypeCode> _codes = new Dictionary<EcsTypeCodeKey, EcsTypeCode>();
private static int _increment = 1; private static int _increment = 1;
private static object _lock = new object(); private static readonly object _lock = new object();
public static int Count public static int Count
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]

View File

@ -44,6 +44,7 @@ namespace DCFApixels.DragonECS.Internal
} }
} }
} }
#if ENABLE_IL2CPP #if ENABLE_IL2CPP
[Il2CppSetOption(Option.NullChecks, false)] [Il2CppSetOption(Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)] [Il2CppSetOption(Option.ArrayBoundsChecks, false)]