public static YamlAssetPath ConvertPath(GraphNodePath path, int inNonIdentifiableType)
        {
            var currentNode = (AssetNode)path.RootNode;
            var result      = new YamlAssetPath();
            var i           = 0;

            foreach (var item in path.Path)
            {
                switch (item.Type)
                {
                case GraphNodePath.ElementType.Member:
                    var member = (string)item.Value;
                    result.PushMember(member);
                    currentNode = (AssetNode)((IGraphNode)currentNode).TryGetChild(member);
                    break;

                case GraphNodePath.ElementType.Target:
                    if (i < path.Path.Count - 1)
                    {
                        currentNode = (AssetNode)((IGraphNode)currentNode).Target;
                    }
                    break;

                case GraphNodePath.ElementType.Index:
                    var index = (Index)item.Value;
                    if (inNonIdentifiableType > 0 || currentNode.IsNonIdentifiableCollectionContent)
                    {
                        result.PushIndex(index.Value);
                    }
                    else
                    {
                        var id = currentNode.IndexToId(index);
                        // Create a new id if we don't have any so far
                        if (id == ItemId.Empty)
                        {
                            id = ItemId.New();
                        }
                        result.PushItemId(id);
                    }
                    if (i < path.Path.Count - 1)
                    {
                        currentNode = (AssetNode)((IGraphNode)currentNode).IndexedTarget(index);
                    }
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
                ++i;
            }
            return(result);
        }
 public static YamlAssetPath ConvertPath(GraphNodePath path, int inNonIdentifiableType)
 {
     var currentNode = (AssetNode)path.RootNode;
     var result = new YamlAssetPath();
     var i = 0;
     foreach (var item in path.Path)
     {
         switch (item.Type)
         {
             case GraphNodePath.ElementType.Member:
                 var member = (string)item.Value;
                 result.PushMember(member);
                 currentNode = (AssetNode)((IGraphNode)currentNode).TryGetChild(member);
                 break;
             case GraphNodePath.ElementType.Target:
                 if (i < path.Path.Count - 1)
                 {
                     currentNode = (AssetNode)((IGraphNode)currentNode).Target;
                 }
                 break;
             case GraphNodePath.ElementType.Index:
                 var index = (Index)item.Value;
                 if (inNonIdentifiableType > 0 || currentNode.IsNonIdentifiableCollectionContent)
                 {
                     result.PushIndex(index.Value);
                 }
                 else
                 {
                     var id = currentNode.IndexToId(index);
                     // Create a new id if we don't have any so far
                     if (id == ItemId.Empty)
                         id = ItemId.New();
                     result.PushItemId(id);
                 }
                 if (i < path.Path.Count - 1)
                 {
                     currentNode = (AssetNode)((IGraphNode)currentNode).IndexedTarget(index);
                 }
                 break;
             default:
                 throw new ArgumentOutOfRangeException();
         }
         ++i;
     }
     return result;
 }
예제 #3
0
        public void TestAbstracteferenceObjectAbstractSerialization()
        {
            var obj = new Container {
                Referenceable3 = new Referenceable {
                    Id = GuidGenerator.Get(1), Value = "Test"
                }
            };

            obj.Referenceable4 = (Referenceable)obj.Referenceable3;
            var objectReferences = new YamlAssetMetadata <Guid>();
            var path             = new YamlAssetPath();

            path.PushMember(nameof(Container.Referenceable4));
            objectReferences.Set(path, obj.Referenceable4.Id);
            var yaml = SerializeAsString(obj, objectReferences);

            Assert.AreEqual(AbstractReferenceAbstractObjectYaml, yaml);
        }
예제 #4
0
        public void TestConcreteReferenceConcreteObjectSerialization()
        {
            var obj = new Container {
                Referenceable1 = new Referenceable {
                    Id = GuidGenerator.Get(1), Value = "Test"
                }
            };

            obj.Referenceable2 = obj.Referenceable1;
            var objectReferences = new YamlAssetMetadata <Guid>();
            var path             = new YamlAssetPath();

            path.PushMember(nameof(Container.Referenceable2));
            objectReferences.Set(path, obj.Referenceable2.Id);
            var yaml = SerializeAsString(obj, objectReferences);

            Assert.AreEqual(ConcreteReferenceConcreteObjectYaml, yaml);
        }
예제 #5
0
        public void TestAbstractNonIdentifiableReferenceableDictionarySerialization()
        {
            var obj  = new NonIdentifiableCollectionContainer();
            var item = new Referenceable {
                Id = GuidGenerator.Get(1), Value = "Test"
            };

            obj.AbstractRefDictionary.Add("Item1", item);
            obj.AbstractRefDictionary.Add("Item2", item);
            var objectReferences = new YamlAssetMetadata <Guid>();
            var path             = new YamlAssetPath();

            path.PushMember(nameof(CollectionContainer.AbstractRefDictionary));
            path.PushIndex("Item1");
            objectReferences.Set(path, GuidGenerator.Get(1));
            var yaml = SerializeAsString(obj, objectReferences);

            Assert.AreEqual(AbstractNonIdentifiableReferenceableDictionaryYaml, yaml);
        }
예제 #6
0
        public void TestConcreteNonIdentifiableReferenceableListSerialization()
        {
            var obj  = new NonIdentifiableCollectionContainer();
            var item = new Referenceable {
                Id = GuidGenerator.Get(1), Value = "Test"
            };

            obj.ConcreteRefList.Add(item);
            obj.ConcreteRefList.Add(item);
            var objectReferences = new YamlAssetMetadata <Guid>();
            var path             = new YamlAssetPath();

            path.PushMember(nameof(CollectionContainer.ConcreteRefList));
            path.PushIndex(0);
            objectReferences.Set(path, GuidGenerator.Get(1));
            var yaml = SerializeAsString(obj, objectReferences);

            Assert.AreEqual(ConcreteNonIdentifiableReferenceableListYaml, yaml);
        }
예제 #7
0
        public void TestConcreteReferenceableDictionarySerialization()
        {
            var obj  = new CollectionContainer();
            var item = new Referenceable {
                Id = GuidGenerator.Get(1), Value = "Test"
            };

            obj.ConcreteRefDictionary.Add("Item1", item);
            obj.ConcreteRefDictionary.Add("Item2", item);
            var objectReferences = new YamlAssetMetadata <Guid>();
            var ids = CollectionItemIdHelper.GetCollectionItemIds(obj.ConcreteRefDictionary);

            ids["Item1"] = IdentifierGenerator.Get(1);
            ids["Item2"] = IdentifierGenerator.Get(2);
            var path = new YamlAssetPath();

            path.PushMember(nameof(CollectionContainer.ConcreteRefDictionary));
            path.PushItemId(IdentifierGenerator.Get(1));
            objectReferences.Set(path, GuidGenerator.Get(1));
            var yaml = SerializeAsString(obj, objectReferences);

            Assert.AreEqual(ConcreteReferenceableDictionaryYaml, yaml);
        }
예제 #8
0
        public void TestAbstractReferenceableListSerialization()
        {
            var obj  = new CollectionContainer();
            var item = new Referenceable {
                Id = GuidGenerator.Get(1), Value = "Test"
            };

            obj.AbstractRefList.Add(item);
            obj.AbstractRefList.Add(item);
            var objectReferences = new YamlAssetMetadata <Guid>();
            var ids = CollectionItemIdHelper.GetCollectionItemIds(obj.AbstractRefList);

            ids[0] = IdentifierGenerator.Get(1);
            ids[1] = IdentifierGenerator.Get(2);
            var path = new YamlAssetPath();

            path.PushMember(nameof(CollectionContainer.AbstractRefList));
            path.PushItemId(IdentifierGenerator.Get(1));
            objectReferences.Set(path, GuidGenerator.Get(1));
            var yaml = SerializeAsString(obj, objectReferences);

            Assert.AreEqual(AbstractReferenceableListYaml, yaml);
        }
예제 #9
0
        public static YamlAssetPath ConvertPath(GraphNodePath path, int inNonIdentifiableType)
        {
            var currentNode = (IAssetNode)path.RootNode;
            var result      = new YamlAssetPath();
            var i           = 0;

            foreach (var item in path.Path)
            {
                switch (item.Type)
                {
                case GraphNodePath.ElementType.Member:
                    var member = (string)item.Value;
                    result.PushMember(member);
                    var objectNode = currentNode as IObjectNode;
                    if (objectNode == null)
                    {
                        throw new InvalidOperationException($"An IObjectNode was expected when processing the path [{path}]");
                    }
                    currentNode = (IAssetNode)objectNode.TryGetChild(member);
                    break;

                case GraphNodePath.ElementType.Target:
                    if (i < path.Path.Count - 1)
                    {
                        var targetingMemberNode = currentNode as IMemberNode;
                        if (targetingMemberNode == null)
                        {
                            throw new InvalidOperationException($"An IMemberNode was expected when processing the path [{path}]");
                        }
                        currentNode = (IAssetNode)targetingMemberNode.Target;
                    }
                    break;

                case GraphNodePath.ElementType.Index:
                    var index      = (Index)item.Value;
                    var memberNode = currentNode as AssetMemberNode;
                    if (memberNode == null)
                    {
                        throw new InvalidOperationException($"An AssetMemberNode was expected when processing the path [{path}]");
                    }
                    if (inNonIdentifiableType > 0 || memberNode.IsNonIdentifiableCollectionContent)
                    {
                        result.PushIndex(index.Value);
                    }
                    else
                    {
                        var id = memberNode.IndexToId(index);
                        // Create a new id if we don't have any so far
                        if (id == ItemId.Empty)
                        {
                            id = ItemId.New();
                        }
                        result.PushItemId(id);
                    }
                    if (i < path.Path.Count - 1)
                    {
                        currentNode = (IAssetNode)currentNode.IndexedTarget(index);
                    }
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
                ++i;
            }
            return(result);
        }
        public static YamlAssetPath ConvertPath([NotNull] GraphNodePath path, int inNonIdentifiableType = 0)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            var currentNode = (IAssetNode)path.RootNode;
            var result      = new YamlAssetPath();
            var i           = 0;

            foreach (var item in path.Path)
            {
                switch (item.Type)
                {
                case GraphNodePath.ElementType.Member:
                {
                    var member = item.Name;
                    result.PushMember(member);
                    var objectNode = currentNode as IObjectNode;
                    if (objectNode == null)
                    {
                        throw new InvalidOperationException($"An IObjectNode was expected when processing the path [{path}]");
                    }
                    currentNode = (IAssetNode)objectNode.TryGetChild(member);
                    break;
                }

                case GraphNodePath.ElementType.Target:
                {
                    if (i < path.Path.Count - 1)
                    {
                        var targetingMemberNode = currentNode as IMemberNode;
                        if (targetingMemberNode == null)
                        {
                            throw new InvalidOperationException($"An IMemberNode was expected when processing the path [{path}]");
                        }
                        currentNode = (IAssetNode)targetingMemberNode.Target;
                    }
                    break;
                }

                case GraphNodePath.ElementType.Index:
                {
                    var index      = item.Index;
                    var objectNode = currentNode as AssetObjectNode;
                    if (objectNode == null)
                    {
                        throw new InvalidOperationException($"An IObjectNode was expected when processing the path [{path}]");
                    }
                    if (inNonIdentifiableType > 0 || !CollectionItemIdHelper.HasCollectionItemIds(objectNode.Retrieve()))
                    {
                        result.PushIndex(index.Value);
                    }
                    else
                    {
                        var id = objectNode.IndexToId(index);
                        // Create a new id if we don't have any so far
                        if (id == ItemId.Empty)
                        {
                            id = ItemId.New();
                        }
                        result.PushItemId(id);
                    }
                    if (i < path.Path.Count - 1)
                    {
                        currentNode = (IAssetNode)objectNode.IndexedTarget(index);
                    }
                    break;
                }

                default:
                    throw new ArgumentOutOfRangeException();
                }
                ++i;
            }
            return(result);
        }
        public void TestGenerateOverridesForSerializationOfCollectionItem()
        {
            const string expectedYaml = @"!SiliconStudio.Assets.Quantum.Tests.Types+SomeObject,SiliconStudio.Assets.Quantum.Tests
Value*: OverriddenString
";
            var asset = new Types.MyAsset4 { MyObjects = { new Types.SomeObject { Value = "String1" }, new Types.SomeObject { Value = "String2" } } };
            var context = DeriveAssetTest<Types.MyAsset4>.DeriveAsset(asset);
            var derivedPropertyNode = (AssetNode)((IGraphNode)context.DerivedGraph.RootNode)[nameof(Types.MyAsset4.MyObjects)].IndexedTarget(new Index(1));
            derivedPropertyNode[nameof(Types.SomeObject.Value)].Content.Update("OverriddenString");
            var expectedPath = new YamlAssetPath();
            expectedPath.PushMember(nameof(Types.SomeObject.Value));

            var overrides = AssetPropertyGraph.GenerateOverridesForSerialization(derivedPropertyNode);
            Assert.AreEqual(1, overrides.Count);
            Assert.True(overrides.ContainsKey(expectedPath));
            Assert.AreEqual(OverrideType.New, overrides[expectedPath]);

            // Test deserialization
            SerializeAndCompare(context.DerivedAsset.MyObjects[1], overrides, expectedYaml);
            bool aliasOccurred;
            var instance = (Types.SomeObject)AssetFileSerializer.Default.Load(DeriveAssetTest<Types.MyAsset9>.ToStream(expectedYaml), null, null, out aliasOccurred, out overrides);
            Assert.AreEqual("OverriddenString", instance.Value);
            Assert.AreEqual(1, overrides.Count);
            Assert.True(overrides.ContainsKey(expectedPath));
            Assert.AreEqual(OverrideType.New, overrides[expectedPath]);
        }
        public void TestGenerateOverridesForSerializationOfObjectMember()
        {
            const string expectedYaml = @"!SiliconStudio.Assets.Quantum.Tests.Types+SomeObject,SiliconStudio.Assets.Quantum.Tests
Value*: OverriddenString
";
            var asset = new Types.MyAsset9 { MyObject = new Types.SomeObject { Value = "String1" } };
            var context = DeriveAssetTest<Types.MyAsset9>.DeriveAsset(asset);
            var derivedPropertyNode = (AssetNode)((IGraphNode)context.DerivedGraph.RootNode)[nameof(Types.MyAsset9.MyObject)];
            derivedPropertyNode.Target[nameof(Types.SomeObject.Value)].Content.Update("OverriddenString");
            var expectedPath = new YamlAssetPath();
            expectedPath.PushMember(nameof(Types.SomeObject.Value));

            var overrides = AssetPropertyGraph.GenerateOverridesForSerialization(derivedPropertyNode);
            Assert.AreEqual(1, overrides.Count);
            Assert.True(overrides.ContainsKey(expectedPath));
            Assert.AreEqual(OverrideType.New, overrides[expectedPath]);

            // We expect the same resulting path both from the member node and the target object node
            overrides = AssetPropertyGraph.GenerateOverridesForSerialization(derivedPropertyNode.Target);
            Assert.AreEqual(1, overrides.Count);
            Assert.True(overrides.ContainsKey(expectedPath));
            Assert.AreEqual(OverrideType.New, overrides[expectedPath]);

            // Test deserialization
            SerializeAndCompare(context.DerivedAsset.MyObject, overrides, expectedYaml);
            bool aliasOccurred;
            var instance = (Types.SomeObject)AssetFileSerializer.Default.Load(DeriveAssetTestBase.ToStream(expectedYaml), null, null, out aliasOccurred, out overrides);
            Assert.AreEqual("OverriddenString", instance.Value);
            Assert.AreEqual(1, overrides.Count);
            Assert.True(overrides.ContainsKey(expectedPath));
            Assert.AreEqual(OverrideType.New, overrides[expectedPath]);
        }