Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ContentReferenceSerializer.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;
5 using SharpYaml.Events;
6 using SharpYaml.Serialization;
7 using SiliconStudio.Core;
8 using SiliconStudio.Core.IO;
9 using SiliconStudio.Core.Serialization;
10 using SiliconStudio.Core.Yaml;
11 
12 namespace SiliconStudio.Assets.Serializers
13 {
14  [YamlSerializerFactory]
15  internal class ContentReferenceSerializer : AssetScalarSerializerBase
16  {
17  public static readonly ContentReferenceSerializer Default = new ContentReferenceSerializer();
18 
19  public override bool CanVisit(Type type)
20  {
21  return (typeof(ContentReference).IsAssignableFrom(type));
22  }
23 
24  public override object ConvertFrom(ref ObjectContext context, Scalar fromScalar)
25  {
26  var contentReference = (ContentReference)context.Instance ?? (ContentReference)Activator.CreateInstance(context.Descriptor.Type);
27 
28  Guid guid;
29  UFile location;
30  if (!AssetReference.TryParse(fromScalar.Value, out guid, out location))
31  {
32  throw new YamlException(fromScalar.Start, fromScalar.End, "Unable to decode asset reference [{0}]. Expecting format GUID:LOCATION".ToFormat(fromScalar.Value));
33  }
34 
35  contentReference.Id = guid;
36  contentReference.Location = location;
37  return contentReference;
38  }
39  public override string ConvertTo(ref ObjectContext objectContext)
40  {
41  var contentReference = ((ContentReference)objectContext.Instance);
42  return string.Format("{0}:{1}", contentReference.Id, contentReference.Location);
43  }
44  }
45 }
Use the default mode depending on the type of the field/property.
Defines a normalized file path. See UPath for details. This class cannot be inherited.
Definition: UFile.cs:13