4 using System.Collections;
5 using System.Collections.Generic;
6 using System.Collections.Specialized;
7 using System.Diagnostics;
8 using System.Threading;
9 using SiliconStudio.Core;
10 using SiliconStudio.Core.Diagnostics;
11 using SiliconStudio.Core.IO;
13 namespace SiliconStudio.Assets
19 [DebuggerDisplay(
"Count = {Count}")]
22 private readonly
Package package;
23 private object syncRoot;
26 private readonly Dictionary<string, Guid> mapPathToId;
29 private readonly Dictionary<Guid, string> mapIdToPath;
32 private readonly Dictionary<Guid, AssetItem> mapIdToAsset;
37 private bool collectionChangedSuspended;
50 if (package == null)
throw new ArgumentNullException(
"package");
51 this.package = package;
52 mapPathToId =
new Dictionary<string, Guid>();
53 mapIdToPath =
new Dictionary<Guid, string>();
54 mapIdToAsset =
new Dictionary<Guid, AssetItem>();
75 public bool IsDirty {
get; set; }
84 return mapIdToAsset.ContainsKey(assetId);
95 if (!mapPathToId.TryGetValue(location, out
id))
110 mapIdToAsset.TryGetValue(assetId, out value);
121 if (registeredItems.Contains(item))
130 item.Package = package;
133 var asset = item.Asset;
134 asset.IsIdLocked =
true;
137 mapPathToId.Add(item.Location, item.Id);
138 mapIdToPath.Add(item.Id, item.Location);
139 mapIdToAsset.Add(item.Id, item);
140 registeredItems.Add(item);
143 if (!collectionChangedSuspended)
145 var handler = CollectionChanged;
148 handler(
this,
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
159 foreach (var registeredItem
in registeredItems)
161 RemoveInternal(registeredItem);
165 registeredItems.Clear();
168 mapIdToAsset.Clear();
171 if (!collectionChangedSuspended)
173 var handler = CollectionChanged;
176 handler(
this,
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
189 if (assetItem == null)
throw new ArgumentNullException(
"assetItem");
190 return registeredItems.Contains(assetItem);
200 registeredItems.CopyTo(array, arrayIndex);
210 if (registeredItems.Remove(item))
213 registeredItems.Remove(item);
214 mapIdToAsset.Remove(item.Id);
215 mapIdToPath.Remove(item.Id);
216 mapPathToId.Remove(item.Location);
218 RemoveInternal(item);
221 if (!collectionChangedSuspended)
223 var handler = CollectionChanged;
226 handler(
this,
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item));
241 var item = Find(itemId);
254 collectionChangedSuspended =
true;
262 collectionChangedSuspended =
false;
265 var handler = CollectionChanged;
268 handler(
this,
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
272 private void RemoveInternal(
AssetItem item)
275 item.Asset.IsIdLocked =
false;
286 return registeredItems.Count;
294 public bool IsReadOnly
331 throw new ArgumentNullException(
"item",
"Cannot add an empty asset item reference");
334 if (registeredItems.Contains(item))
336 throw new ArgumentException(
"Asset already exist in this collection",
"item");
339 if (item.
Id == Guid.Empty)
341 throw new ArgumentException(
"Cannot add an asset with an empty Id",
"item");
346 throw new ArgumentException(
"Cannot add an asset that is already added to another package",
"item");
349 var location = item.Location;
350 if (mapPathToId.ContainsKey(location))
352 throw new ArgumentException(
"An asset [{0}] with the same location [{1}] is already registered ".ToFormat(mapPathToId[location], location.GetDirectoryAndFileName()),
"item");
355 if (!
string.IsNullOrEmpty(location.GetFileExtension()))
357 throw new ArgumentException(
"An asset [{0}] cannot be registered with an extension [{1}]".ToFormat(location, location.GetFileExtension()),
"item");
360 if (mapIdToPath.ContainsKey(item.
Id))
362 throw new ArgumentException(
"An asset with the same id [{0}] is already registered with the location [{1}]".ToFormat(item.
Id, location.GetDirectoryAndFileName()),
"item");
365 if (location.HasDrive)
367 throw new ArgumentException(
"Asset location [{0}] cannot contain drive information".ToFormat(location),
"item");
370 if (location.IsAbsolute)
372 throw new ArgumentException(
"Asset location [{0}] must be relative and not absolute (not start with '/')".ToFormat(location),
"item");
375 if (location.GetDirectory() != null && location.GetDirectory().StartsWith(
".."))
377 throw new ArgumentException(
"Asset location [{0}] cannot start with relative '..'".ToFormat(location),
"item");
387 if (otherPackage.Assets.ContainsById(item.
Id))
389 throw new ArgumentException(
"Cannot add the asset [{0}] that is already in different package [{1}] in the current session".ToFormat(item.
Id, otherPackage.Id));
398 return registeredItems.GetEnumerator();
401 IEnumerator IEnumerable.GetEnumerator()
403 return GetEnumerator();
407 void ICollection.CopyTo(Array array,
int index)
409 foreach (var item
in this)
411 array.SetValue(item, index++);
415 object ICollection.SyncRoot
419 if (syncRoot == null)
420 Interlocked.CompareExchange<
object>(ref syncRoot,
new object(), (object)null);
421 return this.syncRoot;
425 bool ICollection.IsSynchronized
bool Contains(AssetItem assetItem)
Checks this collection contains the specified asset reference, throws an exception if not found...
void Add(AssetItem item)
Adds an AssetItem to this instance.
void SuspendCollectionChanged()
Suspends the collection changed that can happen on this collection.
void CheckCanAdd(AssetItem item)
Checks if the specified item can be add to this collection.
Guid Id
Gets the unique identifier of this asset.
PackageAssetCollection(Package package)
Initializes a new instance of the PackageAssetCollection class.
PackageCollection Packages
Gets the packages.
bool ContainsById(Guid assetId)
Determines whether this instance contains an asset with the specified identifier. ...
void Clear()
Removes all items from this instance.
A collection of AssetItem that contains only absolute location without any drive information. This class cannot be inherited.
An asset item part of a Package accessible through SiliconStudio.Assets.Package.Assets.
bool Remove(AssetItem item)
Removes an AssetItem from this instance.
Package Package
Gets the package where this asset is stored.
void CopyTo(AssetItem[] array, int arrayIndex)
Copies items to the specified array.
PackageSession Session
Gets the session.
void ResumeCollectionChanged()
Resumes the collection changed that happened on this collection and fire a NotifyCollectionChangedAct...
AssetItem Find(string location)
Finds an asset by its location.
Use this class to provide a debug output in Visual Studio debugger.
IEnumerator< AssetItem > GetEnumerator()
AssetItem Find(Guid assetId)
Finds an asset by its location.
NotifyCollectionChangedEventHandler CollectionChanged
Occurs when the collection changes.
A package managing assets.
bool RemoveById(Guid itemId)
Removes an AssetItem from this instance.
An interface that tags if an object is dirty.