forked from NoteFly/NoteFly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PluginsManager.cs
486 lines (441 loc) · 17.9 KB
/
PluginsManager.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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//-----------------------------------------------------------------------
// <copyright file="PluginsManager.cs" company="NoteFly">
// NoteFly a note application.
// Copyright (C) 2012-2013 Tom
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// </copyright>
//-----------------------------------------------------------------------
namespace NoteFly
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
/// <summary>
/// PluginsManager class, provides plugins functions
/// </summary>
public static class PluginsManager
{
/// <summary>
/// A list of installed plugins dll filenames
/// </summary>
private static List<string> installedplugins = new List<string>();
/// <summary>
/// All the enabled plugins
/// </summary>
private static List<IPlugin.IPlugin> enabledplugins = new List<IPlugin.IPlugin>();
/// <summary>
/// An array of dll files that are not allow to be loaded as notefly plugin in the notefly plugin folder.
/// </summary>
private static string[] excludedplugindlls;
/// <summary>
/// Gets the installed plugins as array.
/// </summary>
public static string[] InstalledPlugins
{
get
{
return installedplugins.ToArray();
}
}
/// <summary>
/// Gets the list with all enabled plugins
/// </summary>
public static List<IPlugin.IPlugin> EnabledPlugins
{
get
{
return enabledplugins;
}
}
/// <summary>
/// Load all enabled plugins
/// </summary>
public static void LoadPlugins()
{
installedplugins.Clear();
enabledplugins.Clear();
if (Directory.Exists(Settings.ProgramPluginsFolder))
{
string[] enabledpluginsfilenames = Settings.ProgramPluginsEnabled.Split('|');
string[] dllfiles = GetDllFilesFolder(Settings.ProgramPluginsFolder);
excludedplugindlls = Settings.ProgramPluginsDllexclude.Split('|');
for (int i = 0; i < dllfiles.Length; ++i)
{
if (IsPluginFileExcluded(dllfiles[i]))
{
continue;
}
installedplugins.Add(dllfiles[i]);
foreach (string enabledpluginfilename in enabledpluginsfilenames)
{
if (enabledpluginfilename.Equals(dllfiles[i], StringComparison.Ordinal))
{
EnablePlugin(dllfiles[i]);
}
}
}
}
else
{
Log.Write(LogType.info, "Plugin folder does not exist, recreate it.");
Directory.CreateDirectory(Settings.ProgramPluginsFolder);
}
}
/// <summary>
/// Load a plugin.
/// </summary>
/// <param name="dllfilename">The current list with loaded plugins</param>
public static void EnablePlugin(string dllfilename)
{
try
{
System.Reflection.Assembly pluginassembly = null;
pluginassembly = System.Reflection.Assembly.LoadFrom(Path.Combine(Settings.ProgramPluginsFolder, dllfilename));
if (pluginassembly != null)
{
foreach (Type curplugintype in pluginassembly.GetTypes())
{
if (curplugintype.IsPublic && !curplugintype.IsAbstract && !curplugintype.IsSealed)
{
if (!curplugintype.FullName.Equals(curplugintype.Namespace + "." + curplugintype.Namespace))
{
continue;
}
Type plugintype = pluginassembly.GetType(curplugintype.ToString(), false, true);
if (plugintype == null)
{
continue;
}
IPlugin.IPlugin plugin = (IPlugin.IPlugin)Activator.CreateInstance(pluginassembly.GetType(curplugintype.ToString()));
plugin.Register(dllfilename, NoteFly.Program.Notes);
enabledplugins.Add(plugin);
}
}
}
}
catch (Exception ex)
{
Log.Write(LogType.exception, "Can't load file: " + dllfilename + " " + ex.Message);
}
}
/// <summary>
/// Disable a plugin
/// </summary>
/// <param name="dllfilename">The plugin .dll file filename.</param>
/// <returns>Return true if dll filename was found and plugin is disabled succesfully.</returns>
public static bool DisablePlugin(string dllfilename)
{
for (int i = 0; i < enabledplugins.Count; i++)
{
if (enabledplugins[i].Filename.Equals(dllfilename, StringComparison.Ordinal))
{
enabledplugins[i].Unregister();
enabledplugins.RemoveAt(i);
return true;
}
}
return false;
}
/// <summary>
/// Get the names of all plugins installed.
/// </summary>
/// <returns>An array with installed plugin names.</returns>
public static string[] GetInstalledPlugins()
{
List<string> pluginsnames = new List<string>();
for (int i = 0; i < installedplugins.Count; i++)
{
Assembly pluginassembly = System.Reflection.Assembly.LoadFrom(Path.Combine(Settings.ProgramPluginsFolder, installedplugins[i]));
string pluginname = GetPluginName(pluginassembly);
if (!string.IsNullOrEmpty(pluginname))
{
pluginsnames.Add(pluginname);
}
}
return pluginsnames.ToArray();
}
#region Getting plugin details
/// <summary>
/// Get the name of the plugin.
/// </summary>
/// <param name="pluginassembly">The plugin assembly</param>
/// <returns>The name of the plugin</returns>
public static string GetPluginName(Assembly pluginassembly)
{
string pluginname = Strings.T("unknown");
object[] atttitle = pluginassembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
if (atttitle.Length > 0)
{
AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)atttitle[0];
if (titleAttribute.Title != string.Empty)
{
if (titleAttribute.Title.Length > 200)
{
pluginname = titleAttribute.Title.Substring(0, 200);
}
else
{
pluginname = titleAttribute.Title;
}
}
else
{
Log.Write(LogType.exception, "Plugin " + pluginassembly.Location + " has no name.");
}
}
return pluginname;
}
/// <summary>
/// Get author or author company of the plugin.
/// </summary>
/// <param name="pluginassembly">The plugin assembly</param>
/// <returns>The author or company of the plugin</returns>
public static string GetPluginAuthor(Assembly pluginassembly)
{
string pluginauthor = Strings.T("unknown");
object[] attributes = pluginassembly.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
if (attributes.Length != 0)
{
if (!string.IsNullOrEmpty(((AssemblyCompanyAttribute)attributes[0]).Company))
{
if (((AssemblyCompanyAttribute)attributes[0]).Company.Length > 200)
{
pluginauthor = ((AssemblyCompanyAttribute)attributes[0]).Company.Substring(0, 200);
}
else
{
pluginauthor = ((AssemblyCompanyAttribute)attributes[0]).Company;
}
}
else
{
Log.Write(LogType.exception, "Plugin " + pluginassembly.Location + " has no author.");
}
}
return pluginauthor;
}
/// <summary>
/// Get file (short)description of the plugin.
/// </summary>
/// <param name="pluginassembly">The plugin assembly</param>
/// <returns>The description of the plugin</returns>
public static string GetPluginDescription(Assembly pluginassembly)
{
string plugindescription = string.Empty;
object[] attdesc = pluginassembly.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
if (attdesc.Length != 0)
{
if (((AssemblyDescriptionAttribute)attdesc[0]).Description.Length > 1000)
{
plugindescription = ((AssemblyDescriptionAttribute)attdesc[0]).Description.Substring(0, 1000);
}
else
{
plugindescription = ((AssemblyDescriptionAttribute)attdesc[0]).Description;
}
}
return plugindescription;
}
/// <summary>
/// Get version of the plugin as string.
/// </summary>
/// <param name="pluginassembly">The plugin assembly</param>
/// <returns>The version of the plugin as string</returns>
public static string GetPluginVersion(Assembly pluginassembly)
{
if (pluginassembly.GetName().Version != null)
{
return pluginassembly.GetName().Version.ToString();
}
else
{
Log.Write(LogType.exception, "Plugin " + pluginassembly.Location + " has no version information.");
return Strings.T("unknown");
}
}
#endregion Getting plugin details
/// <summary>
/// Get if a plugin dll file enabled.
/// </summary>
/// <param name="dllfilename">The plugin dll file</param>
/// <returns>True if plugin is enabled.</returns>
public static bool IsPluginEnabled(string dllfilename)
{
if (enabledplugins == null)
{
return false;
}
for (int i = 0; i < enabledplugins.Count; i++)
{
if (enabledplugins[i].Filename.Equals(dllfilename, StringComparison.Ordinal))
{
return true;
}
}
return false;
}
/// <summary>
/// Get the version of a plugin by the plugin name.
/// </summary>
/// <param name="pluginname">The name of the plugin</param>
/// <returns>An array with the major, minor and release version numbers.</returns>
public static short[] GetPluginVersionByName(string pluginname)
{
short[] pluginversion = new short[3];
for (int i = 0; i < installedplugins.Count; i++)
{
Assembly pluginassembly = System.Reflection.Assembly.LoadFrom(Path.Combine(Settings.ProgramPluginsFolder, installedplugins[i]));
if (GetPluginName(pluginassembly).Equals(pluginname, StringComparison.Ordinal))
{
string pluginversionstring = GetPluginVersion(pluginassembly);
pluginversion = Program.ParserVersionString(pluginversionstring);
}
}
return pluginversion;
}
/// <summary>
/// Get the version information from the IPlugin libery.
/// </summary>
/// <returns>Array with version numbers major, minor and release numbers.</returns>
public static short[] GetIPluginVersion()
{
string versionipluginstring = string.Empty;
System.Reflection.Assembly ipluginasm;
try
{
ipluginasm = System.Reflection.Assembly.ReflectionOnlyLoadFrom(Path.Combine(Program.InstallFolder, "IPlugin.dll"));
versionipluginstring = ipluginasm.GetName().Version.Major + "." + ipluginasm.GetName().Version.Minor + "." + ipluginasm.GetName().Version.Build;
}
catch (FileNotFoundException)
{
Log.Write(LogType.exception, "File IPlugin.dll not found.");
}
catch (FileLoadException)
{
Log.Write(LogType.exception, "Cannot load iplugin.dll");
}
short[] ipluginversion = Program.ParserVersionString(versionipluginstring);
return ipluginversion;
}
/// <summary>
/// Save the enabled plugin settings.
/// </summary>
public static void SaveEnabledPlugins()
{
StringBuilder sbenabledplugin = new StringBuilder();
if (enabledplugins != null)
{
bool first = true;
for (int i = 0; i < enabledplugins.Count; i++)
{
if (first)
{
first = false;
}
else
{
sbenabledplugin.Append("|");
}
sbenabledplugin.Append(enabledplugins[i].Filename);
}
}
Settings.ProgramPluginsEnabled = sbenabledplugin.ToString();
xmlUtil.WriteSettings();
}
/// <summary>
/// Get all the dll filenames(without full path) in the plugin directory.
/// </summary>
/// <returns>All dll filenames as string array</returns>
private static string[] GetDllFilesFolder(string folder)
{
string[] pluginfiles = Directory.GetFiles(folder, "*.dll", SearchOption.TopDirectoryOnly);
string[] pluginfilenames = new string[pluginfiles.Length];
for (int i = 0; i < pluginfiles.Length; i++)
{
pluginfilenames[i] = Path.GetFileName(pluginfiles[i]);
}
pluginfiles = null;
return pluginfilenames;
}
/// <summary>
/// Is the dll files excluded as plugin in the plugin directory.
/// </summary>
/// <param name="dllfilename">The dll filename without path</param>
/// <returns>True if it's excluded</returns>
private static bool IsPluginFileExcluded(string dllfilename)
{
if (excludedplugindlls != null)
{
for (int i = 0; i < excludedplugindlls.Length; i++)
{
if (dllfilename.Equals(excludedplugindlls[i], StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
}
return false;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public static bool UpdatePluginReplaceFiles()
{
bool pluginstoupdate = false;
string pluginupdatefolder = Program.GetNewPluginFolder(false);
if (Directory.Exists(pluginupdatefolder))
{
string[] newplugins = GetDllFilesFolder(pluginupdatefolder);
if (newplugins.Length > 0)
{
pluginstoupdate = true;
}
for (int i = 0; i < newplugins.Length; i++)
{
if (File.GetAttributes(Path.Combine(pluginupdatefolder, newplugins[i])) == FileAttributes.System)
{
continue;
}
bool removeoldplugin = false;
if (File.Exists(Path.Combine(Settings.ProgramPluginsFolder, newplugins[i])))
{
// create backup old plugin version
File.Move(Path.Combine(Settings.ProgramPluginsFolder, newplugins[i]), Path.Combine(Settings.ProgramPluginsFolder, newplugins[i] + ".bak"));
removeoldplugin = true;
}
// install new plugin
try
{
File.Move(Path.Combine(pluginupdatefolder, newplugins[i]), Path.Combine(Settings.ProgramPluginsFolder, newplugins[i]));
}
catch (Exception ex)
{
removeoldplugin = false;
Log.Write(LogType.exception, ex.Message);
}
if (removeoldplugin)
{
// delete backup
File.Delete(Path.Combine(Settings.ProgramPluginsFolder, newplugins[i] + ".bak"));
}
}
}
return pluginstoupdate;
}
}
}