/
Installer.cs
564 lines (522 loc) · 23.7 KB
/
Installer.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
using System.Collections;
using System.ComponentModel;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
namespace System.Configuration.Install
{
/// <summary>Provides the foundation for custom installations.</summary>
[DefaultEvent("AfterInstall")]
public class Installer : Component
{
private InstallerCollection _installers;
internal Installer parent;
private InstallEventHandler _afterCommitHandler;
private InstallEventHandler _afterInstallHandler;
private InstallEventHandler _afterRollbackHandler;
private InstallEventHandler _afterUninstallHandler;
private InstallEventHandler _beforeCommitHandler;
private InstallEventHandler _beforeInstallHandler;
private InstallEventHandler _beforeRollbackHandler;
private InstallEventHandler _beforeUninstallHandler;
private const string WrappedExceptionSource = "WrappedExceptionSource";
/// <summary>Gets or sets information about the current installation.</summary>
/// <returns>An <see cref="T:System.Configuration.Install.InstallContext" /> that contains information about the current installation.</returns>
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public InstallContext Context { get; set; }
/// <summary>Gets the help text for all the installers in the installer collection.</summary>
/// <returns>The help text for all the installers in the installer collection, including the description of what the installer does and the command line options for the installation executable, such as the InstallUtil.exe utility, that can be passed to and understood by this installer.</returns>
/// <exception cref="T:System.NullReferenceException">One of the installers in the installer collection specifies a null reference instead of help text. A likely cause for this exception is that a field to contain the help text is defined but not initialized.</exception>
[ResDescription("Desc_Installer_HelpText")]
public virtual string HelpText
{
get
{
var stringBuilder = new StringBuilder();
for (var i = 0; i < Installers.Count; i++)
{
var helpText = Installers[i].HelpText;
if (helpText.Length > 0)
{
stringBuilder.Append("\r\n");
stringBuilder.Append(helpText);
}
}
return stringBuilder.ToString();
}
}
/// <summary>Gets the collection of installers that this installer contains.</summary>
/// <returns>An <see cref="T:System.Configuration.Install.InstallerCollection" /> containing the collection of installers associated with this installer.</returns>
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public InstallerCollection Installers => _installers ?? (_installers = new InstallerCollection(this));
/// <summary>Gets or sets the installer containing the collection that this installer belongs to.</summary>
/// <returns>An <see cref="T:System.Configuration.Install.Installer" /> containing the collection that this instance belongs to, or null if this instance does not belong to a collection.</returns>
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[TypeConverter(typeof(InstallerParentConverter))]
[ResDescription("Desc_Installer_Parent")]
public Installer Parent
{
get => parent;
set
{
if (value == this)
{
throw new InvalidOperationException(Res.GetString("InstallBadParent"));
}
if (value != parent)
{
if (value != null && InstallerTreeContains(value))
{
throw new InvalidOperationException(Res.GetString("InstallRecursiveParent"));
}
if (parent != null)
{
var num = parent.Installers.IndexOf(this);
if (num != -1)
{
parent.Installers.RemoveAt(num);
}
}
parent = value;
if (parent != null && !parent.Installers.Contains(this))
{
parent.Installers.Add(this);
}
}
}
}
/// <summary>Occurs after all the installers in the <see cref="P:System.Configuration.Install.Installer.Installers" /> property have committed their installations.</summary>
public event InstallEventHandler Committed
{
add => _afterCommitHandler = (InstallEventHandler)Delegate.Combine(_afterCommitHandler, value);
remove => _afterCommitHandler = (InstallEventHandler)Delegate.Remove(_afterCommitHandler, value);
}
/// <summary>Occurs after the <see cref="M:System.Configuration.Install.Installer.Install(System.Collections.IDictionary)" /> methods of all the installers in the <see cref="P:System.Configuration.Install.Installer.Installers" /> property have run.</summary>
public event InstallEventHandler AfterInstall
{
add => _afterInstallHandler = (InstallEventHandler)Delegate.Combine(_afterInstallHandler, value);
remove => _afterInstallHandler = (InstallEventHandler)Delegate.Remove(_afterInstallHandler, value);
}
/// <summary>Occurs after the installations of all the installers in the <see cref="P:System.Configuration.Install.Installer.Installers" /> property are rolled back.</summary>
public event InstallEventHandler AfterRollback
{
add => _afterRollbackHandler = (InstallEventHandler)Delegate.Combine(_afterRollbackHandler, value);
remove => _afterRollbackHandler = (InstallEventHandler)Delegate.Remove(_afterRollbackHandler, value);
}
/// <summary>Occurs after all the installers in the <see cref="P:System.Configuration.Install.Installer.Installers" /> property perform their uninstallation operations.</summary>
public event InstallEventHandler AfterUninstall
{
add => _afterUninstallHandler = (InstallEventHandler)Delegate.Combine(_afterUninstallHandler, value);
remove => _afterUninstallHandler = (InstallEventHandler)Delegate.Remove(_afterUninstallHandler, value);
}
/// <summary>Occurs before the installers in the <see cref="P:System.Configuration.Install.Installer.Installers" /> property committ their installations.</summary>
public event InstallEventHandler Committing
{
add => _beforeCommitHandler = (InstallEventHandler)Delegate.Combine(_beforeCommitHandler, value);
remove => _beforeCommitHandler = (InstallEventHandler)Delegate.Remove(_beforeCommitHandler, value);
}
/// <summary>Occurs before the <see cref="M:System.Configuration.Install.Installer.Install(System.Collections.IDictionary)" /> method of each installer in the installer collection has run.</summary>
public event InstallEventHandler BeforeInstall
{
add => _beforeInstallHandler = (InstallEventHandler)Delegate.Combine(_beforeInstallHandler, value);
remove => _beforeInstallHandler = (InstallEventHandler)Delegate.Remove(_beforeInstallHandler, value);
}
/// <summary>Occurs before the installers in the <see cref="P:System.Configuration.Install.Installer.Installers" /> property are rolled back.</summary>
public event InstallEventHandler BeforeRollback
{
add => _beforeRollbackHandler = (InstallEventHandler)Delegate.Combine(_beforeRollbackHandler, value);
remove => _beforeRollbackHandler = (InstallEventHandler)Delegate.Remove(_beforeRollbackHandler, value);
}
/// <summary>Occurs before the installers in the <see cref="P:System.Configuration.Install.Installer.Installers" /> property perform their uninstall operations.</summary>
public event InstallEventHandler BeforeUninstall
{
add => _beforeUninstallHandler = (InstallEventHandler)Delegate.Combine(_beforeUninstallHandler, value);
remove => _beforeUninstallHandler = (InstallEventHandler)Delegate.Remove(_beforeUninstallHandler, value);
}
internal bool InstallerTreeContains(Installer target)
{
if (Installers.Contains(target))
{
return true;
}
foreach (Installer installer in Installers)
{
if (installer.InstallerTreeContains(target))
{
return true;
}
}
return false;
}
/// <summary>When overridden in a derived class, completes the install transaction.</summary>
/// <param name="savedState">An <see cref="T:System.Collections.IDictionary" /> that contains the state of the computer after all the installers in the collection have run. </param>
/// <exception cref="T:System.ArgumentException">The <paramref name="savedState" /> parameter is null.-or- The saved-state <see cref="T:System.Collections.IDictionary" /> might have been corrupted. </exception>
/// <exception cref="T:System.Configuration.Install.InstallException">An exception occurred during the <see cref="M:System.Configuration.Install.Installer.Commit(System.Collections.IDictionary)" /> phase of the installation. This exception is ignored and the installation continues. However, the application might not function correctly after the installation is complete. </exception>
public virtual void Commit(IDictionary savedState)
{
if (savedState == null)
{
throw new ArgumentException(Res.GetString("InstallNullParameter", "savedState"));
}
if (savedState["_reserved_lastInstallerAttempted"] == null || savedState["_reserved_nestedSavedStates"] == null)
{
throw new ArgumentException(Res.GetString("InstallDictionaryMissingValues", "savedState"));
}
Exception ex = null;
try
{
OnCommitting(savedState);
}
catch (Exception ex2)
{
WriteEventHandlerError(Res.GetString("InstallSeverityWarning"), "OnCommitting", ex2);
Context.LogMessage(Res.GetString("InstallCommitException"));
ex = ex2;
}
var num = Convert.ToInt32(savedState["_reserved_lastInstallerAttempted"]);
var array = ToDictionaries(savedState["_reserved_nestedSavedStates"]);
if (num + 1 != array.Length || num >= Installers.Count)
{
throw new ArgumentException(Res.GetString("InstallDictionaryCorrupted", "savedState"));
}
for (var i = 0; i < Installers.Count; i++)
{
Installers[i].Context = Context;
}
for (var j = 0; j <= num; j++)
{
try
{
Installers[j].Commit(array[j]);
}
catch (Exception ex3)
{
if (!IsWrappedException(ex3))
{
Context.LogMessage(Res.GetString("InstallLogCommitException", Installers[j].ToString()));
LogException(ex3, Context);
Context.LogMessage(Res.GetString("InstallCommitException"));
}
ex = ex3;
}
}
savedState["_reserved_nestedSavedStates"] = array;
savedState.Remove("_reserved_lastInstallerAttempted");
try
{
OnCommitted(savedState);
}
catch (Exception ex4)
{
WriteEventHandlerError(Res.GetString("InstallSeverityWarning"), "OnCommitted", ex4);
Context.LogMessage(Res.GetString("InstallCommitException"));
ex = ex4;
}
if (ex != null)
{
var ex5 = ex;
if (!IsWrappedException(ex))
{
ex5 = new InstallException(Res.GetString("InstallCommitException"), ex);
ex5.Source = "WrappedExceptionSource";
}
throw ex5;
}
}
/// <summary>When overridden in a derived class, performs the installation.</summary>
/// <param name="stateSaver">An <see cref="T:System.Collections.IDictionary" /> used to save information needed to perform a commit, rollback, or uninstall operation. </param>
/// <exception cref="T:System.ArgumentException">The <paramref name="stateSaver" /> parameter is null. </exception>
/// <exception cref="T:System.Exception">An exception occurred in the <see cref="E:System.Configuration.Install.Installer.BeforeInstall" /> event handler of one of the installers in the collection.-or- An exception occurred in the <see cref="E:System.Configuration.Install.Installer.AfterInstall" /> event handler of one of the installers in the collection. </exception>
public virtual void Install(IDictionary stateSaver)
{
if (stateSaver == null)
{
throw new ArgumentException(Res.GetString("InstallNullParameter", "stateSaver"));
}
try
{
OnBeforeInstall(stateSaver);
}
catch (Exception ex)
{
WriteEventHandlerError(Res.GetString("InstallSeverityError"), "OnBeforeInstall", ex);
throw new InvalidOperationException(Res.GetString("InstallEventException", "OnBeforeInstall", GetType().FullName), ex);
}
var num = -1;
var arrayList = new ArrayList();
try
{
for (var i = 0; i < Installers.Count; i++)
{
Installers[i].Context = Context;
}
for (var j = 0; j < Installers.Count; j++)
{
var installer = Installers[j];
IDictionary dictionary = new Hashtable();
try
{
num = j;
installer.Install(dictionary);
}
finally
{
arrayList.Add(dictionary);
}
}
}
finally
{
stateSaver.Add("_reserved_lastInstallerAttempted", num);
stateSaver.Add("_reserved_nestedSavedStates", arrayList.ToArray(typeof(IDictionary)));
}
try
{
OnAfterInstall(stateSaver);
}
catch (Exception ex2)
{
WriteEventHandlerError(Res.GetString("InstallSeverityError"), "OnAfterInstall", ex2);
throw new InvalidOperationException(Res.GetString("InstallEventException", "OnAfterInstall", GetType().FullName), ex2);
}
}
internal static void LogException(Exception e, InstallContext context)
{
var flag = true;
while (e != null)
{
if (flag)
{
context.LogMessage(e.GetType().FullName + ": " + e.Message);
flag = false;
}
else
{
context.LogMessage(Res.GetString("InstallLogInner", e.GetType().FullName, e.Message));
}
if (context.IsParameterTrue("showcallstack"))
{
context.LogMessage(e.StackTrace);
}
e = e.InnerException;
}
}
private bool IsWrappedException(Exception e)
{
if (e is InstallException && e.Source == "WrappedExceptionSource")
{
return e.TargetSite.ReflectedType == typeof(Installer);
}
return false;
}
/// <summary>Raises the <see cref="E:System.Configuration.Install.Installer.Committed" /> event.</summary>
/// <param name="savedState">An <see cref="T:System.Collections.IDictionary" /> that contains the state of the computer after all the installers in the <see cref="P:System.Configuration.Install.Installer.Installers" /> property run. </param>
protected virtual void OnCommitted(IDictionary savedState)
{
_afterCommitHandler?.Invoke(this, new InstallEventArgs(savedState));
}
/// <summary>Raises the <see cref="E:System.Configuration.Install.Installer.AfterInstall" /> event.</summary>
/// <param name="savedState">An <see cref="T:System.Collections.IDictionary" /> that contains the state of the computer after all the installers contained in the <see cref="P:System.Configuration.Install.Installer.Installers" /> property have completed their installations. </param>
protected virtual void OnAfterInstall(IDictionary savedState)
{
_afterInstallHandler?.Invoke(this, new InstallEventArgs(savedState));
}
/// <summary>Raises the <see cref="E:System.Configuration.Install.Installer.AfterRollback" /> event.</summary>
/// <param name="savedState">An <see cref="T:System.Collections.IDictionary" /> that contains the state of the computer after the installers contained in the <see cref="P:System.Configuration.Install.Installer.Installers" /> property are rolled back. </param>
protected virtual void OnAfterRollback(IDictionary savedState)
{
_afterRollbackHandler?.Invoke(this, new InstallEventArgs(savedState));
}
/// <summary>Raises the <see cref="E:System.Configuration.Install.Installer.AfterUninstall" /> event.</summary>
/// <param name="savedState">An <see cref="T:System.Collections.IDictionary" /> that contains the state of the computer after all the installers contained in the <see cref="P:System.Configuration.Install.Installer.Installers" /> property are uninstalled. </param>
protected virtual void OnAfterUninstall(IDictionary savedState)
{
_afterUninstallHandler?.Invoke(this, new InstallEventArgs(savedState));
}
/// <summary>Raises the <see cref="E:System.Configuration.Install.Installer.Committing" /> event.</summary>
/// <param name="savedState">An <see cref="T:System.Collections.IDictionary" /> that contains the state of the computer before the installers in the <see cref="P:System.Configuration.Install.Installer.Installers" /> property are committed. </param>
protected virtual void OnCommitting(IDictionary savedState)
{
_beforeCommitHandler?.Invoke(this, new InstallEventArgs(savedState));
}
/// <summary>Raises the <see cref="E:System.Configuration.Install.Installer.BeforeInstall" /> event.</summary>
/// <param name="savedState">An <see cref="T:System.Collections.IDictionary" /> that contains the state of the computer before the installers in the <see cref="P:System.Configuration.Install.Installer.Installers" /> property are installed. This <see cref="T:System.Collections.IDictionary" /> object should be empty at this point. </param>
protected virtual void OnBeforeInstall(IDictionary savedState)
{
_beforeInstallHandler?.Invoke(this, new InstallEventArgs(savedState));
}
/// <summary>Raises the <see cref="E:System.Configuration.Install.Installer.BeforeRollback" /> event.</summary>
/// <param name="savedState">An <see cref="T:System.Collections.IDictionary" /> that contains the state of the computer before the installers in the <see cref="P:System.Configuration.Install.Installer.Installers" /> property are rolled back. </param>
protected virtual void OnBeforeRollback(IDictionary savedState)
{
_beforeRollbackHandler?.Invoke(this, new InstallEventArgs(savedState));
}
/// <summary>Raises the <see cref="E:System.Configuration.Install.Installer.BeforeUninstall" /> event.</summary>
/// <param name="savedState">An <see cref="T:System.Collections.IDictionary" /> that contains the state of the computer before the installers in the <see cref="P:System.Configuration.Install.Installer.Installers" /> property uninstall their installations. </param>
protected virtual void OnBeforeUninstall(IDictionary savedState)
{
_beforeUninstallHandler?.Invoke(this, new InstallEventArgs(savedState));
}
/// <summary>When overridden in a derived class, restores the pre-installation state of the computer.</summary>
/// <param name="savedState">An <see cref="T:System.Collections.IDictionary" /> that contains the pre-installation state of the computer. </param>
/// <exception cref="T:System.ArgumentException">The <paramref name="savedState" /> parameter is null.-or- The saved-state <see cref="T:System.Collections.IDictionary" /> might have been corrupted. </exception>
/// <exception cref="T:System.Configuration.Install.InstallException">An exception occurred during the <see cref="M:System.Configuration.Install.Installer.Rollback(System.Collections.IDictionary)" /> phase of the installation. This exception is ignored and the rollback continues. However, the computer might not be fully reverted to its initial state after the rollback completes. </exception>
public virtual void Rollback(IDictionary savedState)
{
if (savedState == null)
{
throw new ArgumentException(Res.GetString("InstallNullParameter", "savedState"));
}
if (savedState["_reserved_lastInstallerAttempted"] == null || savedState["_reserved_nestedSavedStates"] == null)
{
throw new ArgumentException(Res.GetString("InstallDictionaryMissingValues", "savedState"));
}
Exception ex = null;
try
{
OnBeforeRollback(savedState);
}
catch (Exception ex2)
{
WriteEventHandlerError(Res.GetString("InstallSeverityWarning"), "OnBeforeRollback", ex2);
Context.LogMessage(Res.GetString("InstallRollbackException"));
ex = ex2;
}
var num = Convert.ToInt32(savedState["_reserved_lastInstallerAttempted"]);
var array = ToDictionaries(savedState["_reserved_nestedSavedStates"]);
if (num + 1 != array.Length || num >= Installers.Count)
{
throw new ArgumentException(Res.GetString("InstallDictionaryCorrupted", "savedState"));
}
for (var num2 = Installers.Count - 1; num2 >= 0; num2--)
{
Installers[num2].Context = Context;
}
for (var num3 = num; num3 >= 0; num3--)
{
try
{
Installers[num3].Rollback(array[num3]);
}
catch (Exception ex3)
{
if (!IsWrappedException(ex3))
{
Context.LogMessage(Res.GetString("InstallLogRollbackException", Installers[num3].ToString()));
LogException(ex3, Context);
Context.LogMessage(Res.GetString("InstallRollbackException"));
}
ex = ex3;
}
}
try
{
OnAfterRollback(savedState);
}
catch (Exception ex4)
{
WriteEventHandlerError(Res.GetString("InstallSeverityWarning"), "OnAfterRollback", ex4);
Context.LogMessage(Res.GetString("InstallRollbackException"));
ex = ex4;
}
if (ex != null)
{
var ex5 = ex;
if (!IsWrappedException(ex))
{
ex5 = new InstallException(Res.GetString("InstallRollbackException"), ex);
ex5.Source = "WrappedExceptionSource";
}
throw ex5;
}
}
/// <summary>When overridden in a derived class, removes an installation.</summary>
/// <param name="savedState">An <see cref="T:System.Collections.IDictionary" /> that contains the state of the computer after the installation was complete. </param>
/// <exception cref="T:System.ArgumentException">The saved-state <see cref="T:System.Collections.IDictionary" /> might have been corrupted. </exception>
/// <exception cref="T:System.Configuration.Install.InstallException">An exception occurred while uninstalling. This exception is ignored and the uninstall continues. However, the application might not be fully uninstalled after the uninstallation completes. </exception>
public virtual void Uninstall(IDictionary savedState)
{
Exception ex = null;
try
{
OnBeforeUninstall(savedState);
}
catch (Exception ex2)
{
WriteEventHandlerError(Res.GetString("InstallSeverityWarning"), "OnBeforeUninstall", ex2);
Context.LogMessage(Res.GetString("InstallUninstallException"));
ex = ex2;
}
IDictionary[] array;
if (savedState != null)
{
array = ToDictionaries(savedState["_reserved_nestedSavedStates"]);
if (array == null || array.Length != Installers.Count)
{
throw new ArgumentException(Res.GetString("InstallDictionaryCorrupted", "savedState"));
}
}
else
{
array = new IDictionary[Installers.Count];
}
for (var num = Installers.Count - 1; num >= 0; num--)
{
Installers[num].Context = Context;
}
for (var num2 = Installers.Count - 1; num2 >= 0; num2--)
{
try
{
Installers[num2].Uninstall(array[num2]);
}
catch (Exception ex3)
{
if (!IsWrappedException(ex3))
{
Context.LogMessage(Res.GetString("InstallLogUninstallException", Installers[num2].ToString()));
LogException(ex3, Context);
Context.LogMessage(Res.GetString("InstallUninstallException"));
}
ex = ex3;
}
}
try
{
OnAfterUninstall(savedState);
}
catch (Exception ex4)
{
WriteEventHandlerError(Res.GetString("InstallSeverityWarning"), "OnAfterUninstall", ex4);
Context.LogMessage(Res.GetString("InstallUninstallException"));
ex = ex4;
}
if (ex != null)
{
var ex5 = ex;
if (!IsWrappedException(ex))
{
ex5 = new InstallException(Res.GetString("InstallUninstallException"), ex);
ex5.Source = "WrappedExceptionSource";
}
throw ex5;
}
}
private static IDictionary[] ToDictionaries(object savedState)
{
if (savedState is JArray array)
{
return array.Select(_ => _.ToObject<IDictionary>()).ToArray();
}
return (IDictionary[])savedState;
}
private void WriteEventHandlerError(string severity, string eventName, Exception e)
{
Context.LogMessage(Res.GetString("InstallLogError", severity, eventName, GetType().FullName));
LogException(e, Context);
}
}
}