Skip to content

Commit 227eff4

Browse files
committed
fix abstract classes and properly dispose unused processes in single instance
1 parent 411327b commit 227eff4

8 files changed

Lines changed: 69 additions & 51 deletions

File tree

FlashpointSecurePlayer/DownloadsBefore.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ namespace FlashpointSecurePlayer {
1616
public class DownloadsBefore : Modifications {
1717
public DownloadsBefore(EventHandler importStart, EventHandler importStop) : base(importStart, importStop) { }
1818

19-
private void Activate() { }
20-
2119
public async Task ActivateAsync(string templateName, List<string> downloadsBeforeNames) {
2220
base.Activate(templateName);
21+
2322
//ModificationsElement modificationsElement = GetModificationsElement(true, Name);
2423

2524
// purpose of the following code is to asynchronously

FlashpointSecurePlayer/EnvironmentVariables.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
namespace FlashpointSecurePlayer {
1818
public class EnvironmentVariables : Modifications {
1919
private const string __COMPAT_LAYER = nameof(__COMPAT_LAYER);
20+
2021
private IList<string> UnmodifiableComparableNames { get; } = new List<string> { FP_STARTUP_PATH, FP_HTDOCS_FILE }.AsReadOnly();
21-
private readonly object activationLock = new object();
22-
private readonly object deactivationLock = new object();
2322

2423
public EnvironmentVariables(EventHandler importStart, EventHandler importStop) : base(importStart, importStop) { }
2524

@@ -79,7 +78,7 @@ private string GetValue(EnvironmentVariablesElement environmentVariablesElement)
7978
return value;
8079
}
8180

82-
new public void Activate(string templateName) {
81+
public override void Activate(string templateName) {
8382
lock (activationLock) {
8483
base.Activate(templateName);
8584

@@ -220,11 +219,12 @@ private string GetValue(EnvironmentVariablesElement environmentVariablesElement)
220219
}
221220
}
222221
}
223-
224-
public void Deactivate(MODIFICATIONS_REVERT_METHOD modificationsRevertMethod = MODIFICATIONS_REVERT_METHOD.CRASH_RECOVERY) {
222+
223+
public void Deactivate(MODIFICATIONS_REVERT_METHOD modificationsRevertMethod) {
225224
lock (deactivationLock) {
226225
// do the reverse of activation because we can
227226
base.Deactivate();
227+
228228
TemplateElement activeTemplateElement = GetActiveTemplateElement(false);
229229

230230
// if the activation backup doesn't exist, we don't need to do stuff
@@ -357,5 +357,9 @@ public void Deactivate(MODIFICATIONS_REVERT_METHOD modificationsRevertMethod = M
357357
}
358358
}
359359
}
360+
361+
public override void Deactivate() {
362+
Deactivate(MODIFICATIONS_REVERT_METHOD.CRASH_RECOVERY);
363+
}
360364
}
361365
}

FlashpointSecurePlayer/Modifications.cs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public Modifications(EventHandler importStart, EventHandler importStop) {
2727
Deactivate();
2828
}
2929

30-
protected virtual void OnImportStart(EventArgs e) {
30+
private void OnImportStart(EventArgs e) {
3131
EventHandler eventHandler = importStart;
3232

3333
if (eventHandler == null) {
@@ -37,7 +37,7 @@ protected virtual void OnImportStart(EventArgs e) {
3737
eventHandler(this, e);
3838
}
3939

40-
protected virtual void OnImportStop(EventArgs e) {
40+
private void OnImportStop(EventArgs e) {
4141
EventHandler eventHandler = importStop;
4242

4343
if (eventHandler == null) {
@@ -47,20 +47,25 @@ protected virtual void OnImportStop(EventArgs e) {
4747
eventHandler(this, e);
4848
}
4949

50+
private readonly object importStartedLock = new object();
5051
private bool importStarted = false;
5152

5253
protected bool ImportStarted {
5354
get {
54-
return importStarted;
55+
lock (importStartedLock) {
56+
return importStarted;
57+
}
5558
}
5659

5760
set {
58-
importStarted = value;
59-
60-
if (importStarted) {
61-
OnImportStart(EventArgs.Empty);
62-
} else {
63-
OnImportStop(EventArgs.Empty);
61+
lock (importStartedLock) {
62+
importStarted = value;
63+
64+
if (importStarted) {
65+
OnImportStart(EventArgs.Empty);
66+
} else {
67+
OnImportStop(EventArgs.Empty);
68+
}
6469
}
6570
}
6671
}
@@ -84,7 +89,7 @@ protected bool ImportPaused {
8489

8590
protected string TemplateName { get; set; } = String.Empty;
8691

87-
protected void StartImport(string templateName) {
92+
public virtual void StartImport(string templateName) {
8893
if (ImportStarted) {
8994
throw new InvalidOperationException("Cannot Start Import when the Import is in progress.");
9095
}
@@ -108,7 +113,7 @@ protected void StartImport(string templateName) {
108113
}
109114

110115
// async for inheritence reasons
111-
protected void StopImport() {
116+
public virtual void StopImport() {
112117
if (!ImportStarted) {
113118
throw new InvalidOperationException("Cannot Stop Import when the Import has not started.");
114119
}
@@ -125,7 +130,9 @@ protected void StopImport() {
125130
*/
126131
}
127132

128-
public void Activate(string templateName) {
133+
protected readonly object activationLock = new object();
134+
135+
public virtual void Activate(string templateName) {
129136
if (ImportStarted) {
130137
throw new InvalidOperationException("Cannot Activate when the Import is in progress.");
131138
}
@@ -140,7 +147,9 @@ public void Activate(string templateName) {
140147
//SetFlashpointSecurePlayerSection();
141148
}
142149

143-
public void Deactivate() {
150+
protected readonly object deactivationLock = new object();
151+
152+
public virtual void Deactivate() {
144153
if (ImportStarted) {
145154
throw new InvalidOperationException("Cannot Deactivate when the Import is in progress.");
146155
}

FlashpointSecurePlayer/OldCPUSimulator.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public bool TestRunningWithOldCPUSimulator() {
5151
}
5252

5353
public void Activate(string templateName, ref ProcessStartInfo softwareProcessStartInfo, out bool softwareIsOldCPUSimulator) {
54-
OldCPUSimulatorElement oldCPUSimulatorElement = null;
5554
softwareIsOldCPUSimulator = false;
5655

5756
base.Activate(templateName);
@@ -74,7 +73,7 @@ public void Activate(string templateName, ref ProcessStartInfo softwareProcessSt
7473
return;
7574
}
7675

77-
oldCPUSimulatorElement = modificationsElement.OldCPUSimulator;
76+
OldCPUSimulatorElement oldCPUSimulatorElement = modificationsElement.OldCPUSimulator;
7877

7978
if (!oldCPUSimulatorElement.ElementInformation.IsPresent) {
8079
return;

FlashpointSecurePlayer/RegistryStates.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@ public enum TYPE {
5858
private const string IMPORT_RESUME = "FLASHPOINTSECUREPLAYERREGISTRYSTATEIMPORTRESUME";
5959
private const string IMPORT_PAUSE = "FLASHPOINTSECUREPLAYERREGISTRYSTATEIMPORTPAUSE";
6060

61-
private readonly object activationLock = new object();
62-
private readonly object deactivationLock = new object();
63-
6461
private string fullPath = null;
6562
private PathNames pathNames = null;
6663
private EventWaitHandle resumeEventWaitHandle = new ManualResetEvent(false);
@@ -937,7 +934,7 @@ private async Task StopImportAsync(bool sync) {
937934
}
938935
}
939936

940-
new public void StopImport() {
937+
public override void StopImport() {
941938
// do not await this, bool hack
942939
#pragma warning disable CS4014
943940
StopImportAsync(true);
@@ -948,7 +945,7 @@ public async Task StopImportAsync() {
948945
await StopImportAsync(false).ConfigureAwait(false);
949946
}
950947

951-
new public void Activate(string templateName) {
948+
public override void Activate(string templateName) {
952949
lock (activationLock) {
953950
base.Activate(templateName);
954951

@@ -1203,9 +1200,10 @@ public async Task StopImportAsync() {
12031200
}
12041201
}
12051202

1206-
public void Deactivate(MODIFICATIONS_REVERT_METHOD modificationsRevertMethod = MODIFICATIONS_REVERT_METHOD.CRASH_RECOVERY) {
1203+
public void Deactivate(MODIFICATIONS_REVERT_METHOD modificationsRevertMethod) {
12071204
lock (deactivationLock) {
12081205
base.Deactivate();
1206+
12091207
TemplateElement activeTemplateElement = GetActiveTemplateElement(false);
12101208

12111209
// if the activation state doesn't exist, we don't need to do stuff
@@ -1507,6 +1505,10 @@ public void Deactivate(MODIFICATIONS_REVERT_METHOD modificationsRevertMethod = M
15071505
}
15081506
}
15091507

1508+
public override void Deactivate() {
1509+
Deactivate(MODIFICATIONS_REVERT_METHOD.CRASH_RECOVERY);
1510+
}
1511+
15101512
private void QueueModification(ulong safeKeyHandle, DateTime timeStamp, RegistryStateElement registryStateElement) {
15111513
if (queuedModifications == null) {
15121514
queuedModifications = new Dictionary<ulong, SortedList<DateTime, List<RegistryStateElement>>>();

FlashpointSecurePlayer/RunAsAdministrator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public RunAsAdministrator(EventHandler importStart, EventHandler importStop) : b
1414

1515
public void Activate(string templateName, bool runAsAdministrator) {
1616
base.Activate(templateName);
17+
1718
/*
1819
ModificationsElement modificationsElement = GetModificationsElement(true, Name);
1920

FlashpointSecurePlayer/Shared.cs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ public ConfigurationElement Get(int index) {
984984
return BaseGet(index) as ConfigurationElement;
985985
}
986986

987-
public ConfigurationElement Get(string name) {
987+
public virtual ConfigurationElement Get(string name) {
988988
//BaseAdd(modificationsElement);
989989
return BaseGet(name) as ConfigurationElement;
990990
}
@@ -994,7 +994,7 @@ public void Set(ConfigurationElement configurationElement) {
994994
BaseAdd(configurationElement);
995995
}
996996

997-
public void Remove(string name) {
997+
public virtual void Remove(string name) {
998998
if (BaseGet(name) != null) {
999999
BaseRemove(name);
10001000
}
@@ -1401,14 +1401,12 @@ protected override ConfigurationElement CreateNewElement() {
14011401
return new EnvironmentVariablesElement();
14021402
}
14031403

1404-
new public ConfigurationElement Get(string name) {
1405-
name = name.ToUpperInvariant();
1406-
return base.Get(name);
1404+
public override ConfigurationElement Get(string name) {
1405+
return base.Get(name.ToUpperInvariant());
14071406
}
14081407

1409-
new public void Remove(string name) {
1410-
name = name.ToUpperInvariant();
1411-
base.Remove(name);
1408+
public override void Remove(string name) {
1409+
base.Remove(name.ToUpperInvariant());
14121410
}
14131411
}
14141412

@@ -1636,7 +1634,6 @@ public string Name {
16361634
if (!String.IsNullOrEmpty(valueName)) {
16371635
valueName = valueName.ToUpperInvariant();
16381636
}
1639-
16401637
return keyName + "\\" + valueName;
16411638
}
16421639
}
@@ -1657,14 +1654,12 @@ protected override ConfigurationElement CreateNewElement() {
16571654
return new RegistryStateElement();
16581655
}
16591656

1660-
new public ConfigurationElement Get(string name) {
1661-
name = name.ToUpperInvariant();
1662-
return base.Get(name);
1657+
public override ConfigurationElement Get(string name) {
1658+
return base.Get(name.ToUpperInvariant());
16631659
}
16641660

1665-
new public void Remove(string name) {
1666-
name = name.ToUpperInvariant();
1667-
base.Remove(name);
1661+
public override void Remove(string name) {
1662+
base.Remove(name.ToUpperInvariant());
16681663
}
16691664

16701665
public BINARY_TYPE BinaryType {
@@ -1874,13 +1869,11 @@ protected override ConfigurationElement CreateNewElement() {
18741869
}
18751870

18761871
new public TemplateElement Get(string name) {
1877-
name = name.ToLowerInvariant();
1878-
return base.Get(name) as TemplateElement;
1872+
return base.Get(name.ToLowerInvariant()) as TemplateElement;
18791873
}
18801874

1881-
new public void Remove(string name) {
1882-
name = name.ToLowerInvariant();
1883-
base.Remove(name);
1875+
public override void Remove(string name) {
1876+
base.Remove(name.ToLowerInvariant());
18841877
}
18851878
}
18861879

FlashpointSecurePlayer/SingleInstance.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public SingleInstance(EventHandler importStart, EventHandler importStop) : base(
5555
return ShowClosableMessageBox(new Task[] { task }, text, caption, messageBoxButtons, messageBoxIcon);
5656
}
5757

58-
new public void Activate(string templateName) {
58+
public override void Activate(string templateName) {
5959
base.Activate(templateName);
6060

6161
if (String.IsNullOrEmpty(TemplateName)) {
@@ -103,6 +103,7 @@ public SingleInstance(EventHandler importStart, EventHandler importStop) : base(
103103
string processName = null;
104104
// GetProcessesByName can't have extension (stupidly)
105105
string activeProcessName = Path.GetFileNameWithoutExtension(executable);
106+
bool clear = false;
106107

107108
do {
108109
// the strict list is the one which will be checked against for real
@@ -119,13 +120,23 @@ public SingleInstance(EventHandler importStart, EventHandler importStop) : base(
119120
if (processesByName[i] != null) {
120121
processName = GetProcessName(processesByName[i]).ToString();
121122

123+
clear = false;
124+
122125
try {
123-
if (ComparePaths(executable, processName)) {
124-
processesByNameStrict.Push(processesByName[i]);
126+
if (!ComparePaths(executable, processName)) {
127+
clear = true;
125128
}
126129
} catch {
127130
// fail silently
128131
}
132+
133+
if (clear) {
134+
processesByName[i].Dispose();
135+
} else {
136+
processesByNameStrict.Push(processesByName[i]);
137+
}
138+
139+
processesByName[i] = null;
129140
}
130141
}
131142
} else {

0 commit comments

Comments
 (0)