corrade-vassal – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | vero | 1 | using System; |
2 | using System.Collections.Generic; |
||
3 | using System.IO; |
||
4 | using Gtk; |
||
5 | using GridProxy; |
||
6 | using GridProxyGUI; |
||
7 | using System.Reflection; |
||
8 | |||
9 | namespace GridProxyGUI |
||
10 | { |
||
11 | public class PluginInfo |
||
12 | { |
||
13 | public bool LoadOnStartup; |
||
14 | public string Path; |
||
15 | |||
16 | public string Name |
||
17 | { |
||
18 | get |
||
19 | { |
||
20 | if (string.IsNullOrEmpty(Path)) return string.Empty; |
||
21 | return System.IO.Path.GetFileName(Path); |
||
22 | } |
||
23 | } |
||
24 | |||
25 | public string Dir |
||
26 | { |
||
27 | get |
||
28 | { |
||
29 | if (string.IsNullOrEmpty(Path)) return string.Empty; |
||
30 | return System.IO.Path.GetDirectoryName(Path); |
||
31 | } |
||
32 | } |
||
33 | public List<string> Modules = new List<string>(); |
||
34 | } |
||
35 | |||
36 | public class PluginsScroller : TreeView |
||
37 | { |
||
38 | |||
39 | public ListStore Store; |
||
40 | |||
41 | public PluginsScroller() |
||
42 | { |
||
43 | TreeViewColumn col = new TreeViewColumn(); |
||
44 | col.Title = "Load on Starup"; |
||
45 | CellRendererToggle cell = new CellRendererToggle(); |
||
46 | cell.Toggled += new ToggledHandler((sender, e) => |
||
47 | { |
||
48 | TreeIter iter; |
||
49 | if (Store.GetIterFromString(out iter, e.Path)) |
||
50 | { |
||
51 | PluginInfo item = Store.GetValue(iter, 0) as PluginInfo; |
||
52 | if (null != item) |
||
53 | { |
||
54 | item.LoadOnStartup = !item.LoadOnStartup; |
||
55 | Store.SetValue(iter, 0, item); |
||
56 | } |
||
57 | } |
||
58 | }); |
||
59 | cell.Activatable = true; |
||
60 | col.PackStart(cell, true); |
||
61 | col.SetCellDataFunc(cell, (TreeViewColumn column, CellRenderer xcell, TreeModel model, TreeIter iter) => |
||
62 | { |
||
63 | var item = Store.GetValue(iter, 0) as PluginInfo; |
||
64 | if (item != null) |
||
65 | { |
||
66 | ((CellRendererToggle)cell).Active = item.LoadOnStartup; |
||
67 | } |
||
68 | }); |
||
69 | AppendColumn(col); |
||
70 | |||
71 | col = new TreeViewColumn(); |
||
72 | col.Title = "Plugin"; |
||
73 | CellRendererText cellText = new CellRendererText(); |
||
74 | col.PackStart(cellText, true); |
||
75 | col.SetCellDataFunc(cellText, (TreeViewColumn column, CellRenderer xcell, TreeModel model, TreeIter iter) => |
||
76 | { |
||
77 | var item = Store.GetValue(iter, 0) as PluginInfo; |
||
78 | if (item != null) |
||
79 | { |
||
80 | ((CellRendererText)xcell).Text = string.Format("{0} ({1})", item.Name, string.Join(", ", item.Modules.ToArray())); |
||
81 | } |
||
82 | }); |
||
83 | AppendColumn(col); |
||
84 | |||
85 | col = new TreeViewColumn(); |
||
86 | col.Title = "Path"; |
||
87 | cellText = new CellRendererText(); |
||
88 | col.PackStart(cellText, true); |
||
89 | col.SetCellDataFunc(cellText, (TreeViewColumn column, CellRenderer xcell, TreeModel model, TreeIter iter) => |
||
90 | { |
||
91 | var item = Store.GetValue(iter, 0) as PluginInfo; |
||
92 | if (item != null) |
||
93 | { |
||
94 | ((CellRendererText)xcell).Text = item.Path; |
||
95 | } |
||
96 | }); |
||
97 | AppendColumn(col); |
||
98 | |||
99 | Store = new ListStore(typeof(PluginInfo)); |
||
100 | Model = Store; |
||
101 | HeadersVisible = true; |
||
102 | ShowAll(); |
||
103 | } |
||
104 | |||
105 | List<FileFilter> GetFileFilters() |
||
106 | { |
||
107 | List<FileFilter> filters = new List<FileFilter>(); |
||
108 | |||
109 | FileFilter filter = new FileFilter(); |
||
110 | filter.Name = "Grid Proxy Plugin (*.dll; *.exe)"; |
||
111 | filter.AddPattern("*.dll"); |
||
112 | filter.AddPattern("*.exe"); |
||
113 | filters.Add(filter); |
||
114 | |||
115 | filter = new FileFilter(); |
||
116 | filter.Name = "All Files (*.*)"; |
||
117 | filter.AddPattern("*.*"); |
||
118 | filters.Add(filter); |
||
119 | |||
120 | return filters; |
||
121 | } |
||
122 | |||
123 | |||
124 | public bool LoadAssembly(PluginInfo pinfo, ProxyFrame proxy) |
||
125 | { |
||
126 | |||
127 | try |
||
128 | { |
||
129 | pinfo.Modules.Clear(); |
||
130 | bool started = false; |
||
131 | Assembly assembly = Assembly.LoadFile(System.IO.Path.GetFullPath(pinfo.Path)); |
||
132 | foreach (Type t in assembly.GetTypes()) |
||
133 | { |
||
134 | if (t.IsSubclassOf(typeof(ProxyPlugin))) |
||
135 | { |
||
136 | ConstructorInfo info = t.GetConstructor(new Type[] { typeof(ProxyFrame) }); |
||
137 | if (info == null) |
||
138 | { |
||
139 | OpenMetaverse.Logger.Log(string.Format("No suitable contructor for {0} in {1}", t.ToString(), pinfo.Name), OpenMetaverse.Helpers.LogLevel.Warning); |
||
140 | } |
||
141 | else |
||
142 | { |
||
143 | ProxyPlugin plugin = (ProxyPlugin)info.Invoke(new object[] { proxy }); |
||
144 | plugin.Init(); |
||
145 | pinfo.Modules.Add(t.ToString()); |
||
146 | started = true; |
||
147 | } |
||
148 | } |
||
149 | } |
||
150 | |||
151 | if (started) |
||
152 | { |
||
153 | OpenMetaverse.Logger.Log(string.Format("Loaded {0} plugins from {1}: {2}", pinfo.Modules.Count.ToString(), pinfo.Name, string.Join(", ", pinfo.Modules.ToArray())), OpenMetaverse.Helpers.LogLevel.Info); |
||
154 | } |
||
155 | else |
||
156 | { |
||
157 | OpenMetaverse.Logger.Log("Found no plugins in " + pinfo.Name, OpenMetaverse.Helpers.LogLevel.Warning); |
||
158 | } |
||
159 | |||
160 | return started; |
||
161 | } |
||
162 | catch (Exception e) |
||
163 | { |
||
164 | OpenMetaverse.Logger.Log("Failed loading plugins from " + pinfo.Path + ": " + e.Message, OpenMetaverse.Helpers.LogLevel.Error); |
||
165 | } |
||
166 | |||
167 | return false; |
||
168 | } |
||
169 | |||
170 | public void LoadPlugin(ProxyFrame proxy) |
||
171 | { |
||
172 | if (proxy == null) return; |
||
173 | |||
174 | var od = new Gtk.FileChooserDialog(null, "Load Plugin", null, FileChooserAction.Open, "Cancel", ResponseType.Cancel, "Open", ResponseType.Accept); |
||
175 | foreach (var filter in GetFileFilters()) od.AddFilter(filter); |
||
176 | |||
177 | if (od.Run() == (int)ResponseType.Accept) |
||
178 | { |
||
179 | PluginInfo plugin = new PluginInfo(); |
||
180 | plugin.Path = od.Filename; |
||
181 | bool found = false; |
||
182 | Store.Foreach((model, path, iter) => |
||
183 | { |
||
184 | var item = model.GetValue(iter, 0) as PluginInfo; |
||
185 | if (null != item && item.Path == plugin.Path) |
||
186 | { |
||
187 | return found = true; |
||
188 | } |
||
189 | |||
190 | return false; |
||
191 | }); |
||
192 | |||
193 | if (!found && LoadAssembly(plugin, proxy)) |
||
194 | { |
||
195 | Store.AppendValues(plugin); |
||
196 | } |
||
197 | } |
||
198 | od.Destroy(); |
||
199 | |||
200 | } |
||
201 | |||
202 | } |
||
203 | } |