-
Notifications
You must be signed in to change notification settings - Fork 0
/
PluginSystem.cs
183 lines (167 loc) · 7.39 KB
/
PluginSystem.cs
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//Copyright (c) 2015 Christopher Andrews, Alexandre Oliveira, Joshua Blake, William Donaldson.
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
using System;
using System.IO;
using System.Reflection;
using System.Collections.Generic;
using UnityEngine;
namespace UnityPluginSystem
{
public class PluginSystem : MonoBehaviour
{
public bool logging = true;
readonly List<IGamePlugin> loadedPlugins = new List<IGamePlugin>();
Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
//This will find and return the assembly requested if it is already loaded
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
if (assembly.FullName == args.Name)
{
if (logging) Debug.Log("UnityPluginSystem: Resolved plugin assembly reference: " + args.Name);
return assembly;
}
}
if (logging) Debug.Log("UnityPluginSystem: Could not resolve assembly " + args.Name);
return null;
}
public void LoadPlugins()
{
if (logging) Debug.Log("UnityPluginSystem: Loading plugins...");
if (!Directory.Exists (Path.Combine(Environment.CurrentDirectory, "Plugins"))) {
Directory.CreateDirectory (Path.Combine(Environment.CurrentDirectory, "Plugins"));
}
//Load the assemblies from files
List<Assembly> assemblies = new List<Assembly> ();
string[] fileList = Directory.GetFiles (Path.Combine(Environment.CurrentDirectory, "Plugins"), "*", SearchOption.AllDirectories);
foreach (string pluginPath in fileList) {
if (Path.GetExtension (pluginPath).ToLower () == ".dll") {
try {
Assembly currentAssembly = Assembly.LoadFile (pluginPath);
assemblies.Add (currentAssembly);
} catch (Exception e) {
//Something went horribly wrong - do an LogExcep for them
if (logging) Debug.Log("UnityPluginSystem: Exception thrown loading assembly " + pluginPath);
if (logging) Debug.LogException(e);
}
}
}
Type gameInterfaceType = typeof(IGamePlugin);
foreach (Assembly loadedAssembly in assemblies)
{
Type[] loadedTypes = loadedAssembly.GetExportedTypes();
foreach (Type loadedType in loadedTypes)
{
Type[] typeInterfaces = loadedType.GetInterfaces();
bool containsPluginInterface = false;
foreach (Type typeInterface in typeInterfaces)
{
if (typeInterface == gameInterfaceType)
{
containsPluginInterface = true;
}
}
if (containsPluginInterface)
{
if (logging) Debug.Log("UnityPluginSystem: Loading plugin: " + loadedType.FullName);
try
{
IGamePlugin pluginInstance = ActivatePluginType(loadedType);
if (pluginInstance != null)
{
if (logging) Debug.Log("UnityPluginSystem: Plugin loaded");
loadedPlugins.Add(pluginInstance);
}
}
catch (Exception excep)
{
if (logging) Debug.Log ("UnityPluginSystem: Exception thrown loading plugin " +
loadedType.FullName + "(" + loadedType.Assembly.FullName + ")");
if (logging) Debug.LogException(excep);
}
}
else
{
if (logging) Debug.Log("UnityPluginSystem: Nothing to load in plugin: " + loadedType.FullName);
}
}
}
if (logging) Debug.Log("UnityPluginSystem: Done!");
}
private IGamePlugin ActivatePluginType(Type loadedType)
{
try
{
//"as IGamePlugin" will cast or return null if the type is not a IGamePlugin
IGamePlugin pluginInstance = Activator.CreateInstance(loadedType) as IGamePlugin;
return pluginInstance;
}
catch (Exception e)
{
if (logging) Debug.Log ("UnityPluginSystem: Exception thrown while activating plugin " + loadedType.Name);
if (logging) Debug.LogException(e);
return null;
}
}
//Fire Update
public void Update()
{
foreach (var plugin in loadedPlugins)
{
try
{
plugin.Update();
}
catch (Exception e)
{
Type type = plugin.GetType();
if (logging) Debug.Log ("UnityPluginSystem: Exception thrown in Update event for " +
type.FullName + " (" + type.Assembly.FullName + ")");
if (logging) Debug.LogException(e);
}
}
}
//Fire Initialize
public void Awake()
{
if (GameObject.FindObjectsOfType<PluginSystem>().Length > 1)
{
GameObject.Destroy(this.gameObject);
}
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
DontDestroyOnLoad(this);
LoadPlugins();
foreach (var plugin in loadedPlugins)
{
try
{
plugin.Initialize();
}
catch (Exception e)
{
Type type = plugin.GetType();
if (logging) Debug.Log ( "UnityPluginSystem: Exception thrown in Initialize event for " +
type.FullName + " (" + type.Assembly.FullName + ")");
if (logging) Debug.LogException(e);
}
}
}
}
}