Browse Source

Better DynamicEntityDescriptorInfo, going to write how to use it properly on the Svelto.ECS wiki

fixed use of PlatformProfiler
I decided to support empty EntityViewStruct
tags/2.7
sebas77 6 years ago
parent
commit
df48933496
12 changed files with 91 additions and 46 deletions
  1. +18
    -6
      Svelto.ECS/DynamicEntityDescriptorInfo.cs
  2. +5
    -0
      Svelto.ECS/EGID.cs
  3. +0
    -2
      Svelto.ECS/EnginesRoot.Engines.cs
  4. +1
    -1
      Svelto.ECS/EnginesRoot.GenericEntityFactory.cs
  5. +1
    -1
      Svelto.ECS/EnginesRoot.Submission.cs
  6. +18
    -1
      Svelto.ECS/EntitiesDB.cs
  7. +3
    -1
      Svelto.ECS/EntityBuilder.cs
  8. +25
    -0
      Svelto.ECS/EntityDescriptorInfo.cs
  9. +3
    -9
      Svelto.ECS/EntityViewUtility.cs
  10. +3
    -0
      Svelto.ECS/IEntitiesDB.cs
  11. +12
    -4
      Svelto.ECS/IEntityFactory.cs
  12. +2
    -21
      Svelto.ECS/StaticEntityDescriptorInfo.cs

+ 18
- 6
Svelto.ECS/DynamicEntityDescriptorInfo.cs View File

