forked from mkaring/ConfuserEx
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPluginDiscovery.cs
More file actions
149 lines (138 loc) · 5.29 KB
/
Copy pathPluginDiscovery.cs
File metadata and controls
149 lines (138 loc) · 5.29 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Microsoft.Extensions.Logging;
namespace Confuser.Core {
/// <summary>
/// Discovers available protection plugins.
/// </summary>
public class PluginDiscovery {
/// <summary>
/// The default plugin discovery service.
/// </summary>
internal static readonly PluginDiscovery Instance = new PluginDiscovery();
/// <summary>
/// Initializes a new instance of the <see cref="PluginDiscovery" /> class.
/// </summary>
protected PluginDiscovery() { }
/// <summary>
/// Retrieves the available protection plugins.
/// </summary>
/// <param name="context">The working context.</param>
/// <param name="protections">A list of resolved protections.</param>
/// <param name="packers">A list of resolved packers.</param>
/// <param name="components">A list of resolved components.</param>
public void GetPlugins(ConfuserContext context, out IList<Protection> protections, out IList<Packer> packers, out IList<ConfuserComponent> components) {
protections = new List<Protection>();
packers = new List<Packer>();
components = new List<ConfuserComponent>();
GetPluginsInternal(context, protections, packers, components);
}
/// <summary>
/// Determines whether the specified type has an accessible default constructor.
/// </summary>
/// <param name="type">The type.</param>
/// <returns><c>true</c> if the specified type has an accessible default constructor; otherwise, <c>false</c>.</returns>
public static bool HasAccessibleDefConstructor(Type type) {
ConstructorInfo ctor = type.GetConstructor(Type.EmptyTypes);
if (ctor == null) return false;
return ctor.IsPublic;
}
/// <summary>
/// Adds plugins in the assembly to the protection list.
/// </summary>
/// <param name="context">The working context.</param>
/// <param name="protections">The working list of protections.</param>
/// <param name="packers">The working list of packers.</param>
/// <param name="components">The working list of components.</param>
/// <param name="asm">The assembly.</param>
protected static void AddPlugins(
ConfuserContext context, IList<Protection> protections, IList<Packer> packers,
IList<ConfuserComponent> components, Assembly asm) {
foreach (var module in asm.GetLoadedModules()) {
Type[] moduleTypes;
try {
moduleTypes = module.GetTypes();
}
catch (ReflectionTypeLoadException ex) {
// A plugin assembly may reference dependencies that are not present; keep the
// types that did load instead of crashing plugin discovery.
moduleTypes = Array.FindAll(ex.Types, t => t != null);
}
foreach (var i in moduleTypes) {
if (i.IsAbstract || !HasAccessibleDefConstructor(i))
continue;
if (typeof(Protection).IsAssignableFrom(i)) {
try {
protections.Add((Protection)Activator.CreateInstance(i));
}
catch (Exception ex) {
context.Logger.LogError(ex, "Failed to instantiate protection '" + i.Name + "'.");
}
}
else if (typeof(Packer).IsAssignableFrom(i)) {
try {
packers.Add((Packer)Activator.CreateInstance(i));
}
catch (Exception ex) {
context.Logger.LogError(ex, "Failed to instantiate packer '" + i.Name + "'.");
}
}
else if (typeof(ConfuserComponent).IsAssignableFrom(i)) {
try {
components.Add((ConfuserComponent)Activator.CreateInstance(i));
}
catch (Exception ex) {
context.Logger.LogError(ex, "Failed to instantiate component '" + i.Name + "'.");
}
}
}
}
context.CheckCancellation();
}
/// <summary>
/// Retrieves the available protection plugins.
/// </summary>
/// <param name="context">The working context.</param>
/// <param name="protections">The working list of protections.</param>
/// <param name="packers">The working list of packers.</param>
/// <param name="components">The working list of components.</param>
protected virtual void GetPluginsInternal(
ConfuserContext context, IList<Protection> protections,
IList<Packer> packers, IList<ConfuserComponent> components) {
protections.Add(new WatermarkingProtection());
try {
Assembly protAsm = Assembly.Load("Confuser.Protections");
AddPlugins(context, protections, packers, components, protAsm);
}
catch (Exception ex) {
context.Logger.LogWarning(ex, "Failed to load built-in protections.");
}
try {
Assembly renameAsm = Assembly.Load("Confuser.Renamer");
AddPlugins(context, protections, packers, components, renameAsm);
}
catch (Exception ex) {
context.Logger.LogWarning(ex, "Failed to load renamer.");
}
try {
Assembly renameAsm = Assembly.Load("Confuser.DynCipher");
AddPlugins(context, protections, packers, components, renameAsm);
}
catch (Exception ex) {
context.Logger.LogWarning(ex, "Failed to load dynamic cipher library.");
}
foreach (string pluginPath in context.Project.PluginPaths) {
string realPath = Path.Combine(context.BaseDirectory, pluginPath);
try {
Assembly plugin = Assembly.LoadFile(realPath);
AddPlugins(context, protections, packers, components, plugin);
}
catch (Exception ex) {
context.Logger.LogWarning(ex, "Failed to load plugin '" + pluginPath + "'.");
}
}
}
}
}