Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
AssetCloner.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.IO;
5 using SiliconStudio.Assets.Serializers;
6 using SiliconStudio.Core;
7 using SiliconStudio.Core.Yaml;
8 
9 namespace SiliconStudio.Assets
10 {
11  /// <summary>
12  /// Allows to clone an asset or values stored in an asset.
13  /// </summary>
14  public struct AssetCloner
15  {
16  private readonly object streamOrValueType;
17 
18  /// <summary>
19  /// Initializes a new instance of the <see cref="AssetCloner" /> struct.
20  /// </summary>
21  /// <param name="value">The value to clone.</param>
22  /// <param name="keepOnlySealedOverride">if set to <c>true</c> to discard override information except sealed.</param>
23  public AssetCloner(object value, bool keepOnlySealedOverride = false)
24  {
25  // Clone only if value is not a value type
26  if (value != null && !value.GetType().IsValueType)
27  {
28  // TODO Clone is not supporting SourceCodeAsset (The SourceCodeAsset.Text won't be cloned)
29  var stream = new MemoryStream();
30  YamlSerializer.Serialize(stream, value, keepOnlySealedOverride);
31  streamOrValueType = stream;
32  }
33  else
34  {
35  streamOrValueType = value;
36  }
37  }
38 
39  /// <summary>
40  /// Clones the current value of this cloner with the specified new shadow registry (optional)
41  /// </summary>
42  /// <returns>A clone of the value associated with this cloner.</returns>
43  public object Clone()
44  {
45  var stream = streamOrValueType as Stream;
46  if (stream != null)
47  {
48  stream.Position = 0;
49  var newObject = YamlSerializer.Deserialize(stream);
50  return newObject;
51  }
52  // Else this is a value type, so it is cloned automatically
53  return streamOrValueType;
54  }
55 
56  /// <summary>
57  /// Clones the specified asset using asset serialization.
58  /// </summary>
59  /// <param name="asset">The asset.</param>
60  /// <param name="keepOnlySealedOverride">if set to <c>true</c> to discard override information except sealed.</param>
61  /// <returns>A clone of the asset.</returns>
62  /// TODO: This code is not efficient as it is using YAML serialization for cloning assets
63  public static object Clone(object asset, bool keepOnlySealedOverride = false)
64  {
65  if (asset == null)
66  {
67  return null;
68  }
69  var cloner = new AssetCloner(asset, keepOnlySealedOverride);
70  return cloner.Clone();
71  }
72  }
73 }
AssetCloner(object value, bool keepOnlySealedOverride=false)
Initializes a new instance of the AssetCloner struct.
Definition: AssetCloner.cs:23
object Clone()
Clones the current value of this cloner with the specified new shadow registry (optional) ...
Definition: AssetCloner.cs:43
static object Clone(object asset, bool keepOnlySealedOverride=false)
Clones the specified asset using asset serialization.
Definition: AssetCloner.cs:63
Allows to clone an asset or values stored in an asset.
Definition: AssetCloner.cs:14