-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
205 lines (179 loc) · 7.08 KB
/
Copy pathApp.xaml.cs
File metadata and controls
205 lines (179 loc) · 7.08 KB
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
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
namespace ArIED61850Tester;
public partial class App : Application
{
private static readonly object UiErrorSync = new();
private static string _lastUiErrorSignature = string.Empty;
private static DateTime _lastUiErrorUtc = DateTime.MinValue;
private static int _uiErrorHandlerActive;
private CancellationTokenSource? _updateCancellation;
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// P2 installs one calm industrial visual system before StartupUri materializes.
// Existing XAML keeps its semantic resource keys while the overlay replaces
// glare-heavy white/blue surfaces with Blue Steel + Light Greige equivalents.
InstallP2BlueSteelGreigeTheme();
// Keep every IED card on one reusable template key while replacing the
// legacy bitmap fallback with the compact ARVREL-derived vector fascia.
// Installing this before StartupUri materializes also makes the portable
// smoke test validate that the vector ResourceDictionary is packaged.
InstallArvrelMiniIedFascia();
if (Array.Exists(e.Args, argument =>
string.Equals(argument, "--portable-smoke-test", StringComparison.OrdinalIgnoreCase)))
{
Shutdown(RunPortableSmokeTest());
return;
}
GridUxBehavior.Install();
FaultRecordUxBehavior.Install();
DispatcherUnhandledException += OnDispatcherUnhandledException;
TaskScheduler.UnobservedTaskException += (_, args) => args.SetObserved();
_updateCancellation = new CancellationTokenSource();
Dispatcher.BeginInvoke(
DispatcherPriority.ApplicationIdle,
new Action(() => _ = AppUpdateCoordinator.RunLazyAsync(_updateCancellation.Token)));
}
protected override void OnActivated(EventArgs e)
{
base.OnActivated(e);
// A handful of legacy controls own local visual resources. Re-apply the
// tiny P2 adapter when windows activate so newly opened FAT workspaces also
// inherit the selected industrial theme without touching engine workflows.
P2BlueSteelGreigeUx.ApplyToOpenWindows(this);
}
private void InstallP2BlueSteelGreigeTheme()
{
var themeResources = new ResourceDictionary
{
Source = new Uri(
"/ARSAS;component/Resources/P2BlueSteelGreige.xaml",
UriKind.Relative)
};
// Application.Resources itself has higher lookup precedence than merged
// dictionaries, so copy the P2 entries into the primary dictionary. This
// lets every existing StaticResource key resolve to the new visual contract
// when StartupUri builds its windows.
foreach (DictionaryEntry entry in themeResources)
Resources[entry.Key] = entry.Value;
}
private void InstallArvrelMiniIedFascia()
{
var fasciaResources = new ResourceDictionary
{
Source = new Uri(
"/ARSAS;component/Resources/ArvrelMiniIedFascia.xaml",
UriKind.Relative)
};
if (fasciaResources["ArvrelMiniIedRelayFrontPanelTemplate"] is not ControlTemplate template)
{
throw new InvalidOperationException(
"The packaged ARVREL mini IED fascia resource is missing its ControlTemplate.");
}
Resources["IedRelayFrontPanelTemplate"] = template;
}
private static int RunPortableSmokeTest()
{
try
{
var lockPath = Path.Combine(AppContext.BaseDirectory, "engines", "ARIEC61850.lock.json");
if (!File.Exists(lockPath))
{
WritePortableSmokeDiagnostic($"Engine lock was not extracted. BaseDirectory={AppContext.BaseDirectory}; expected={lockPath}");
return 21;
}
foreach (var assemblyName in new[]
{
"AR.Iec61850",
"AR.Iec61850.Transports.Npcap",
"SharpPcap",
"PacketDotNet"
})
{
_ = Assembly.Load(new AssemblyName(assemblyName));
}
var probePath = Path.Combine(Path.GetTempPath(), $"ARSAS-portable-{Guid.NewGuid():N}.tmp");
File.WriteAllText(probePath, "portable-smoke-test");
File.Delete(probePath);
return 0;
}
catch (Exception ex)
{
WritePortableSmokeDiagnostic(ex.ToString());
return 22;
}
}
private static void WritePortableSmokeDiagnostic(string message)
{
try
{
File.WriteAllText(
Path.Combine(Path.GetTempPath(), "ARSAS-portable-smoke-error.txt"),
message);
}
catch
{
// Diagnostics must never replace the original smoke-test result.
}
}
protected override void OnExit(ExitEventArgs e)
{
_updateCancellation?.Cancel();
_updateCancellation?.Dispose();
_updateCancellation = null;
base.OnExit(e);
}
private static void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
// Mark the exception handled first. A modal MessageBox here can recursively trigger
// the same layout/binding exception and create an endless stack of dialogs.
e.Handled = true;
var exception = e.Exception;
var signature = $"{exception.GetType().FullName}|{exception.Message}";
var nowUtc = DateTime.UtcNow;
lock (UiErrorSync)
{
if (signature.Equals(_lastUiErrorSignature, StringComparison.Ordinal) &&
nowUtc - _lastUiErrorUtc < TimeSpan.FromSeconds(10))
{
Debug.WriteLine($"Suppressed repeated ARSAS UI error: {signature}");
return;
}
_lastUiErrorSignature = signature;
_lastUiErrorUtc = nowUtc;
}
if (Interlocked.Exchange(ref _uiErrorHandlerActive, 1) != 0)
return;
try
{
Debug.WriteLine(exception);
if (Current?.MainWindow is MainWindow mainWindow)
mainWindow.ReportUnexpectedUiError(exception);
}
catch (Exception reportingError)
{
Debug.WriteLine($"Failed to route ARSAS UI error to Diagnostics: {reportingError}");
}
finally
{
var dispatcher = Current?.Dispatcher;
if (dispatcher == null || dispatcher.HasShutdownStarted)
{
Interlocked.Exchange(ref _uiErrorHandlerActive, 0);
}
else
{
dispatcher.BeginInvoke(
DispatcherPriority.ContextIdle,
new Action(() => Interlocked.Exchange(ref _uiErrorHandlerActive, 0)));
}
}
}
}