Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
PluginResolver.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.IO;
6 using System.Linq;
7 using System.Reflection;
8 using System.Text;
9 using System.Threading.Tasks;
10 using SiliconStudio.Core.Diagnostics;
11 
12 namespace SiliconStudio.BuildEngine
13 {
14  public class PluginResolver
15  {
16  public IEnumerable<string> PluginAssemblyLocations { get { return pluginAssemblyLocations; } }
17 
18  private readonly List<string> pluginAssemblyLocations = new List<string>();
19 
20  private readonly Logger logger;
21 
22  public PluginResolver(Logger logger = null)
23  {
24  this.logger = logger;
25  }
26 
27  public void Register()
28  {
29  AppDomain.CurrentDomain.AssemblyResolve += (sender, e) => LoadAssembly(new AssemblyName(e.Name));
30  }
31 
32  public Assembly LoadAssembly(AssemblyName assemblyName)
33  {
34  return LoadAssembly(assemblyName.Name);
35  }
36 
37  public Assembly LoadAssembly(string assemblyName)
38  {
39  foreach (string pluginLocation in pluginAssemblyLocations)
40  {
41  if (pluginLocation != assemblyName)
42  {
43  string fileName = Path.GetFileNameWithoutExtension(pluginLocation);
44  if (fileName != assemblyName)
45  continue;
46  }
47 
48  if (logger != null)
49  logger.Debug("Loading plugin: {0}", pluginLocation);
50  return Assembly.LoadFrom(pluginLocation);
51  }
52  return null;
53  }
54 
55  public string FindAssembly(string assemblyFileName)
56  {
57  foreach (string pluginLocation in pluginAssemblyLocations)
58  {
59  if (pluginLocation != assemblyFileName)
60  {
61  string fileName = Path.GetFileName(pluginLocation);
62  if (fileName != assemblyFileName)
63  continue;
64  }
65 
66  if (logger != null)
67  logger.Debug("Loading plugin: {0}", pluginLocation);
68  return pluginLocation;
69  }
70  return null;
71  }
72 
73  public void AddPlugin(string filePath)
74  {
75  pluginAssemblyLocations.Add(filePath);
76  }
77 
78  public void AddPluginFolder(string folder)
79  {
80  if (!Directory.Exists(folder))
81  return;
82 
83  foreach (string filePath in Directory.EnumerateFiles(folder, "*.dll"))
84  {
85  if (logger != null)
86  logger.Debug("Detected plugin: {0}", Path.GetFileNameWithoutExtension(filePath));
87  pluginAssemblyLocations.Add(filePath);
88  }
89  }
90  }
91 }
Assembly LoadAssembly(AssemblyName assemblyName)
Base implementation for ILogger.
Definition: Logger.cs:10
string FindAssembly(string assemblyFileName)
Assembly LoadAssembly(string assemblyName)