Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ItemListCompiler.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 
4 using System;
5 using System.Collections.Generic;
6 using System.IO;
7 
8 using SiliconStudio.BuildEngine;
9 
10 namespace SiliconStudio.Assets.Compiler
11 {
12  /// <summary>
13  /// The base class to compile a series of <see cref="AssetItem"/>s using associated <see cref="IAssetCompiler"/>s.
14  /// An item list compiler only creates the build steps required to creates some output items.
15  /// The result of a compilation has then to be executed by the build engine to effectively create the outputs items.
16  /// </summary>
17  public abstract class ItemListCompiler
18  {
19  private readonly CompilerRegistry<IAssetCompiler> compilerRegistry;
20  private int latestPriority;
21 
22  /// <summary>
23  /// Create an instance of <see cref="ItemListCompiler"/> using the provided compiler registry.
24  /// </summary>
25  /// <param name="compilerRegistry">The registry that contains the compiler to use for each asset type</param>
26  protected ItemListCompiler(CompilerRegistry<IAssetCompiler> compilerRegistry)
27  {
28  this.compilerRegistry = compilerRegistry;
29  }
30 
31  /// <summary>
32  /// Compile the required build steps necessary to produce the desired outputs items.
33  /// </summary>
34  /// <param name="context">The context source.</param>
35  /// <param name="assetItems">The list of items to compile</param>
36  /// <param name="compilationResult">The current compilation result, containing the build steps and the logging</param>
37  protected void Compile(CompilerContext context, IEnumerable<AssetItem> assetItems,
38  AssetCompilerResult compilationResult)
39  {
40  foreach (var assetItem in assetItems)
41  {
42  var itemBuildStep = CompileItem(context, compilationResult, assetItem);
43  if (itemBuildStep != null)
44  compilationResult.BuildSteps.Add(itemBuildStep);
45  }
46  }
47 
48  /// <summary>
49  /// Compile the required build step necessary to produce the desired output item.
50  /// </summary>
51  /// <param name="context">The context.</param>
52  /// <param name="compilationResult">The compilation result.</param>
53  /// <param name="assetItem">The asset item.</param>
54  protected BuildStep CompileItem(CompilerContext context, AssetCompilerResult compilationResult, AssetItem assetItem)
55  {
56  // First try to find an asset compiler for this particular asset.
57  IAssetCompiler compiler;
58  try
59  {
60  compiler = compilerRegistry.GetCompiler(assetItem.Asset.GetType());
61  }
62  catch (Exception ex)
63  {
64  compilationResult.Error("Cannot find a compiler for asset [{0}] from path [{1}]", ex, assetItem.Id,
65  assetItem.Location);
66  return null;
67  }
68 
69  if (compiler == null)
70  {
71  return null;
72  }
73 
74  // Second we are compiling the asset (generating a build step)
75  try
76  {
77  var resultPerAssetType = compiler.Compile(context, assetItem);
78 
79  // Raise the AssetCompiled event.
80  var handler = AssetCompiled;
81  if (handler != null)
82  handler(this, new AssetCompiledArgs(assetItem, resultPerAssetType));
83 
84  resultPerAssetType.CopyTo(compilationResult);
85 
86  if (resultPerAssetType.BuildSteps == null)
87  return null;
88 
89  // Build the module string
90  var assetAbsolutePath = assetItem.FullPath;
91  assetAbsolutePath = Path.GetFullPath(assetAbsolutePath);
92  var module = string.Format("{0}(1,1)", assetAbsolutePath);
93 
94  // Assign module string to all command build steps
95  SetModule(resultPerAssetType.BuildSteps, module);
96 
97  // Add a wait command to the build steps if required by the item build
98  if (resultPerAssetType.ShouldWaitForPreviousBuilds)
99  compilationResult.BuildSteps.Add(new WaitBuildStep());
100 
101  foreach (var buildStep in resultPerAssetType.BuildSteps)
102  {
103  buildStep.Priority = latestPriority++;
104  }
105 
106  // Add the item result build steps the item list result build steps
107  return resultPerAssetType.BuildSteps;
108  }
109  catch (Exception ex)
110  {
111  compilationResult.Error("Unexpected exception while compiling asset [{0}] from path [{1}]", ex, assetItem.Id,
112  assetItem.Location);
113  return null;
114  }
115  }
116 
117  /// <summary>
118  /// Sets recursively the <see cref="BuildStep.Module"/>.
119  /// </summary>
120  /// <param name="buildStep">The build step.</param>
121  /// <param name="module">The module.</param>
122  private void SetModule(BuildStep buildStep, string module)
123  {
124  if (buildStep.Module == null)
125  buildStep.Module = module;
126 
127  var enumerableBuildStep = buildStep as EnumerableBuildStep;
128  if (enumerableBuildStep != null && enumerableBuildStep.Steps != null)
129  {
130  foreach (var child in enumerableBuildStep.Steps)
131  {
132  SetModule(child, module);
133  }
134  }
135  }
136 
137  /// <summary>
138  /// Raised when a single asset has been compiled.
139  /// </summary>
140  public EventHandler<AssetCompiledArgs> AssetCompiled;
141  }
142 }
A BuildStep that can spawn multiple BuildStep. Input and output tracking and merging will be performe...
Result of a compilation of assets when using IAssetCompiler.Compile
ItemListCompiler(CompilerRegistry< IAssetCompiler > compilerRegistry)
Create an instance of ItemListCompiler using the provided compiler registry.
BuildStep CompileItem(CompilerContext context, AssetCompilerResult compilationResult, AssetItem assetItem)
Compile the required build step necessary to produce the desired output item.
string Module
Gets or sets the module associated with this build step, used when logging error/information.
Definition: BuildStep.cs:22
The context used when compiling an asset in a Package.
An asset item part of a Package accessible through SiliconStudio.Assets.Package.Assets.
Definition: AssetItem.cs:17
When embedded in a EnumerableBuildStep, this build step will force all previous computations to be fi...
The base class to compile a series of AssetItems using associated IAssetCompilers. An item list compiler only creates the build steps required to creates some output items. The result of a compilation has then to be executed by the build engine to effectively create the outputs items.
EventHandler< AssetCompiledArgs > AssetCompiled
Raised when a single asset has been compiled.
void Compile(CompilerContext context, IEnumerable< AssetItem > assetItems, AssetCompilerResult compilationResult)
Compile the required build steps necessary to produce the desired outputs items.
The class represents the argument of the ItemListCompiler.AssetCompiled event raised by the ItemListC...
Main interface for compiling an Asset.