Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
EffectParameterKeyStandardGeneratorSerializer.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.Collections.Generic;
5 using System.Linq;
6 
7 using SharpDX.DirectWrite;
8 
9 using SharpYaml.Serialization;
10 using SharpYaml.Serialization.Serializers;
11 
12 using SiliconStudio.Assets.Serializers;
13 using SiliconStudio.Core.Reflection;
14 using SiliconStudio.Core.Yaml;
15 using SiliconStudio.Paradox.Assets.Effect;
16 using SiliconStudio.Paradox.Assets.Effect.ValueGenerators;
17 using SiliconStudio.Paradox.Effects;
18 
20 
21 namespace SiliconStudio.Paradox.Assets.Serializers
22 {
23  [YamlSerializerFactory]
24  internal class EffectParameterKeyStandardGeneratorSerializer : DictionarySerializer, IDataCustomVisitor
25  {
26  public override IYamlSerializable TryCreate(SerializerContext context, ITypeDescriptor typeDescriptor)
27  {
28  var type = typeDescriptor.Type;
29  return CanVisit(type) ? this : null;
30  }
31  protected override KeyValuePair<object, object> ReadDictionaryItem(ref ObjectContext objectContext, KeyValuePair<Type, Type> keyValueType)
32  {
33  var readPair = base.ReadDictionaryItem(ref objectContext, keyValueType);
34 
35  if (readPair.Value != null && readPair.Value.GetType() == typeof(List<object>))
36  {
37  var key = (ParameterKey)readPair.Key;
38 
39  var newValueType = typeof(EffectParameterValuesGenerator<>).MakeGenericType(key.PropertyType);
40  var newList = (IEffectParameterValueGenerator)Activator.CreateInstance(newValueType);
41 
42  foreach (var item in (List<object>)readPair.Value)
43  newList.AddValue(key, item);
44 
45  readPair = new KeyValuePair<object, object>(readPair.Key, newList);
46  }
47 
48  return readPair;
49  }
50 
51  protected override void WriteDictionaryItem(ref ObjectContext objectContext, KeyValuePair<object, object> keyValue, KeyValuePair<Type, Type> types)
52  {
53  var key = keyValue.Key as ParameterKey;
54  if (key != null && keyValue.Value != null)
55  {
56  var valueType = keyValue.Value.GetType();
57  if (valueType != typeof(List<object>) && valueType.IsGenericType && valueType.GetGenericTypeDefinition() == typeof(EffectParameterValuesGenerator<>))
58  {
59  // cast in List<object>
60  var newTypes = new KeyValuePair<Type, Type>(types.Key, typeof(List<object>));
61  var newList = ((IEffectParameterValueGenerator)keyValue.Value).GenerateValues(keyValue.Key as ParameterKey).ToList();
62  var newKeyValue = new KeyValuePair<object, object>(keyValue.Key, newList);
63  base.WriteDictionaryItem(ref objectContext, newKeyValue, newTypes);
64  return;
65  }
66  }
67 
68  base.WriteDictionaryItem(ref objectContext, keyValue, types);
69  }
70 
71  public bool CanVisit(Type type)
72  {
73  return typeof(EffectParameterKeyStandardGenerator) == type;
74  }
75 
76  public void Visit(ref VisitorContext context)
77  {
78  // Visit a GenericDictionary without visiting properties
79  context.Visitor.VisitObject(context.Instance, context.Descriptor, false);
80  }
81  }
82 }
Key of an effect parameter.
Definition: ParameterKey.cs:15
Default implementation for a IEffectParameterGenerator using a dictionary of ParameterKey associated ...
A custom visitor used by DataVisitorBase.
Provides access members of a type.
SharpYaml.Serialization.ITypeDescriptor ITypeDescriptor