Skip to content

Commit 120fd91

Browse files
committed
fix: ensure the FP_PROXY and FP_PROXY_PORT environment variables do not conflict with the Environment Variables Modification
1 parent 3dd60d8 commit 120fd91

3 files changed

Lines changed: 163 additions & 63 deletions

File tree

FlashpointSecurePlayer/EnvironmentVariables.cs

Lines changed: 89 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,80 @@ private string GetComparableName(string name) {
3131
return comparableNameLength == -1 ? name : name.Substring(0, comparableNameLength);
3232
}
3333

34+
private string GetFlashpointProxyName(string name, out string comparableName) {
35+
comparableName = null;
36+
37+
if (name == null) {
38+
return name;
39+
}
40+
41+
comparableName = GetComparableName(name);
42+
43+
if (comparableName == null) {
44+
return name;
45+
}
46+
47+
if (comparableName.Equals(FlashpointProxy.FLASHPOINT_SECURE_PLAYER_PROXY, StringComparison.OrdinalIgnoreCase)) {
48+
comparableName = FlashpointProxy.FP_PROXY;
49+
} else if (comparableName.Equals(FlashpointProxy.FLASHPOINT_SECURE_PLAYER_PROXY_PORT, StringComparison.OrdinalIgnoreCase)) {
50+
comparableName = FlashpointProxy.FP_PROXY_PORT;
51+
}
52+
53+
try {
54+
if (comparableName.Equals(FlashpointProxy.FP_PROXY, StringComparison.OrdinalIgnoreCase)) {
55+
name = FlashpointProxy.FP_PROXY;
56+
57+
FlashpointProxy.GetPreferences(out bool proxy, out int port);
58+
59+
if (Environment.GetEnvironmentVariable(FlashpointProxy.FLASHPOINT_SECURE_PLAYER_PROXY, EnvironmentVariableTarget.Process) == null) {
60+
Environment.SetEnvironmentVariable(FlashpointProxy.FLASHPOINT_SECURE_PLAYER_PROXY, proxy ? "1" : "0", EnvironmentVariableTarget.Process);
61+
}
62+
} else if (comparableName.Equals(FlashpointProxy.FP_PROXY_PORT, StringComparison.OrdinalIgnoreCase)) {
63+
name = FlashpointProxy.FP_PROXY_PORT;
64+
65+
FlashpointProxy.GetPreferences(out bool proxy, out int port);
66+
67+
if (Environment.GetEnvironmentVariable(FlashpointProxy.FLASHPOINT_SECURE_PLAYER_PROXY_PORT, EnvironmentVariableTarget.Process) == null) {
68+
Environment.SetEnvironmentVariable(FlashpointProxy.FLASHPOINT_SECURE_PLAYER_PROXY_PORT, port.ToString(), EnvironmentVariableTarget.Process);
69+
}
70+
}
71+
} catch (SecurityException ex) {
72+
LogExceptionToLauncher(ex);
73+
throw new TaskRequiresElevationException("Getting the \"" + FlashpointProxy.FLASHPOINT_SECURE_PLAYER_PROXY + "\" Environment Variable requires elevation.");
74+
} catch (Exception ex) {
75+
LogExceptionToLauncher(ex);
76+
throw new InvalidEnvironmentVariablesException("Failed to get the \"" + FlashpointProxy.FLASHPOINT_SECURE_PLAYER_PROXY + "\" Environment Variable.");
77+
}
78+
return name;
79+
}
80+
3481
private string GetValue(EnvironmentVariablesElement environmentVariablesElement) {
3582
if (String.IsNullOrEmpty(environmentVariablesElement.Find)) {
3683
return environmentVariablesElement.Value;
3784
}
3885

39-
string comparableName = GetComparableName(environmentVariablesElement.Name);
86+
string value = null;
87+
88+
string name = GetFlashpointProxyName(environmentVariablesElement.Name, out string comparableName);
89+
90+
if (name == null) {
91+
return value;
92+
}
4093

4194
if (comparableName != null) {
4295
if (comparableName.Equals(__COMPAT_LAYER, StringComparison.OrdinalIgnoreCase)) {
4396
throw new InvalidEnvironmentVariablesException("Find and replace with the \"" + __COMPAT_LAYER + "\" Environment Variable is not supported.");
4497
}
4598
}
4699

47-
string value = null;
48-
49100
try {
50-
value = Environment.GetEnvironmentVariable(environmentVariablesElement.Name, EnvironmentVariableTarget.Process);
101+
value = Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process);
51102
} catch (SecurityException ex) {
52103
LogExceptionToLauncher(ex);
53-
throw new TaskRequiresElevationException("Getting the \"" + environmentVariablesElement.Name + "\" Environment Variable requires elevation.");
104+
throw new TaskRequiresElevationException("Getting the \"" + name + "\" Environment Variable requires elevation.");
54105
} catch (Exception ex) {
55106
LogExceptionToLauncher(ex);
56-
throw new InvalidEnvironmentVariablesException("Failed to get the \"" + environmentVariablesElement.Name + "\" Environment Variable.");
107+
throw new InvalidEnvironmentVariablesException("Failed to get the \"" + name + "\" Environment Variable.");
57108
}
58109

