Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
AssetBase.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 SiliconStudio.Core;
5 using SiliconStudio.Core.IO;
6 using SiliconStudio.Core.Serialization;
7 using SiliconStudio.Core.Serialization.Serializers;
8 
9 namespace SiliconStudio.Assets
10 {
11  /// <summary>
12  /// An asset item part of a <see cref="Package"/> accessible through <see cref="Package.Assets"/>.
13  /// </summary>
14  [DataContract("AssetBase")]
15  [DataSerializer(typeof(AssetBase.Serializer))]
16  public sealed class AssetBase : IContentReference
17  {
18  private readonly UFile location;
19  private readonly Asset asset;
20 
21  /// <summary>
22  /// The location used for the default import base.
23  /// </summary>
24  public static readonly UFile DefaultImportBase = new UFile("--import--");
25 
26  /// <summary>
27  /// Initializes a new instance of the <see cref="AssetBase"/> class.
28  /// </summary>
29  /// <param name="asset">The asset.</param>
30  public AssetBase(Asset asset) : this(DefaultImportBase, asset)
31  {
32  }
33 
34  /// <summary>
35  /// Initializes a new instance of the <see cref="AssetItem"/> class.
36  /// </summary>
37  /// <param name="location">The location.</param>
38  /// <param name="asset">The asset.</param>
39  public AssetBase(UFile location, Asset asset)
40  {
41  if (location == null) throw new ArgumentNullException("location");
42  if (asset == null) throw new ArgumentNullException("asset");
43  this.location = location;
44  this.asset = asset;
45  }
46 
47  /// <summary>
48  /// Gets the location of this asset.
49  /// </summary>
50  /// <value>The location.</value>
51  public string Location
52  {
53  get
54  {
55  return location;
56  }
57  }
58 
59  /// <summary>
60  /// Gets the asset unique identifier.
61  /// </summary>
62  /// <value>The identifier.</value>
63  public Guid Id
64  {
65  get
66  {
67  return asset.Id;
68  }
69  }
70 
71  /// <summary>
72  /// Gets or sets the asset.
73  /// </summary>
74  /// <value>The asset.</value>
75  public Asset Asset
76  {
77  get
78  {
79  return asset;
80  }
81  }
82 
83  /// <summary>
84  /// Gets a value indicating whether this instance is a root import base (the original import).
85  /// </summary>
86  /// <value><c>true</c> if this instance is an import base; otherwise, <c>false</c>.</value>
87  public bool IsRootImport
88  {
89  get
90  {
91  return location == DefaultImportBase && Id == Guid.Empty;
92  }
93  }
94 
95  public override string ToString()
96  {
97  return string.Format("{0} => {1}", location, Id);
98  }
99 
100  internal class Serializer : DataSerializer<AssetBase>
101  {
102  public override void Serialize(ref AssetBase assetBase, ArchiveMode mode, SerializationStream stream)
103  {
104  if (mode == ArchiveMode.Serialize)
105  {
106  stream.Write(assetBase.Location);
107  stream.SerializeExtended(assetBase.Asset, mode);
108  }
109  else
110  {
111  var location = stream.ReadString();
112  Asset asset = null;
113  stream.SerializeExtended(ref asset, mode);
114  assetBase = new AssetBase(location, asset);
115  }
116  }
117  }
118  }
119 }
AssetBase(UFile location, Asset asset)
Initializes a new instance of the AssetItem class.
Definition: AssetBase.cs:39
Base class for Asset.
Definition: Asset.cs:14
An asset item part of a Package accessible through Package.Assets.
Definition: AssetBase.cs:16
AssetBase(Asset asset)
Initializes a new instance of the AssetBase class.
Definition: AssetBase.cs:30
override string ToString()
Definition: AssetBase.cs:95
Base class for implementation of SerializationStream.
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
An interface that provides a reference to an asset.
Defines a normalized file path. See UPath for details. This class cannot be inherited.
Definition: UFile.cs:13