mirror of
https://github.com/DCFApixels/DragonECS.git
synced 2025-09-18 01:44:35 +08:00
simplifying the signature of EcsWorld.GetPool
This commit is contained in:
parent
c7b0083d98
commit
416504db45
@ -1,11 +1,11 @@
|
||||
namespace DCFApixels.DragonECS
|
||||
{
|
||||
public class SingleSubject<TComponent, TPool> : EcsSubject where TPool : IEcsPoolImplementation<TComponent>, new()
|
||||
public class SingleSubject<TPool> : EcsSubject where TPool : IEcsPoolImplementation, new()
|
||||
{
|
||||
public readonly TPool pool;
|
||||
public SingleSubject(Builder b)
|
||||
{
|
||||
pool = b.Include<TComponent, TPool>();
|
||||
pool = b.Include<TPool>();
|
||||
}
|
||||
}
|
||||
public class CombinedSubject<S0, S1> : EcsSubject
|
||||
|
@ -68,35 +68,35 @@ namespace DCFApixels.DragonECS
|
||||
}
|
||||
|
||||
#region Include/Exclude/Optional
|
||||
public sealed override TPool Include<TComponent, TPool>()
|
||||
public sealed override TPool Include<TPool>()
|
||||
{
|
||||
IncludeImplicit<TComponent>();
|
||||
return _world.GetPool<TComponent, TPool>();
|
||||
IncludeImplicit(typeof(TPool).GetGenericArguments()[0]);
|
||||
return _world.GetPool<TPool>();
|
||||
}
|
||||
public sealed override TPool Exclude<TComponent, TPool>()
|
||||
public sealed override TPool Exclude<TPool>()
|
||||
{
|
||||
ExcludeImplicit<TComponent>();
|
||||
return _world.GetPool<TComponent, TPool>();
|
||||
ExcludeImplicit(typeof(TPool).GetGenericArguments()[0]);
|
||||
return _world.GetPool<TPool>();
|
||||
}
|
||||
public sealed override TPool Optional<TComponent, TPool>()
|
||||
public sealed override TPool Optional<TPool>()
|
||||
{
|
||||
return _world.GetPool<TComponent, TPool>();
|
||||
return _world.GetPool<TPool>();
|
||||
}
|
||||
private void IncludeImplicit<TComponent>()
|
||||
private void IncludeImplicit(Type type)
|
||||
{
|
||||
int id = _world.GetComponentID<TComponent>();
|
||||
int id = _world.GetComponentID(type);
|
||||
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
||||
if (_inc.Contains(id) || _exc.Contains(id)) throw new EcsFrameworkException($"{typeof(TComponent).Name} already in constraints list.");
|
||||
if (_inc.Contains(id) || _exc.Contains(id)) throw new EcsFrameworkException($"{type.Name} already in constraints list.");
|
||||
#endif
|
||||
_inc.Add(_world.GetComponentID<TComponent>());
|
||||
_inc.Add(id);
|
||||
}
|
||||
private void ExcludeImplicit<TComponent>()
|
||||
private void ExcludeImplicit(Type type)
|
||||
{
|
||||
int id = _world.GetComponentID<TComponent>();
|
||||
int id = _world.GetComponentID(type);
|
||||
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
||||
if (_inc.Contains(id) || _exc.Contains(id)) throw new EcsFrameworkException($"{typeof(TComponent).Name} already in constraints list.");
|
||||
if (_inc.Contains(id) || _exc.Contains(id)) throw new EcsFrameworkException($"{type.Name} already in constraints list.");
|
||||
#endif
|
||||
_exc.Add(_world.GetComponentID<TComponent>());
|
||||
_exc.Add(id);
|
||||
}
|
||||
#endregion
|
||||
|
||||
@ -127,13 +127,13 @@ namespace DCFApixels.DragonECS
|
||||
#if UNITY_2020_3_OR_NEWER
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
#endif
|
||||
private void SupportReflectionHack<TComponent, TPool>() where TPool : IEcsPoolImplementation<TComponent>, new()
|
||||
private void SupportReflectionHack<TPool>() where TPool : IEcsPoolImplementation, new()
|
||||
{
|
||||
Include<TComponent, TPool>();
|
||||
Exclude<TComponent, TPool>();
|
||||
Optional<TComponent, TPool>();
|
||||
IncludeImplicit<TComponent>();
|
||||
ExcludeImplicit<TComponent>();
|
||||
Include<TPool>();
|
||||
Exclude<TPool>();
|
||||
Optional<TPool>();
|
||||
IncludeImplicit(null);
|
||||
ExcludeImplicit(null);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
@ -154,9 +154,9 @@ namespace DCFApixels.DragonECS
|
||||
#region BuilderBase
|
||||
public abstract class EcsSubjectBuilderBase
|
||||
{
|
||||
public abstract TPool Include<TComponent, TPool>() where TPool : IEcsPoolImplementation<TComponent>, new();
|
||||
public abstract TPool Exclude<TComponent, TPool>() where TPool : IEcsPoolImplementation<TComponent>, new();
|
||||
public abstract TPool Optional<TComponent, TPool>() where TPool : IEcsPoolImplementation<TComponent>, new();
|
||||
public abstract TPool Include<TPool>() where TPool : IEcsPoolImplementation, new();
|
||||
public abstract TPool Exclude<TPool>() where TPool : IEcsPoolImplementation, new();
|
||||
public abstract TPool Optional<TPool>() where TPool : IEcsPoolImplementation, new();
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
@ -104,7 +104,8 @@ namespace DCFApixels.DragonECS
|
||||
#endregion
|
||||
|
||||
#region ComponentInfo
|
||||
public int GetComponentID<T>() => WorldMetaStorage.GetComponentId<T>(_worldTypeID);
|
||||
public int GetComponentID<T>() => WorldMetaStorage.GetComponentID<T>(_worldTypeID);
|
||||
public int GetComponentID(Type type) => WorldMetaStorage.GetComponentID(type, _worldTypeID);
|
||||
public Type GetComponentType(int componentID) => WorldMetaStorage.GetComponentType(_worldTypeID, componentID);
|
||||
public bool IsComponentTypeDeclared<T>() => IsComponentTypeDeclared(typeof(T));
|
||||
public bool IsComponentTypeDeclared(Type type) => WorldMetaStorage.IsComponentTypeDeclared(_worldTypeID, type);
|
||||
@ -114,9 +115,9 @@ namespace DCFApixels.DragonECS
|
||||
#if UNITY_2020_3_OR_NEWER
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
#endif
|
||||
public TPool GetPool<TComponent, TPool>() where TPool : IEcsPoolImplementation<TComponent>, new()
|
||||
public TPool GetPool<TPool>() where TPool : IEcsPoolImplementation, new()
|
||||
{
|
||||
int uniqueID = WorldMetaStorage.GetComponentId<TComponent>(_worldTypeID);
|
||||
int uniqueID = WorldMetaStorage.GetPoolID<TPool>(_worldTypeID);
|
||||
if (uniqueID >= _pools.Length)
|
||||
{
|
||||
int oldCapacity = _pools.Length;
|
||||
@ -133,7 +134,7 @@ namespace DCFApixels.DragonECS
|
||||
}
|
||||
public TSubject GetSubject<TSubject>() where TSubject : EcsSubject
|
||||
{
|
||||
int uniqueID = WorldMetaStorage.GetSubjectId<TSubject>(_worldTypeID);
|
||||
int uniqueID = WorldMetaStorage.GetSubjectID<TSubject>(_worldTypeID);
|
||||
if (uniqueID >= _subjects.Length)
|
||||
Array.Resize(ref _subjects, _subjects.Length << 1);
|
||||
if (_subjects[uniqueID] == null)
|
||||
@ -142,7 +143,7 @@ namespace DCFApixels.DragonECS
|
||||
}
|
||||
public TExecutor GetExecutor<TExecutor>() where TExecutor : EcsQueryExecutor, new()
|
||||
{
|
||||
int index = WorldMetaStorage.GetExecutorId<TExecutor>(_worldTypeID);
|
||||
int index = WorldMetaStorage.GetExecutorID<TExecutor>(_worldTypeID);
|
||||
if (index >= _executors.Length)
|
||||
Array.Resize(ref _executors, _executors.Length << 1);
|
||||
if (_executors[index] == null)
|
||||
@ -155,7 +156,7 @@ namespace DCFApixels.DragonECS
|
||||
}
|
||||
public T Get<T>() where T : class, new()
|
||||
{
|
||||
int index = WorldMetaStorage.GetWorldComponentId<T>(_worldTypeID);
|
||||
int index = WorldMetaStorage.GetWorldComponentID<T>(_worldTypeID);
|
||||
if (index >= _components.Length)
|
||||
Array.Resize(ref _executors, _executors.Length << 1);
|
||||
|
||||
|
@ -199,20 +199,20 @@ namespace DCFApixels.DragonECS
|
||||
{
|
||||
public static EcsPool<TComponent> GetPool<TComponent>(this EcsWorld self) where TComponent : struct, IEcsComponent
|
||||
{
|
||||
return self.GetPool<TComponent, EcsPool<TComponent>>();
|
||||
return self.GetPool<EcsPool<TComponent>>();
|
||||
}
|
||||
|
||||
public static EcsPool<TComponent> Include<TComponent>(this EcsSubjectBuilderBase self) where TComponent : struct, IEcsComponent
|
||||
{
|
||||
return self.Include<TComponent, EcsPool<TComponent>>();
|
||||
return self.Include<EcsPool<TComponent>>();
|
||||
}
|
||||
public static EcsPool<TComponent> Exclude<TComponent>(this EcsSubjectBuilderBase self) where TComponent : struct, IEcsComponent
|
||||
{
|
||||
return self.Exclude<TComponent, EcsPool<TComponent>>();
|
||||
return self.Exclude<EcsPool<TComponent>>();
|
||||
}
|
||||
public static EcsPool<TComponent> Optional<TComponent>(this EcsSubjectBuilderBase self) where TComponent : struct, IEcsComponent
|
||||
{
|
||||
return self.Optional<TComponent, EcsPool<TComponent>>();
|
||||
return self.Optional<EcsPool<TComponent>>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -191,20 +191,20 @@ namespace DCFApixels.DragonECS
|
||||
{
|
||||
public static EcsTagPool<TTagComponent> GetPool<TTagComponent>(this EcsWorld self) where TTagComponent : struct, IEcsTagComponent
|
||||
{
|
||||
return self.GetPool<TTagComponent, EcsTagPool<TTagComponent>>();
|
||||
return self.GetPool<EcsTagPool<TTagComponent>>();
|
||||
}
|
||||
|
||||
public static EcsTagPool<TTagComponent> Include<TTagComponent>(this EcsSubjectBuilderBase self) where TTagComponent : struct, IEcsTagComponent
|
||||
{
|
||||
return self.Include<TTagComponent, EcsTagPool<TTagComponent>>();
|
||||
return self.Include<EcsTagPool<TTagComponent>>();
|
||||
}
|
||||
public static EcsTagPool<TTagComponent> Exclude<TTagComponent>(this EcsSubjectBuilderBase self) where TTagComponent : struct, IEcsTagComponent
|
||||
{
|
||||
return self.Exclude<TTagComponent, EcsTagPool<TTagComponent>>();
|
||||
return self.Exclude<EcsTagPool<TTagComponent>>();
|
||||
}
|
||||
public static EcsTagPool<TTagComponent> Optional<TTagComponent>(this EcsSubjectBuilderBase self) where TTagComponent : struct, IEcsTagComponent
|
||||
{
|
||||
return self.Optional<TTagComponent, EcsTagPool<TTagComponent>>();
|
||||
return self.Optional<EcsTagPool<TTagComponent>>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,8 @@ using System.Runtime.CompilerServices;
|
||||
|
||||
namespace DCFApixels.DragonECS
|
||||
{
|
||||
internal static class WorldMetaStorage
|
||||
//TODO этот класс требует переработки, изначально такая конструкция имела хорошую производительность, но сейчас он слишком раздулся
|
||||
internal static class WorldMetaStorage
|
||||
{
|
||||
private static int _tokenCount = 0;
|
||||
private static List<ResizerBase> _resizers = new List<ResizerBase>();
|
||||
@ -37,15 +38,18 @@ namespace DCFApixels.DragonECS
|
||||
return id;
|
||||
}
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetWorldId<TWorldArchetype>() => WorldIndex<TWorldArchetype>.id;
|
||||
public static int GetWorldID<TWorldArchetype>() => WorldIndex<TWorldArchetype>.id;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetComponentId<T>(int worldID) => Component<T>.Get(worldID);
|
||||
public static int GetComponentID<T>(int worldID) => Component<T>.Get(worldID);
|
||||
public static int GetComponentID(Type type, int worldID) => _metas[worldID].GetComponentID(type);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetSubjectId<T>(int worldID) => Subject<T>.Get(worldID);
|
||||
public static int GetPoolID<T>(int worldID) => Pool<T>.Get(worldID);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetExecutorId<T>(int worldID) => Executor<T>.Get(worldID);
|
||||
public static int GetSubjectID<T>(int worldID) => Subject<T>.Get(worldID);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetWorldComponentId<T>(int worldID) => WorldComponent<T>.Get(worldID);
|
||||
public static int GetExecutorID<T>(int worldID) => Executor<T>.Get(worldID);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetWorldComponentID<T>(int worldID) => WorldComponent<T>.Get(worldID);
|
||||
public static bool IsComponentTypeDeclared(int worldID, Type type) => _metas[worldID].IsDeclaredType(type);
|
||||
public static Type GetComponentType(int worldID, int componentID) => _metas[worldID].GetComponentType(componentID);
|
||||
|
||||
@ -58,14 +62,54 @@ namespace DCFApixels.DragonECS
|
||||
}
|
||||
|
||||
#region Containers
|
||||
public static class PoolComponentIdArrays
|
||||
{
|
||||
private static Dictionary<Type, int[]> _componentTypeArrayPairs = new Dictionary<Type, int[]>();
|
||||
|
||||
public static int[] GetIdsArray(Type type)
|
||||
{
|
||||
int targetSize = _tokenCount;
|
||||
if (!_componentTypeArrayPairs.TryGetValue(type, out int[] result))
|
||||
{
|
||||
result = new int[targetSize];
|
||||
for (int i = 0; i < result.Length; i++)
|
||||
result[i] = -1;
|
||||
_componentTypeArrayPairs.Add(type, result);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(result.Length < targetSize)
|
||||
{
|
||||
int oldSize = result.Length;
|
||||
Array.Resize(ref result, targetSize);
|
||||
ArrayUtility.Fill(result, -1, oldSize, targetSize);
|
||||
_componentTypeArrayPairs[type] = result;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static int GetComponentID(Type type, int token)
|
||||
{
|
||||
GetIdsArray(type);
|
||||
ref int id = ref _componentTypeArrayPairs[type][token];
|
||||
if (id < 0)
|
||||
{
|
||||
var meta = _metas[token];
|
||||
id = meta.componentCount++;
|
||||
meta.AddType(id, type);
|
||||
}
|
||||
return id;
|
||||
}
|
||||
}
|
||||
private static class Pool<T>
|
||||
{
|
||||
public static int[] ids;
|
||||
private static Type componentType = typeof(T).GetGenericArguments()[0];
|
||||
static Pool()
|
||||
{
|
||||
ids = new int[_tokenCount];
|
||||
for (int i = 0; i < ids.Length; i++)
|
||||
ids[i] = -1;
|
||||
ids = PoolComponentIdArrays.GetIdsArray(componentType);
|
||||
_resizers.Add(new Resizer());
|
||||
}
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
@ -74,9 +118,7 @@ namespace DCFApixels.DragonECS
|
||||
ref int id = ref ids[token];
|
||||
if (id < 0)
|
||||
{
|
||||
var meta = _metas[token];
|
||||
id = meta.GetComponentID(typeof(T).GetGenericArguments()[0]);
|
||||
meta.AddType(id, typeof(T));
|
||||
id = PoolComponentIdArrays.GetComponentID(componentType, token);
|
||||
}
|
||||
return id;
|
||||
}
|
||||
@ -86,9 +128,7 @@ namespace DCFApixels.DragonECS
|
||||
public override int[] IDS => ids;
|
||||
public override void Resize(int size)
|
||||
{
|
||||
int oldSize = ids.Length;
|
||||
Array.Resize(ref ids, size);
|
||||
ArrayUtility.Fill(ids, -1, oldSize, size);
|
||||
ids = PoolComponentIdArrays.GetIdsArray(componentType);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -97,9 +137,7 @@ namespace DCFApixels.DragonECS
|
||||
public static int[] ids;
|
||||
static Component()
|
||||
{
|
||||
ids = new int[_tokenCount];
|
||||
for (int i = 0; i < ids.Length; i++)
|
||||
ids[i] = -1;
|
||||
ids = PoolComponentIdArrays.GetIdsArray(typeof(T));
|
||||
_resizers.Add(new Resizer());
|
||||
}
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
@ -108,9 +146,7 @@ namespace DCFApixels.DragonECS
|
||||
ref int id = ref ids[token];
|
||||
if (id < 0)
|
||||
{
|
||||
var meta = _metas[token];
|
||||
id = meta.componentCount++;
|
||||
meta.AddType(id, typeof(T));
|
||||
id = PoolComponentIdArrays.GetComponentID(typeof(T), token);
|
||||
}
|
||||
return id;
|
||||
}
|
||||
@ -120,9 +156,7 @@ namespace DCFApixels.DragonECS
|
||||
public override int[] IDS => ids;
|
||||
public override void Resize(int size)
|
||||
{
|
||||
int oldSize = ids.Length;
|
||||
Array.Resize(ref ids, size);
|
||||
ArrayUtility.Fill(ids, -1, oldSize, size);
|
||||
ids = PoolComponentIdArrays.GetIdsArray(typeof(T));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -218,6 +252,7 @@ namespace DCFApixels.DragonECS
|
||||
}
|
||||
#endregion
|
||||
|
||||
public struct XXX : IEcsComponent { }
|
||||
private class WorldTypeMeta
|
||||
{
|
||||
public int id;
|
||||
@ -239,12 +274,7 @@ namespace DCFApixels.DragonECS
|
||||
public bool IsDeclaredType(Type type) => _declaredComponentTypes.ContainsKey(type);
|
||||
public int GetComponentID(Type type)
|
||||
{
|
||||
if (!IsDeclaredType(type))
|
||||
{
|
||||
RuntimeHelpers.RunClassConstructor(type.TypeHandle);
|
||||
typeof(Component<>).MakeGenericType(type);
|
||||
}
|
||||
return _declaredComponentTypes[type];
|
||||
return PoolComponentIdArrays.GetComponentID(type, id);
|
||||
}
|
||||
public WorldTypeMeta()
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user