Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
AssetReference.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 
8 namespace SiliconStudio.Assets
9 {
10  /// <summary>
11  /// An asset reference.
12  /// </summary>
13  [DataContract]
14  [DataStyle(DataStyle.Compact)]
15  public abstract class AssetReference : ITypedContentReference, IEquatable<AssetReference>
16  {
17  private readonly UFile location;
18  private readonly Guid id;
19 
20  /// <summary>
21  /// Initializes a new instance of the <see cref="AssetReference"/> class.
22  /// </summary>
23  /// <param name="id">The unique identifier of the asset.</param>
24  /// <param name="location">The location.</param>
25  internal AssetReference(Guid id, UFile location)
26  {
27  this.location = location;
28  this.id = id;
29  }
30 
31  /// <summary>
32  /// Gets or sets the unique identifier of the reference asset.
33  /// </summary>
34  /// <value>The unique identifier of the reference asset..</value>
35  [DataMember(10)]
36  public Guid Id
37  {
38  get
39  {
40  return id;
41  }
42  }
43 
44  /// <summary>
45  /// Gets or sets the location of the asset.
46  /// </summary>
47  /// <value>The location.</value>
48  [DataMember(20)]
49  public string Location
50  {
51  get
52  {
53  return location;
54  }
55  }
56 
57  public bool Equals(AssetReference other)
58  {
59  if (ReferenceEquals(null, other)) return false;
60  if (ReferenceEquals(this, other)) return true;
61  return Equals(location, other.location) && id.Equals(other.id);
62  }
63 
64  public override bool Equals(object obj)
65  {
66  if (ReferenceEquals(null, obj)) return false;
67  if (ReferenceEquals(this, obj)) return true;
68  return obj is AssetReference && Equals((AssetReference)obj);
69  }
70 
71  public override int GetHashCode()
72  {
73  unchecked
74  {
75  return ((location != null ? location.GetHashCode() : 0)*397) ^ id.GetHashCode();
76  }
77  }
78 
79  /// <summary>
80  /// Implements the ==.
81  /// </summary>
82  /// <param name="left">The left.</param>
83  /// <param name="right">The right.</param>
84  /// <returns>The result of the operator.</returns>
85  public static bool operator ==(AssetReference left, AssetReference right)
86  {
87  return Equals(left, right);
88  }
89 
90  /// <summary>
91  /// Implements the !=.
92  /// </summary>
93  /// <param name="left">The left.</param>
94  /// <param name="right">The right.</param>
95  /// <returns>The result of the operator.</returns>
96  public static bool operator !=(AssetReference left, AssetReference right)
97  {
98  return !Equals(left, right);
99  }
100 
101  /// <inheritdoc/>
102  public override string ToString()
103  {
104  // WARNING: This should not be modified as it is used for serializing
105  return string.Format("{0}:{1}", id, location);
106  }
107 
108  [DataMemberIgnore]
109  public abstract Type Type { get; }
110 
111  /// <summary>
112  /// Tries to parse an asset reference in the format "GUID:Location".
113  /// </summary>
114  /// <param name="referenceType">The referenceType.</param>
115  /// <param name="id">The identifier.</param>
116  /// <param name="location">The location.</param>
117  /// <returns><c>true</c> if parsing was successful, <c>false</c> otherwise.</returns>
118  public static AssetReference New(Type referenceType, Guid id, UFile location)
119  {
120  if (referenceType == null) throw new ArgumentNullException("referenceType");
121  if (!typeof(AssetReference).IsAssignableFrom(referenceType)) throw new ArgumentException("Reference must inherit from AssetReference", "referenceType");
122 
123  return (AssetReference)Activator.CreateInstance(referenceType, id, location);
124  }
125 
126  /// <summary>
127  /// Tries to parse an asset reference in the format "GUID:Location".
128  /// </summary>
129  /// <param name="assetReferenceText">The asset reference.</param>
130  /// <param name="guid">The unique identifier.</param>
131  /// <param name="location">The location.</param>
132  /// <returns><c>true</c> if parsing was successful, <c>false</c> otherwise.</returns>
133  /// <exception cref="System.ArgumentNullException">assetReferenceText</exception>
134  public static bool TryParse(string assetReferenceText, out Guid guid, out UFile location)
135  {
136  if (assetReferenceText == null) throw new ArgumentNullException("assetReferenceText");
137 
138  guid = Guid.Empty;
139  location = null;
140  int indexOf = assetReferenceText.IndexOf(':');
141  if (indexOf < 0)
142  {
143  return false;
144  }
145  if (!Guid.TryParse(assetReferenceText.Substring(0, indexOf), out guid))
146  {
147  return false;
148  }
149  location = new UFile(assetReferenceText.Substring(indexOf + 1));
150 
151  return true;
152  }
153 
154  /// <summary>
155  /// Tries to parse an asset reference in the format "GUID:Location".
156  /// </summary>
157  /// <param name="referenceType"></param>
158  /// <param name="assetReferenceText">The asset reference.</param>
159  /// <param name="assetReference">The reference.</param>
160  /// <returns><c>true</c> if parsing was successful, <c>false</c> otherwise.</returns>
161  public static bool TryParse(Type referenceType, string assetReferenceText, out AssetReference assetReference)
162  {
163  if (referenceType == null) throw new ArgumentNullException("referenceType");
164  if (assetReferenceText == null) throw new ArgumentNullException("assetReferenceText");
165 
166  assetReference = null;
167  Guid guid;
168  UFile location;
169  if (!TryParse(assetReferenceText, out guid, out location))
170  {
171  return false;
172  }
173  assetReference = New(referenceType, guid, location);
174  return true;
175  }
176  }
177 
178  /// <summary>
179  /// Extension methods for <see cref="AssetReference"/>
180  /// </summary>
181  public static class AssetReferenceExtensions
182  {
183  /// <summary>
184  /// Determines whether the specified asset reference has location. If the reference is null, return <c>false</c>.
185  /// </summary>
186  /// <param name="assetReference">The asset reference.</param>
187  /// <returns><c>true</c> if the specified asset reference has location; otherwise, <c>false</c>.</returns>
188  public static bool HasLocation(this AssetReference assetReference)
189  {
190  return assetReference != null && assetReference.Location != null;
191  }
192  }
193 
194  /// <summary>
195  /// A typed content reference
196  /// </summary>
197  /// <typeparam name="T"></typeparam>
198  [DataContract("aref")]
199  [DataStyle(DataStyle.Compact)]
200  public sealed class AssetReference<T> : AssetReference where T : Asset
201  {
202  /// <summary>
203  /// Initializes a new instance of the <see cref="AssetReference" /> class.
204  /// </summary>
205  /// <param name="id">The unique identifier of the asset.</param>
206  /// <param name="location">The location.</param>
207  public AssetReference(Guid id, UFile location) : base(id, location)
208  {
209  }
210 
211  [DataMemberIgnore]
212  public override Type Type
213  {
214  get
215  {
216  return typeof(T);
217  }
218  }
219  }
220 }
static bool TryParse(string assetReferenceText, out Guid guid, out UFile location)
Tries to parse an asset reference in the format "GUID:Location".
override bool Equals(object obj)
Base class for Asset.
Definition: Asset.cs:14
Extension methods for AssetReference
static bool HasLocation(this AssetReference assetReference)
Determines whether the specified asset reference has location. If the reference is null...
static bool TryParse(Type referenceType, string assetReferenceText, out AssetReference assetReference)
Tries to parse an asset reference in the format "GUID:Location".
override int GetHashCode()
Definition: UPath.cs:302
bool Equals(AssetReference other)
AssetReference(Guid id, UFile location)
Initializes a new instance of the AssetReference class.
DataStyle
Specifies the style used for textual serialization when an array/list or a dictionary/map must be ser...
Definition: DataStyle.cs:9
static AssetReference New(Type referenceType, Guid id, UFile location)
Tries to parse an asset reference in the format "GUID:Location".
Defines a normalized file path. See UPath for details. This class cannot be inherited.
Definition: UFile.cs:13