update type code

This commit is contained in:
Mikhail 2024-11-05 17:16:50 +08:00
parent 4e54be72bf
commit ab06cbce58
4 changed files with 50 additions and 40 deletions

View File

@ -452,7 +452,7 @@ namespace DCFApixels.DragonECS
private static string CreateLogString(EcsTypeCode[] inc, EcsTypeCode[] exc) private static string CreateLogString(EcsTypeCode[] inc, EcsTypeCode[] exc)
{ {
#if (DEBUG && !DISABLE_DEBUG) #if (DEBUG && !DISABLE_DEBUG)
string converter(EcsTypeCode o) { return EcsDebugUtility.GetGenericTypeName(EcsTypeCodeManager.FindTypeOfCode(o), 1); } string converter(EcsTypeCode o) { return EcsTypeCodeManager.FindTypeOfCode(o).ToString(); }
return $"Inc({string.Join(", ", inc.Select(converter))}) Exc({string.Join(", ", exc.Select(converter))})"; return $"Inc({string.Join(", ", inc.Select(converter))}) Exc({string.Join(", ", exc.Select(converter))})";
#else #else
return $"Inc({string.Join(", ", inc)}) Exc({string.Join(", ", exc)})"; // Release optimization return $"Inc({string.Join(", ", inc)}) Exc({string.Join(", ", exc)})"; // Release optimization
@ -478,7 +478,7 @@ namespace DCFApixels.DragonECS
ID = mask.ID; ID = mask.ID;
included = mask._incs; included = mask._incs;
excluded = mask._excs; excluded = mask._excs;
Type converter(EcsTypeCode o) { return EcsTypeCodeManager.FindTypeOfCode(o); } Type converter(EcsTypeCode o) { return EcsTypeCodeManager.FindTypeOfCode(o).Type; }
includedTypes = included.Select(converter).ToArray(); includedTypes = included.Select(converter).ToArray();
excludedTypes = excluded.Select(converter).ToArray(); excludedTypes = excluded.Select(converter).ToArray();
} }

View File

@ -73,34 +73,6 @@ namespace DCFApixels.DragonECS
private List<IEcsWorldEventListener> _listeners = new List<IEcsWorldEventListener>(); private List<IEcsWorldEventListener> _listeners = new List<IEcsWorldEventListener>();
private List<IEcsEntityEventListener> _entityListeners = new List<IEcsEntityEventListener>(); private List<IEcsEntityEventListener> _entityListeners = new List<IEcsEntityEventListener>();
private static Stack<EcsWorldConfig> _configInitOnlynStack = new Stack<EcsWorldConfig>(4);
protected internal static EcsWorldConfig Config_InitOnly
{
get
{
if (_configInitOnlynStack.TryPeek(out EcsWorldConfig result))
{
return result;
}
Throw.UndefinedException();
return null;
}
}
private readonly ref struct ConfigInitOnlyScope
{
private readonly Stack<EcsWorldConfig> _stack;
public ConfigInitOnlyScope(Stack<EcsWorldConfig> stack, EcsWorldConfig config)
{
_stack = stack;
_stack.Push(config);
}
public void Dispose()
{
_stack.Peek();
}
}
#region Properties #region Properties
EcsWorld IEntityStorage.World EcsWorld IEntityStorage.World
{ {
@ -171,8 +143,6 @@ namespace DCFApixels.DragonECS
lock (_worldLock) lock (_worldLock)
{ {
if (configs == null) { configs = ConfigContainer.Empty; } if (configs == null) { configs = ConfigContainer.Empty; }
_configInitOnlynStack.Push(configs.GetWorldConfigOrDefault());
bool nullWorld = this is NullWorld; bool nullWorld = this is NullWorld;
if (nullWorld == false && worldID == NULL_WORLD_ID) if (nullWorld == false && worldID == NULL_WORLD_ID)
{ {
@ -194,7 +164,6 @@ namespace DCFApixels.DragonECS
if (_worlds[worldID] != null) if (_worlds[worldID] != null)
{ {
_worldIdDispenser.Release(worldID); _worldIdDispenser.Release(worldID);
_configInitOnlynStack.Pop();
Throw.Exception("The world with the specified ID has already been created\r\n"); Throw.Exception("The world with the specified ID has already been created\r\n");
} }
} }
@ -211,9 +180,9 @@ namespace DCFApixels.DragonECS
int entitiesCapacity = ArrayUtility.NormalizeSizeToPowerOfTwo(config.EntitiesCapacity); int entitiesCapacity = ArrayUtility.NormalizeSizeToPowerOfTwo(config.EntitiesCapacity);
_entityDispenser = new IdDispenser(entitiesCapacity, 0, OnEntityDispenserResized); _entityDispenser = new IdDispenser(entitiesCapacity, 0, OnEntityDispenserResized);
GetComponentTypeID<NullComponent>(); _executorCoures = new Dictionary<(Type, object), IQueryExecutorImplementation>(config.PoolComponentsCapacity);
_configInitOnlynStack.Pop(); GetComponentTypeID<NullComponent>();
} }
} }
public void Destroy() public void Destroy()

