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;
        }
Ejemplo n.º 2
0
        public static Result Visit(EntityHierarchyData entityHierarchy)
        {
            if (entityHierarchy == null) throw new ArgumentNullException("entityHierarchy");

            var entityReferenceVistor = new EntityReferenceAnalysis();
            entityReferenceVistor.Visit(entityHierarchy);

            return entityReferenceVistor.Result;
        }
Ejemplo n.º 3
0
        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);
                }
            }
        }
Ejemplo n.º 4
0
        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));
                }
            }
        }
Ejemplo n.º 5
0
        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));
                }
            }
        }
Ejemplo n.º 6
0
        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;
        }
Ejemplo n.º 8
0
        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);
        }
Ejemplo n.º 9
0
        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);
        }
Ejemplo n.º 10
0
        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));
                }
            }
        }
Ejemplo n.º 11
0
        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));
                }
            }
        }
Ejemplo n.º 12
0
        /// <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();
        }
Ejemplo n.º 13
0
 public EntityAsset()
 {
     Hierarchy = new EntityHierarchyData();
 }
Ejemplo n.º 14
0
 public static void UpdateEntityReferences(EntityHierarchyData entityHierarchy)
 {
     // TODO: Either remove this function or make it do something!
 }
Ejemplo n.º 15
0
        /// <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();
        }
Ejemplo n.º 16
0
 public static void FixupEntityReferences(EntityHierarchyData hierarchyData)
 {
     FixupEntityReferences(hierarchyData, hierarchyData);
 }
Ejemplo n.º 17
0
 /// <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)
 {
 }
Ejemplo n.º 18
0
 public EntityDiffNode(EntityHierarchyData entityHierarchy)
     : this(entityHierarchy, entityHierarchy.RootEntity)
 {
 }
Ejemplo n.º 19
0
 public EntityAsset()
 {
     SerializedVersion = AssetFormatVersion;
     Hierarchy = new EntityHierarchyData();
 }
Ejemplo n.º 20
0
 /// <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)
 {
 }
Ejemplo n.º 21
0
 public EntityAsset()
 {
     Hierarchy = new EntityHierarchyData();
 }
Ejemplo n.º 22
0
        /// <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);
                }
            }
        }
Ejemplo n.º 23
0
 public EntityTreeAsset(EntityHierarchyData hierarchy)
 {
     Root = new EntityDiffNode(hierarchy);
 }
Ejemplo n.º 24
0
 public EntityAsset()
 {
     SerializedVersion = AssetFormatVersion;
     Hierarchy         = new EntityHierarchyData();
 }
Ejemplo n.º 25
0
 protected EntityGroupAssetBase()
 {
     Hierarchy = new EntityHierarchyData();
 }
Ejemplo n.º 26
0
 public EntityTreeAsset(EntityHierarchyData hierarchy)
 {
     Root = new EntityDiffNode(hierarchy);
 }
 public EntityDictionary(EntityHierarchyData source)
 {
     this.source = source;
 }
Ejemplo n.º 28
0
 public EntityDiffNode(EntityHierarchyData entityHierarchy)
     : this(entityHierarchy, entityHierarchy.RootEntity)
 {
 }
Ejemplo n.º 29
0
        /// <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);
                }
            }
        }
Ejemplo n.º 30
0
 public static void UpdateEntityReferences(EntityHierarchyData entityHierarchy)
 {
     // TODO: Either remove this function or make it do something!
 }