59110
if (value == null) {
@@ -109,6 +160,7 @@ public override void Activate(string templateName) {
109160
ModificationsElement activeModificationsElement = activeTemplateElement.Modifications;
110161

111162
// initialize variables
163+
string name = null;
112164
string comparableName = null;
113165
string value = null;
114166
List<string> values = null;
@@ -141,26 +193,30 @@ public override void Activate(string templateName) {
141193
throw new ConfigurationErrorsException("The Environment Variables Element (" + i + ") is null while creating the Active Environment Variables Element.");
142194
}
143195

144-
comparableName = GetComparableName(environmentVariablesElement.Name);
196+
name = GetFlashpointProxyName(environmentVariablesElement.Name, out comparableName);
197+
198+
if (name == null) {
199+
throw new InvalidEnvironmentVariablesException("The name is null while creating the Active Environment Variables Element.");
200+
}
145201

146202
if (comparableName != null) {
147203
if (UnmodifiableComparableNames.Contains(comparableName, StringComparer.OrdinalIgnoreCase)) {
148-
throw new InvalidEnvironmentVariablesException("The \"" + environmentVariablesElement.Name + "\" Environment Variable could not be modified while creating the Active Environment Variables Element.");
204+
throw new InvalidEnvironmentVariablesException("The \"" + name + "\" Environment Variable could not be modified while creating the Active Environment Variables Element.");
149205
}
150206
}
151207

152208
try {
153209
activeEnvironmentVariablesElement = new EnvironmentVariablesElement {
154-
Name = environmentVariablesElement.Name,
210+
Name = name,
155211
Find = environmentVariablesElement.Find,
156-
Value = Environment.GetEnvironmentVariable(environmentVariablesElement.Name, EnvironmentVariableTarget.Process)
212+
Value = Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process)
157213
};
158214
} catch (SecurityException ex) {
159215
LogExceptionToLauncher(ex);
160-
throw new TaskRequiresElevationException("Getting the \"" + environmentVariablesElement.Name + "\" Environment Variable requires elevation.");
216+
throw new TaskRequiresElevationException("Getting the \"" + name + "\" Environment Variable requires elevation.");
161217
} catch (Exception ex) {
162218
LogExceptionToLauncher(ex);
163-
throw new InvalidEnvironmentVariablesException("Failed to get the \"" + environmentVariablesElement.Name + "\" Environment Variable.");
219+
throw new InvalidEnvironmentVariablesException("Failed to get the \"" + name + "\" Environment Variable.");
164220
}
165221

166222
activeModificationsElement.EnvironmentVariables.Set(activeEnvironmentVariablesElement);
@@ -176,25 +232,29 @@ public override void Activate(string templateName) {
176232
if (environmentVariablesElement == null) {
177233
throw new ConfigurationErrorsException("The Environment Variables Element (" + i + ") is null.");
178234
}
235+
236+
name = GetFlashpointProxyName(environmentVariablesElement.Name, out comparableName);
179237

180-
comparableName = GetComparableName(environmentVariablesElement.Name);
238+
if (name == null) {
239+
throw new InvalidEnvironmentVariablesException("The name is null.");
240+
}
181241

182242
if (comparableName != null) {
183243
if (UnmodifiableComparableNames.Contains(comparableName, StringComparer.OrdinalIgnoreCase)) {
184-
throw new InvalidEnvironmentVariablesException("The \"" + environmentVariablesElement.Name + "\" Environment Variable could not be modified at this time.");
244+
throw new InvalidEnvironmentVariablesException("The \"" + name + "\" Environment Variable could not be modified at this time.");
185245
}
186246
}
187247

188248
value = GetValue(environmentVariablesElement);
189249

190250
try {
191-
Environment.SetEnvironmentVariable(environmentVariablesElement.Name, Environment.ExpandEnvironmentVariables(value), EnvironmentVariableTarget.Process);
251+
Environment.SetEnvironmentVariable(name, Environment.ExpandEnvironmentVariables(value), EnvironmentVariableTarget.Process);
192252
} catch (SecurityException ex) {
193253
LogExceptionToLauncher(ex);
194-
throw new TaskRequiresElevationException("Setting the \"" + environmentVariablesElement.Name + "\" Environment Variable requires elevation.");
254+
throw new TaskRequiresElevationException("Setting the \"" + name + "\" Environment Variable requires elevation.");
195255
} catch (Exception ex) {
196256
LogExceptionToLauncher(ex);
197-
throw new InvalidEnvironmentVariablesException("Failed to set the \"" + environmentVariablesElement.Name + "\" Environment Variable.");
257+
throw new InvalidEnvironmentVariablesException("Failed to set the \"" + name + "\" Environment Variable.");
198258
}
199259

200260
// now throw up a restart in Web Browser Mode for Compatibility Settings
@@ -272,6 +332,7 @@ public void Deactivate(MODIFICATIONS_REVERT_METHOD modificationsRevertMethod) {
272332
}
273333

274334
// initialize variables
335+
string name = null;
275336
string comparableName = null;
276337
string value = null;
277338
List<string> values = null;
@@ -306,11 +367,15 @@ public void Deactivate(MODIFICATIONS_REVERT_METHOD modificationsRevertMethod) {
306367
throw new InvalidEnvironmentVariablesException("The Environment Variable element (" + i + ") is null.");
307368
}
308369

309-
activeEnvironmentVariablesElement = activeModificationsElement.EnvironmentVariables.Get(environmentVariablesElement.Name) as EnvironmentVariablesElement;
370+
name = GetFlashpointProxyName(environmentVariablesElement.Name, out comparableName);
310371

311-
if (activeEnvironmentVariablesElement != null) {
312-
comparableName = GetComparableName(activeEnvironmentVariablesElement.Name);
372+
if (name == null) {
373+
throw new InvalidEnvironmentVariablesException("The name is null.");
374+
}
313375

376+
activeEnvironmentVariablesElement = activeModificationsElement.EnvironmentVariables.Get(name) as EnvironmentVariablesElement;
377+
378+
if (activeEnvironmentVariablesElement != null) {
314379
if (comparableName != null) {
315380
if (UnmodifiableComparableNames.Contains(comparableName, StringComparer.OrdinalIgnoreCase)) {
316381
throw new InvalidEnvironmentVariablesException("The \"" + activeEnvironmentVariablesElement.Name + "\" Environment Variable could not be modified at this time.");
@@ -325,10 +390,10 @@ public void Deactivate(MODIFICATIONS_REVERT_METHOD modificationsRevertMethod) {
325390
Environment.SetEnvironmentVariable(activeEnvironmentVariablesElement.Name, null, EnvironmentVariableTarget.Process);
326391
} catch (SecurityException ex) {
327392
LogExceptionToLauncher(ex);
328-
throw new TaskRequiresElevationException("Deleting the \"" + environmentVariablesElement.Name + "\" Environment Variable requires elevation.");
393+
throw new TaskRequiresElevationException("Deleting the \"" + name + "\" Environment Variable requires elevation.");
329394
} catch (Exception ex) {
330395
LogExceptionToLauncher(ex);
331-
throw new InvalidEnvironmentVariablesException("Failed to delete the \"" + environmentVariablesElement.Name + "\" Environment Variable.");
396+
throw new InvalidEnvironmentVariablesException("Failed to delete the \"" + name + "\" Environment Variable.");
332397
}
333398
} else {
334399
// don't reset Compatibility Settings if we're restarting for Web Browser Mode
@@ -340,10 +405,10 @@ public void Deactivate(MODIFICATIONS_REVERT_METHOD modificationsRevertMethod) {
340405
Environment.SetEnvironmentVariable(activeEnvironmentVariablesElement.Name, activeEnvironmentVariablesElement.Value, EnvironmentVariableTarget.Process);
341406
} catch (SecurityException ex) {
342407
LogExceptionToLauncher(ex);
343-
throw new TaskRequiresElevationException("Setting the \"" + environmentVariablesElement.Name + "\" Environment Variable requires elevation.");
408+
throw new TaskRequiresElevationException("Setting the \"" + name + "\" Environment Variable requires elevation.");
344409
} catch (Exception ex) {
345410
LogExceptionToLauncher(ex);
346-
throw new InvalidEnvironmentVariablesException("Failed to set the \"" + environmentVariablesElement.Name + "\" Environment Variable.");
411+
throw new InvalidEnvironmentVariablesException("Failed to set the \"" + name + "\" Environment Variable.");
347412
}
348413
}
349414
}

0 commit comments

Comments
 (0)