Skip to content

Commit d8444eb

Browse files
committed
standard error/output handling
1 parent 20b61e2 commit d8444eb

File tree

4 files changed

+77
-14
lines changed

4 files changed

+77
-14
lines changed

FlashpointSecurePlayer/FlashpointSecurePlayer.cs

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public partial class FlashpointSecurePlayer : Form {
3434
private string server = EMPTY_MODE_NAME;
3535
private string software = EMPTY_MODE_NAME;
3636
private ProcessStartInfo softwareProcessStartInfo = null;
37+
private bool softwareIsOldCPUSimulator = false;
3738

3839
private string ModificationsName { get; set; } = ACTIVE_EXE_CONFIGURATION_NAME;
3940
private bool RunAsAdministratorModification { get; set; } = false;
@@ -318,7 +319,7 @@ private async Task ActivateModificationsAsync(string commandLine, ErrorDelegate
318319

319320
if (modificationsElement.OldCPUSimulator.ElementInformation.IsPresent) {
320321
try {
321-
oldCPUSimulator.Activate(ModificationsName, ref server, ref software, ref softwareProcessStartInfo);
322+
oldCPUSimulator.Activate(ModificationsName, ref server, ref software, ref softwareProcessStartInfo, out softwareIsOldCPUSimulator);
322323
} catch (OldCPUSimulatorFailedException) {
323324
errorDelegate(Properties.Resources.OldCPUSimulatorFailed);
324325
} catch (System.Configuration.ConfigurationErrorsException) {
@@ -605,33 +606,78 @@ await ActivateModificationsAsync(software, delegate (string text) {
605606
softwareProcessStartInfo.WorkingDirectory = Path.GetDirectoryName(fullPath);
606607
}
607608

608-
Process process = Process.Start(softwareProcessStartInfo);
609+
Process softwareProcess = Process.Start(softwareProcessStartInfo);
609610

610611
try {
611-
ProcessSync.Start(process);
612+
ProcessSync.Start(softwareProcess);
612613
} catch (JobObjectException) {
613614
// popup message box and blow up
614615
ProgressManager.ShowError();
615616
MessageBox.Show(Properties.Resources.JobObjectNotCreated, Properties.Resources.FlashpointSecurePlayer, MessageBoxButtons.OK, MessageBoxIcon.Error);
616-
process.Kill();
617+
softwareProcess.Kill();
617618
Environment.Exit(-1);
618619
return;
619620
}
620621

621622
ProgressManager.CurrentGoal.Steps++;
622623
Hide();
623624

624-
if (!process.HasExited) {
625-
process.WaitForExit();
625+
if (!softwareProcess.HasExited) {
626+
softwareProcess.WaitForExit();
626627
}
627628

628-
Application.Exit();
629+
Show();
630+
631+
string softwareProcessStandardError = null;
632+
string softwareProcessStandardOutput = null;
633+
634+
if (softwareProcessStartInfo.RedirectStandardError) {
635+
softwareProcessStandardError = softwareProcess.StandardError.ReadToEnd();
636+
}
637+
638+
if (softwareProcessStartInfo.RedirectStandardOutput) {
639+
softwareProcessStandardOutput = softwareProcess.StandardOutput.ReadToEnd();
640+
}
641+
642+
if (softwareIsOldCPUSimulator) {
643+
switch (softwareProcess.ExitCode) {
644+
case 0:
645+
break;
646+
case -1:
647+
if (!String.IsNullOrEmpty(softwareProcessStandardError)) {
648+
string[] lastSoftwareProcessStandardErrors = softwareProcessStandardError.Split('\n');
649+
string lastSoftwareProcessStandardError = null;
650+
651+
if (lastSoftwareProcessStandardErrors.Length > 1) {
652+
lastSoftwareProcessStandardError = lastSoftwareProcessStandardErrors[lastSoftwareProcessStandardErrors.Length - 2];
653+
}
654+
655+
if (!String.IsNullOrEmpty(lastSoftwareProcessStandardError)) {
656+
MessageBox.Show(lastSoftwareProcessStandardError);
657+
}
658+
}
659+
break;
660+
case -2:
661+
MessageBox.Show("You cannot run multiple instances of Old CPU Simulator.");
662+
break;
663+
case -3:
664+
MessageBox.Show("Failed to Create New String");
665+
break;
666+
case -4:
667+
MessageBox.Show("Failed to Set String");
668+
break;
669+
default:
670+
MessageBox.Show("Failed to Simulate Old CPU");
671+
break;
672+
}
673+
}
629674
} catch {
630675
Show();
631676
ProgressManager.ShowError();
632677
MessageBox.Show(Properties.Resources.ProcessFailedStart, Properties.Resources.FlashpointSecurePlayer, MessageBoxButtons.OK, MessageBoxIcon.Error);
633-
Application.Exit();
634678
}
679+
680+
Application.Exit();
635681
return;
636682
}
637683
} finally {

FlashpointSecurePlayer/OldCPUSimulator.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ namespace FlashpointSecurePlayer {
1717
class OldCPUSimulator : Modifications {
1818
public OldCPUSimulator(Form form) : base(form) { }
1919

20-
public void Activate(string name, ref string server, ref string software, ref ProcessStartInfo softwareProcessStartInfo) {
20+
public void Activate(string name, ref string server, ref string software, ref ProcessStartInfo softwareProcessStartInfo, out bool softwareIsOldCPUSimulator) {
2121
OldCPUSimulatorElement oldCPUSimulatorElement = null;
22+
softwareIsOldCPUSimulator = false;
2223

2324
base.Activate(name);
2425
ModificationsElement modificationsElement = GetModificationsElement(false, Name);
@@ -74,12 +75,22 @@ public void Activate(string name, ref string server, ref string software, ref Pr
7475

7576
try {
7677
Process oldCPUSimulatorProcess = Process.Start(oldCPUSimulatorProcessStartInfo);
77-
string oldCPUSimulatorProcessStandardOutput = oldCPUSimulatorProcess.StandardOutput.ReadToEnd();
7878

7979
if (!oldCPUSimulatorProcess.HasExited) {
8080
oldCPUSimulatorProcess.WaitForExit();
8181
}
8282

83+
string oldCPUSimulatorProcessStandardError = null;
84+
string oldCPUSimulatorProcessStandardOutput = null;
85+
86+
if (oldCPUSimulatorProcessStartInfo.RedirectStandardError) {
87+
oldCPUSimulatorProcessStandardError = oldCPUSimulatorProcess.StandardError.ReadToEnd();
88+
}
89+
90+
if (oldCPUSimulatorProcessStartInfo.RedirectStandardOutput) {
91+
oldCPUSimulatorProcessStandardOutput = oldCPUSimulatorProcess.StandardOutput.ReadToEnd();
92+
}
93+
8394
if (oldCPUSimulatorProcess.ExitCode != 0 || !long.TryParse(oldCPUSimulatorProcessStandardOutput.Split('\n').Last(), out currentMhz)) {
8495
throw new OldCPUSimulatorFailedException("Failed to get current rate.");
8596
}
@@ -119,11 +130,16 @@ public void Activate(string name, ref string server, ref string software, ref Pr
119130
// this becomes effectively the new thing passed as --software
120131
// the shared function is used both here and GUI side for restarts
121132
software = OLD_CPU_SIMULATOR_PATH + " " + GetOldCPUSimulatorProcessStartInfoArguments(oldCPUSimulatorElement, oldCPUSimulatorSoftware.ToString());
133+
softwareIsOldCPUSimulator = true;
122134

123135
if (softwareProcessStartInfo == null) {
124136
softwareProcessStartInfo = new ProcessStartInfo();
125137
}
126138

139+
softwareProcessStartInfo.RedirectStandardError = true;
140+
softwareProcessStartInfo.RedirectStandardOutput = false;
141+
softwareProcessStartInfo.RedirectStandardInput = false;
142+
127143
// hide the Old CPU Simulator window... we always do this
128144
HideWindow(ref softwareProcessStartInfo);
129145

FlashpointSecurePlayer/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ The Old CPU Simulator Modification has six attributes. The only required attribu
313313

314314
If the current rate is slower than the target rate, the Old CPU Simulator Modification is ignored.
315315

316-
The player will look for Old CPU Simulator in the OldCPUSimulator folder. It must be Old CPU Simulator 1.6.3 or newer.
316+
The player will look for Old CPU Simulator in the OldCPUSimulator folder. It must be Old CPU Simulator 1.6.4 or newer.
317317

318318
# Curation Flow
319319
Let's curate the game Zenerchi. The first step is to add the ActiveX Control to the ActiveX folder in FPSoftware. Here is the location we'll use for Zenerchi:

FlashpointSecurePlayer/Shared.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,6 +1502,10 @@ public static void RestartApplication(bool runAsAdministrator, Form form, string
15021502
};
15031503
}
15041504

1505+
processStartInfo.RedirectStandardError = false;
1506+
processStartInfo.RedirectStandardOutput = false;
1507+
processStartInfo.RedirectStandardInput = false;
1508+
15051509
if (runAsAdministrator) {
15061510
processStartInfo.UseShellExecute = true;
15071511
processStartInfo.Verb = "runas";
@@ -1663,9 +1667,6 @@ public static void HideWindow(ref ProcessStartInfo processStartInfo) {
16631667
}
16641668

16651669
processStartInfo.UseShellExecute = false;
1666-
processStartInfo.RedirectStandardError = false;
1667-
processStartInfo.RedirectStandardOutput = false;
1668-
processStartInfo.RedirectStandardInput = false;
16691670
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
16701671
processStartInfo.CreateNoWindow = true;
16711672
processStartInfo.ErrorDialog = false;

0 commit comments

Comments
 (0)