Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
AssetItemSerializer.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.ComponentModel;
5 
6 using SharpYaml.Serialization;
7 using SharpYaml.Serialization.Serializers;
8 using SiliconStudio.Core;
9 using SiliconStudio.Core.IO;
10 using SiliconStudio.Core.Reflection;
11 using SiliconStudio.Core.Yaml;
13 
14 namespace SiliconStudio.Assets.Serializers
15 {
16  /// <summary>
17  /// A Yaml Serializer for <see cref="AssetBase"/>. Because this type is immutable
18  /// we need to implement a special serializer.
19  /// </summary>
20  [YamlSerializerFactory]
21  internal class AssetItemSerializer : ObjectSerializer, IDataCustomVisitor
22  {
23  public override IYamlSerializable TryCreate(SerializerContext context, ITypeDescriptor typeDescriptor)
24  {
25  return CanVisit(typeDescriptor.Type) ? this : null;
26  }
27 
28  protected override void CreateOrTransformObject(ref ObjectContext objectContext)
29  {
30  objectContext.Instance = objectContext.SerializerContext.IsSerializing ? new AssetItemMutable((AssetItem)objectContext.Instance) : new AssetItemMutable();
31  }
32 
33  protected override void TransformObjectAfterRead(ref ObjectContext objectContext)
34  {
35  objectContext.Instance = ((AssetItemMutable)objectContext.Instance).ToAssetItem();
36  }
37 
38  private class AssetItemMutable
39  {
40  public AssetItemMutable()
41  {
42  }
43 
44  public AssetItemMutable(AssetItem item)
45  {
46  Location = item.Location;
47  SourceFolder = item.SourceFolder;
48  Asset = item.Asset;
49  }
50 
51  [DataMember(0)]
52  public UFile Location;
53 
54  [DataMember(1)]
55  [DefaultValue(null)]
56  public UDirectory SourceFolder;
57 
58  [DataMember(2)]
59  public Asset Asset;
60 
61  public AssetItem ToAssetItem()
62  {
63  return new AssetItem(Location, Asset) { SourceFolder = SourceFolder };
64  }
65  }
66 
67  public bool CanVisit(Type type)
68  {
69  return type == typeof(AssetItem);
70  }
71 
72  public void Visit(ref VisitorContext context)
73  {
74  context.Visitor.VisitObject(context.Instance, context.Descriptor, true);
75  }
76  }
77 }
Type Type
Gets the type described by this instance.
SharpYaml.Serialization.ITypeDescriptor ITypeDescriptor
Defines a normalized directory path. See UPath for details. This class cannot be inherited.
Definition: UDirectory.cs:13
A custom visitor used by DataVisitorBase.
Provides access members of a type.
Defines a normalized file path. See UPath for details. This class cannot be inherited.
Definition: UFile.cs:13