Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
AssetBaseSerializer.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 SharpYaml.Serialization;
5 using SharpYaml.Serialization.Serializers;
6 using SiliconStudio.Core;
7 using SiliconStudio.Core.IO;
8 using SiliconStudio.Core.Reflection;
9 using SiliconStudio.Core.Yaml;
11 
12 namespace SiliconStudio.Assets.Serializers
13 {
14  /// <summary>
15  /// A Yaml Serializer for <see cref="AssetBase"/>. Because this type is immutable
16  /// we need to implement a special serializer.
17  /// </summary>
18  [YamlSerializerFactory]
19  internal class AssetBaseSerializer : ObjectSerializer, IDataCustomVisitor
20  {
21  public override IYamlSerializable TryCreate(SerializerContext context, ITypeDescriptor typeDescriptor)
22  {
23  return CanVisit(typeDescriptor.Type) ? this : null;
24  }
25 
26  protected override void CreateOrTransformObject(ref ObjectContext objectContext)
27  {
28  objectContext.Instance = objectContext.SerializerContext.IsSerializing ? new AssetBaseMutable((AssetBase)objectContext.Instance) : new AssetBaseMutable();
29  }
30 
31  protected override void TransformObjectAfterRead(ref ObjectContext objectContext)
32  {
33  objectContext.Instance = ((AssetBaseMutable)objectContext.Instance).ToAssetBase();
34  }
35 
36  private class AssetBaseMutable
37  {
38  public AssetBaseMutable()
39  {
40  }
41 
42  public AssetBaseMutable(AssetBase item)
43  {
44  Location = item.Location;
45  Asset = item.Asset;
46  }
47 
48  [DataMember(0)]
49  public UFile Location;
50 
51  [DataMember(1)]
52  public Asset Asset;
53 
54  public AssetBase ToAssetBase()
55  {
56  return new AssetBase(Location, Asset);
57  }
58  }
59 
60  public bool CanVisit(Type type)
61  {
62  return type == typeof(AssetBase);
63  }
64 
65  public void Visit(ref VisitorContext context)
66  {
67  // Only visit the instance without visiting childrens
68  context.Visitor.VisitObject(context.Instance, context.Descriptor, false);
69  }
70  }
71 }
Base class for Asset.
Definition: Asset.cs:14
SharpYaml.Serialization.ITypeDescriptor ITypeDescriptor
Type Type
Gets the type described by this instance.
An asset item part of a Package accessible through Package.Assets.
Definition: AssetBase.cs:16
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