Skip to content

Commit

Permalink
Fix not redirecting stdout/stderr of process
Browse files Browse the repository at this point in the history
  • Loading branch information
psyGamer committed Aug 29, 2024
1 parent 2a45527 commit 44f6fb7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 60 deletions.
94 changes: 37 additions & 57 deletions CelesteTAS-EverestInterop/EverestInterop/StudioHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,75 +195,55 @@ private static async Task DownloadStudio() {

// Fix lost file permissions
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) {
string? line;

var chmodProc = Process.Start("chmod", ["+x", Path.Combine(StudioDirectory, "CelesteStudio")]);
await chmodProc.WaitForExitAsync().ConfigureAwait(false);

if (chmodProc.ExitCode != 0) {
$"Install failed! Couldn't make Studio executable: {chmodProc.ExitCode}".Log(LogLevel.Error);

while ((line = await chmodProc.StandardOutput.ReadLineAsync()) == null) {
line.Log(LogLevel.Info);
}
while ((line = await chmodProc.StandardError.ReadLineAsync()) == null) {
line.Log(LogLevel.Error);
}

if (!await ExecuteCommand(["chmod", "+x", Path.Combine(StudioDirectory, "CelesteStudio")],
errorMessage: "Install failed! Couldn't make Studio executable").ConfigureAwait(false))
{
// Mark install as invalid, so that next launch will try again
File.Delete(VersionFile);
return;
} else {
while ((line = await chmodProc.StandardOutput.ReadLineAsync()) == null) {
line.Log(LogLevel.Debug);
}
}
} else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) {
string? line;

var chmodProc = Process.Start("chmod", ["+x", Path.Combine(StudioDirectory, "CelesteStudio.app", "Contents", "MacOS", "CelesteStudio")]);
await chmodProc.WaitForExitAsync().ConfigureAwait(false);

if (chmodProc.ExitCode != 0) {
$"Install failed! Couldn't make Studio executable: {chmodProc.ExitCode}".Log(LogLevel.Error);

while ((line = await chmodProc.StandardOutput.ReadLineAsync()) == null) {
line.Log(LogLevel.Info);
}
while ((line = await chmodProc.StandardError.ReadLineAsync()) == null) {
line.Log(LogLevel.Error);
}

if (!await ExecuteCommand(["chmod", "+x", Path.Combine(StudioDirectory, "CelesteStudio.app", "Contents", "MacOS", "CelesteStudio")],
errorMessage: "Install failed! Couldn't make Studio executable").ConfigureAwait(false) ||
!await ExecuteCommand(["xattr", "-c", Path.Combine(StudioDirectory, "CelesteStudio.app")],
errorMessage: "Install failed! Couldn't clear Studio app bundle config").ConfigureAwait(false))
{
// Mark install as invalid, so that next launch will try again
File.Delete(VersionFile);
return;
} else {
while ((line = await chmodProc.StandardOutput.ReadLineAsync()) == null) {
line.Log(LogLevel.Debug);
}
}
}
}

var xattrProc = Process.Start("xattr", ["-c", Path.Combine(StudioDirectory, "CelesteStudio.app")]);
await xattrProc.WaitForExitAsync().ConfigureAwait(false);
if (xattrProc.ExitCode != 0) {
$"Install failed! Couldn't clear Studio app bundle config: {xattrProc.ExitCode}".Log(LogLevel.Error);
private static async Task<bool> ExecuteCommand(string[] parameters, string errorMessage) {
var startInfo = new ProcessStartInfo(parameters[0]) {
ArgumentList = { "+x", Path.Combine(StudioDirectory, "CelesteStudio.app", "Contents", "MacOS", "CelesteStudio") },
RedirectStandardOutput = true,
RedirectStandardError = true,
};
foreach (string param in parameters[1..]) {
startInfo.ArgumentList.Add(param);
}

while ((line = await xattrProc.StandardOutput.ReadLineAsync()) == null) {
line.Log(LogLevel.Info);
}
while ((line = await xattrProc.StandardError.ReadLineAsync()) == null) {
line.Log(LogLevel.Error);
}
var proc = Process.Start(startInfo)!;
await proc.WaitForExitAsync().ConfigureAwait(false);

// Mark install as invalid, so that next launch will try again
File.Delete(VersionFile);
return;
} else {
while ((line = await xattrProc.StandardOutput.ReadLineAsync()) == null) {
line.Log(LogLevel.Debug);
}
string? line;

if (proc.ExitCode != 0) {
$"{errorMessage}: Exit Code {proc.ExitCode}".Log(LogLevel.Error);

while ((line = await proc.StandardOutput.ReadLineAsync()) == null) {
line.Log(LogLevel.Info);
}
while ((line = await proc.StandardError.ReadLineAsync()) == null) {
line.Log(LogLevel.Error);
}
} else {
while ((line = await proc.StandardOutput.ReadLineAsync()) == null) {
line.Log(LogLevel.Debug);
}
}

return proc.ExitCode == 0;
}

internal static void LaunchStudio() => Task.Run(async () => {
Expand Down
6 changes: 3 additions & 3 deletions CelesteTAS-EverestInterop/Utils/LogUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public static void DebugLog(this object text, bool outputToCommands, LogLevel lo
}
#endif

public static void LogException(this Exception e, string header, LogLevel logLevel = LogLevel.Warn) {
public static void LogException(this Exception e, string header, LogLevel logLevel = LogLevel.Error) {
header.Log(logLevel);
e.LogDetailed();
Logger.LogDetailed(e, Tag);
}

public static void Log(this object text, LogLevel logLevel = LogLevel.Info) {
Expand Down Expand Up @@ -66,4 +66,4 @@ public static void ConsoleLog(this object text, LogLevel logLevel = LogLevel.Ver
// ignored
}
}
}
}

0 comments on commit 44f6fb7

Please sign in to comment.