Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
PackageSession.Extensions.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.Generic;
5 using System.Linq;
6 using SiliconStudio.Assets.Analysis;
7 using SiliconStudio.Core.Diagnostics;
8 using SiliconStudio.Core.IO;
9 
10 namespace SiliconStudio.Assets
11 {
12  /// <summary>
13  /// Extension methods for <see cref="PackageSession"/>.
14  /// </summary>
15  public static class PackageSessionExtensions
16  {
17  /// <summary>
18  /// Finds an asset from all the packages by its location.
19  /// </summary>
20  /// <param name="session">The session.</param>
21  /// <param name="location">The location of the asset.</param>
22  /// <returns>An <see cref="AssetItem" /> or <c>null</c> if not found.</returns>
23  public static AssetItem FindAsset(this PackageSession session, UFile location)
24  {
25  var packages = session.CurrentPackage != null ? session.GetPackagesFromCurrent() : session.Packages;
26  return packages.Select(packageItem => packageItem.Assets.Find(location)).FirstOrDefault(asset => asset != null);
27  }
28 
29  /// <summary>
30  /// Finds an asset from all the packages by its id.
31  /// </summary>
32  /// <param name="session">The session.</param>
33  /// <param name="assetId">The assetId of the asset.</param>
34  /// <returns>An <see cref="AssetItem" /> or <c>null</c> if not found.</returns>
35  public static AssetItem FindAsset(this PackageSession session, Guid assetId)
36  {
37  return session.Packages.Select(packageItem => packageItem.Assets.Find(assetId)).FirstOrDefault(asset => asset != null);
38  }
39 
40  /// <summary>
41  /// Create a <see cref="PackageSession"/> that can be used to compile an <see cref="AssetItem"/> by analyzing and resolving its dependencies.
42  /// </summary>
43  /// <returns>The package packageSession that can be used to compile the asset item.</returns>
44  public static PackageSession CreateCompilePackageFromAsset(this PackageSession session, AssetItem originalAssetItem)
45  {
46  if (originalAssetItem == null) throw new ArgumentNullException("originalAssetItem");
47 
48  // Find the asset from the session
49  var assetItem = session.FindAsset(originalAssetItem.Id);
50  if (assetItem == null)
51  {
52  throw new ArgumentException("Cannot find the specified AssetItem instance in the session");
53  }
54 
55  // Calculate dependencies
56  var dependencies = session.DependencyManager.ComputeDependencies(assetItem, AssetDependencySearchOptions.Out | AssetDependencySearchOptions.Recursive);
57  var assetItemRootCloned = dependencies.Item.Clone();
58 
59  // Store the fullpath to the sourcefolder, this avoid us to clone hierarchy of packages
60  assetItemRootCloned.SourceFolder = assetItem.FullPath.GetParent();
61 
62  // create the compile root package and package session
63  var assetPackageCloned = new Package();
64  var compilePackageSession = new PackageSession(assetPackageCloned);
65 
66  assetPackageCloned.Assets.Add(assetItemRootCloned);
67 
68  // For each asset item dependency, clone it in the new package
69  foreach (var item in dependencies)
70  {
71  // Only add assets not already added (in case of circular dependencies)
72  if (assetPackageCloned.Assets.Find(item.Id) == null)
73  {
74  // create a copy of the asset item and add it to the appropriate compile package
75  var assetItemCloned = item.Clone();
76 
77  // Store the fullpath to the sourcefolder, this avoid us to clone hierarchy of packages
78  assetItemCloned.SourceFolder = item.FullPath.GetParent();
79  assetPackageCloned.Assets.Add(assetItemCloned);
80  }
81  }
82 
83  return compilePackageSession;
84  }
85  }
86 }
PackageCollection Packages
Gets the packages.
An asset item part of a Package accessible through SiliconStudio.Assets.Package.Assets.
Definition: AssetItem.cs:17
static AssetItem FindAsset(this PackageSession session, UFile location)
Finds an asset from all the packages by its location.
A session for editing a package.
A package managing assets.
Definition: Package.cs:28
static PackageSession CreateCompilePackageFromAsset(this PackageSession session, AssetItem originalAssetItem)
Create a PackageSession that can be used to compile an AssetItem by analyzing and resolving its depen...
static AssetItem FindAsset(this PackageSession session, Guid assetId)
Finds an asset from all the packages by its id.
Defines a normalized file path. See UPath for details. This class cannot be inherited.
Definition: UFile.cs:13