diff --git a/Svelto.ECS/EnginesRoot.Entities.cs b/Svelto.ECS/EnginesRoot.Entities.cs index c7531dc..e82e69d 100644 --- a/Svelto.ECS/EnginesRoot.Entities.cs +++ b/Svelto.ECS/EnginesRoot.Entities.cs @@ -61,10 +61,8 @@ namespace Svelto.ECS CheckAddEntityID(entityID, entityDescriptor); - var dic = EntityFactory.BuildGroupedEntities(entityID, - _groupedEntityToAdd.current, - descriptorEntitiesToBuild, - implementors); + var dic = EntityFactory.BuildGroupedEntities(entityID, _groupedEntityToAdd.current, + descriptorEntitiesToBuild, implementors); return new EntityStructInitializer(entityID, dic); } @@ -86,13 +84,12 @@ namespace Svelto.ECS if (_groupedEntityToAdd.current.TryGetValue(groupID, out @groupBuffer) == false) @groupBuffer = _groupedEntityToAdd.current[groupID] = new Dictionary(); - ITypeSafeDictionary dbList; - for (var index = 0; index < count; index++) { var entityViewBuilder = entityViewsToBuild[index]; var entityViewType = entityViewBuilder.GetEntityType(); + ITypeSafeDictionary dbList; if (group.TryGetValue(entityViewType, out dbList) == false) group[entityViewType] = entityViewBuilder.Preallocate(ref dbList, size); else diff --git a/Svelto.ECS/EnginesRoot.Submission.cs b/Svelto.ECS/EnginesRoot.Submission.cs index c03facc..787dba2 100644 --- a/Svelto.ECS/EnginesRoot.Submission.cs +++ b/Svelto.ECS/EnginesRoot.Submission.cs @@ -72,35 +72,34 @@ namespace Svelto.ECS } } } - } - try + if (_groupedEntityToAdd.current.Count > 0) { - if (_groupedEntityToAdd.current.Count > 0) + using (profiler.Sample("Add")) { - using (profiler.Sample("Add")) - { - //use other as source from now on current will be use to write new entityViews - _groupedEntityToAdd.Swap(); + //use other as source from now on current will be use to write new entityViews + _groupedEntityToAdd.Swap(); + try + { //Note: if N entity of the same type are added on the same frame the Add callback is called N //times on the same frame. if the Add callback builds a new entity, that entity will not //be available in the database until the N callbacks are done solving it could be complicated as //callback and database update must be interleaved. AddEntityViewsToTheDBAndSuitableEngines(_groupedEntityToAdd.other); } + catch (Exception e) + { + Console.LogException(e); + } + finally + { + //other can be cleared now, but let's avoid deleting the dictionary every time + _groupedEntityToAdd.ClearOther(); + } } } - catch (Exception e) - { - Console.LogException(e); - } - finally - { - //other can be cleared now, but let's avoid deleting the dictionary every time - _groupedEntityToAdd.ClearOther(); - } } } diff --git a/Svelto.ECS/EntityBuilder.cs b/Svelto.ECS/EntityBuilder.cs index 533dc70..dc7a068 100644 --- a/Svelto.ECS/EntityBuilder.cs +++ b/Svelto.ECS/EntityBuilder.cs @@ -91,17 +91,16 @@ namespace Svelto.ECS { 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)); + "building an entity with already used entity id! id".FastConcat(entityID).FastConcat(" ", ENTITY_VIEW_NAME)); - T lentityView; - EntityView.BuildEntityView(entityID, out lentityView); + T entityView; + EntityView.BuildEntityView(entityID, out entityView); - this.FillEntityView(ref lentityView + this.FillEntityView(ref entityView , entityViewBlazingFastReflection - , implementors - , DESCRIPTOR_NAME); + , implementors); - castedDic.Add(entityID.entityID, ref lentityView); + castedDic.Add(entityID.entityID, ref entityView); } else { @@ -136,11 +135,11 @@ namespace Svelto.ECS get { return EntityView.cachedFields; } } - static readonly Type ENTITY_VIEW_TYPE = typeof(T); - static readonly string DESCRIPTOR_NAME = ENTITY_VIEW_TYPE.ToString(); - static readonly bool needsReflection = typeof(IEntityViewStruct).IsAssignableFrom(typeof(T)); - static readonly T defaultIt = default(T); - static readonly Type EGIDType = typeof(Svelto.ECS.EGID); + static readonly Type ENTITY_VIEW_TYPE = typeof(T); + static readonly string ENTITY_VIEW_NAME = ENTITY_VIEW_TYPE.ToString(); + static readonly bool needsReflection = typeof(IEntityViewStruct).IsAssignableFrom(typeof(T)); + static readonly T defaultIt = default(T); + static readonly Type EGIDType = typeof(EGID); internal T _initializer; } diff --git a/Svelto.ECS/EntityViewUtility.cs b/Svelto.ECS/EntityViewUtility.cs index 6757d1b..71dff37 100644 --- a/Svelto.ECS/EntityViewUtility.cs +++ b/Svelto.ECS/EntityViewUtility.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using Svelto.DataStructures; using Svelto.ECS; +using Svelto.ECS.Internal; using Svelto.Utilities; static class EntityViewUtility @@ -9,8 +10,7 @@ static class EntityViewUtility public static void FillEntityView(this IEntityBuilder entityBuilder , ref T entityView , FasterList>> entityViewBlazingFastReflection - , object[] implementors - , string entityDescriptorName) + , object[] implementors) { int count; @@ -35,10 +35,13 @@ static class EntityViewUtility { var componentType = interfaces[iindex]; #if DEBUG && !PROFILER - Tuple implementorHolder; + Tuple implementorData; - if (implementorsByType.TryGetValue(componentType, out implementorHolder)) - implementorHolder.numberOfImplementations++; + if (implementorsByType.TryGetValue(componentType, out implementorData)) + { + implementorData.numberOfImplementations++; + implementorsByType[componentType] = implementorData; + } else implementorsByType[componentType] = new Tuple(implementor, 1); #else @@ -49,7 +52,7 @@ static class EntityViewUtility #if DEBUG && !PROFILER else { - Svelto.Utilities.Console.Log(NULL_IMPLEMENTOR_ERROR.FastConcat("Type ", entityDescriptorName, " entityView ", + Svelto.Utilities.Console.Log(NULL_IMPLEMENTOR_ERROR.FastConcat(" entityView ", entityBuilder.GetEntityType().ToString())); } #endif @@ -68,20 +71,17 @@ static class EntityViewUtility if (implementorsByType.TryGetValue(fieldType, out component) == false) { - var e = new Exception(NOT_FOUND_EXCEPTION + " Component Type: " + fieldType.Name + - " - EntityView: " + entityBuilder.GetEntityType().Name + - " - EntityDescriptor " + entityDescriptorName); + var e = new ECSException(NOT_FOUND_EXCEPTION + " Component Type: " + fieldType.Name + + " - EntityView: " + entityBuilder.GetEntityType().Name); throw e; } #if DEBUG && !PROFILER if (component.numberOfImplementations > 1) - Svelto.Utilities.Console.LogError(DUPLICATE_IMPLEMENTOR_ERROR.FastConcat( - "Component Type: ", fieldType.Name, - " implementor: ", - component.implementorType.ToString()) + - " - EntityView: " + - entityBuilder.GetEntityType().Name + " - EntityDescriptor " + entityDescriptorName); + throw new ECSException(DUPLICATE_IMPLEMENTOR_ERROR.FastConcat( + "Component Type: ", fieldType.Name, " implementor: ", + component.implementorType.ToString()) + " - EntityView: " + + entityBuilder.GetEntityType().Name); #endif #if DEBUG && !PROFILER fieldSetter.Value(ref entityView, component.implementorType); @@ -94,7 +94,7 @@ static class EntityViewUtility } - //this is used to avoid newing a dictionary every time, but it's used locally only and it's clearead for each use + //this is used to avoid newing a dictionary every time, but it's used locally only and it's cleared for each use #if DEBUG && !PROFILER static readonly Dictionary> implementorsByType = new Dictionary>(); @@ -118,10 +118,12 @@ static class EntityViewUtility static readonly Dictionary _cachedTypes = new Dictionary(); const string DUPLICATE_IMPLEMENTOR_ERROR = - "Svelto.ECS the same component is implemented with more than one implementor. This is considered an error and MUST be fixed. "; + "Svelto.ECS the same component is implemented with more than one implementor. This is " + + "considered an error and MUST be fixed. "; const string NULL_IMPLEMENTOR_ERROR = - "Svelto.ECS Null implementor, please be careful about the implementors passed to avoid performance loss "; + "Svelto.ECS Null implementor, please be careful about the implementors passed to avoid " + + "performance loss "; const string NOT_FOUND_EXCEPTION = "Svelto.ECS Implementor not found for an EntityView. "; } \ No newline at end of file diff --git a/Svelto.ECS/GenericEntityDescriptor.cs b/Svelto.ECS/GenericEntityDescriptor.cs index a280bba..d483450 100644 --- a/Svelto.ECS/GenericEntityDescriptor.cs +++ b/Svelto.ECS/GenericEntityDescriptor.cs @@ -37,7 +37,12 @@ { static GenericEntityDescriptor() { - _entityBuilders = new IEntityBuilder[] {new EntityBuilder(), new EntityBuilder(), new EntityBuilder()}; + _entityBuilders = new IEntityBuilder[] + { + new EntityBuilder(), + new EntityBuilder(), + new EntityBuilder() + }; } public IEntityBuilder[] entitiesToBuild @@ -55,7 +60,13 @@ { static GenericEntityDescriptor() { - _entityBuilders = new IEntityBuilder[] {new EntityBuilder(), new EntityBuilder(), new EntityBuilder(), new EntityBuilder()}; + _entityBuilders = new IEntityBuilder[] + { + new EntityBuilder(), + new EntityBuilder(), + new EntityBuilder(), + new EntityBuilder() + }; } public IEntityBuilder[] entitiesToBuild @@ -74,7 +85,14 @@ { static GenericEntityDescriptor() { - _entityBuilders = new IEntityBuilder[] {new EntityBuilder(), new EntityBuilder(), new EntityBuilder(), new EntityBuilder(), new EntityBuilder()}; + _entityBuilders = new IEntityBuilder[] + { + new EntityBuilder(), + new EntityBuilder(), + new EntityBuilder(), + new EntityBuilder(), + new EntityBuilder() + }; } public IEntityBuilder[] entitiesToBuild @@ -94,7 +112,15 @@ { static GenericEntityDescriptor() { - _entityBuilders = new IEntityBuilder[] {new EntityBuilder(), new EntityBuilder(), new EntityBuilder(), new EntityBuilder(), new EntityBuilder(), new EntityBuilder()}; + _entityBuilders = new IEntityBuilder[] + { + new EntityBuilder(), + new EntityBuilder(), + new EntityBuilder(), + new EntityBuilder(), + new EntityBuilder(), + new EntityBuilder() + }; } public IEntityBuilder[] entitiesToBuild