Skip to content

Commit

Permalink
feat: Report Studio installation errors with a text file
Browse files Browse the repository at this point in the history
  • Loading branch information
psyGamer committed Sep 28, 2024
1 parent 4102b03 commit 8ae5c18
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 15 deletions.
74 changes: 62 additions & 12 deletions CelesteTAS-EverestInterop/Source/EverestInterop/StudioHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using Celeste.Mod;
using StudioCommunication.Util;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using TAS.Module;
Expand Down Expand Up @@ -92,10 +95,10 @@ private static void Load() {
Task.Run(async () => {
// Kill all Studio instances to avoid issues with file usage
foreach (var process in Process.GetProcesses().Where(process => process.ProcessName is "CelesteStudio" or "Celeste Studio")) {
$"Killing process {process} ({process.Id})...".Log(LogLevel.Verbose);
process.Kill();
await process.WaitForExitAsync().WaitAsync(TimeSpan.FromSeconds(30.0f)).ConfigureAwait(false);
"Process killed".Log(LogLevel.Verbose);
$"Closing process {process} ({process.Id})...".Log(LogLevel.Verbose);
process.Terminate();
await process.WaitForExitAsync().WaitAsync(TimeSpan.FromSeconds(10.0f)).ConfigureAwait(false);
"Process terminated".Log(LogLevel.Verbose);
}

// If Studio fails to find the game directory for some reason, that's where "TAS Files" will be placed
Expand All @@ -117,7 +120,7 @@ private static void Load() {
await DownloadStudio().ConfigureAwait(false);
installed = true;
} catch (Exception ex) {
ex.LogException("Failed to install Studio");
ReportError("Failed to install Studio", ex.StackTrace);

// Cleanup install
if (Directory.Exists(TempStudioInstallDirectory)) {
Expand Down Expand Up @@ -201,8 +204,9 @@ private static async Task DownloadStudio() {
});

if (!File.Exists(DownloadPath)) {
"Download failed! The Studio archive went missing".Log(LogLevel.Error);
ReportError("Download failed! The Studio archive went missing");
StudioUpdateBanner.CurrentState = StudioUpdateBanner.State.Failure;
StudioUpdateBanner.FadeoutTimer = 5.0f;
return;
}
"Finished download".Log();
Expand All @@ -229,8 +233,9 @@ private static async Task DownloadStudio() {
await using (var fs = File.OpenRead(DownloadPath)) {
string hash = BitConverter.ToString(await md5.ComputeHashAsync(fs)).Replace("-", "");
if (!Checksum.Equals(hash, StringComparison.OrdinalIgnoreCase)) {
$"Download failed! Invalid checksum for Studio archive file: Expected {Checksum} got {hash}".Log(LogLevel.Error);
ReportError($"Download failed! Invalid checksum for Studio archive file: Expected {Checksum} got {hash}");
StudioUpdateBanner.CurrentState = StudioUpdateBanner.State.Failure;
StudioUpdateBanner.FadeoutTimer = 5.0f;
return;
}
$"Downloaded Studio archive has a valid checksum: {hash}".Log(LogLevel.Verbose);
Expand Down Expand Up @@ -306,14 +311,24 @@ private static async Task<bool> ExecuteCommand(string[] parameters, string error
string? line;

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

List<string> stdoutLines = [], stderrLines = [];
while ((line = await proc.StandardOutput.ReadLineAsync()) != null) {
line.Log(LogLevel.Info);
stdoutLines.Add(line);
}
while ((line = await proc.StandardError.ReadLineAsync()) != null) {
line.Log(LogLevel.Error);
stderrLines.Add(line);
}

ReportError(
$"{errorMessage}: Exit Code {proc.ExitCode}",
$"""
Standard Out:
{string.Join(Environment.NewLine, stdoutLines)}
Standard Error:
{string.Join(Environment.NewLine, stderrLines)}
""");
stdoutLines.ForEach(l => l.Log());
stderrLines.ForEach(l => l.Log(LogLevel.Error));
} else {
while ((line = await proc.StandardOutput.ReadLineAsync()) != null) {
line.Log(LogLevel.Debug);
Expand Down Expand Up @@ -360,7 +375,42 @@ internal static void LaunchStudio() => Task.Run(async () => {

"Successfully launched Studio".Log();
} catch (Exception ex) {
ex.LogException("Failed to launch Studio");
ReportError("Failed to launch Studio", ex.StackTrace);
ex.LogException();
}
});

private static void ReportError(string error, string? additionalInfo = null) {
error.Log(LogLevel.Error);

if (!Directory.Exists(StudioDirectory)) {
Directory.CreateDirectory(StudioDirectory);
}

string path = Path.Combine(StudioDirectory, "error_report.txt");
string text =
$"""
=== Celeste Studio v{CurrentStudioVersion} - Install failed ===
{DateTime.Now.ToString(CultureInfo.InvariantCulture)}
The following error occured while trying to install Celeste Studio:
{error}
This may be caused by a bad internet connection. You can manually download Celeste Studio by following these steps:
1. Close Celeste
2. Manually download this file: {DownloadURL}
3. Rename it to be called "CelesteStudio.zip" (ensure you have file extensions enabled!)
4. Place it under "<celeste-install>/CelesteStudio/CelesteStudio.zip" (create the "CelesteStudio" directory if it doesn't already exist. The "<celeste-install>" directory is the same as where "Celeste.exe" / "Celeste.dll" is located)
5. Re-open Celeste
If the error persists, please report this issue. And send this ENTIRE file along with it.
(This file is located under "{path}")
Additional Information:
{additionalInfo ?? "<not-available>"}
""";

File.WriteAllText(path, text);
ProcessHelper.OpenInDefaultApp(path);
}
}
4 changes: 4 additions & 0 deletions CelesteTAS-EverestInterop/Source/Utils/LogUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public static void DebugLog(this object text, bool outputToCommands, LogLevel lo
}
#endif

public static void LogException(this Exception e) {
Logger.LogDetailed(e, Tag);
}

public static void LogException(this Exception e, string header, LogLevel logLevel = LogLevel.Error) {
header.Log(logLevel);
Logger.LogDetailed(e, Tag);
Expand Down
1 change: 1 addition & 0 deletions Studio/CelesteStudio/Controls/Markdown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Eto.Forms;
using Markdig.Syntax;
using Markdig.Syntax.Inlines;
using StudioCommunication.Util;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down
1 change: 1 addition & 0 deletions Studio/CelesteStudio/Dialog/SnippetDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using CelesteStudio.Util;
using Eto.Drawing;
using Eto.Forms;
using StudioCommunication.Util;
using System;

namespace CelesteStudio.Dialog;
Expand Down
1 change: 1 addition & 0 deletions Studio/CelesteStudio/ErrorLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using System.Text;
using CelesteStudio.Util;
using StudioCommunication.Util;
using System.Globalization;

namespace CelesteStudio;
Expand Down
1 change: 1 addition & 0 deletions Studio/CelesteStudio/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Eto;
using Eto.Drawing;
using Eto.Forms;
using StudioCommunication.Util;
using System.Diagnostics;
using System.Net;
using Tomlet;
Expand Down
1 change: 1 addition & 0 deletions Studio/CelesteStudio/Studio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using FontDialog = CelesteStudio.Dialog.FontDialog;
using Eto.Forms.ThemedControls;
using StudioCommunication;
using StudioCommunication.Util;
using System.Collections.Generic;

namespace CelesteStudio;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Diagnostics;
using System.Runtime.InteropServices;
using Eto.Forms;
using System;

namespace CelesteStudio.Util;
namespace StudioCommunication.Util;

public static class ProcessHelper
{
Expand All @@ -14,7 +14,7 @@ public static void OpenInDefaultApp(string path) {
} else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) {
Process.Start("open", [path]);
} else {
MessageBox.Show($"Cannot open '{path}' in it's default app, since platform is not recognized", MessageBoxType.Error);
throw new NotImplementedException($"Unsupported platform: {RuntimeInformation.OSDescription} with {RuntimeInformation.OSArchitecture}");
}
}

Expand Down

0 comments on commit 8ae5c18

Please sign in to comment.