Skip to content
Permalink
Browse files

[Assets] Made !file and !directory tags mandatory in Yaml for easier …

…dependency analysis
  • Loading branch information
xen2 committed Oct 2, 2018
1 parent f608ae5 commit 49240a819316616a230d9165e6daf1c4a8976397
@@ -0,0 +1,80 @@
// Copyright (c) Xenko contributors (https://xenko.com) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Xunit;
using Xenko.Core;
using Xenko.Core.Annotations;
using Xenko.Core.Reflection;
using Xenko.Core.Yaml;
using Xenko.Core.IO;

namespace Xenko.Core.Assets.Tests.Yaml
{
public class TestUFile
{
[DataContract("TestUFileClass")]
public class TestUFileClass
{
public UFile File { get; set; }
}

private const string YamlWithFileTag = @"!TestUFileClass
File: !file test.txt
";

private const string YamlWithoutFileTag = @"!TestUFileClass
File: test.txt
";

private static string Serialize(object instance)
{
using (var stream = new MemoryStream())
{
AssetYamlSerializer.Default.Serialize(stream, instance);
stream.Flush();
stream.Position = 0;
return new StreamReader(stream).ReadToEnd();
}
}

private static T Deserialize<T>(string data)
{
using (var stream = new MemoryStream())
{
var streamWriter = new StreamWriter(stream);
streamWriter.Write(data);
streamWriter.Flush();
stream.Position = 0;

return (T)AssetYamlSerializer.Default.Deserialize(stream, typeof(T));
}
}

[Fact]
public void TestWithFileTagRoundtrip()
{
// Check deserialization works
var obj = Deserialize<TestUFileClass>(YamlWithFileTag);
Assert.Equal("test.txt", obj.File);

// Check object is serialized back properly
var yaml = Serialize(obj);
Assert.Equal(YamlWithFileTag, yaml);
}

[Fact]
public void TestWithoutFileTagRoundtrip()
{
// Check deserialization works
var obj = Deserialize<TestUFileClass>(YamlWithoutFileTag);
Assert.Equal("test.txt", obj.File);

// Check object is serialized back properly (it should have the file tag now)
var yaml = Serialize(obj);
Assert.Equal(YamlWithFileTag, yaml);
}
}
}
@@ -2,8 +2,8 @@
Id: 00000000-0000-0000-0000-000000000000
Tags: []
Description: This is a test
AssetDirectory: !UDirectory /test/dynamic/path/to/file/in/object/property
AssetUrl: !UFile /test/dynamic/path/to/file/in/object/property
AssetDirectory: !dir /test/dynamic/path/to/file/in/object/property
AssetUrl: !file /test/dynamic/path/to/file/in/object/property
CustomReference2: 00000000-0000-0000-0000-000000000000:/this/is/an/url/to/MyCustomReference2
CustomReferences:
63000000630000006300000063000000: 00000000-0000-0000-0000-000000000000:/this/is/an/url/to/MyCustomReferenceItem1
@@ -9,7 +9,7 @@ namespace Xenko.Core.IO
/// <summary>
/// Defines a normalized directory path. See <see cref="UPath"/> for details. This class cannot be inherited.
/// </summary>
[DataContract("UDirectory")]
[DataContract("dir")]
[TypeConverter(typeof(UDirectoryTypeConverter))]
public sealed class UDirectory : UPath
{
@@ -9,7 +9,7 @@ namespace Xenko.Core.IO
/// <summary>
/// Defines a normalized file path. See <see cref="UPath"/> for details. This class cannot be inherited.
/// </summary>
[DataContract("UFile")]
[DataContract("file")]
[TypeConverter(typeof(UFileTypeConverter))]
public sealed class UFile : UPath
{
@@ -31,5 +31,13 @@ public override string ConvertTo(ref ObjectContext objectContext)
var path = ((UDirectory)objectContext.Instance);
return path.FullPath;
}

protected override void WriteScalar(ref ObjectContext objectContext, ScalarEventInfo scalar)
{
// Force tag when writing back
scalar.Tag = objectContext.SerializerContext.TagFromType(typeof(UDirectory));
scalar.IsPlainImplicit = false;
base.WriteScalar(ref objectContext, scalar);
}
}
}
@@ -31,5 +31,13 @@ public override string ConvertTo(ref ObjectContext objectContext)
var path = ((UFile)objectContext.Instance);
return path.FullPath;
}

protected override void WriteScalar(ref ObjectContext objectContext, ScalarEventInfo scalar)
{
// Force tag when writing back
scalar.Tag = objectContext.SerializerContext.TagFromType(typeof(UFile));
scalar.IsPlainImplicit = false;
base.WriteScalar(ref objectContext, scalar);
}
}
}

0 comments on commit 49240a8

Please sign in to comment.
You can’t perform that action at this time.