mirror of
https://github.com/DCFApixels/DragonECS.git
synced 2025-09-18 18:14:37 +08:00
refactoring
This commit is contained in:
parent
dd8b126c27
commit
615d201424
@ -1,16 +1,18 @@
|
||||
#if DISABLE_DEBUG
|
||||
#undef DEBUG
|
||||
#endif
|
||||
using DCFApixels.DragonECS.Core;
|
||||
using DCFApixels.DragonECS.Internal;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using static DCFApixels.DragonECS.EcsConsts;
|
||||
|
||||
namespace DCFApixels.DragonECS
|
||||
{
|
||||
using static EcsConsts;
|
||||
#region EcsProfilerMarker
|
||||
public readonly struct EcsProfilerMarker
|
||||
{
|
||||
#if DEBUG || DRAGONECS_ENABLE_DEBUG_SERVICE
|
||||
@ -75,6 +77,7 @@ namespace DCFApixels.DragonECS
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static explicit operator EcsProfilerMarker(string markerName) { return new EcsProfilerMarker(markerName); }
|
||||
}
|
||||
#endregion
|
||||
|
||||
[MetaColor(MetaColor.DragonRose)]
|
||||
[MetaGroup(PACK_GROUP, DEBUG_GROUP)]
|
||||
@ -82,6 +85,7 @@ namespace DCFApixels.DragonECS
|
||||
[MetaID("DragonECS_10A4587C92013B55820D8604D718A1C3")]
|
||||
public static class EcsDebug
|
||||
{
|
||||
#region Set
|
||||
public static void Set<T>() where T : DebugService, new()
|
||||
{
|
||||
DebugService.Set<T>();
|
||||
@ -90,7 +94,9 @@ namespace DCFApixels.DragonECS
|
||||
{
|
||||
DebugService.Set(service);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Print
|
||||
#if UNITY_2021_3_OR_NEWER
|
||||
[UnityEngine.HideInCallstack]
|
||||
#endif
|
||||
@ -168,6 +174,9 @@ namespace DCFApixels.DragonECS
|
||||
DebugService.CurrentThreadInstance.Print(tag, v);
|
||||
#endif
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Other
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Break()
|
||||
{
|
||||
@ -175,9 +184,12 @@ namespace DCFApixels.DragonECS
|
||||
DebugService.CurrentThreadInstance.Break();
|
||||
#endif
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
public static OnPrintHandler OnPrint = delegate { };
|
||||
public delegate void OnPrintHandler(string tag, object v);
|
||||
#endregion
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------------------------------------//
|
||||
@ -185,19 +197,23 @@ namespace DCFApixels.DragonECS
|
||||
public abstract class DebugService
|
||||
{
|
||||
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]
|
||||
private static DebugService _currentThreadInstanceClone;
|
||||
[ThreadStatic]
|
||||
private static DebugService _currentThreadInstance; // для сравнения
|
||||
|
||||
private static IdDispenser _idDispenser = new IdDispenser(16, 0);
|
||||
private static Dictionary<string, int> _nameIdTable = new Dictionary<string, int>();
|
||||
private readonly static IdDispenser _idDispenser = new IdDispenser(16, 0);
|
||||
private readonly static Dictionary<string, int> _nameIdTable = new Dictionary<string, int>();
|
||||
|
||||
#region Properties
|
||||
public static bool IsNullOrDefault
|
||||
{
|
||||
get { return _instance == null || _instance is NullDebugService || _instance is DefaultDebugService; }
|
||||
}
|
||||
public static DebugService Instance
|
||||
{
|
||||
get { return _instance; }
|
||||
@ -231,7 +247,11 @@ namespace DCFApixels.DragonECS
|
||||
#region Static Constructor
|
||||
static DebugService()
|
||||
{
|
||||
#if !UNITY_5_3_OR_NEWER
|
||||
Set(new NullDebugService());
|
||||
#else
|
||||
Set(new DefaultDebugService());
|
||||
#endif
|
||||
}
|
||||
#endregion
|
||||
|
||||
@ -357,6 +377,11 @@ namespace DCFApixels.DragonECS
|
||||
public delegate void OnServiceChangedHandler(DebugService service);
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
namespace DCFApixels.DragonECS.Core
|
||||
{
|
||||
#region DebugServiceExtensions
|
||||
public static class DebugServiceExtensions
|
||||
{
|
||||
public static void PrintWarning(this DebugService self, object v)
|
||||
@ -386,7 +411,9 @@ namespace DCFApixels.DragonECS
|
||||
}
|
||||
//TODO PrintJson возможно будет добавлено когда-то
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region DefaultServices
|
||||
//------------------------------------------------------------------------------------------------------------//
|
||||
|
||||
public sealed class NullDebugService : DebugService
|
||||
@ -404,7 +431,6 @@ namespace DCFApixels.DragonECS
|
||||
|
||||
public sealed class DefaultDebugService : DebugService
|
||||
{
|
||||
#if !UNITY_5_3_OR_NEWER
|
||||
private const string PROFILER_MARKER = "ProfilerMark";
|
||||
private const string PROFILER_MARKER_CACHE = "[" + PROFILER_MARKER + "] ";
|
||||
|
||||
@ -573,14 +599,6 @@ namespace DCFApixels.DragonECS
|
||||
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
|
||||
}
|
@ -45,7 +45,7 @@ namespace DCFApixels.DragonECS
|
||||
private const string NULL_NAME = "NULL";
|
||||
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 int _increment = 1;
|
||||
|
||||
@ -84,7 +84,7 @@ namespace DCFApixels.DragonECS
|
||||
|
||||
_name = NULL_NAME,
|
||||
_typeName = NULL_NAME,
|
||||
_color = new MetaColor(MetaColor.Black),
|
||||
_color = MetaColor.Black,
|
||||
_description = new MetaDescription("", NULL_NAME),
|
||||
_group = MetaGroup.Empty,
|
||||
_tags = Array.Empty<string>(),
|
||||
|
@ -125,7 +125,7 @@ namespace DCFApixels.DragonECS
|
||||
#endregion
|
||||
|
||||
//Инициализация аспектов проходит в синхронизированном состоянии, поэтому использование _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 EcsMask _mask;
|
||||
|
@ -114,20 +114,18 @@ namespace DCFApixels.DragonECS
|
||||
private static IEcsProcess[] _buffer;
|
||||
private T[] CreateProcess<T>() where T : IEcsProcess
|
||||
{
|
||||
if (_buffer == null || _buffer.Length < _allSystems.Length)
|
||||
{
|
||||
Array.Resize(ref _buffer, _allSystems.Length);
|
||||
}
|
||||
int l = 0;
|
||||
ArrayUtility.UpsizeWithoutCopy(ref _buffer, _allSystems.Length);
|
||||
int bufferLength = 0;
|
||||
for (int i = 0, iMax = _allSystems.Length; i < iMax; i++)
|
||||
{
|
||||
if (_allSystems[i] is T)
|
||||
{
|
||||
_buffer[l++] = _allSystems[i];
|
||||
_buffer[bufferLength++] = _allSystems[i];
|
||||
}
|
||||
}
|
||||
T[] result = new T[l];
|
||||
Array.Copy(_buffer, result, l);
|
||||
T[] result = new T[bufferLength];
|
||||
Array.Copy(_buffer, result, bufferLength);
|
||||
Array.Clear(_buffer, 0, bufferLength);
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
|
@ -955,10 +955,8 @@ namespace DCFApixels.DragonECS
|
||||
public ReadOnlySpan<object> GetComponentsFor(int entityID)
|
||||
{
|
||||
int count = GetComponentTypeIDsFor_Internal(entityID, ref _componentIDsBuffer);
|
||||
if (_componentsBuffer == null || _componentsBuffer.Length < count)
|
||||
{
|
||||
_componentsBuffer = new object[count];
|
||||
}
|
||||
ArrayUtility.UpsizeWithoutCopy(ref _componentIDsBuffer, count);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
_componentsBuffer[i] = _pools[_componentIDsBuffer[i]].GetRaw(entityID);
|
||||
@ -986,14 +984,7 @@ namespace DCFApixels.DragonECS
|
||||
private unsafe int GetComponentTypeIDsFor_Internal(int entityID, ref int[] componentIDs)
|
||||
{
|
||||
var itemsCount = GetComponentsCount(entityID);
|
||||
if (componentIDs == null)
|
||||
{
|
||||
componentIDs = new int[itemsCount];
|
||||
}
|
||||
if (componentIDs.Length < itemsCount)
|
||||
{
|
||||
Array.Resize(ref componentIDs, itemsCount);
|
||||
}
|
||||
ArrayUtility.UpsizeWithoutCopy(ref componentIDs, itemsCount);
|
||||
|
||||
if (itemsCount <= 0) { return 0; }
|
||||
|
||||
@ -1170,6 +1161,7 @@ namespace DCFApixels.DragonECS
|
||||
public long Version { get { return _world.Version; } }
|
||||
public IEcsPool[] Pools { get { return _world._pools; } }
|
||||
public short ID { get { return _world.ID; } }
|
||||
public bool IsDestroyed { get { return _world._isDestroyed; } }
|
||||
public List<MaskQueryExecutor> MaskQueries { get { return _queries; } }
|
||||
public DebuggerProxy(EcsWorld world)
|
||||
{
|
||||
|
@ -26,11 +26,11 @@ namespace DCFApixels.DragonECS
|
||||
|
||||
private static EcsWorld[] _worlds = Array.Empty<EcsWorld>();
|
||||
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 readonly object _worldLock = new object();
|
||||
|
||||
private StructList<WorldComponentPoolAbstract> _worldComponentPools;
|
||||
private int _builtinWorldComponentsCount = 0;
|
||||
private static readonly object _worldLock = new object();
|
||||
|
||||
static EcsWorld()
|
||||
{
|
||||
@ -92,6 +92,7 @@ namespace DCFApixels.DragonECS
|
||||
}
|
||||
world = null;
|
||||
}
|
||||
_worlds = Array.Empty<EcsWorld>();
|
||||
_worldIdDispenser.ReleaseAll();
|
||||
}
|
||||
|
||||
@ -137,8 +138,8 @@ namespace DCFApixels.DragonECS
|
||||
private static short _count;
|
||||
private static short[] _recycledItems = new short[4];
|
||||
private static short _recycledItemsCount;
|
||||
private static IEcsWorldComponent<T> _interface = EcsWorldComponentHandler<T>.instance;
|
||||
private static Abstract _controller = new Abstract();
|
||||
private static readonly IEcsWorldComponent<T> _interface = EcsWorldComponentHandler<T>.instance;
|
||||
private static readonly Abstract _controller = new Abstract();
|
||||
static WorldComponentPool()
|
||||
{
|
||||
_allWorldComponentPools.Add(_controller);
|
||||
|
@ -134,6 +134,39 @@ namespace DCFApixels.DragonECS.Internal
|
||||
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>
|
||||
{
|
||||
|
@ -22,7 +22,7 @@ namespace DCFApixels.DragonECS.Internal
|
||||
{
|
||||
private static readonly Dictionary<EcsTypeCodeKey, EcsTypeCode> _codes = new Dictionary<EcsTypeCodeKey, EcsTypeCode>();
|
||||
private static int _increment = 1;
|
||||
private static object _lock = new object();
|
||||
private static readonly object _lock = new object();
|
||||
public static int Count
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
|
@ -44,6 +44,7 @@ namespace DCFApixels.DragonECS.Internal
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if ENABLE_IL2CPP
|
||||
[Il2CppSetOption(Option.NullChecks, false)]
|
||||
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
|
||||
|
Loading…
Reference in New Issue
Block a user