2023-10-15 02:26:56 +00:00
|
|
|
using System;
|
2016-08-22 01:05:41 +00:00
|
|
|
using System.Text;
|
|
|
|
using System.Windows.Forms;
|
2024-05-30 15:40:08 +00:00
|
|
|
using PKHeX.Core;
|
2016-08-22 01:05:41 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.WinForms;
|
|
|
|
|
|
|
|
public sealed partial class ErrorWindow : Form
|
2016-08-22 01:05:41 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
public static DialogResult ShowErrorDialog(string friendlyMessage, Exception ex, bool allowContinue)
|
2016-08-22 01:05:41 +00:00
|
|
|
{
|
2024-05-30 15:40:08 +00:00
|
|
|
string lang = GetDisplayLanguage();
|
2023-10-15 02:26:56 +00:00
|
|
|
using var dialog = new ErrorWindow(lang);
|
2024-05-30 15:40:08 +00:00
|
|
|
dialog.LoadException(ex, friendlyMessage, allowContinue);
|
2022-06-18 18:04:24 +00:00
|
|
|
var dialogResult = dialog.ShowDialog();
|
|
|
|
if (dialogResult == DialogResult.Abort)
|
|
|
|
Environment.Exit(1);
|
|
|
|
return dialogResult;
|
|
|
|
}
|
2016-08-22 01:05:41 +00:00
|
|
|
|
2024-05-30 15:40:08 +00:00
|
|
|
private static string GetDisplayLanguage()
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2024-05-30 15:40:08 +00:00
|
|
|
try { return Main.CurrentLanguage; }
|
|
|
|
catch { return System.Threading.Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName; }
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2016-08-22 01:05:41 +00:00
|
|
|
|
2024-05-30 15:40:08 +00:00
|
|
|
private ErrorWindow(string? lang = GameLanguage.DefaultLanguage)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2024-05-30 15:40:08 +00:00
|
|
|
InitializeComponent();
|
|
|
|
WinFormsUtil.TranslateInterface(this, lang ?? GetDisplayLanguage());
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2016-08-22 01:05:41 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
2023-12-04 04:13:20 +00:00
|
|
|
/// Gets or sets the visibility of the "Continue" button.
|
2022-06-18 18:04:24 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <remarks>For UI exceptions, continuing could be safe.
|
|
|
|
/// For application exceptions, continuing is not possible, so the button should not be shown.</remarks>
|
|
|
|
private bool ShowContinue
|
|
|
|
{
|
|
|
|
set => B_Continue.Visible = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Friendly, context-specific method shown to the user.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>This property is intended to be a user-friendly context-specific message about what went wrong.
|
|
|
|
/// For example: "An error occurred while attempting to automatically load the save file."</remarks>
|
|
|
|
private string Message
|
|
|
|
{
|
|
|
|
get => L_Message.Text;
|
|
|
|
set => L_Message.Text = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Exception? _error;
|
2016-08-22 01:05:41 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public Exception Error
|
|
|
|
{
|
|
|
|
get => _error ?? throw new ArgumentNullException(nameof(_error));
|
|
|
|
set
|
2016-08-22 01:05:41 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
_error = value;
|
|
|
|
UpdateExceptionDetailsMessage();
|
2016-08-22 01:05:41 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2016-08-22 01:05:41 +00:00
|
|
|
|
2024-05-30 15:40:08 +00:00
|
|
|
public void LoadException(Exception ex, string friendlyMessage, bool allowContinue) => Invoke(() =>
|
|
|
|
{
|
|
|
|
ShowContinue = allowContinue;
|
|
|
|
Message = friendlyMessage;
|
|
|
|
Error = ex;
|
|
|
|
});
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private void UpdateExceptionDetailsMessage()
|
|
|
|
{
|
|
|
|
var details = new StringBuilder();
|
|
|
|
details.AppendLine("Exception Details:");
|
|
|
|
details.AppendLine(Error.ToString());
|
|
|
|
details.AppendLine();
|
2018-07-27 02:34:27 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
details.AppendLine("Loaded Assemblies:");
|
|
|
|
details.AppendLine("--------------------");
|
|
|
|
try
|
2016-08-22 01:05:41 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
foreach (var item in AppDomain.CurrentDomain.GetAssemblies())
|
2016-08-22 01:05:41 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
details.AppendLine(item.FullName);
|
|
|
|
details.AppendLine(item.Location);
|
|
|
|
details.AppendLine();
|
2016-08-30 02:22:15 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
catch (Exception ex)
|
2016-08-30 02:22:15 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
details.AppendLine("An error occurred while listing the Loaded Assemblies:");
|
|
|
|
details.AppendLine(ex.ToString());
|
|
|
|
}
|
|
|
|
details.AppendLine("--------------------");
|
2016-08-30 02:22:15 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Include message in case it contains important information, like a file path.
|
|
|
|
details.AppendLine("User Message:");
|
|
|
|
details.AppendLine(Message);
|
2016-08-30 02:22:15 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
T_ExceptionDetails.Text = details.ToString();
|
|
|
|
}
|
2016-08-22 01:05:41 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private void ClickCopyException(object sender, EventArgs e) => WinFormsUtil.SetClipboardText(T_ExceptionDetails.Text);
|
2016-08-22 01:05:41 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private void ClickContinue(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
DialogResult = DialogResult.OK;
|
|
|
|
Close();
|
|
|
|
}
|
2016-08-22 01:05:41 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private void ClickAbort(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
DialogResult = DialogResult.Abort;
|
|
|
|
Close();
|
2016-08-22 01:05:41 +00:00
|
|
|
}
|
|
|
|
}
|