Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
CompilerRegistry.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 
7 using SiliconStudio.Core;
8 
9 namespace SiliconStudio.Assets.Compiler
10 {
11  /// <summary>
12  /// A registry containing the compiler associated to all the asset types
13  /// </summary>
14  /// <typeparam name="T">The type of the class implementing the <see cref="IAssetCompiler"/> interface to register.</typeparam>
15  public class CompilerRegistry<T> where T: class, IAssetCompiler
16  {
17  private readonly Dictionary<Type, T> typeToCompiler = new Dictionary<Type, T>();
18 
19  /// <summary>
20  /// Gets or sets the default compiler to use when no compiler are explicitly registered for a type.
21  /// </summary>
22  public T DefaultCompiler { get; set; }
23 
24  /// <summary>
25  /// Register a compiler for a given <see cref="Asset"/> type.
26  /// </summary>
27  /// <param name="type">The type of asset the compiler can compile</param>
28  /// <param name="compiler">The compiler to use</param>
29  public void RegisterCompiler(Type type, T compiler)
30  {
31  if (compiler == null) throw new ArgumentNullException("compiler");
32 
33  AssertAssetType(type);
34 
35  typeToCompiler[type] = compiler;
36  }
37 
38  /// <summary>
39  /// Gets the compiler associated to an <see cref="Asset"/> type.
40  /// </summary>
41  /// <param name="type">The type of the <see cref="Asset"/></param>
42  /// <returns>The compiler associated the provided asset type or null if no compiler exists for that type.</returns>
43  public T GetCompiler(Type type)
44  {
45  AssertAssetType(type);
46 
47  if (!typeToCompiler.ContainsKey(type))
48  return DefaultCompiler;
49 
50  return typeToCompiler[type];
51  }
52 
53  private static void AssertAssetType(Type assetType)
54  {
55  if (assetType == null)
56  throw new ArgumentNullException("assetType");
57 
58  if (!typeof(Asset).IsAssignableFrom(assetType))
59  throw new ArgumentException("Type [{0}] must be assignable to Asset".ToFormat(assetType), "assetType");
60  }
61  }
62 }
T GetCompiler(Type type)
Gets the compiler associated to an Asset type.
Base class for Asset.
Definition: Asset.cs:14
void RegisterCompiler(Type type, T compiler)
Register a compiler for a given Asset type.
Main interface for compiling an Asset.