-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPluginReloader.cs
171 lines (146 loc) · 4.74 KB
/
PluginReloader.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Sandbox.ModAPI;
using VRage.FileSystem;
using VRage.Game;
using VRage.Input;
using VRage.Plugins;
using VRage.Utils;
namespace Digi.PluginReloader
{
public class PluginReloaderPlugin : IPlugin, IHandleInputPlugin
{
const string PluginsPath = "DynamicPlugins"; // relative to appdata folder
object GameInstance;
readonly List<IPlugin> Plugins = new List<IPlugin>();
public void Init(object gameInstance)
{
GameInstance = gameInstance;
MyLog.Default.WriteLine("PluginReloader initialized, press Ctrl+Alt+L ingame to reload plugins");
MyLog.Default.Flush();
LoadPlugins();
}
public void Dispose()
{
UnloadPlugins();
GameInstance = null;
}
public void Update()
{
try
{
if(MyInput.Static == null)
return;
if(MyInput.Static.IsNewKeyPressed(MyKeys.L) && MyInput.Static.IsAnyAltKeyPressed() && MyInput.Static.IsAnyCtrlKeyPressed())
{
UnloadPlugins();
LoadPlugins();
// I don't really know what I'm doing here xD
GC.Collect();
GC.WaitForFullGCComplete();
GC.Collect();
}
foreach(IPlugin plugin in Plugins)
{
try
{
plugin.Update();
}
catch(Exception e)
{
LogError(e);
}
}
}
catch(Exception e)
{
LogError(e);
}
}
public void HandleInput()
{
try
{
foreach(IPlugin plugin in Plugins)
{
try
{
IHandleInputPlugin hip = plugin as IHandleInputPlugin;
hip?.HandleInput();
}
catch(Exception e)
{
LogError(e);
}
}
}
catch(Exception e)
{
LogError(e);
}
}
void LoadPlugins()
{
string dir = Path.Combine(MyFileSystem.UserDataPath, PluginsPath);
if(!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
return;
}
string[] fileNames = Directory.GetFiles(dir, "*.dll", SearchOption.TopDirectoryOnly);
foreach(string filePath in fileNames)
{
byte[] bytes = File.ReadAllBytes(filePath);
Assembly assembly = Assembly.Load(bytes);
CreateInstances(assembly);
}
}
void CreateInstances(Assembly assembly)
{
foreach(Type type in assembly.GetTypes())
{
try
{
if(type.IsAbstract)
continue;
if(!type.GetInterfaces().Contains(typeof(IPlugin)))
continue;
if(type.Name == nameof(PluginReloaderPlugin))
continue;
IPlugin plugin = (IPlugin)assembly.CreateInstance(type.FullName);
plugin.Init(GameInstance);
Plugins.Add(plugin);
string msg = $"PluginReloader loaded plugin: {plugin}";
MyAPIGateway.Utilities?.ShowNotification(msg, 2000, MyFontEnum.Green);
MyLog.Default.WriteLine(msg);
}
catch(Exception ex)
{
LogError($"Cannot create instance of '{type.FullName}': {ex.ToString()}");
}
}
}
void UnloadPlugins()
{
foreach(IPlugin plugin in Plugins)
{
plugin.Dispose();
}
Plugins.Clear();
}
void LogError(string text)
{
string message = $"PluginReloader error: {text}";
MyLog.Default.WriteLine(message);
MyAPIGateway.Utilities?.ShowNotification(message, 10000, MyFontEnum.Red);
}
void LogError(Exception e)
{
MyLog.Default.WriteLine($"PluginReloader error: {e.ToString()}");
MyAPIGateway.Utilities?.ShowNotification($"PluginReloader error: {e.Message}", 10000, MyFontEnum.Red);
}
}
}