Skip to content

Commit b53e81f

Browse files
committed
move null checks into using blocks
1 parent 481e2d6 commit b53e81f

6 files changed

Lines changed: 68 additions & 115 deletions

File tree

FlashpointSecurePlayer/FlashpointSecurePlayerGUI.cs

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public FlashpointSecurePlayerGUI() {
7070
// multiple instances open, blow up immediately
7171
applicationMutex.Close();
7272
applicationMutex = null;
73-
throw new InvalidOperationException("You must not run multiple instances of Flashpoint Secure Player.");
73+
throw new InvalidOperationException("You cannot run multiple instances of Flashpoint Secure Player.");
7474
}
7575
} catch (InvalidOperationException ex) {
7676
LogExceptionToLauncher(ex);
@@ -144,26 +144,15 @@ private void ShowNoGameSelected() {
144144
// to reduce the amount of stupid in the #help-me-please channel
145145
//ShowError(Properties.Resources.GameNotCuratedCorrectly);
146146
StringBuilder text = new StringBuilder(Properties.Resources.NoGameSelected);
147-
148-
Process parentProcess = null;
149-
150-
try {
151-
parentProcess = GetParentProcess();
152-
} catch {
153-
// fail silently
154-
}
155147

156148
string parentProcessFileName = null;
157149

158-
if (parentProcess != null) {
159-
try {
150+
try {
151+
using (Process parentProcess = GetParentProcess()) {
160152
parentProcessFileName = Path.GetFileName(GetProcessName(parentProcess).ToString());
161-
} catch {
162-
// fail silently
163-
} finally {
164-
parentProcess.Dispose();
165-
parentProcess = null;
166153
}
154+
} catch {
155+
// fail silently
167156
}
168157

169158
if (parentProcessFileName == null
@@ -283,30 +272,21 @@ private void AskLaunchWithOldCPUSimulator() {
283272
if (!modificationsElement.ElementInformation.IsPresent) {
284273
return;
285274
}
286-
287-
Process parentProcess = null;
288-
289-
try {
290-
parentProcess = GetParentProcess();
291-
} catch {
292-
// fail silently
293-
}
294275

295276
string parentProcessFileName = null;
296277

297-
if (parentProcess != null) {
298-
try {
299-
parentProcessFileName = Path.GetFileName(GetProcessName(parentProcess).ToString());
300-
} catch (Exception ex) {
301-
LogExceptionToLauncher(ex);
302-
ProgressManager.ShowError();
303-
MessageBox.Show(Properties.Resources.ProcessUnableToStart, Properties.Resources.FlashpointSecurePlayer, MessageBoxButtons.OK, MessageBoxIcon.Error);
304-
Application.Exit();
305-
throw new InvalidModificationException("The Modification does not work unless run with Old CPU Simulator which failed to get the Parent Process File Name.");
306-
} finally {
307-
parentProcess.Dispose();
308-
parentProcess = null;
278+
try {
279+
using (Process parentProcess = GetParentProcess()) {
280+
if (parentProcess != null) {
281+
parentProcessFileName = Path.GetFileName(GetProcessName(parentProcess).ToString());
282+
}
309283
}
284+
} catch (Exception ex) {
285+
LogExceptionToLauncher(ex);
286+
ProgressManager.ShowError();
287+
MessageBox.Show(Properties.Resources.ProcessUnableToStart, Properties.Resources.FlashpointSecurePlayer, MessageBoxButtons.OK, MessageBoxIcon.Error);
288+
Application.Exit();
289+
throw new InvalidModificationException("The Modification does not work unless run with Old CPU Simulator which failed to get the Parent Process File Name.");
310290
}
311291

312292
if (parentProcessFileName != null) {
@@ -680,11 +660,11 @@ private void ActivateMode(TemplateElement templateElement, ErrorDelegate errorDe
680660
throw new InvalidModeException("The Mode failed to create a Job Object.");
681661
}
682662

683-
if (softwareProcess == null) {
684-
throw new InvalidModeException("The Mode failed to create the Process.");
685-
}
686-
687663
using (softwareProcess) {
664+
if (softwareProcess == null) {
665+
throw new InvalidModeException("The Mode failed to create the Process.");
666+
}
667+
688668
Hide();
689669

690670
softwareProcess.WaitForExit();

FlashpointSecurePlayer/OldCPUSimulator.cs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,17 @@ public bool TestRunningWithOldCPUSimulator() {
2121
// now, we might already be running under Old CPU Simulator
2222
// we don't want to start a new instance in that case
2323
// the user has manually started Old CPU Simulator already
24-
Process parentProcess = null;
25-
26-
try {
27-
parentProcess = GetParentProcess();
28-
} catch {
29-
// fail silently
30-
}
31-
3224
string parentProcessFileName = null;
3325

34-
if (parentProcess != null) {
35-
try {
36-
parentProcessFileName = Path.GetFileName(GetProcessName(parentProcess).ToString());
37-
} catch (Exception ex) {
38-
LogExceptionToLauncher(ex);
39-
throw new InvalidOldCPUSimulatorException("Failed to get the Parent Process File Name.");
40-
} finally {
41-
parentProcess.Dispose();
42-
parentProcess = null;
26+
try {
27+
using (Process parentProcess = GetParentProcess()) {
28+
if (parentProcess != null) {
29+
parentProcessFileName = Path.GetFileName(GetProcessName(parentProcess).ToString());
30+
}
4331
}
32+
} catch (Exception ex) {
33+
LogExceptionToLauncher(ex);
34+
throw new InvalidOldCPUSimulatorException("Failed to get the Parent Process File Name.");
4435
}
4536

4637
if (parentProcessFileName != null) {

FlashpointSecurePlayer/Properties/Resources.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FlashpointSecurePlayer/Properties/Resources.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ If you choose No, the application will exit.</value>
194194
<value>The CPU's clock speed could not be determined.</value>
195195
</data>
196196
<data name="OCS_NoMultipleInstances" xml:space="preserve">
197-
<value>You must not run multiple instances of Old CPU Simulator.</value>
197+
<value>You cannot run multiple instances of Old CPU Simulator.</value>
198198
</data>
199199
<data name="OCS_OldCPUNotSimulated" xml:space="preserve">
200200
<value>The old CPU could not be simulated.</value>

FlashpointSecurePlayer/RegistryStates.cs

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -348,26 +348,21 @@ private RegistryKey CreateKeyInRegistryView(string keyName, RegistryKeyPermissio
348348
}
349349

350350
private void SetKeyInRegistryView(string keyName, RegistryView registryView) {
351-
RegistryKey registryKey = CreateKeyInRegistryView(keyName, RegistryKeyPermissionCheck.Default, registryView);
352-
353-
if (registryKey == null) {
354-
// key is invalid
355-
throw new ArgumentException("The key \"" + keyName + "\" is invalid.");
351+
using (RegistryKey registryKey = CreateKeyInRegistryView(keyName, RegistryKeyPermissionCheck.Default, registryView)) {
352+
if (registryKey == null) {
353+
// key is invalid
354+
throw new ArgumentException("The key \"" + keyName + "\" is invalid.");
355+
}
356356
}
357-
358-
registryKey.Dispose();
359-
registryKey = null;
360357
}
361358

362359
private void DeleteKeyInRegistryView(string keyName, RegistryView registryView) {
363-
RegistryKey registryKey = OpenBaseKeyInRegistryView(keyName, registryView);
364-
365-
if (registryKey == null) {
366-
// base key does not exist (good!)
367-
return;
368-
}
360+
using (RegistryKey registryKey = OpenBaseKeyInRegistryView(keyName, registryView)) {
361+
if (registryKey == null) {
362+
// base key does not exist (good!)
363+
return;
364+
}
369365

370-
using (registryKey) {
371366
int subKeyNameIndex = keyName.IndexOf("\\") + 1;
372367

373368
if (subKeyNameIndex > 0) {
@@ -377,17 +372,13 @@ private void DeleteKeyInRegistryView(string keyName, RegistryView registryView)
377372
}
378373

379374
private object GetValueInRegistryView(string keyName, string valueName, RegistryView registryView) {
380-
RegistryKey registryKey = OpenKeyInRegistryView(keyName, false, registryView);
381-
382-
if (registryKey == null) {
383-
// key does not exist
384-
return null;
385-
}
386-
387-
object value = null;
375+
using (RegistryKey registryKey = OpenKeyInRegistryView(keyName, false, registryView)) {
376+
if (registryKey == null) {
377+
// key does not exist
378+
return null;
379+
}
388380

389-
using (registryKey) {
390-
value = registryKey.GetValue(valueName, null, RegistryValueOptions.DoNotExpandEnvironmentNames);
381+
object value = registryKey.GetValue(valueName, null, RegistryValueOptions.DoNotExpandEnvironmentNames);
391382
RegistryValueKind? valueKind = null;
392383

393384
try {
@@ -422,14 +413,12 @@ private object GetValueInRegistryView(string keyName, string valueName, Registry
422413
}
423414

424415
private void SetValueInRegistryView(string keyName, string valueName, object value, RegistryValueKind valueKind, RegistryView registryView) {
425-
RegistryKey registryKey = CreateKeyInRegistryView(keyName, RegistryKeyPermissionCheck.ReadWriteSubTree, registryView);
426-
427-
if (registryKey == null) {
428-
// key is invalid
429-
throw new ArgumentException("The key \"" + keyName + "\" is invalid.");
430-
}
416+
using (RegistryKey registryKey = CreateKeyInRegistryView(keyName, RegistryKeyPermissionCheck.ReadWriteSubTree, registryView)) {
417+
if (registryKey == null) {
418+
// key is invalid
419+
throw new ArgumentException("The key \"" + keyName + "\" is invalid.");
420+
}
431421

432-
using (registryKey) {
433422
switch (valueKind) {
434423
case RegistryValueKind.Binary:
435424
if (value is string binaryValue) {
@@ -448,27 +437,23 @@ private void SetValueInRegistryView(string keyName, string valueName, object val
448437
}
449438

450439
private void DeleteValueInRegistryView(string keyName, string valueName, RegistryView registryView) {
451-
RegistryKey registryKey = OpenKeyInRegistryView(keyName, true, registryView);
452-
453-
if (registryKey == null) {
454-
// key does not exist (good!)
455-
return;
456-
}
440+
using (RegistryKey registryKey = OpenKeyInRegistryView(keyName, true, registryView)) {
441+
if (registryKey == null) {
442+
// key does not exist (good!)
443+
return;
444+
}
457445

458-
using (registryKey) {
459446
registryKey.DeleteValue(valueName);
460447
}
461448
}
462449

463450
private RegistryValueKind? GetValueKindInRegistryView(string keyName, string valueName, RegistryView registryView) {
464-
RegistryKey registryKey = OpenKeyInRegistryView(keyName, false, registryView);
465-
466-
if (registryKey == null) {
467-
// key does not exist
468-
return null;
469-
}
451+
using (RegistryKey registryKey = OpenKeyInRegistryView(keyName, false, registryView)) {
452+
if (registryKey == null) {
453+
// key does not exist
454+
return null;
455+
}
470456

471-
using (registryKey) {
472457
return registryKey.GetValueKind(valueName);
473458
}
474459
}

FlashpointSecurePlayer/SingleInstance.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ public SingleInstance(EventHandler importStart, EventHandler importStop) : base(
100100

101101
Process[] processesByName = null;
102102
Stack<Process> processesByNameStrict = null;
103-
Process processByNameStrict = null;
104103
string processName = null;
105104
// GetProcessesByName can't have extension (stupidly)
106105
string activeProcessName = Path.GetFileNameWithoutExtension(executable);
@@ -140,16 +139,14 @@ public SingleInstance(EventHandler importStart, EventHandler importStop) : base(
140139
if (processesByNameStrict.Any()) {
141140
DialogResult? dialogResult = ShowClosableMessageBox(Task.Run(delegate () {
142141
while (processesByNameStrict.Any()) {
143-
processByNameStrict = processesByNameStrict.Peek();
144-
145-
try {
146-
if (processByNameStrict != null) {
147-
processByNameStrict.WaitForExit();
148-
processByNameStrict.Dispose();
149-
processByNameStrict = null;
142+
using (Process processByNameStrict = processesByNameStrict.Peek()) {
143+
try {
144+
if (processByNameStrict != null) {
145+
processByNameStrict.WaitForExit();
146+
}
147+
} finally {
148+
processesByNameStrict.Pop();
150149
}
151-
} finally {
152-
processesByNameStrict.Pop();
153150
}
154151
}
155152
}), String.Format(Properties.Resources.ProcessCompatibilityConflict, activeProcessName), Properties.Resources.FlashpointSecurePlayer, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);

0 commit comments

Comments
 (0)