forked from stride3d/stride
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityCloner.cs
More file actions
214 lines (188 loc) · 9.08 KB
/
EntityCloner.cs
File metadata and controls
214 lines (188 loc) · 9.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Collections.Generic;
using System.IO;
using Stride.Animations;
using Stride.Audio;
using Stride.Core;
using Stride.Core.Serialization;
using Stride.Graphics;
using Stride.Graphics.Font;
using Stride.Rendering;
namespace Stride.Engine.Design
{
/// <summary>
/// Provides method for deep cloning of en <see cref="Entity"/>.
/// </summary>
[DataSerializerGlobal(typeof(CloneSerializer<Effect>), Profile = "Clone")]
[DataSerializerGlobal(typeof(CloneSerializer<SpriteSheet>), Profile = "Clone")]
[DataSerializerGlobal(typeof(CloneSerializer<SamplerState>), Profile = "Clone")]
[DataSerializerGlobal(typeof(CloneSerializer<Texture>), Profile = "Clone")]
[DataSerializerGlobal(typeof(CloneSerializer<Mesh>), Profile = "Clone")]
[DataSerializerGlobal(typeof(CloneSerializer<Model>), Profile = "Clone")]
[DataSerializerGlobal(typeof(CloneSerializer<AnimationClip>), Profile = "Clone")]
[DataSerializerGlobal(typeof(CloneSerializer<Sound>), Profile = "Clone")]
[DataSerializerGlobal(typeof(CloneSerializer<string>), Profile = "Clone")]
[DataSerializerGlobal(typeof(CloneSerializer<OfflineRasterizedSpriteFont>), Profile = "Clone")]
[DataSerializerGlobal(typeof(CloneSerializer<RuntimeRasterizedSpriteFont>), Profile = "Clone")]
[DataSerializerGlobal(typeof(CloneSerializer<SignedDistanceFieldSpriteFont>), Profile = "Clone")]
[DataSerializerGlobal(typeof(CloneSerializer<Prefab>), Profile = "Clone")]
[DataSerializerGlobal(typeof(CloneSerializer<Entity>), Profile = "Clone")]
public class EntityCloner
{
private static readonly CloneContext cloneContext = new CloneContext();
private static SerializerSelector cloneSerializerSelector = null;
private static SerializerSelector entitySerializerSelector = null;
public static readonly PropertyKey<CloneContext> CloneContextProperty = new PropertyKey<CloneContext>("CloneContext", typeof(EntityCloner));
// CloneObject TLS used to clone entities, so that we don't create one everytime we clone
[ThreadStatic]
private static HashSet<object> clonedObjectsTLS;
private static HashSet<object> ClonedObjects()
{
return clonedObjectsTLS ?? (clonedObjectsTLS = new HashSet<object>());
}
/// <summary>
/// Instantiates the content of the prefab provided.
/// <see cref="Prefab.Entities"/>, children <see cref="Entity"/> and their <see cref="EntityComponent"/> will be cloned.
/// Other assets will be shared.
/// </summary>
/// <param name="prefab">The prefab to instantiate.</param>
/// <returns>A clone of this prefab's <see cref="Prefab.Entities"/></returns>
public static List<Entity> Instantiate(Prefab prefab)
{
if (prefab == null) throw new ArgumentNullException(nameof(prefab));
var clonedObjects = ClonedObjects();
try
{
foreach (var entity in prefab.Entities)
{
CollectEntityTreeHelper(entity, clonedObjects);
}
return Clone(clonedObjects, null, prefab.Entities);
}
finally
{
clonedObjects.Clear();
}
}
/// <summary>
/// Clones the specified entity.
/// <see cref="Entity"/>, children <see cref="Entity"/> and their <see cref="EntityComponent"/> will be cloned.
/// Other assets will be shared.
/// </summary>
/// <param name="entity">The entity.</param>
/// <returns>A cloned entity</returns>
public static Entity Clone(Entity entity)
{
var clonedObjects = ClonedObjects();
try
{
CollectEntityTreeHelper(entity, clonedObjects);
return Clone(clonedObjects, null, entity);
}
finally
{
clonedObjects.Clear();
}
}
/// <summary>
/// Collect entities and components recursively from an entity and add them to a hashset.
/// </summary>
/// <param name="entity">The entity to collect</param>
/// <param name="entityAndComponents">The collected entities and components</param>
internal static void CollectEntityTreeHelper(Entity entity, HashSet<object> entityAndComponents)
{
// Already processed
if (!entityAndComponents.Add(entity))
return;
foreach (var component in entity.Components)
{
entityAndComponents.Add(component);
}
var transformationComponent = entity.Transform;
if (transformationComponent != null)
{
foreach (var child in transformationComponent.Children)
{
CollectEntityTreeHelper(child.Entity, entityAndComponents);
}
}
}
/// <summary>
/// Clones the specified object, taking special care of <see cref="Entity"/>, <see cref="EntityComponent"/> and external assets.
/// User can optionally provides list of cloned objects (list of data reference objects that should be cloned)
/// and mapped objects (list of data reference objects that should be ducplicated using the given instance).
/// </summary>
/// <param name="clonedObjects">The cloned objects.</param>
/// <param name="mappedObjects">The mapped objects.</param>
/// <param name="entity">The entity.</param>
/// <returns>The cloned object.</returns>
private static T Clone<T>(HashSet<object> clonedObjects, TryGetValueFunction<object, object> mappedObjects, T entity) where T : class
{
cloneSerializerSelector ??= new SerializerSelector(true, false, "Default", "Clone");
entitySerializerSelector ??= new SerializerSelector(true, false, "Default");
// Initialize CloneContext
lock (cloneContext)
{
try
{
cloneContext.EntitySerializerSelector = entitySerializerSelector;
cloneContext.ClonedObjects = clonedObjects;
cloneContext.MappedObjects = mappedObjects;
// Serialize
var memoryStream = cloneContext.MemoryStream;
var writer = new BinarySerializationWriter(memoryStream);
writer.Context.SerializerSelector = cloneSerializerSelector;
writer.Context.Set(CloneContextProperty, cloneContext);
writer.SerializeExtended(entity, ArchiveMode.Serialize, null);
// Deserialization reuses this list and expect it to be empty at the beginning.
cloneContext.SerializedObjects.Clear();
// Deserialize
T result = null;
memoryStream.Seek(0, SeekOrigin.Begin);
var reader = new BinarySerializationReader(memoryStream);
reader.Context.SerializerSelector = cloneSerializerSelector;
reader.Context.Set(CloneContextProperty, cloneContext);
reader.SerializeExtended(ref result, ArchiveMode.Deserialize, null);
return result;
}
finally
{
cloneContext.Cleanup();
}
}
}
public delegate bool TryGetValueFunction<in TKey, TResult>(TKey key, out TResult result);
/// <summary>
/// Helper class for cloning <see cref="Entity"/>.
/// </summary>
public class CloneContext
{
public void Cleanup()
{
MemoryStream.SetLength(0);
MappedObjects = null;
SerializedObjects.Clear();
ClonedObjects = null;
SharedObjects.Clear();
EntitySerializerSelector = null;
}
public MemoryStream MemoryStream = new MemoryStream(4096);
public TryGetValueFunction<object, object> MappedObjects;
public readonly HashSet<object> SerializedObjects = new HashSet<object>();
/// <summary>
/// Lists objects that should be cloned.
/// </summary>
public HashSet<object> ClonedObjects;
/// <summary>
/// Stores objects that should be reused in the new cloned instance.
/// </summary>
public readonly List<object> SharedObjects = new List<object>();
/// <summary>
/// Special serializer that goes through <see cref="EntitySerializerSelector"/> and <see cref="CloneEntityComponentSerializer{T}"/>.
/// </summary>
public SerializerSelector EntitySerializerSelector;
}
}
}