-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPlugin.cs
More file actions
101 lines (91 loc) · 3.57 KB
/
Plugin.cs
File metadata and controls
101 lines (91 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using CommandLine;
using InEngine.Core.Exceptions;
namespace InEngine.Core
{
public class Plugin
{
public Assembly Assembly { get; set; }
public string Name { get { return Assembly.GetName().Name; } }
public string Version { get { return Assembly.GetName().Version.ToString(); } }
public Plugin(Assembly assembly)
{
Assembly = assembly;
}
public static Plugin LoadFrom(string assemblyPath)
{
var path = Path.Combine(InEngineSettings.BasePath, assemblyPath);
try
{
return new Plugin(Assembly.LoadFrom(path));
}
catch (Exception exception)
{
throw new PluginNotFoundException($"Plugin not found at {path}", exception);
}
}
public List<T> Make<T>() where T : class, IPluginType
{
return Assembly
.GetTypes()
.Where(x => x.IsClass && typeof(T).IsAssignableFrom(x))
.Select(x => Assembly.CreateInstance(x.FullName) as T)
.ToList();
}
public static List<Plugin> Load<T>() where T : IPluginType
{
var pluginList = new List<Plugin>();
try
{
pluginList.Add(new Plugin(Assembly.GetExecutingAssembly()));
}
catch (Exception exception)
{
throw new PluginNotFoundException("Could not load InEngine.Core plugin.", exception);
}
var assemblies = InEngineSettings
.Make()
.Plugins
.Select(x => Assembly.LoadFrom($"{x}.dll"));
foreach (var assembly in assemblies)
{
try
{
if (assembly.GetTypes().Any(y => y.IsClass && typeof(T).IsAssignableFrom(y)))
pluginList.Add(new Plugin(assembly));
}
catch (Exception exception)
{
throw new PluginNotFoundException($"Could not load {assembly.GetName().Name} plugin.", exception);
}
}
if (!pluginList.Any())
throw new PluginNotFoundException("There are no plugins available.");
return pluginList.OrderBy(x => x.Name).ToList();
}
public ICommand CreateCommandFromClass(string fullCommandName)
{
return Assembly.CreateInstance(fullCommandName) as ICommand;
}
public ICommand CreateCommandFromVerb(string verbName)
{
var commandClassNames = new List<string>();
var optionsList = Make<IOptions>();
foreach (var options in optionsList)
foreach (var property in options.GetType().GetProperties())
foreach (var attribute in property.GetCustomAttributes(true))
if (attribute is VerbOptionAttribute && (attribute as VerbOptionAttribute).LongName == verbName)
commandClassNames.Add(property.PropertyType.FullName);
var commandCount = commandClassNames.Count();
if (commandCount > 1)
throw new AmbiguousCommandException(verbName);
if (commandCount == 0)
throw new CommandNotFoundException(verbName);
return Assembly.CreateInstance(commandClassNames.First()) as ICommand;
}
}
}