Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
TemplateManager.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 
7 namespace SiliconStudio.Assets.Templates
8 {
9  /// <summary>
10  /// Handle templates for creating <see cref="Package"/>, <see cref="ProjectReference"/>
11  /// </summary>
12  public class TemplateManager
13  {
14  private static readonly object ThisLock = new object();
15  private static readonly List<ITemplateGenerator> Generators = new List<ITemplateGenerator>();
16 
17  /// <summary>
18  /// Registers the specified factory.
19  /// </summary>
20  /// <param name="generator">The factory.</param>
21  /// <exception cref="System.ArgumentNullException">factory</exception>
22  public static void Register(ITemplateGenerator generator)
23  {
24  if (generator == null) throw new ArgumentNullException("generator");
25 
26  lock (ThisLock)
27  {
28  if (!Generators.Contains(generator))
29  {
30  Generators.Add(generator);
31  }
32  }
33  }
34 
35  /// <summary>
36  /// Unregisters the specified factory.
37  /// </summary>
38  /// <param name="generator">The factory.</param>
39  /// <exception cref="System.ArgumentNullException">factory</exception>
40  public static void Unregister(ITemplateGenerator generator)
41  {
42  if (generator == null) throw new ArgumentNullException("generator");
43 
44  lock (ThisLock)
45  {
46  Generators.Remove(generator);
47  }
48  }
49 
50  /// <summary>
51  /// Finds all template descriptions.
52  /// </summary>
53  /// <returns>IEnumerable&lt;TemplateGeneratorDescription&gt;.</returns>
55  {
56  // TODO this will not work if the same package has different versions
57  return PackageStore.Instance.GetInstalledPackages().SelectMany(package => package.Templates).OrderBy(tpl => tpl.Order).ThenBy(tpl => tpl.Name);
58  }
59 
60  /// <summary>
61  /// Finds a template generator supporting the specified template description
62  /// </summary>
63  /// <param name="description">The description.</param>
64  /// <returns>A template generator supporting the specified description or null if not found.</returns>
66  {
67  if (description == null) throw new ArgumentNullException("description");
68  lock (ThisLock)
69  {
70  // From most recently registered to older
71  for (int i = Generators.Count - 1; i >=0 ; i--)
72  {
73  var generator = Generators[i];
74  if (generator.IsSupportingTemplate(description))
75  {
76  return generator;
77  }
78  }
79  }
80  return null;
81  }
82  }
83 }
static void Unregister(ITemplateGenerator generator)
Unregisters the specified factory.
Handle templates for creating Package, ProjectReference
Description of a template generator that can be displayed in the GameStudio.
static void Register(ITemplateGenerator generator)
Registers the specified factory.
static IEnumerable< TemplateDescription > FindTemplates()
Finds all template descriptions.
static ITemplateGenerator FindTemplateGenerator(TemplateDescription description)
Finds a template generator supporting the specified template description