public static EntityGroupAssetBase ExtractSceneClone(EntityGroupAssetBase source, Guid sourceRootEntity) { if (source == null) throw new ArgumentNullException("source"); // Note: Instead of copying the whole asset (with its potentially big hierarchy), we first copy the asset only (without the hierarchy), then the sub-hierarchy to extract. // create the hierarchy of the sub-tree var subTreeRoot = source.Hierarchy.Entities[sourceRootEntity].Entity; var subTreeHierarchy = new EntityHierarchyData { Entities = { subTreeRoot }, RootEntities = { sourceRootEntity } }; foreach (var subTreeEntity in subTreeRoot.EnumerateChildren(true)) subTreeHierarchy.Entities.Add(source.Hierarchy.Entities[subTreeEntity.Id]); // clone the entities of the sub-tree var clonedHierarchy = (EntityHierarchyData)AssetCloner.Clone(subTreeHierarchy); clonedHierarchy.Entities[sourceRootEntity].Entity.Transform.Parent = null; // set to null reference outside of the sub-tree EntityAnalysis.FixupEntityReferences(clonedHierarchy); // temporary nullify the hierarchy to avoid to clone it var sourceHierarchy = source.Hierarchy; source.Hierarchy = null; // clone asset without hierarchy var clonedAsset = (EntityGroupAssetBase)AssetCloner.Clone(source); clonedAsset.Hierarchy = clonedHierarchy; // revert the source hierarchy source.Hierarchy = sourceHierarchy; return clonedAsset; }
public static Result Visit(EntityHierarchyData entityHierarchy) { if (entityHierarchy == null) throw new ArgumentNullException("entityHierarchy"); var entityReferenceVistor = new EntityReferenceAnalysis(); entityReferenceVistor.Visit(entityHierarchy); return entityReferenceVistor.Result; }
private static void FixupEntityReferences(object rootToVisit, EntityHierarchyData entityHierarchy) { var entityAnalysisResult = Visit(rootToVisit); // Reverse the list, so that we can still properly update everything // (i.e. if we have a[0], a[1], a[1].Test, we have to do it from back to front to be valid at each step) entityAnalysisResult.EntityReferences.Reverse(); // Updates Entity/EntityComponent references foreach (var entityLink in entityAnalysisResult.EntityReferences) { object obj = null; if (entityLink.EntityComponent != null) { var containingEntity = entityLink.EntityComponent.Entity; if (containingEntity == null) { throw new InvalidOperationException("Found a reference to a component which doesn't have any entity"); } EntityDesign realEntity; if (entityHierarchy.Entities.TryGetValue(containingEntity.Id, out realEntity)) { var componentId = IdentifiableHelper.GetId(entityLink.EntityComponent); obj = realEntity.Entity.Components.FirstOrDefault(c => IdentifiableHelper.GetId(c) == componentId); if (obj == entityLink.EntityComponent) { continue; } } } else { EntityDesign realEntity; if (entityHierarchy.Entities.TryGetValue(entityLink.Entity.Id, out realEntity)) { obj = realEntity.Entity; // If we already have the proper item, let's skip if (obj == entityLink.Entity) continue; } } if (obj != null) { // We could find the referenced item, let's use it entityLink.Path.Apply(rootToVisit, MemberPathAction.ValueSet, obj); } else { // Item could not be found, let's null it entityLink.Path.Apply(rootToVisit, MemberPathAction.ValueClear, null); } } }
public EntityDiffNode(EntityHierarchyData entityHierarchy) { if (entityHierarchy.RootEntities.Count > 0) { Children = new List<EntityDiffNode>(); foreach (var entity in entityHierarchy.RootEntities) { Children.Add(new EntityDiffNode(entityHierarchy, entity)); } } }
public EntityDiffNode(EntityHierarchyData entityHierarchy) { if (entityHierarchy.RootEntities.Count > 0) { Children = new List <EntityDiffNode>(); foreach (var entity in entityHierarchy.RootEntities) { Children.Add(new EntityDiffNode(entityHierarchy, entity)); } } }
public static Result Visit(EntityHierarchyData entityHierarchy) { if (entityHierarchy == null) { throw new ArgumentNullException("obj"); } var entityReferenceVistor = new EntityReferenceAnalysis(); entityReferenceVistor.Visit(entityHierarchy); return(entityReferenceVistor.Result); }
static Entity FindParent(EntityHierarchyData hierarchy, Entity entity) { // Note: we could also use a cache, but it's probably not worth it... (except if we had tens of thousands of new objects at once) // Let's optimize if really needed foreach (var currentEntity in hierarchy.Entities) { var transformationComponent = currentEntity.Entity.Get(TransformComponent.Key); if (transformationComponent == null) continue; if (transformationComponent.Children.Any(x => x.Entity == entity)) return currentEntity.Entity; } return null; }
public static EntityAsset ExtractSceneClone(EntityAsset source, Guid sourceRootEntity) { if (source == null) { throw new ArgumentNullException("source"); } // Note: Instead of copying the whole asset (with its potentially big hierarchy), we first copy the asset only (without the hierarchy), then the sub-hierarchy to extract. // create the hierarchy of the sub-tree var subTreeRoot = source.Hierarchy.Entities[sourceRootEntity]; var subTreeHierarchy = new EntityHierarchyData { Entities = { subTreeRoot }, RootEntities = { sourceRootEntity } }; foreach (var subTreeEntity in subTreeRoot.EnumerateChildren(true)) { subTreeHierarchy.Entities.Add(subTreeEntity); } // clone the entities of the sub-tree var clonedHierarchy = (EntityHierarchyData)AssetCloner.Clone(subTreeHierarchy); clonedHierarchy.Entities[sourceRootEntity].Transform.Parent = null; // set to null reference outside of the sub-tree EntityAnalysis.FixupEntityReferences(clonedHierarchy); // temporary nullify the hierarchy to avoid to clone it var sourceHierarchy = source.Hierarchy; source.Hierarchy = null; // clone asset without hierarchy var clonedAsset = (EntityAsset)AssetCloner.Clone(source); clonedAsset.Hierarchy = clonedHierarchy; // revert the source hierarchy source.Hierarchy = sourceHierarchy; return(clonedAsset); }
static Entity FindParent(EntityHierarchyData hierarchy, Entity entity) { // Note: we could also use a cache, but it's probably not worth it... (except if we had tens of thousands of new objects at once) // Let's optimize if really needed foreach (var currentEntity in hierarchy.Entities) { var transformationComponent = currentEntity.Get(TransformComponent.Key); if (transformationComponent == null) { continue; } if (transformationComponent.Children.Any(x => x.Entity == entity)) { return(currentEntity); } } return(null); }
public EntityDiffNode(EntityHierarchyData entityHierarchy, Guid entityGuid) { var entityData = entityHierarchy.Entities[entityGuid]; Guid = entityGuid; Name = entityData.Name; var transformationComponent = entityData.Get(TransformComponent.Key); if (transformationComponent != null) { Children = new List<EntityDiffNode>(transformationComponent.Children.Count); // Build children var children = transformationComponent.Children; for (int i = 0; i < children.Count; ++i) { Children.Add(new EntityDiffNode(entityHierarchy, children[i].Entity.Id)); } } }
public EntityDiffNode(EntityHierarchyData entityHierarchy, Guid entityGuid) { var entityData = entityHierarchy.Entities[entityGuid]; Guid = entityGuid; Name = entityData.Name; var transformationComponent = entityData.Get(TransformComponent.Key); if (transformationComponent != null) { Children = new List <EntityDiffNode>(transformationComponent.Children.Count); // Build children var children = transformationComponent.Children; for (int i = 0; i < children.Count; ++i) { Children.Add(new EntityDiffNode(entityHierarchy, children[i].Entity.Id)); } } }
/// <summary> /// Remaps the entities identifier. /// </summary> /// <param name="entityHierarchy">The entity hierarchy.</param> /// <param name="idRemapping">The identifier remapping.</param> public static void RemapEntitiesId(EntityHierarchyData entityHierarchy, Dictionary <Guid, Guid> idRemapping) { Guid newId; // Remap entities in asset2 with new Id if (idRemapping.TryGetValue(entityHierarchy.RootEntity, out newId)) { entityHierarchy.RootEntity = newId; } foreach (var entity in entityHierarchy.Entities) { if (idRemapping.TryGetValue(entity.Id, out newId)) { entity.Id = newId; } } // Sort again the EntityCollection (since ID changed) entityHierarchy.Entities.Sort(); }
public EntityAsset() { Hierarchy = new EntityHierarchyData(); }
public static void UpdateEntityReferences(EntityHierarchyData entityHierarchy) { // TODO: Either remove this function or make it do something! }
/// <summary> /// Remaps the entities identifier. /// </summary> /// <param name="entityHierarchy">The entity hierarchy.</param> /// <param name="idRemapping">The identifier remapping.</param> public static void RemapEntitiesId(EntityHierarchyData entityHierarchy, Dictionary<Guid, Guid> idRemapping) { Guid newId; // Remap entities in asset2 with new Id for (int i = 0; i < entityHierarchy.RootEntities.Count; ++i) { if (idRemapping.TryGetValue(entityHierarchy.RootEntities[i], out newId)) entityHierarchy.RootEntities[i] = newId; } foreach (var entity in entityHierarchy.Entities) { if (idRemapping.TryGetValue(entity.Entity.Id, out newId)) entity.Entity.Id = newId; } // Sort again the EntityCollection (since ID changed) entityHierarchy.Entities.Sort(); }
public static void FixupEntityReferences(EntityHierarchyData hierarchyData) { FixupEntityReferences(hierarchyData, hierarchyData); }
/// <summary> /// Updates <see cref="EntityReference.Id"/>, <see cref="EntityReference.Name"/>, <see cref="EntityComponentReference{T}.Entity"/> /// and <see cref="EntityComponentReference{T}.Component"/>, while also checking integrity of given <see cref="EntityAsset"/>. /// </summary> /// <param name="entityHierarchy">The entity asset.</param> public static void UpdateEntityReferences(EntityHierarchyData entityHierarchy) { }
public EntityDiffNode(EntityHierarchyData entityHierarchy) : this(entityHierarchy, entityHierarchy.RootEntity) { }
public EntityAsset() { SerializedVersion = AssetFormatVersion; Hierarchy = new EntityHierarchyData(); }
/// <summary> /// Fixups the entity references, by clearing invalid <see cref="EntityReference.Id"/>, and updating <see cref="EntityReference.Value"/> (same for components). /// </summary> /// <param name="entityHierarchy">The entity asset.</param> public static void FixupEntityReferences(EntityHierarchyData entityHierarchy) { var entityAnalysisResult = Visit(entityHierarchy); // Reverse the list, so that we can still properly update everything // (i.e. if we have a[0], a[1], a[1].Test, we have to do it from back to front to be valid at each step) entityAnalysisResult.EntityReferences.Reverse(); // Updates Entity/EntityComponent references foreach (var entityLink in entityAnalysisResult.EntityReferences) { object obj = null; if (entityLink.EntityComponent != null) { var containingEntity = entityLink.EntityComponent.Entity; if (containingEntity == null) { throw new InvalidOperationException("Found a reference to a component which doesn't have any entity"); } Entity realEntity; if (entityHierarchy.Entities.TryGetValue(containingEntity.Id, out realEntity) && realEntity.Components.TryGetValue(entityLink.EntityComponent.GetDefaultKey(), out obj)) { // If we already have the proper item, let's skip if (obj == entityLink.EntityComponent) continue; } } else if (entityLink.EntityScript != null) { var containingEntity = entityLink.EntityScript.Entity; if (containingEntity == null) { throw new InvalidOperationException("Found a reference to a script which doesn't have any entity"); } Entity realEntity; ScriptComponent scriptComponent; if (entityHierarchy.Entities.TryGetValue(containingEntity.Id, out realEntity) && realEntity.Components.TryGetValue(ScriptComponent.Key, out scriptComponent)) { obj = scriptComponent.Scripts.FirstOrDefault(x => x.Id == entityLink.EntityScript.Id); if (obj == entityLink.EntityScript) continue; } } else { Entity realEntity; if (entityHierarchy.Entities.TryGetValue(entityLink.Entity.Id, out realEntity)) { obj = realEntity; // If we already have the proper item, let's skip if (obj == entityLink.Entity) continue; } } if (obj != null) { // We could find the referenced item, let's use it entityLink.Path.Apply(entityHierarchy, MemberPathAction.ValueSet, obj); } else { // Item could not be found, let's null it entityLink.Path.Apply(entityHierarchy, MemberPathAction.ValueClear, null); } } }
public EntityTreeAsset(EntityHierarchyData hierarchy) { Root = new EntityDiffNode(hierarchy); }
protected EntityGroupAssetBase() { Hierarchy = new EntityHierarchyData(); }
public EntityDictionary(EntityHierarchyData source) { this.source = source; }
/// <summary> /// Fixups the entity references, by clearing invalid <see cref="EntityReference.Id"/>, and updating <see cref="EntityReference.Value"/> (same for components). /// </summary> /// <param name="entityHierarchy">The entity asset.</param> public static void FixupEntityReferences(EntityHierarchyData entityHierarchy) { var entityAnalysisResult = Visit(entityHierarchy); // Reverse the list, so that we can still properly update everything // (i.e. if we have a[0], a[1], a[1].Test, we have to do it from back to front to be valid at each step) entityAnalysisResult.EntityReferences.Reverse(); // Updates Entity/EntityComponent references foreach (var entityLink in entityAnalysisResult.EntityReferences) { object obj = null; if (entityLink.EntityComponent != null) { var containingEntity = entityLink.EntityComponent.Entity; if (containingEntity == null) { throw new InvalidOperationException("Found a reference to a component which doesn't have any entity"); } Entity realEntity; if (entityHierarchy.Entities.TryGetValue(containingEntity.Id, out realEntity) && realEntity.Components.TryGetValue(entityLink.EntityComponent.GetDefaultKey(), out obj)) { // If we already have the proper item, let's skip if (obj == entityLink.EntityComponent) { continue; } } } else if (entityLink.EntityScript != null) { var containingEntity = entityLink.EntityScript.Entity; if (containingEntity == null) { throw new InvalidOperationException("Found a reference to a script which doesn't have any entity"); } Entity realEntity; ScriptComponent scriptComponent; if (entityHierarchy.Entities.TryGetValue(containingEntity.Id, out realEntity) && realEntity.Components.TryGetValue(ScriptComponent.Key, out scriptComponent)) { obj = scriptComponent.Scripts.FirstOrDefault(x => x.Id == entityLink.EntityScript.Id); if (obj == entityLink.EntityScript) { continue; } } } else { Entity realEntity; if (entityHierarchy.Entities.TryGetValue(entityLink.Entity.Id, out realEntity)) { obj = realEntity; // If we already have the proper item, let's skip if (obj == entityLink.Entity) { continue; } } } if (obj != null) { // We could find the referenced item, let's use it entityLink.Path.Apply(entityHierarchy, MemberPathAction.ValueSet, obj); } else { // Item could not be found, let's null it entityLink.Path.Apply(entityHierarchy, MemberPathAction.ValueClear, null); } } }