forked from treellama/weland
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugins.cs
126 lines (108 loc) · 3.14 KB
/
Plugins.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Gtk;
namespace Weland {
public class Plugins {
class PluginInfo {
public string Name;
public MethodInfo GtkRun;
public MethodInfo Run;
};
List<PluginInfo> plugins = new List<PluginInfo>();
public Plugins() {
List<string> files = new List<string>();
string exePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
try {
foreach (string file in Directory.GetFiles(Path.Combine(exePath, "Plugins"), "*.dll")) {
files.Add(file);
}
} catch { }
if (PlatformDetection.IsMac) {
try {
foreach (string file in Directory.GetFiles(Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(exePath)), "Plugins"), "*.dll")) {
files.Add(file);
}
} catch { }
}
string applicationData = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Weland");
try {
foreach (string file in Directory.GetFiles(Path.Combine(applicationData, "Plugins"), "*.dll")) {
files.Add(file);
}
} catch { }
foreach (string file in files)
{
try {
// needs to have at least name and run or gtkrun
Assembly a = Assembly.LoadFrom(file);
foreach (Type t in a.GetTypes())
{
PluginInfo plugin = new PluginInfo();
MethodInfo compatibleMethod = t.GetMethod("Compatible");
if (compatibleMethod == null || !compatibleMethod.IsStatic || !((bool) compatibleMethod.Invoke(0, new object[0])))
{
continue;
}
MethodInfo nameMethod = t.GetMethod("Name");
if (nameMethod != null && nameMethod.IsStatic)
{
plugin.Name = (string) nameMethod.Invoke(0, new object[0]);
plugin.Run = t.GetMethod("Run");
plugin.GtkRun = t.GetMethod("GtkRun");
if (plugin.Run != null && !plugin.Run.IsStatic)
{
plugin.Run = null;
}
if (plugin.GtkRun != null && !plugin.GtkRun.IsStatic)
{
plugin.GtkRun = null;
}
if (plugin.Run != null || plugin.GtkRun != null)
{
plugins.Add(plugin);
break;
}
}
}
} catch {
continue;
}
}
}
public int Length {
get {
return plugins.Count;
}
}
public string GetName(int plugin) {
return plugins[plugin].Name;
}
public void GtkRun(Editor editor, int plugin) {
try
{
object[] args = { editor };
if (plugins[plugin].GtkRun != null) {
plugins[plugin].GtkRun.Invoke(0, args);
} else if (plugins[plugin].Run != null) {
StringWriter log = new StringWriter();
Console.SetOut(log);
plugins[plugin].Run.Invoke(0, args);
StreamWriter stdout = new StreamWriter(Console.OpenStandardOutput());
stdout.AutoFlush = true;
Console.SetOut(stdout);
if (log.GetStringBuilder().Length > 0) {
new LogWindow(plugins[plugin].Name, log.ToString());
}
}
} catch (Exception e) {
MessageDialog d = new MessageDialog(null, DialogFlags.DestroyWithParent, MessageType.Error, ButtonsType.Close, e.Message);
d.Title = "Plugin Exception";
d.SecondaryText = e.StackTrace;
d.Run();
d.Destroy();
}
}
}
}