@@ -5,18 +5,18 @@ namespace Svelto.ECS
{
public struct DynamicEntityDescriptorInfo<TType>:IEntityDescriptor where TType : IEntityDescriptor, new()
{
public DynamicEntityDescriptorInfo(FasterList<IEntityBuilder> extraEntities) : this()
public DynamicEntityDescriptorInfo(IEntityBuilder[] extraEntities)
{
DBC.ECS.Check.Require(extraEntities.Count > 0,
DBC.ECS.Check.Require(extraEntities.Length > 0,
"don't use a DynamicEntityDescriptorInfo if you don't need to use extra EntityViews");

var defaultEntities = EntityDescriptorTemplate<TType>.descriptor.entitiesToBuild;
var length = defaultEntities.Length;

entitiesToBuild = new IEntityBuilder[length + extraEntities.Count + 1];
entitiesToBuild = new IEntityBuilder[length + extraEntities.Length + 1];

Array.Copy(defaultEntities, 0, entitiesToBuild, 0, length);
Array.Copy(extraEntities.ToArrayFast(), 0, entitiesToBuild, length, extraEntities.Count);
Array.Copy(defaultEntities, 0, entitiesToBuild, 0, length);
Array.Copy(extraEntities, 0, entitiesToBuild, length, extraEntities.Length);

var _builder = new EntityBuilder<EntityInfoView>
{
@@ -25,7 +25,19 @@ namespace Svelto.ECS
entitiesToBuild[entitiesToBuild.Length - 1] = _builder;
}

public IEntityBuilder[] entitiesToBuild { get; private set; }
public IEntityBuilder[] entitiesToBuild { get; }
}

public class DynamicEntityDescriptorInfoRef<TType>:IEntityDescriptor where TType : IEntityDescriptor, new()
{
public DynamicEntityDescriptorInfoRef(IEntityBuilder[] extraEntities)
{
_dynamicDescriptor = new DynamicEntityDescriptorInfo(extraEntities);
}

public IEntityBuilder[] entitiesToBuild { get { return _dynamicDescriptor.entitiesToBuild } }

DynamicEntityDescriptorInfo<T> _dynamicDescriptor;
}
public struct EntityInfoView : IEntityStruct


+ 5
- 0
Svelto.ECS/EGID.cs View File

@@ -37,6 +37,11 @@ namespace Svelto.ECS
return (long)groupId << 32 | ((long)(uint)entityId & 0xFFFFFFFF);
}

public static implicit operator int(EGID id)
{
return id.entityID;
}

public bool Equals(long other)
{
return _GID == other;


+ 0
- 2
Svelto.ECS/EnginesRoot.Engines.cs View File

@@ -97,8 +97,6 @@ namespace Svelto.ECS
throw new ArgumentException("Not Supported Engine " + engine.ToString());
}

//The T parameter allows to pass datastructure sthat not necessarly are
//defined with IEngine, but must be defined with IEngine implementations
static void AddEngine<T>(T engine, Type[] entityViewTypes,
Dictionary<Type, FasterList<T>> engines) where T:IEngine
{


+ 1
- 1
Svelto.ECS/EnginesRoot.GenericEntityFactory.cs View File

@@ -34,7 +34,7 @@ namespace Svelto.ECS
{
return _weakEngine.Target.BuildEntity(new EGID(entityID, (int)groupStructId), descriptorEntity, implementors);
}
public void PreallocateEntitySpace<T>(ExclusiveGroup.ExclusiveGroupStruct groupStructId, int size) where T : IEntityDescriptor, new()
{
_weakEngine.Target.Preallocate<T>((int)groupStructId, size);


+ 1
- 1
Svelto.ECS/EnginesRoot.Submission.cs View File

@@ -18,7 +18,7 @@ namespace Svelto.ECS
{
void SubmitEntityViews()
{
using (new PlatformProfiler("Svelto.ECS submit"))
using (new PlatformProfiler("Svelto.ECS").Sample("Entities submit"))
{
if (_entitiesOperations.Count > 0)
{


+ 18
- 1
Svelto.ECS/EntitiesDB.cs View File

@@ -22,6 +22,16 @@ namespace Svelto.ECS.Internal
return typeSafeDictionary.Values;
}

public ReadOnlyCollectionStruct<T> QueryEntityViews<T>(ExclusiveGroup @group) where T : class, IEntityStruct
{
return QueryEntityViews<T>((int) group);
}

public T QueryEntityView<T>(int id, ExclusiveGroup @group) where T : class, IEntityStruct
{
return QueryEntityView<T>(new EGID(id, group));
}

public T[] QueryEntities<T>(int @group, out int count) where T : IEntityStruct
{
TypeSafeDictionary<T> typeSafeDictionary;
@@ -115,6 +125,11 @@ namespace Svelto.ECS.Internal
return TryQueryEntityViewInGroupInternal(entityegid, out entityView);
}
public bool TryQueryEntityView<T>(int id, ExclusiveGroup @group, out T entityView) where T : class, IEntityStruct
{
return TryQueryEntityViewInGroupInternal(new EGID(id, group), out entityView);
}
bool TryQueryEntityViewInGroupInternal<T>(EGID entityGID, out T entityView) where T:class, IEntityStruct
{
entityView = null;
@@ -168,7 +183,9 @@ namespace Svelto.ECS.Internal

static ReadOnlyCollectionStruct<T> RetrieveEmptyEntityViewList<T>()
{
return ReadOnlyCollectionStruct<T>.DefaultList;
var arrayFast = FasterList<T>.DefaultList.ToArrayFast();
return new ReadOnlyCollectionStruct<T>(arrayFast, 0);
}

static T[] RetrieveEmptyEntityViewArray<T>()


+ 3
- 1
Svelto.ECS/EntityBuilder.cs View File

@@ -88,6 +88,8 @@ namespace Svelto.ECS
if (needsReflection == true)
{
DBC.ECS.Check.Require(implementors != null, "Implementors not found while building an EntityView");
DBC.ECS.Check.Require(castedDic.ContainsKey(entityID.entityID) == false,
"building an entity with already used entity id! id".FastConcat(entityID).FastConcat(" ", DESCRIPTOR_NAME));

T lentityView;
EntityView<T>.BuildEntityView(entityID, out lentityView);
@@ -96,7 +98,7 @@ namespace Svelto.ECS
, entityViewBlazingFastReflection
, implementors
, DESCRIPTOR_NAME);
castedDic.Add(entityID.entityID, ref lentityView);
}
else


+ 25
- 0
Svelto.ECS/EntityDescriptorInfo.cs View File

@@ -0,0 +1,25 @@
namespace Svelto.ECS
{
public interface IEntityDescriptor
{
IEntityBuilder[] entitiesToBuild { get; }
}

public class EntityDescriptor : IEntityDescriptor
{
private EntityDescriptor()
{}
protected EntityDescriptor(IEntityBuilder[] entityToBuild)
{
entitiesToBuild = entityToBuild;
}
public IEntityBuilder[] entitiesToBuild { get; }
}

static class EntityDescriptorTemplate<TType> where TType : IEntityDescriptor, new()
{
public static readonly StaticEntityDescriptorInfo<TType> descriptor = new StaticEntityDescriptorInfo<TType>(new TType());
}
}

+ 3
- 9
Svelto.ECS/EntityViewUtility.cs View File

@@ -14,15 +14,11 @@ static class EntityViewUtility
{
int count;

//Very efficent way to collect the fields of every EntityViewType
//efficient way to collect the fields of every EntityViewType
var setters =
FasterList<KeyValuePair<Type, ActionCast<T>>>
.NoVirt.ToArrayFast(entityViewBlazingFastReflection, out count);
#if DEBUG && !PROFILER
if (count == 0)
throw new Exception(NO_COMPONENTS_EXCEPTION.FastConcat("Type ", entityDescriptorName, " entityView ",
entityBuilder.GetEntityType().ToString()));
#endif

for (var index = 0; index < implementors.Length; index++)
{
var implementor = implementors[index];
@@ -120,9 +116,7 @@ static class EntityViewUtility
}
#endif
static readonly Dictionary<Type, Type[]> _cachedTypes = new Dictionary<Type, Type[]>();
const string NO_COMPONENTS_EXCEPTION =
"<color=orange>Svelto.ECS</color> An entity view without component interfaces has been found, if you are using an entity view struct or an entity struct, do not pass implementors";

const string DUPLICATE_IMPLEMENTOR_ERROR =
"<color=orange>Svelto.ECS</color> the same component is implemented with more than one implementor. This is considered an error and MUST be fixed. ";



+ 3
- 0
Svelto.ECS/IEntitiesDB.cs View File

@@ -10,18 +10,21 @@ namespace Svelto.ECS
/// over EntityView
/// </summary>
ReadOnlyCollectionStruct<T> QueryEntityViews<T>(int group) where T : class, IEntityStruct;
ReadOnlyCollectionStruct<T> QueryEntityViews<T>(ExclusiveGroup group) where T : class, IEntityStruct;
/// <summary>
/// All the EntityView related methods are left for back compatibility, but
/// shouldn't be used anymore. Always pick EntityViewStruct or EntityStruct
/// over EntityView
/// </summary>
bool TryQueryEntityView<T>(EGID egid, out T entityView) where T : class, IEntityStruct;
bool TryQueryEntityView<T>(int id, ExclusiveGroup group, out T entityView) where T : class, IEntityStruct;
/// <summary>
/// All the EntityView related methods are left for back compatibility, but
/// shouldn't be used anymore. Always pick EntityViewStruct or EntityStruct
/// over EntityView
/// </summary>
T QueryEntityView<T>(EGID egid) where T : class, IEntityStruct;
T QueryEntityView<T>(int id, ExclusiveGroup group) where T : class, IEntityStruct;
/// <summary>
/// Fast and raw (therefore not safe) return of entities buffer
/// Modifying a buffer would compromise the integrity of the whole DB


+ 12
- 4
Svelto.ECS/IEntityFactory.cs View File

@@ -22,7 +22,8 @@ namespace Svelto.ECS
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="size"></param>
void PreallocateEntitySpace<T>(ExclusiveGroup.ExclusiveGroupStruct groupStructId, int size) where T : IEntityDescriptor, new();
void PreallocateEntitySpace<T>(ExclusiveGroup.ExclusiveGroupStruct groupStructId,
int size) where T : IEntityDescriptor, new();
/// <summary>
/// The EntityDescriptor doesn't need to be ever instantiated. It just describes the Entity
@@ -30,7 +31,7 @@ namespace Svelto.ECS
/// references of the EntityViews components. Please read the articles on my blog
/// to understand better the terminologies
/// Using this function is like building a normal entity, but the entity views
/// are grouped by groupID to be more efficently processed inside engines and
/// are grouped by groupID to be more efficiently processed inside engines and
/// improve cache locality. Either class entityViews and struct entityViews can be
/// grouped.
/// </summary>
@@ -38,7 +39,10 @@ namespace Svelto.ECS
/// <param name="groupStructId"></param>
/// <param name="ed"></param>
/// <param name="implementors"></param>
EntityStructInitializer BuildEntity<T>(int entityID, ExclusiveGroup.ExclusiveGroupStruct groupStructId, object[] implementors) where T:IEntityDescriptor, new();
EntityStructInitializer BuildEntity<T>(int entityID,
ExclusiveGroup.ExclusiveGroupStruct groupStructId,
object[] implementors)
where T : IEntityDescriptor, new();
EntityStructInitializer BuildEntity<T>(EGID egid, object[] implementors) where T:IEntityDescriptor, new();
/// <summary>
/// When the type of the entity is not known (this is a special case!) an EntityDescriptorInfo
@@ -48,7 +52,11 @@ namespace Svelto.ECS
/// <param name="entityDescriptor"></param>
/// <param name="implementors"></param>
///
EntityStructInitializer BuildEntity<T>(int entityID, ExclusiveGroup.ExclusiveGroupStruct groupStructId, T descriptorEntity, object[] implementors) where T:IEntityDescriptor;
EntityStructInitializer BuildEntity<T>(int entityID,
ExclusiveGroup.ExclusiveGroupStruct groupStructId,
T descriptorEntity,
object[] implementors)
where T : IEntityDescriptor;
EntityStructInitializer BuildEntity<T>(EGID egid, T entityDescriptor, object[] implementors) where T:IEntityDescriptor;
}
}

+ 2
- 21
Svelto.ECS/StaticEntityDescriptorInfo.cs View File

@@ -1,25 +1,5 @@
namespace Svelto.ECS
{
public interface IEntityDescriptor
{
IEntityBuilder[] entitiesToBuild { get; }
}

public class EntityDescriptor : IEntityDescriptor
{
protected EntityDescriptor(IEntityBuilder[] entityToBuild)
{
this.entitiesToBuild = entityToBuild;
}

public IEntityBuilder[] entitiesToBuild { get; private set; }
}

public static class EntityDescriptorTemplate<TType> where TType : IEntityDescriptor, new()
{
public static readonly StaticEntityDescriptorInfo<TType> descriptor = new StaticEntityDescriptorInfo<TType>(new TType());
}

public class StaticEntityDescriptorInfo<TType>: IEntityDescriptor where TType : IEntityDescriptor
{
internal StaticEntityDescriptorInfo(TType descriptor)
@@ -27,6 +7,7 @@ namespace Svelto.ECS
entitiesToBuild = descriptor.entitiesToBuild;
}

public IEntityBuilder[] entitiesToBuild { get; private set; }
public IEntityBuilder[] entitiesToBuild { get; }
}
}


Loading…
Cancel
Save