Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
CloneSerializer.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 SiliconStudio.Core.Serialization;
4 using SiliconStudio.Paradox.Effects;
5 
6 namespace SiliconStudio.Paradox.EntityModel
7 {
8  /// <summary>
9  /// Serializer for helping cloning of <see cref="Entity"/>.
10  /// </summary>
11  /// <typeparam name="T"></typeparam>
12  public class CloneSerializer<T> : DataSerializer<T> where T : class
13  {
14  public override void PreSerialize(ref T obj, ArchiveMode mode, SerializationStream stream)
15  {
16  var cloneContext = stream.Context.Get(EntityCloner.CloneContextProperty);
17 
18  if (mode == ArchiveMode.Serialize)
19  {
20  // Check against list of items that should be included in the stream (Entity and EntityComponent whose parent is the cloned Entity).
21  // First try MappedObjects, then ClonedObjects.
22  object mappedObject = null;
23  bool isSharedObject = cloneContext.MappedObjects != null && cloneContext.MappedObjects(obj, out mappedObject);
24 
25  if (!isSharedObject && cloneContext.ClonedObjects != null && !cloneContext.ClonedObjects.Contains(obj))
26  {
27  isSharedObject = true;
28  mappedObject = obj;
29  }
30 
31  stream.Write(isSharedObject);
32 
33  if (isSharedObject)
34  {
35  stream.Write(cloneContext.SharedObjects.Count);
36  cloneContext.SharedObjects.Add(mappedObject);
37  }
38  else
39  {
40  cloneContext.SerializedObjects.Add(obj);
41  }
42  }
43  else
44  {
45  bool isSharedObject = stream.ReadBoolean();
46 
47  if (isSharedObject)
48  {
49  var sharedObjectIndex = stream.ReadInt32();
50  obj = (T)cloneContext.SharedObjects[sharedObjectIndex];
51 
52  // TODO: Hardcoded
53  // Model need to be cloned
54  if (obj is Model)
55  {
56  obj = (T)(object)((Model)(object)obj).Instantiate();
57  }
58  }
59  else
60  {
61  base.PreSerialize(ref obj, mode, stream);
62  cloneContext.SerializedObjects.Add(obj);
63  }
64  }
65  }
66 
67  public override void Serialize(ref T obj, ArchiveMode mode, SerializationStream stream)
68  {
69  var cloneContext = stream.Context.Get(EntityCloner.CloneContextProperty);
70 
71  if (cloneContext.SerializedObjects.Contains(obj))
72  {
73  // Get actual serializer
74  var dataSerializer = cloneContext.EntitySerializerSelector.GetSerializer<T>();
75 
76  // Serialize object
77  //stream.Context.Set(EntitySerializer.InsideEntityComponentProperty, false);
78  dataSerializer.Serialize(ref obj, mode, stream);
79 
80  if (obj is EntityComponent)
81  {
82  // Serialize underlying Entity (needs to be part of the object graph)
83  var entity = ((EntityComponent)(object)obj).Entity;
84  //var entityDataSerializer = cloneContext.EntitySerializer.GetSerializer<Entity>();
85  stream.Serialize(ref entity, mode);
86 
87  ((EntityComponent)(object)obj).Entity = entity;
88  }
89  }
90  }
91  }
92 }
Game entity. It usually aggregates multiple EntityComponent
Definition: Entity.cs:28
override void Serialize(ref T obj, ArchiveMode mode, SerializationStream stream)
Base class for implementation of SerializationStream.
override void PreSerialize(ref T obj, ArchiveMode mode, SerializationStream stream)
Describes how to serialize and deserialize an object without knowing its type. Used as a common base ...
ArchiveMode
Enumerates the different mode of serialization (either serialization or deserialization).
Definition: ArchiveMode.cs:8
Collection of Mesh, each one usually being a different LOD of the same Model. The effect system will ...
Definition: Model.cs:23