Repository navigation
Expand file tree
/
Copy pathExtendedPlugins.cs
More file actions
218 lines (190 loc) · 7.55 KB
/
Copy pathExtendedPlugins.cs
File metadata and controls
218 lines (190 loc) · 7.55 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Threading.Tasks;
using VRage.Plugins;
namespace PluginManager.Core
{
class ExtendedPlugins
{
private const string RepositoryURL = "https://api.github.com/repos/{0}/{1}/releases/latest";
private static List<ExtendedPlugin> m_plugins = new List<ExtendedPlugin>();
public static bool LibraryHasInterface<T>(string filename)
{
bool result = false;
AppDomain domain = AppDomain.CreateDomain("VirtualExtendedPlugins");
try
{
Assembly assembly;
assembly = domain.Load(new AssemblyName(filename));
result = assembly.GetTypes().Where(s => s.GetInterfaces().Contains(typeof(T))).Count() > 0;
}
catch { }
finally
{
AppDomain.Unload(domain);
}
return result;
}
public static void Register()
{
ExtendedPlugin plugin;
bool success;
foreach (var library in Settings.Instance.WatchList)
{
plugin = new ExtendedPlugin(library.Name, out success);
if (success)
{
if (string.IsNullOrEmpty(library.Version)) library.Version = plugin.FileVersion;
if (library.LastUpdate == null) library.LastUpdate = DateTime.MinValue;
m_plugins.Add(plugin);
}
}
}
private static async Task<Serialization.GitHubAPI.Release> GetGitHubReleasAsync(ExtendedPlugin plugin)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Format(RepositoryURL, plugin.Info.UserName, plugin.Info.RepositoryName));
request.KeepAlive = false;
request.UserAgent = ".NET Framework";
request.ContentType = "application/json";
var response = (HttpWebResponse)(await request.GetResponseAsync());
if (response.StatusCode == HttpStatusCode.OK)
using (Stream webStream = response.GetResponseStream())
return Serialization.GitHubAPI.Release.Deserialize(webStream);
}
catch { }
return null;
}
public static void CheckPluginsUpdates()
{
Utilities.Log.WriteLineAndConsole("Check plugins updates...");
List<Task<Serialization.GitHubAPI.Release>> tasks = new List<Task<Serialization.GitHubAPI.Release>>();
foreach (var plugin in m_plugins)
tasks.Add(GetGitHubReleasAsync(plugin));
Task.WhenAll(tasks).Wait();
foreach(var task in tasks)
{
///----------------------task.Result
}
}
public static void Init(object gameInstance)
{
foreach (var plugin in m_plugins)
plugin.Instance.Init(gameInstance);
}
public static void Update()
{
foreach (var plugin in m_plugins)
plugin.Instance.Update();
}
public static void Unload()
{
foreach (var plugin in m_plugins)
plugin.Dispose();
m_plugins.Clear();
}
private class ExtendedPlugin : IDisposable
{
public string Name { get; private set; }
public string FileVersion { get; private set; }
public PackageInfo Info { get; private set; }
public string DomainName { get; private set; }
public AppDomain Domain { get; private set; }
public Assembly Assembly { get; private set; }
/// <summary>
/// First Instance in Assembly
/// </summary>
public IPlugin Instance { get; private set; }
private ExtendedPlugin() { }
public ExtendedPlugin(string name, out bool success)
{
try
{
Name = name;
DomainName = Guid.NewGuid().ToString();
Domain = AppDomain.CreateDomain(DomainName);
Assembly = Assembly.Load(new AssemblyName(Name));
var pluginInterface = Assembly.GetTypes().Where(s => s.GetInterfaces().Contains(typeof(IPlugin))).FirstOrDefault();
if (pluginInterface != null)
{
FileVersion = Assembly.FullName;
Utilities.Log.WriteLineAndConsole($"Creating instance of: {name} / {pluginInterface.FullName}");
Instance = (IPlugin)Activator.CreateInstance(pluginInterface);
Info = PackageInfo.Create(this);
success = true;
}
else
{
Utilities.Log.WriteLineAndConsole($"Error registration plugin. File: \"{name}\" does not contain an interface \"IExtendedPlugin\"");
AppDomain.Unload(Domain);
success = false;
}
}
catch (Exception e)
{
Utilities.Log.WriteLineAndConsole($"Cannot load Plugin: {name}, Error: {e.ToString()}");
AppDomain.Unload(Domain);
success = false;
}
}
public void Dispose()
{
if (Instance != null)
Instance.Dispose();
Name = null; DomainName = null; FileVersion = null;
Info = null; Instance = null; Assembly = null;
if (Domain != null)
AppDomain.Unload(Domain);
GC.SuppressFinalize(this);
}
}
private class PackageInfo : System.Attribute
{
public string UserName;
public string RepositoryName;
public string ReleaseFileName;
public string PackageFileName;
/// <summary>
///[PluginManagerInfoAttribute]
///public class Plugin : IPlugin { }
/// </summary>
/// <param name="plugin"></param>
/// <returns></returns>
public static PackageInfo Create(ExtendedPlugin plugin)
{
var instanceType = plugin.Instance.GetType();
var attributeType = plugin.Assembly.GetType(instanceType.Namespace + ".PackageInfoAttribute");
if (attributeType != null)
{
PackageInfo attribute;
try { attribute = PackageInfo.Create(instanceType.GetCustomAttribute(attributeType)); }
catch { attribute = null; }
if (attribute != null)
return attribute;
}
return null;
}
public static PackageInfo Create(dynamic obj)
{
try
{
return new PackageInfo()
{
UserName = obj.UserName,
RepositoryName = obj.RepositoryName,
ReleaseFileName = obj.FileName,
PackageFileName = obj.PackageFileName
};
}
catch { }
return null;
}
}
private ExtendedPlugins() { }
}
}