-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathPluginReloadModule.cs
More file actions
305 lines (254 loc) · 11.2 KB
/
PluginReloadModule.cs
File metadata and controls
305 lines (254 loc) · 11.2 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//
// This file is part of the KSPPluginReload plugin for Kerbal Space Program, Copyright Joop Selen
// License: http://creativecommons.org/licenses/by-nc-sa/3.0/
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Policy;
using System.Text;
using KSP.IO;
using KramaxPluginReload.Classess;
using KramaxPluginReload.UI;
using UnityEngine;
using System.Reflection.Emit;
using System.Threading;
[KSPAddon(KSPAddon.Startup.Instantly, true)]
public class KramaxPluginReloadModule : MonoBehaviour
{
public KramaxPluginReloadModule()
{
KramaxPluginReload.Classe.Immortal.AddImmortal<KramaxPluginReload.PluginReloadModule>();
}
}
namespace KramaxPluginReload
{
static public class Deb
{
public static void Log(String format, params System.Object[] args)
{
Debug.Log(String.Format(format, args));
}
public static void Log(String message)
{
Debug.Log(message);
}
}
/*
public class Reloadable : Attribute
{
public string Comment { get; set; }
}
*/
public class PluginReloadModule : MonoBehaviour
{
public bool GUIActive;
static public int versionCount = 0;
public static List<PluginClass> PluginClasses = new List<PluginClass>();
public static List<PluginSetting> PluginSettings = new List<PluginSetting>();
public static PluginReloadWindow PluginReloadWindow = new PluginReloadWindow()
{
ReloadCallback = LoadPlugins
};
public PluginReloadModule()
{
Deb.Log("KramaxPluginReload loaded, Version: {0}.",
Assembly.GetExecutingAssembly().GetName().Version);
LoadConfig();
LoadPlugins();
PluginReloadWindow.OpenWindow();
}
private static void LoadConfig()
{
try
{
ConfigNode settings = ConfigNode.Load(KSPUtil.ApplicationRootPath + "GameData/KramaxPluginReload/Settings.cfg");
foreach (ConfigNode node in settings.GetNodes("PluginSetting"))
{
PluginSetting pluginSetting = new PluginSetting()
{
Name = node.GetValue("name"),
Path = node.GetValue("path"),
LoadOnce = bool.Parse(node.GetValue("loadOnce")),
MethodsAllowedToFail = bool.Parse(node.GetValue("methodsAllowedToFail"))
};
PluginSettings.Add(pluginSetting);
}
}
catch (Exception ex)
{
Deb.Log("KramaxPluginReload: Failed to load settings.cfg. Error:\n{0}", ex);
return;
}
}
private static Assembly LoadAssembly(string location)
{
try
{
byte[] assemblyBytes = System.IO.File.ReadAllBytes(location);
Assembly a = Assembly.Load(assemblyBytes);
Deb.Log("KramaxPluginReload: Reloaded assembly: {0} version: {1}.", a.GetName().Name, a.GetName().Version);
return a;
}
catch (Exception ex)
{
Deb.Log("KramaxPluginReload: Failed to load plugin from file {0}. Error:\n\n{1}", location, ex);
}
return null;
}
static Type CreateUniqueSubClass(ModuleBuilder moduleBldr, Type originalType, int versionUid)
{
String newClassName = String.Format("{0}_{1}_", originalType.Name, versionUid);
Deb.Log("CreateUniqueSubClass: new class name is {0}", newClassName);
TypeBuilder typeBldr =
moduleBldr.DefineType(newClassName, TypeAttributes.Public | TypeAttributes.Class, originalType);
ConstructorBuilder ctor = typeBldr.DefineDefaultConstructor(MethodAttributes.Public);
return typeBldr.CreateType();
}
private static void LoadPlugins()
{
versionCount = versionCount + 1;
Deb.Log("KramaxPluginReload: (Re)loading plugins with version {0}.", versionCount);
Type type = typeof(KramaxReloadExtensions.ReloadableMonoBehaviour);
foreach (PluginSetting setting in PluginSettings)
{
//Skip reloading of loadonce assemblies
if (setting.LoadOnce == true) continue;
//Call ondestroy on alive classes
List<PluginClass> toRemove = new List<PluginClass>();
foreach (PluginClass pluginClass in PluginClasses.Where(pc => pc.pluginSetting == setting))
{
if (pluginClass.alive)
{
pluginClass.DeleteInstance();
}
toRemove.Add(pluginClass);
}
//Remove old class references
foreach (PluginClass r in toRemove)
{
PluginClasses.Remove(r);
}
var assembly = LoadAssembly(setting.Path);
//Remove assembly if reloading failed
if (assembly == null)
{
foreach (PluginClass pluginClass in PluginClasses.Where(pc => pc.pluginSetting == setting).ToList())
{
PluginClasses.Remove(pluginClass);
}
continue;
}
String tmpAssemblyName = String.Format("KramaxPIRLAsmb_{0}", versionCount);
String tmpModuleName = String.Format("KramaxPIRLMod_{0}", versionCount);
AssemblyBuilder assemblyBldr =
Thread.GetDomain().DefineDynamicAssembly(new AssemblyName(tmpAssemblyName),
AssemblyBuilderAccess.Run);
ModuleBuilder moduleBldr = assemblyBldr.DefineDynamicModule(tmpModuleName);
IList<Type> derivedClassess =
(from t in assembly.GetTypes()
where t.IsSubclassOf(typeof(MonoBehaviour))
select t).ToList();
List<PluginClass> plugins = new List<PluginClass>();
Dictionary<Type, Type> typeMapping = new Dictionary<Type, Type>();
foreach (var derivedClass in derivedClassess)
{
Deb.Log("KramaxPluginReload.LoadPlugins: got type {0}.", derivedClass.Name);
Deb.Log("KramaxPluginReload.LoadPlugins: from assembly {0}, v{1}.",
derivedClass.Assembly.GetName().Name,
derivedClass.Assembly.GetName().Version);
System.Attribute[] attrs = System.Attribute.GetCustomAttributes(derivedClass);
KSPAddon kspAddon = null;
foreach (var att in attrs)
{
if (att is KSPAddon)
{
kspAddon = att as KSPAddon;
}
}
if (!derivedClass.IsSubclassOf(typeof(KramaxReloadExtensions.ReloadableMonoBehaviour)))
{
Deb.Log("KramaxPluginReload.LoadPlugins: ERROR type {0} is not ReloadableMonoBehaviour subclass.",
derivedClass.Name);
continue;
}
if (kspAddon != null)
{
Deb.Log("KramaxPluginReload.LoadPlugins: type {0} will be top-level component.", derivedClass.Name);
PluginClass pluginClass = new PluginClass();
pluginClass.pluginSetting = setting;
pluginClass.originalType = derivedClass;
pluginClass.kspAddon = kspAddon;
pluginClass.type = CreateUniqueSubClass(moduleBldr, derivedClass, versionCount);
plugins.Add(pluginClass);
}
else
{
Deb.Log("KramaxPluginReload.LoadPlugins: type {0} will be sub-level component.", derivedClass.Name);
var newType = CreateUniqueSubClass(moduleBldr, derivedClass, versionCount);
typeMapping[derivedClass] = newType;
}
}
foreach (var plugin in plugins)
{
plugin.typeMapping = typeMapping;
PluginClasses.Add(plugin);
}
}
foreach (var pluginClass in PluginClasses)
{
if (pluginClass.kspAddon.once == false || pluginClass.fired == false)
{
bool awake = false;
switch (pluginClass.kspAddon.startup)
{
case KSPAddon.Startup.Instantly:
case KSPAddon.Startup.EveryScene:
//TODO: Check wether PSystem should even respawn.
case KSPAddon.Startup.PSystemSpawn:
awake = true;
break;
case KSPAddon.Startup.Credits:
awake = (HighLogic.LoadedScene == GameScenes.CREDITS);
break;
case KSPAddon.Startup.EditorAny:
awake = (HighLogic.LoadedScene == GameScenes.EDITOR);
break;
case KSPAddon.Startup.Flight:
awake = (HighLogic.LoadedScene == GameScenes.FLIGHT);
break;
case KSPAddon.Startup.MainMenu:
awake = (HighLogic.LoadedScene == GameScenes.MAINMENU);
break;
case KSPAddon.Startup.Settings:
awake = (HighLogic.LoadedScene == GameScenes.SETTINGS);
break;
case KSPAddon.Startup.SpaceCentre:
awake = (HighLogic.LoadedScene == GameScenes.SPACECENTER);
break;
case KSPAddon.Startup.TrackingStation:
awake = (HighLogic.LoadedScene == GameScenes.TRACKSTATION);
break;
}
if (awake)
{
Deb.Log("KramaxPluginReload.LoadPlugins: plugin should be awake: {0}.", pluginClass.Name);
pluginClass.CreateInstance();
}
}
}
Deb.Log("KramaxPluginReload.LoadPlugins: Plugins (re)loaded.");
}
public void Update()
{
if ((Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt)) && Input.GetKeyDown(KeyCode.P))
{
if (PluginReloadWindow.Visible == false)
PluginReloadWindow.OpenWindow();
else
PluginReloadWindow.CloseWindow();
}
}
}
}