4 using System.Collections;
5 using System.Collections.Generic;
6 using System.Runtime.CompilerServices;
8 using SiliconStudio.Assets.Visitors;
9 using SiliconStudio.Core.IO;
10 using SiliconStudio.Core.Reflection;
11 using SiliconStudio.Core.Serialization;
13 namespace SiliconStudio.Assets.Analysis
21 private static readonly
object CachingLock =
new object();
23 private static readonly Dictionary<object, List<AssetReferenceLink>> CachingReferences =
new Dictionary<object, List<AssetReferenceLink>>();
25 private static bool enableCaching;
31 internal static bool EnableCaching
41 if (enableCaching != value)
43 CachingReferences.Clear();
46 enableCaching = value;
56 public static List<AssetReferenceLink>
Visit(
object obj)
58 if (obj == null)
throw new ArgumentNullException(
"obj");
60 List<AssetReferenceLink> assetReferences = null;
66 if (CachingReferences.TryGetValue(obj, out assetReferences))
68 assetReferences =
new List<AssetReferenceLink>(assetReferences);
73 if (assetReferences == null)
75 assetReferences =
new List<AssetReferenceLink>();
77 var assetReferenceVistor =
new AssetReferenceVistor { References = assetReferences };
78 assetReferenceVistor.Visit(obj);
84 CachingReferences[obj] = assetReferences;
89 return assetReferences;
94 public AssetReferenceVistor()
96 References =
new List<AssetReferenceLink>();
99 public List<AssetReferenceLink> References {
get; set; }
101 public override void VisitObject(
object obj,
ObjectDescriptor descriptor,
bool visitMembers)
103 base.VisitObject(obj, descriptor, visitMembers);
105 if (reference != null)
107 AddLink(reference, (guid, location) =>
109 reference.Id = guid.HasValue ? guid.Value : reference.Id;
110 reference.Location = location;
118 base.VisitArrayItem(array, descriptor, index, item, itemDescriptor);
119 var assetReference = item as AssetReference;
120 if (assetReference != null)
125 var newValue = AssetReference.New(descriptor.ElementType, guid.HasValue ? guid.Value : assetReference.Id, location);
126 array.SetValue(newValue, index);
132 var assetBase = item as AssetBase;
133 if (assetBase != null)
138 var newValue =
new AssetBase(location, assetBase.Asset);
139 array.SetValue(newValue, index);
143 else if (item is
UFile)
148 var newValue =
new UFile(location);
149 array.SetValue(newValue, index);
158 var newValue =
new UFile(location);
159 array.SetValue(newValue, index);
168 base.VisitCollectionItem(collection, descriptor, index, item, itemDescriptor);
169 var assetReference = item as AssetReference;
171 if (assetReference != null)
173 var list = (IList)collection;
174 AddLink(assetReference, (guid, location) => list[index] = AssetReference.New(descriptor.ElementType, guid.HasValue ? guid.Value : assetReference.Id, location));
178 var assetBase = item as AssetBase;
179 if (assetBase != null)
181 var list = (IList)collection;
182 AddLink(assetBase, (guid, location) => list[index] =
new AssetBase(location, assetBase.Asset));
184 else if (item is UFile)
186 var list = (IList)collection;
187 AddLink(item, (guid, location) => list[index] =
new UFile(location));
189 else if (item is UDirectory)
191 var list = (IList)collection;
192 AddLink(item, (guid, location) => list[index] =
new UDirectory(location));
199 base.VisitDictionaryKeyValue(dictionaryObj, descriptor, key, keyDescriptor, value, valueDescriptor);
200 var assetReference = value as AssetReference;
201 if (assetReference != null)
203 AddLink(assetReference,
206 var newValue = AssetReference.New(descriptor.ValueType, guid.HasValue ? guid.Value : assetReference.Id, location);
207 descriptor.SetValue(dictionaryObj, key, newValue);
213 var assetBase = value as AssetBase;
214 if (assetBase != null)
219 var newValue =
new AssetBase(location, assetBase.Asset);
220 descriptor.SetValue(dictionaryObj, key, newValue);
224 else if (value is UFile)
229 var newValue =
new UFile(location);
230 descriptor.SetValue(dictionaryObj, key, newValue);
234 else if (value is UDirectory)
239 var newValue =
new UDirectory(location);
240 descriptor.SetValue(dictionaryObj, key, newValue);
249 base.VisitObjectMember(container, containerDescriptor, member, value);
250 var assetReference = value as AssetReference;
251 if (assetReference != null)
253 AddLink(assetReference,
256 var newValue = AssetReference.New(member.Type, guid.HasValue ? guid.Value : assetReference.Id, location);
257 member.Set(container, newValue);
263 var assetBase = value as AssetBase;
264 if (assetBase != null)
269 var newValue =
new AssetBase(location, assetBase.Asset);
270 member.Set(container, newValue);
274 else if (value is UFile)
279 var newValue =
new UFile(location);
280 member.Set(container, newValue);
284 else if (value is UDirectory)
289 var newValue =
new UDirectory(location);
290 member.Set(container, newValue);
297 private void AddLink(
object value, Func<Guid?, string, object> updateReference)
299 References.Add(
new AssetReferenceLink(CurrentPath.Clone(), value, updateReference));
Provides a descriptor for a System.Collections.ICollection.
This analysis provides a method for visiting asset and file references (IContentReference or UFile or...
Default implementation of a ITypeDescriptor.
Describe a member of an object.
Provides a descriptor for a System.Collections.IDictionary.
Defines a normalized directory path. See UPath for details. This class cannot be inherited.
static List< AssetReferenceLink > Visit(object obj)
Gets all references (subclass of IContentReference and UFile) from the specified asset ...
Provides access members of a type.
A descriptor for an array.
Defines a normalized file path. See UPath for details. This class cannot be inherited.