Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
DataContentConverterSerializer.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 SiliconStudio.Core.Serialization.Assets;
5 using SiliconStudio.Core.Serialization.Contents;
6 
7 namespace SiliconStudio.Core.Serialization.Converters
8 {
9  public class DataContentConverterSerializer<TSource> : ContentSerializerBase<TSource>
10  {
11  private DataConverter converter;
12 
13  public override Type SerializationType
14  {
15  get { return converter.DataType; }
16  }
17 
19  : this(typeof(object))
20  {
21 
22  }
23 
24  protected DataContentConverterSerializer(Type dataType)
25  {
26  // For now, get data type from converter
27  // Not sure if it will be good enough for everything.
28  // Another option would be to do it when data types are auto-generated.
29  converter = ConverterContext.GetDataConverter(dataType, typeof(TSource), ConverterContext.ConversionType.ObjectToData);
30  if (converter == null)
31  throw new InvalidOperationException(string.Format("Could not find a valid converter for type {0}", typeof(TSource)));
32  }
33 
34  public override void Serialize(ContentSerializerContext context, SerializationStream stream, ref TSource obj)
35  {
36  // Serialize object
37  if (context.Mode == ArchiveMode.Deserialize)
38  {
39  AssetManager.AssetReference assetReference;
40 
41  // Recursively call deserialization of this stream as another type.
42  // We use special DeserializeObjectRecursive instead of DeserializeObject to avoid reopening this stream.
43  var dataObject = context.AssetManager.DeserializeObjectRecursive(null, out assetReference, context.Url, converter.DataType, AssetManagerLoaderSettings.IgnoreReferences, context, stream.NativeStream, SerializationType);
44 
45  object source = null;
46 
47  // Transfer Context information to the Converter
48  ConverterContext converterContext = context.ConverterContext;
49  if (converterContext == null)
50  {
51  // First time: create context, and register current object
52  converterContext = new ConverterContext { Tags = stream.Context.Tags };
53  context.ConverterContext = converterContext;
54  }
55 
56  // Pre-construct object (if available)
57  converterContext.ConvertFromData(dataObject, ref source, ConvertFromDataFlags.Construct);
58 
59  // If object could be constructed, register it so that it can properly be referenced
60  if (source != null)
61  context.AssetManager.SetAssetObject(context.AssetReference, source);
62 
63  // Actually convert object
64  converterContext.ConvertFromData(dataObject, ref source, ConvertFromDataFlags.Convert);
65 
66  obj = (TSource)source;
67 
68  // Unload data object (not necessary anymore)
69  context.AssetManager.Unload(dataObject);
70  }
71  else
72  {
73  // Transfer Context information to the Converter
74  object dataObject = null;
75  converter.ConvertToData(new ConverterContext { Tags = stream.Context.Tags }, ref dataObject, obj);
76 
77  MemberNonSealedSerializer.SerializeExtended(stream, converter.DataType, ref dataObject, context.Mode);
78  }
79  }
80  }
81 
82  public class DataContentConverterSerializer<TData, TSource> : DataContentConverterSerializer<TSource>
83  {
85  : base(typeof(TData))
86  {
87  }
88  }
89 }
override void Serialize(ContentSerializerContext context, SerializationStream stream, ref TSource obj)
Base class for converters to/from a data type.
Base class for implementation of SerializationStream.
HRESULT Convert(_In_ const Image &srcImage, _In_ DXGI_FORMAT format, _In_ DWORD filter, _In_ float threshold, _Out_ ScratchImage &image)
ArchiveMode
Enumerates the different mode of serialization (either serialization or deserialization).
Definition: ArchiveMode.cs:8