View File

@ -8,7 +8,7 @@ namespace DCFApixels.DragonECS
{ {
public partial class EcsWorld public partial class EcsWorld
{ {
private readonly Dictionary<(Type, object), IQueryExecutorImplementation> _executorCoures = new Dictionary<(Type, object), IQueryExecutorImplementation>(Config_InitOnly.PoolComponentsCapacity); private readonly Dictionary<(Type, object), IQueryExecutorImplementation> _executorCoures;
public TExecutor GetExecutorForMask<TExecutor>(IComponentMask gmask) public TExecutor GetExecutorForMask<TExecutor>(IComponentMask gmask)
where TExecutor : MaskQueryExecutor, new() where TExecutor : MaskQueryExecutor, new()
{ {

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
#if ENABLE_IL2CPP #if ENABLE_IL2CPP
@ -8,6 +9,46 @@ using Unity.IL2CPP.CompilerServices;
namespace DCFApixels.DragonECS.Internal namespace DCFApixels.DragonECS.Internal
{ {
#if ENABLE_IL2CPP
[Il2CppSetOption(Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif
[DebuggerDisplay("{" + nameof(GetDebuggerDisplay) + "(),nq}")]
internal readonly struct EcsTypeCodeKey : IEquatable<EcsTypeCodeKey>
{
public readonly Type Type;
public readonly string NameKey;
public EcsTypeCodeKey(Type type, string nameKey)
{
Type = type;
NameKey = nameKey;
}
public bool Equals(EcsTypeCodeKey other)
{
return Type == other.Type && NameKey == other.NameKey;
}
public override bool Equals(object obj)
{
return obj is EcsTypeCodeKey other && Equals(other);
}
public override int GetHashCode()
{
return HashCode.Combine(Type, NameKey);
}
public override string ToString()
{
if (string.IsNullOrEmpty(NameKey))
{
return Type.ToString();
}
return $"{Type} {NameKey}";
}
public static implicit operator EcsTypeCodeKey(Type type) { return new EcsTypeCodeKey(type, string.Empty); }
private string GetDebuggerDisplay()
{
return ToString();
}
}
//TODO разработать возможность ручного устанавливания ID типам. //TODO разработать возможность ручного устанавливания ID типам.
//это может быть полезно как детерминированность для сети //это может быть полезно как детерминированность для сети
#if ENABLE_IL2CPP #if ENABLE_IL2CPP
@ -16,7 +57,7 @@ namespace DCFApixels.DragonECS.Internal
#endif #endif
internal static class EcsTypeCodeManager internal static class EcsTypeCodeManager
{ {
private static readonly Dictionary<Type, EcsTypeCode> _codes = new Dictionary<Type, 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 object _lock = new object();
public static int Count public static int Count
@ -40,7 +81,7 @@ namespace DCFApixels.DragonECS.Internal
public static EcsTypeCode Get<T>() { return EcsTypeCodeCache<T>.code; } public static EcsTypeCode Get<T>() { return EcsTypeCodeCache<T>.code; }
public static bool Has(Type type) { return _codes.ContainsKey(type); } public static bool Has(Type type) { return _codes.ContainsKey(type); }
public static bool Has<T>() { return _codes.ContainsKey(typeof(T)); } public static bool Has<T>() { return _codes.ContainsKey(typeof(T)); }
public static Type FindTypeOfCode(EcsTypeCode typeCode) public static EcsTypeCodeKey FindTypeOfCode(EcsTypeCode typeCode)
{ {
foreach (var item in _codes) foreach (var item in _codes)
{ {
@ -67,9 +108,9 @@ namespace DCFApixels.DragonECS.Internal
#endif #endif
internal struct TypeCodeInfo internal struct TypeCodeInfo
{ {
public Type type; public EcsTypeCodeKey type;
public EcsTypeCode code; public EcsTypeCode code;
public TypeCodeInfo(Type type, EcsTypeCode code) public TypeCodeInfo(EcsTypeCodeKey type, EcsTypeCode code)
{ {
this.type = type; this.type = type;
this.code = code; this.code = code;