3 using System.Collections.Generic;
6 using SiliconStudio.Core;
7 using SiliconStudio.Core.Serialization;
8 using SiliconStudio.Core.Serialization.Serializers;
9 using SiliconStudio.Paradox.DataModel;
10 using SiliconStudio.Paradox.Effects;
11 using SiliconStudio.Paradox.Engine;
12 using SiliconStudio.Paradox.Graphics;
14 namespace SiliconStudio.
Paradox.EntityModel
19 [DataSerializerGlobal(typeof(CloneSerializer<Effect>), Profile =
"Clone")]
20 [DataSerializerGlobal(typeof(CloneSerializer<SpriteGroup>), Profile =
"Clone")]
21 [DataSerializerGlobal(typeof(CloneSerializer<BlendState>), Profile =
"Clone")]
22 [DataSerializerGlobal(typeof(CloneSerializer<RasterizerState>), Profile =
"Clone")]
23 [DataSerializerGlobal(typeof(CloneSerializer<SamplerState>), Profile =
"Clone")]
24 [DataSerializerGlobal(typeof(CloneSerializer<DepthStencilState>), Profile =
"Clone")]
25 [DataSerializerGlobal(typeof(CloneSerializer<Texture1D>), Profile =
"Clone")]
26 [DataSerializerGlobal(typeof(CloneSerializer<Texture2D>), Profile =
"Clone")]
27 [DataSerializerGlobal(typeof(CloneSerializer<Texture3D>), Profile =
"Clone")]
28 [DataSerializerGlobal(typeof(CloneSerializer<TextureCube>), Profile =
"Clone")]
32 private static CloneContext cloneContext =
new CloneContext();
45 var clonedObjects =
new HashSet<object>();
50 clonedObjects.Add(currentEntity);
51 foreach (var component
in currentEntity.Tags.Where(x => x.Value is
EntityComponent))
53 clonedObjects.Add(component.Value);
57 return Clone(clonedObjects, null, entity);
70 private static T Clone<T>(HashSet<object> clonedObjects, TryGetValueFunction<object, object> mappedObjects, T entity) where T :
class
72 if (cloneSerializerSelector == null)
75 cloneSerializerSelector.ReuseReferences =
true;
77 cloneSerializerSelector
78 .RegisterProfile(
"Default")
79 .RegisterProfile(
"Clone")
81 .RegisterSerializer(
new CloneSerializer<string>())
82 .RegisterSerializer(
new CloneSerializer<Effect>())
83 .RegisterSerializer(
new CloneSerializer<Mesh>())
84 .RegisterSerializer(
new CloneSerializer<Model>())
85 .RegisterSerializer(
new CloneSerializer<AnimationClip>());
93 cloneContext.EntitySerializerSelector = cloneSerializerSelector;
95 cloneContext.ClonedObjects = clonedObjects;
96 cloneContext.MappedObjects = mappedObjects;
99 var memoryStream = cloneContext.MemoryStream;
101 writer.Context.SerializerSelector = cloneSerializerSelector;
102 writer.Context.Set(CloneContextProperty, cloneContext);
103 writer.SerializeExtended(entity, ArchiveMode.Serialize, null);
106 cloneContext.SerializedObjects.Clear();
110 memoryStream.Seek(0, SeekOrigin.Begin);
112 reader.Context.SerializerSelector = cloneSerializerSelector;
113 reader.Context.Set(CloneContextProperty, cloneContext);
114 reader.SerializeExtended(ref result, ArchiveMode.Deserialize, null);
120 cloneContext.Cleanup();
125 public delegate
bool TryGetValueFunction<in TKey, TResult>(TKey key, out TResult result);
130 internal class CloneContext
132 public void Cleanup()
134 MemoryStream.SetLength(0);
135 MappedObjects = null;
136 SerializedObjects.Clear();
137 ContentReferences.Clear();
138 ClonedObjects = null;
139 SharedObjects.Clear();
140 EntitySerializerSelector = null;
143 public MemoryStream MemoryStream =
new MemoryStream(4096);
145 public TryGetValueFunction<object, object> MappedObjects;
147 public readonly HashSet<object> SerializedObjects =
new HashSet<object>();
149 public readonly List<ContentReference> ContentReferences =
new List<ContentReference>();
154 public HashSet<object> ClonedObjects;
159 public readonly List<object> SharedObjects =
new List<object>();
Game entity. It usually aggregates multiple EntityComponent
DataSerializerGenericMode
Defines what generic parameters to pass to the serializer.
static Entity Clone(Entity entity)
Clones the specified entity. Entity, children Entity and their EntityComponent will be cloned...
static HashSet< Entity > CollectEntityTree(Entity entity)
Implements SerializationStream as a binary writer.
Implements SerializationStream as a binary reader.
Serializer context. It holds DataSerializer{T} objects and their factories.
A class that represents a tag propety.
Provides method for deep cloning of en Entity.