| Name | Message | Date |
|---|---|---|
| 📄 MainViewModel.cs | 5 days ago |
📄
ui/src/ViewModels/MainViewModel.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
using System; using System.IO; using System.Threading; using System.Threading.Tasks; using Cfa.Ui.Graph; using Cfa.Ui.Services; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; namespace Cfa.Ui.ViewModels; public sealed class MainViewModel : ObservableObject, IDisposable { private readonly IDesktopServices desktop; private readonly IScanSessionFactory sessions; private readonly CancellationTokenSource lifetime = new(); private IScanSession? scanner; private FileInfo? solution; private bool isBusy; private bool disposed; private string address = ""; private string status = "Open a solution to explore its call graph."; private GraphScene? scene; public MainViewModel(IDesktopServices desktop, IScanSessionFactory sessions) { this.desktop = desktop; this.sessions = sessions; OpenSolutionCommand = new AsyncRelayCommand(OpenSolutionAsync, () => !IsBusy); AnalyzeCommand = new AsyncRelayCommand(AnalyzeAsync, () => CanAnalyze); LoadConfigCommand = new AsyncRelayCommand(LoadConfigAsync, () => CanAnalyze); OpenSourceCommand = new AsyncRelayCommand<GraphNode>(OpenSourceAsync); } public IAsyncRelayCommand OpenSolutionCommand { get; } public IAsyncRelayCommand AnalyzeCommand { get; } public IAsyncRelayCommand LoadConfigCommand { get; } public IAsyncRelayCommand<GraphNode> OpenSourceCommand { get; } public bool CanAnalyze => scanner is not null && !IsBusy; public bool HasScene => Scene is not null; public string Address { get => address; set => SetProperty(ref address, value); } public string Status { get => status; private set => SetProperty(ref status, value); } public bool IsBusy { get => isBusy; private set { if (SetProperty(ref isBusy, value)) { OnPropertyChanged(nameof(CanAnalyze)); OpenSolutionCommand.NotifyCanExecuteChanged(); AnalyzeCommand.NotifyCanExecuteChanged(); LoadConfigCommand.NotifyCanExecuteChanged(); } } } public GraphScene? Scene { get => scene; private set { if (SetProperty(ref scene, value)) { OnPropertyChanged(nameof(HasScene)); } } } private async Task OpenSolutionAsync() { IsBusy = true; try { var file = await desktop.PickSolutionAsync(); if (file is null) { return; } Status = "Loading " + file.Name + "..."; var loaded = await Task.Run(() => sessions.OpenAsync(file, lifetime.Token), lifetime.Token); if (disposed) { loaded.Dispose(); return; } scanner?.Dispose(); scanner = loaded; solution = file; Scene = null; Status = file.Name + " loaded. Enter a source path:line."; } catch (OperationCanceledException) when (lifetime.IsCancellationRequested) { } catch (Exception exception) { Status = "Solution loading failed."; await desktop.ShowErrorAsync(exception.Message); } finally { FinishOperation(); } } private async Task LoadConfigAsync() { IsBusy = true; try { var file = await desktop.PickConfigAsync(); if (file is null) return; await Task.Run(() => scanner!.LoadConfig(file), lifetime.Token); if (!disposed) { Scene = null; Status = "Config loaded: " + file.FullName; } } catch (OperationCanceledException) when (lifetime.IsCancellationRequested) { } catch (Exception exception) { Status = "Config loading failed."; await desktop.ShowErrorAsync(exception.Message); } finally { FinishOperation(); } } private async Task AnalyzeAsync() { IsBusy = true; try { var separator = Address.LastIndexOf(':'); if (separator <= 0 || !int.TryParse(Address[(separator + 1)..], out var line) || line < 1) { throw new ArgumentException("Enter a source file and line, for example core/src/Scanner.cs:50."); } var path = Address[..separator].Trim(); var file = new FileInfo(Path.GetFullPath(path, solution!.DirectoryName!)); Status = "Analyzing call graph..."; var current = scanner!; var result = await Task.Run( async () => { var analysis = await current.AnalyzeAsync(file, line, lifetime.Token); return GraphLayout.Build(analysis, lifetime.Token); }, lifetime.Token ); if (!disposed) { Scene = result; Status = $"{result.Nodes.Count} occurrences, {result.Edges.Count} calls. Right/middle-drag to pan; scroll to zoom."; } } catch (OperationCanceledException) when (lifetime.IsCancellationRequested) { } catch (Exception exception) { Status = "Analysis failed."; await desktop.ShowErrorAsync(exception.Message); } finally { FinishOperation(); } } private async Task OpenSourceAsync(GraphNode? node) { if (node?.SourceLink is not { } link) { return; } try { await desktop.OpenSourceAsync(link); } catch (Exception exception) { await desktop.ShowErrorAsync(exception.Message); } } public void Dispose() { if (disposed) { return; } disposed = true; lifetime.Cancel(); if (!IsBusy) { scanner?.Dispose(); scanner = null; lifetime.Dispose(); } } private void FinishOperation() { IsBusy = false; if (disposed) { scanner?.Dispose(); scanner = null; lifetime.Dispose(); } } }