-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgramUI.cs
390 lines (344 loc) · 10.8 KB
/
ProgramUI.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
using System.Diagnostics;
using System.Text;
using Cilurbo.Analyzers;
using Cilurbo.MetadataTables;
using Cilurbo.Services;
using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.TypeSystem;
using Terminal.Gui;
using Terminal.Gui.Trees;
namespace Cilurbo;
partial class Program {
static int sep = 40;
static readonly TreeView metadata_tree = new () {
X = 0,
Y = 2,
Width = sep,
Height = Dim.Fill (1),
CanFocus = true,
MultiSelect = false,
};
static readonly TabView tabs = new () {
X = sep,
Y = 1,
Width = Dim.Fill (),
Height = Dim.Fill (1),
CanFocus = true,
};
static readonly TableView table = new () {
X = 0,
Y = 0,
Width = Dim.Fill (),
Height = Dim.Fill (1),
CanFocus = true,
MultiSelect = false,
Shortcut = Key.CtrlMask | Key.A,
};
static int SetupUI (Toplevel top)
{
StatusBar statusBar = new (new StatusItem [] {
new (Key.F9, "~F9~ Menu", null),
new (Key.Null, "...", null),
});
metadata_tree.SelectionChanged += (sender, e) => {
statusBar.Items [1].Title = e.NewValue.Text;
};
metadata_tree.ObjectActivated += ObjectActivated;
metadata_tree.KeyPress += (e) => {
if (e.KeyEvent.Key == (Key.CtrlMask | Key.D)) {
if (metadata_tree.HasFocus) {
DashSupport.Open (metadata_tree.SelectedObject);
e.Handled = true;
}
}
};
MenuBar menu = new (new MenuBarItem [] {
new ("_File", new MenuItem? [] {
new ("_Open...", "", FileOpen, null, null, Key.CtrlMask | Key.O),
new ("Open Assembly List...", "", FileOpenList, null, null, Key.CtrlMask | Key.R),
new ("_Save Assembly List...", "", FileSaveList, () => { return metadata_tree.Objects.Any (); }, null, Key.CtrlMask | Key.S),
null,
new ("_Quit", "", FileQuit, null, null, Key.CtrlMask | Key.Q),
}),
new ("_Edit", new MenuItem [] {
new ("_Copy", "", EditCopy, null, null, Key.CtrlMask | Key.C),
}),
new ("_View", new MenuItem? [] {
new ("Metadata Tree", "", ViewMetadataTree, null, null, Key.F1),
new ("Disassembler View (IL)", "", ViewDisassembler, null, null, Key.F2),
new ("Decompiler View (C#)", "", ViewDecompiler, null, null, Key.F3),
new ("Metadata Tables", "", ViewMetadataTables, () => { return metadata_tree.SelectedObject is AssemblyNode; }, null, Key.F4),
null,
new ("Collapse All Nodes", "", ViewCollapseAllNodes, null, null, Key.CtrlMask | Key.U),
null,
new ("Enlarge Metadata Tree", "", EnlargeTreeView, null, null, Key.ShiftMask | Key.CursorRight),
new ("Reduce Metadata Tree", "", ReduceTreeView, null, null, Key.ShiftMask | Key.CursorLeft),
null,
new ("_Preferences...", "", ViewPreferences, null, null, Key.F5),
}),
new ("_Analyzers", AnalyzersManager.BuildMenu ()),
new ("_Help", new MenuItem [] {
new ("Key Bindings...", "", HelpKeyBindings),
new ("About...", "", HelpAbout),
}),
});
top.Add (menu);
Label label = new ("Loaded Assemblies") {
X = 0,
Y = 1,
Width = Dim.Fill (),
Height = 1,
CanFocus = false,
};
top.Add (label, metadata_tree, tabs, statusBar);
top.ColorScheme = Colors.Base;
SetupScrollBar (); // superview won't be null here
return 0;
}
static void ObjectActivated (ObjectActivatedEventArgs<ITreeNode> e)
{
var node = e.ActivatedObject;
switch (node) {
case AssemblyReferenceNode arn:
if (arn.Tag is AssemblyReference ar) {
var pe = AssemblyResolver.Resolver.Resolve (ar);
if (pe is not null) {
var a = metadata_tree.Find (pe);
if (a is null) {
Add (pe, selectAndGoto: true);
} else {
metadata_tree.Select (a);
}
EnsureSourceView ().Show (pe);
}
}
break;
case BaseTypeNode btn:
if (btn.Tag is ITypeDefinition bt) {
var pe = bt.ParentModule.PEFile;
if (pe is null)
return;
// find the PE node (and load/add it if needed)...
var f = metadata_tree.Find (pe);
if (f is null) {
Add (pe, selectAndGoto: false);
f = metadata_tree.Find (pe);
}
// then search for the type from that node...
if (f is not null) {
metadata_tree.Expand (f); // pre-expand the PE node since we're searching below it
var btmt = bt.MetadataToken;
f = metadata_tree.Find (f, (n) => (n is TypeNode tn) && btmt.Equals (tn.TypeDefinition.MetadataToken));
}
if (f is not null) {
metadata_tree.Select (f);
EnsureSourceView ().Show (f.Tag);
}
}
break;
case ResourceNode res:
metadata_tree.Expand (node);
EnsureResourceView ().Show (res.Resource);
metadata_tree.SetFocus ();
break;
default:
metadata_tree.Expand (node);
EnsureSourceView ().Show (node.Tag);
metadata_tree.SetFocus ();
break;
}
}
public static MetadataNode? SelectedNode => metadata_tree.SelectedObject as MetadataNode;
public static MetadataNode? Select (Predicate<ITreeNode> predicate)
{
var f = metadata_tree.Find (predicate);
return metadata_tree.Select (f);
}
public static AssemblyNode Add (PEFile pe, bool selectAndGoto = true)
{
var an = metadata_tree.Add (pe);
if (selectAndGoto) {
metadata_tree.SelectedObject = an;
metadata_tree.GoTo (an);
}
return an;
}
public static void AddTab (TabView.Tab tab, bool andSelect = true)
{
tabs.AddTab (tab, andSelect);
}
public static void SelectTab (TabView.Tab tab)
{
tabs.SelectedTab = tab;
}
static void EnlargeTreeView ()
{
if (sep > 80)
return;
sep++;
var p = tabs.X + 1;
tabs.X = p;
tabs.Width = Dim.Fill ();
metadata_tree.Width += 1;
}
static void ReduceTreeView ()
{
if (sep < 10)
return;
sep--;
tabs.X -= 1;
tabs.Width = Dim.Fill ();
metadata_tree.Width -= 1;
}
static void FileOpen ()
{
using OpenDialog d = new ("Open Assemblies", "", assemblies_file_extensions, OpenDialog.OpenMode.File) {
AllowsMultipleSelection = true,
};
Application.Run (d);
if (!d.Canceled && d.FilePaths.Count > 0) {
foreach (var f in d.FilePaths) {
LoadFile (f);
}
}
}
static void FileOpenList ()
{
using OpenDialog d = new ("Open Assembly List", "", lists_file_extensions, OpenDialog.OpenMode.File);
Application.Run (d);
if (!d.Canceled && (d.FilePath is not null)) {
// this means a list can include another list, e.g. an app list can include a framework list
LoadFile (d.FilePath.ToString ()!);
}
}
static void FileSaveList ()
{
using SaveDialog d = new ("Save Assembly List", "", lists_file_extensions);
Application.Run (d);
if (!d.Canceled && (d.FilePath is not null)) {
StringBuilder list = new ();
foreach (var node in metadata_tree.Objects) {
if (node.Tag is PEFile pe) {
list.AppendLine (pe.FileName);
}
}
File.WriteAllText (d.FilePath.ToString ()!, list.ToString ());
}
}
static void FileQuit ()
{
Application.Top.Running = false;
}
static void EditCopy ()
{
if (Application.Top.Focused is TextView tv)
tv.Copy ();
}
static void ViewMetadataTree ()
{
metadata_tree.SetFocus ();
}
static void ViewDisassembler ()
{
EnsureSourceView ().Language = Languages.IL;
}
static void ViewDecompiler ()
{
EnsureSourceView ().Language = Languages.CSharp;
}
static void ViewCollapseAllNodes ()
{
metadata_tree.CollapseAll ();
metadata_tree.SetFocus ();
}
static void ViewMetadataTables ()
{
// menu is disabled (same condition) but this gets called anyway if the (menu) shortcut is used
if (metadata_tree.SelectedObject.Tag is not PEFile pe)
return;
EnsureMetadataTableView ().PEFile = pe;
tabs.SelectedTab = metadata_tab;
metadata_tab.View.SetFocus ();
}
static void ViewPreferences ()
{
MessageBox.Query ("Preferences", "TODO", "_Ok");
}
static void HelpKeyBindings ()
{
ExecSupport.Run ("open", "https://github.com/spouliot/cilurbo/wiki/KeyBindings");
}
static void HelpAbout ()
{
StringBuilder aboutMessage = new ();
aboutMessage.AppendLine ("A simple, text-user interface over .net assemblies");
aboutMessage.AppendLine (@"_________ .__.__ ___. ");
aboutMessage.AppendLine (@"\_ ___ \|__| | __ ________\_ |__ ____ ");
aboutMessage.AppendLine (@"/ \ \/| | | | | \_ __ \ __ \ / _ \ ");
aboutMessage.AppendLine (@"\ \___| | |_| | /| | \/ \_\ ( <_> )");
aboutMessage.AppendLine (@" \______ /__|____/____/ |__| |___ /\____/ ");
aboutMessage.AppendLine (@" \/ \/ ");
aboutMessage.AppendLine ("");
aboutMessage.AppendLine ($"Version: {typeof (Program).Assembly.GetName ().Version}");
aboutMessage.AppendLine ($"Using ICSharpCode.Decompiler {FileVersionInfo.GetVersionInfo (typeof (PEFile).Assembly.Location).ProductVersion}");
aboutMessage.AppendLine ($"and Terminal.Gui {FileVersionInfo.GetVersionInfo (typeof (Application).Assembly.Location).ProductVersion}");
aboutMessage.AppendLine ("");
MessageBox.Query ("About Cilurbo", aboutMessage.ToString (), "_Ok");
}
// copied from terminal.gui samples
static void SetupScrollBar ()
{
// When using scroll bar leave the last row of the control free (for over-rendering with scroll bar)
metadata_tree.Style.LeaveLastRow = true;
ScrollBarView _scrollBar = new (metadata_tree, true);
_scrollBar.ShowScrollIndicator = true;
_scrollBar.ChangedPosition += () => {
metadata_tree.ScrollOffsetVertical = _scrollBar.Position;
if (metadata_tree.ScrollOffsetVertical != _scrollBar.Position) {
_scrollBar.Position = metadata_tree.ScrollOffsetVertical;
}
metadata_tree.SetNeedsDisplay ();
};
_scrollBar.OtherScrollBarView.ChangedPosition += () => {
metadata_tree.ScrollOffsetHorizontal = _scrollBar.OtherScrollBarView.Position;
if (metadata_tree.ScrollOffsetHorizontal != _scrollBar.OtherScrollBarView.Position) {
_scrollBar.OtherScrollBarView.Position = metadata_tree.ScrollOffsetHorizontal;
}
metadata_tree.SetNeedsDisplay ();
};
metadata_tree.DrawContent += (e) => {
_scrollBar.Size = metadata_tree.ContentHeight;
_scrollBar.Position = metadata_tree.ScrollOffsetVertical;
_scrollBar.OtherScrollBarView.Size = metadata_tree.GetContentWidth (true);
_scrollBar.OtherScrollBarView.Position = metadata_tree.ScrollOffsetHorizontal;
_scrollBar.Refresh ();
};
}
static readonly TabView.Tab source_tab = new ("", new SourceView ());
public static SourceView EnsureSourceView ()
{
if (!tabs.Tabs.Contains (source_tab)) {
tabs.AddTab (source_tab, true);
}
tabs.SelectedTab = source_tab;
return (source_tab.View as SourceView)!;
}
static readonly TabView.Tab resource_tab = new ("", new ResourceView ());
public static ResourceView EnsureResourceView ()
{
if (!tabs.Tabs.Contains (resource_tab)) {
tabs.AddTab (resource_tab, true);
}
tabs.SelectedTab = resource_tab;
return (resource_tab.View as ResourceView)!;
}
static readonly TabView.Tab metadata_tab = new ("Metadata", new MetadataView ());
public static MetadataView EnsureMetadataTableView ()
{
if (!tabs.Tabs.Contains (metadata_tab)) {
tabs.AddTab (metadata_tab, true);
}
tabs.SelectedTab = metadata_tab;
return (metadata_tab.View as MetadataView)!;
}
}