Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
PackageExtensions.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 System.Collections;
5 using System.Collections.Generic;
6 using System.Linq;
7 
8 using SiliconStudio.Core.IO;
9 
10 namespace SiliconStudio.Assets
11 {
12  /// <summary>
13  /// Extensions for <see cref="Package"/>
14  /// </summary>
15  public static class PackageExtensions
16  {
17  /// <summary>
18  /// Finds the package dependencies for the specified <see cref="Package" />. See remarks.
19  /// </summary>
20  /// <param name="rootPackage">The root package.</param>
21  /// <param name="includeRootPackage">if set to <c>true</c> [include root package].</param>
22  /// <param name="isRecursive">if set to <c>true</c> [is recursive].</param>
23  /// <returns>List&lt;Package&gt;.</returns>
24  /// <exception cref="System.ArgumentNullException">rootPackage</exception>
25  /// <exception cref="System.ArgumentException">Root package must be part of a session;rootPackage</exception>
26  public static PackageCollection FindDependencies(this Package rootPackage, bool includeRootPackage = false, bool isRecursive = true)
27  {
28  if (rootPackage == null) throw new ArgumentNullException("rootPackage");
29  var packages = new PackageCollection();
30 
31  if (includeRootPackage)
32  {
33  packages.Add(rootPackage);
34  }
35 
36  FillPackageDependencies(rootPackage, isRecursive, packages);
37 
38  return packages;
39  }
40 
41  /// <summary>
42  /// Determines whether the specified packages contains an asset by its guid.
43  /// </summary>
44  /// <param name="packages">The packages.</param>
45  /// <param name="assetGuid">The asset unique identifier.</param>
46  /// <returns><c>true</c> if the specified packages contains asset; otherwise, <c>false</c>.</returns>
47  public static bool ContainsAsset(this IEnumerable<Package> packages, Guid assetGuid)
48  {
49  return packages.Any(package => package.Assets.ContainsById(assetGuid));
50  }
51 
52  /// <summary>
53  /// Determines whether the specified packages contains an asset by its location.
54  /// </summary>
55  /// <param name="packages">The packages.</param>
56  /// <param name="location">The location.</param>
57  /// <returns><c>true</c> if the specified packages contains asset; otherwise, <c>false</c>.</returns>
58  public static bool ContainsAsset(this IEnumerable<Package> packages, UFile location)
59  {
60  return packages.Any(package => package.Assets.Find(location) != null);
61  }
62 
63  private static void FillPackageDependencies(Package rootPackage, bool isRecursive, ICollection<Package> packagesFound)
64  {
65  var session = rootPackage.Session;
66 
67  if (session == null && (rootPackage.Meta.Dependencies.Count > 0 || rootPackage.LocalDependencies.Count > 0))
68  {
69  throw new InvalidOperationException("Cannot query package with dependencies when it is not attached to a session");
70  }
71 
72  // 1. Load store package
73  foreach (var packageDependency in rootPackage.Meta.Dependencies)
74  {
75  var package = session.Packages.Find(packageDependency);
76  if (package == null)
77  {
78  continue;
79  }
80 
81  if (!packagesFound.Contains(package))
82  {
83  packagesFound.Add(package);
84 
85  if (isRecursive)
86  {
87  FillPackageDependencies(package, isRecursive, packagesFound);
88  }
89  }
90  }
91 
92  // 2. Load local packages
93  foreach (var packageReference in rootPackage.LocalDependencies)
94  {
95  var package = session.Packages.Find(packageReference.Id);
96  if (package == null)
97  {
98  continue;
99  }
100 
101  if (!packagesFound.Contains(package))
102  {
103  packagesFound.Add(package);
104 
105  if (isRecursive)
106  {
107  FillPackageDependencies(package, isRecursive, packagesFound);
108  }
109  }
110  }
111 
112  }
113  }
114 }
PackageDependencyCollection Dependencies
Gets the package dependencies.
Definition: PackageMeta.cs:158
static PackageCollection FindDependencies(this Package rootPackage, bool includeRootPackage=false, bool isRecursive=true)
Finds the package dependencies for the specified Package. See remarks.
static bool ContainsAsset(this IEnumerable< Package > packages, Guid assetGuid)
Determines whether the specified packages contains an asset by its guid.
static bool ContainsAsset(this IEnumerable< Package > packages, UFile location)
Determines whether the specified packages contains an asset by its location.
List< PackageReference > LocalDependencies
Gets the local package dependencies used by this package (only valid for local references). Global dependencies are defined through the Meta property in PackageMeta.Dependencies
Definition: Package.cs:91
A package managing assets.
Definition: Package.cs:28
PackageMeta Meta
Gets or sets the metadata associated with this package.
Definition: Package.cs:82
Defines a normalized file path. See UPath for details. This class cannot be inherited.
Definition: UFile.cs:13