4 using System.Collections.Generic;
6 using SiliconStudio.Core.IO;
8 namespace SiliconStudio.Assets.Analysis
20 public delegate
bool ContainsAssetWithLocationDelegate(
UFile location);
27 public delegate
bool ContainsAssetWithIdDelegate(Guid guid);
29 private readonly HashSet<Guid> existingIds;
30 private readonly HashSet<string> existingLocations;
32 private readonly ContainsAssetWithLocationDelegate containsLocation;
34 private readonly ContainsAssetWithIdDelegate containsId;
48 public AssetResolver(ContainsAssetWithLocationDelegate containsAssetWithLocation, ContainsAssetWithIdDelegate containsAssetWithId)
50 existingIds =
new HashSet<Guid>();
51 existingLocations =
new HashSet<string>(StringComparer.OrdinalIgnoreCase);
52 ContainsAssetWithLocation = containsAssetWithLocation;
53 ContainsAssetWithId = containsAssetWithId;
54 containsLocation = DefaultContainsLocation;
55 containsId = DefaultContainsId;
62 public HashSet<string> ExistingLocations
66 return existingLocations;
74 public HashSet<Guid> ExistingIds
86 public bool AlwaysCreateNewId {
get; set; }
92 public ContainsAssetWithLocationDelegate ContainsAssetWithLocation {
get; set; }
98 public ContainsAssetWithIdDelegate ContainsAssetWithId {
get; set; }
109 var result = FindAvailableLocation(location, containsLocation, out newLocation);
110 ExistingLocations.Add(newLocation ?? location);
123 var result = AlwaysCreateNewId || containsId(assetId);
126 newGuid = Guid.NewGuid();
128 ExistingIds.Add(newGuid);
140 if (package == null)
throw new ArgumentNullException(
"package");
142 var packagesForLocation = package.FindDependencies(
true);
143 var packagesForIds = package.Session != null ? package.Session.Packages : packagesForLocation;
145 return new AssetResolver(packagesForLocation.ContainsAsset, packagesForIds.ContainsAsset);
156 if (packages == null)
throw new ArgumentNullException(
"packages");
157 return new AssetResolver(packages.ContainsAsset, packages.ContainsAsset);
163 private bool DefaultContainsLocation(
UFile location)
165 if (ExistingLocations.Contains(location))
169 if (ContainsAssetWithLocation != null)
171 return ContainsAssetWithLocation(location);
179 private bool DefaultContainsId(Guid guid)
181 if (ExistingIds.Contains(guid))
185 if (ContainsAssetWithId != null)
187 return ContainsAssetWithId(guid);
201 private static bool FindAvailableLocation(
UFile location, ContainsAssetWithLocationDelegate containsAssetWithLocation, out
UFile newLocation)
203 if (location == null)
throw new ArgumentNullException(
"location");
204 if (containsAssetWithLocation == null)
throw new ArgumentNullException(
"containsAssetWithLocation");
206 var pathStr = location.FullPath;
208 string newPath = pathStr;
209 while (containsAssetWithLocation(newPath))
211 newPath = pathStr +
"_" + i;
215 newLocation = newPath;
bool RegisterLocation(UFile location, out UFile newLocation)
Finds a name available for a new asset. This method will try to create a name based on an existing na...
static AssetResolver FromPackage(Package package)
Creates a new AssetResolver using an existing package to check the existence of asset locations and i...
Helper to find available new asset locations and identifiers.
AssetResolver()
Initializes a new instance of the AssetResolver class.
bool RegisterId(Guid assetId, out Guid newGuid)
Registers an asset identifier for usage.
AssetResolver(ContainsAssetWithLocationDelegate containsAssetWithLocation, ContainsAssetWithIdDelegate containsAssetWithId)
Initializes a new instance of the AssetResolver class.
A package managing assets.
static AssetResolver FromPackage(IList< Package > packages)
Creates a new AssetResolver using an existing package to check the existence of asset locations and i...
Defines a normalized file path. See UPath for details. This class cannot be inherited.