Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
AssemblyRegistry.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.Reflection;
6 using SiliconStudio.Core.Diagnostics;
7 
8 namespace SiliconStudio.Core.Reflection
9 {
10  /// <summary>
11  /// Provides a basic infrastructure to associate an assembly with some categories and to
12  /// query and register on new registered assembly event.
13  /// </summary>
14  public static class AssemblyRegistry
15  {
16  private static readonly Logger Log = GlobalLogger.GetLogger("AssemblyRegistry");
17  private static readonly object Lock = new object();
18  private static readonly Dictionary<string, HashSet<Assembly>> MapCategoryToAssemblies = new Dictionary<string, HashSet<Assembly>>();
19  private static readonly Dictionary<Assembly, HashSet<string>> MapAssemblyToCategories = new Dictionary<Assembly, HashSet<string>>();
20 
21  /// <summary>
22  /// Occurs when an assembly is registered.
23  /// </summary>
24  public static event EventHandler<AssemblyRegisteredEventArgs> AssemblyRegistered;
25 
26  /// <summary>
27  /// Finds all registered assemblies.
28  /// </summary>
29  /// <returns>A set of all assembly registered.</returns>
30  /// <exception cref="System.ArgumentNullException">categories</exception>
31  public static HashSet<Assembly> FindAll()
32  {
33  lock (Lock)
34  {
35  return new HashSet<Assembly>(MapAssemblyToCategories.Keys);
36  }
37  }
38 
39  /// <summary>
40  /// Finds registered assemblies that are associated with the specified categories.
41  /// </summary>
42  /// <param name="categories">The categories.</param>
43  /// <returns>A set of assembly associated with the specified categories.</returns>
44  /// <exception cref="System.ArgumentNullException">categories</exception>
45  public static HashSet<Assembly> Find(IEnumerable<string> categories)
46  {
47  if (categories == null) throw new ArgumentNullException("categories");
48  var assemblies = new HashSet<Assembly>();
49  lock (Lock)
50  {
51  foreach (var category in categories)
52  {
53  if (category == null)
54  continue;
55 
56  HashSet<Assembly> assembliesFound;
57  if (MapCategoryToAssemblies.TryGetValue(category, out assembliesFound))
58  {
59  foreach (var assembly in assembliesFound)
60  assemblies.Add(assembly);
61  }
62  }
63  }
64  return assemblies;
65  }
66 
67  /// <summary>
68  /// Finds registered assemblies that are associated with the specified categories.
69  /// </summary>
70  /// <param name="categories">The categories.</param>
71  /// <returns>A set of assemblies associated with the specified categories.</returns>
72  /// <exception cref="System.ArgumentNullException">categories</exception>
73  public static HashSet<Assembly> Find(params string[] categories)
74  {
75  return Find((IEnumerable<string>)categories);
76  }
77 
78  /// <summary>
79  /// Finds registered categories that are associated with the specified assembly.
80  /// </summary>
81  /// <param name="assembly">The assembly.</param>
82  /// <returns>A set of category associated with the specified assembly.</returns>
83  /// <exception cref="System.ArgumentNullException">categories</exception>
84  public static HashSet<string> FindCategories(Assembly assembly)
85  {
86  if (assembly == null) throw new ArgumentNullException("assembly");
87  var categories = new HashSet<string>();
88  lock (Lock)
89  {
90  HashSet<string> categoriesFound;
91  if (MapAssemblyToCategories.TryGetValue(assembly, out categoriesFound))
92  {
93  foreach (var category in categoriesFound)
94  categories.Add(category);
95  }
96  }
97  return categories;
98  }
99 
100  /// <summary>
101  /// Registers an assembly with the specified categories.
102  /// </summary>
103  /// <param name="assembly">The assembly.</param>
104  /// <param name="categories">The categories to associate with this assembly.</param>
105  /// <exception cref="System.ArgumentNullException">
106  /// assembly
107  /// or
108  /// categories
109  /// </exception>
110  public static void Register(Assembly assembly, IEnumerable<string> categories)
111  {
112  if (assembly == null) throw new ArgumentNullException("assembly");
113  if (categories == null) throw new ArgumentNullException("categories");
114 
115  HashSet<string> currentRegisteredCategories = null;
116 
117  lock (Lock)
118  {
119  HashSet<string> registeredCategoriesPerAssembly;
120  if (!MapAssemblyToCategories.TryGetValue(assembly, out registeredCategoriesPerAssembly))
121  {
122  registeredCategoriesPerAssembly = new HashSet<string>();
123  MapAssemblyToCategories.Add(assembly, registeredCategoriesPerAssembly);
124  }
125 
126  foreach (var category in categories)
127  {
128  if (string.IsNullOrWhiteSpace(category))
129  {
130  Log.Error("Invalid empty category for assembly [{0}]", assembly);
131  continue;
132  }
133 
134  if (registeredCategoriesPerAssembly.Add(category))
135  {
136  if (currentRegisteredCategories == null)
137  {
138  currentRegisteredCategories = new HashSet<string>();
139  }
140  currentRegisteredCategories.Add(category);
141  }
142 
143  HashSet<Assembly> registeredAssembliesPerCategory;
144  if (!MapCategoryToAssemblies.TryGetValue(category, out registeredAssembliesPerCategory))
145  {
146  registeredAssembliesPerCategory = new HashSet<Assembly>();
147  MapCategoryToAssemblies.Add(category, registeredAssembliesPerCategory);
148  }
149 
150  registeredAssembliesPerCategory.Add(assembly);
151  }
152  }
153 
154  if (currentRegisteredCategories != null)
155  {
156  OnAssemblyRegistered(assembly, currentRegisteredCategories);
157  }
158  }
159 
160  /// <summary>
161  /// Registers an assembly with the specified categories.
162  /// </summary>
163  /// <param name="assembly">The assembly.</param>
164  /// <param name="categories">The categories to associate with this assembly.</param>
165  /// <exception cref="System.ArgumentNullException">
166  /// assembly
167  /// or
168  /// categories
169  /// </exception>
170  public static void Register(Assembly assembly, params string[] categories)
171  {
172  Register(assembly, (IEnumerable<string>)categories);
173  }
174 
175  private static void OnAssemblyRegistered(Assembly assembly, HashSet<string> categories)
176  {
177  EventHandler<AssemblyRegisteredEventArgs> handler = AssemblyRegistered;
178  if (handler != null) handler(null, new AssemblyRegisteredEventArgs(assembly, categories));
179  }
180  }
181 }
static HashSet< Assembly > Find(params string[] categories)
Finds registered assemblies that are associated with the specified categories.
static void Register(Assembly assembly, params string[] categories)
Registers an assembly with the specified categories.
Keys
Enumeration for keys.
Definition: Keys.cs:8
static HashSet< string > FindCategories(Assembly assembly)
Finds registered categories that are associated with the specified assembly.
static EventHandler< AssemblyRegisteredEventArgs > AssemblyRegistered
Occurs when an assembly is registered.
static HashSet< Assembly > FindAll()
Finds all registered assemblies.
static HashSet< Assembly > Find(IEnumerable< string > categories)
Finds registered assemblies that are associated with the specified categories.
Base implementation for ILogger.
Definition: Logger.cs:10
Provides a basic infrastructure to associate an assembly with some categories and to query and regist...
An event occuring when an assembly is registered with AssemblyRegistry.
static void Register(Assembly assembly, IEnumerable< string > categories)
Registers an assembly with the specified categories.
Output message to log right away.