Skip to content

Commit 53f2e1e

Browse files
committed
[Major] improves the error dialog window
1 parent 97869fb commit 53f2e1e

5 files changed

Lines changed: 187 additions & 5 deletions

File tree

Controls/ModInfos.xaml.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Windows;
88
using System.Windows.Controls;
99
using ModShardLauncher.Core.Errors;
10+
using ModShardLauncher.Core.UI;
1011
using ModShardLauncher.Mods;
1112
using Serilog;
1213

@@ -59,9 +60,7 @@ private async void Save_Click(object sender, EventArgs e)
5960
{
6061
Main.Instance.LogModListStatus();
6162
Log.Information("Failed patching vanilla");
62-
string messageBoxText = "Do you want to save changes?";
63-
string caption = diag.Title();
64-
MessageBox.Show(messageBoxText, caption);
63+
ErrorMessageDialog.Show(diag.Title(), diag.MessageDialog(), Main.Instance.logPath);
6564
}
6665

6766
// reload the data

Core/Errors/MSLDiagnostic.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Drawing;
3+
using System.Windows.Forms;
24
using Serilog;
35

46
namespace ModShardLauncher.Core.Errors
@@ -39,7 +41,41 @@ public void ToLog()
3941
}
4042
public string Title()
4143
{
42-
return $"{ModName} failed";
44+
return $"Patching {ModName} failed";
45+
}
46+
public string MessageDialog()
47+
{
48+
RichTextBox message = new();
49+
50+
message.SelectionColor = Color.Black;
51+
message.AppendText("An error was encountered while patching the mod ");
52+
message.SelectionColor = Color.Blue;
53+
message.AppendText(ModName);
54+
message.SelectionColor = Color.Black;
55+
message.AppendText(":\n");
56+
57+
if (FileName is not null)
58+
{
59+
message.SelectionColor = Color.Black;
60+
message.AppendText("In file ");
61+
message.SelectionColor = Color.Blue;
62+
message.AppendText(FileName);
63+
}
64+
if (PatchingMethod is not null)
65+
{
66+
message.SelectionColor = Color.Black;
67+
message.AppendText(" by ");
68+
message.SelectionColor = Color.Blue;
69+
message.AppendText(PatchingMethod);
70+
message.SelectionColor = Color.Black;
71+
message.AppendText(":\n");
72+
}
73+
74+
message.SelectionColor = Color.Black;
75+
message.AppendText("\n");
76+
message.SelectionFont = new Font(message.Font, FontStyle.Bold);
77+
message.AppendText(exception.ToString());
78+
return message.Rtf;
4379
}
4480
}
4581
}

Core/UI/ErrorMessageDialog.cs

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
using System;
2+
using System.Drawing;
3+
using System.Windows.Forms;
4+
using Serilog;
5+
6+
namespace ModShardLauncher.Core.UI
7+
{
8+
public class ErrorMessageDialog : Form
9+
{
10+
private readonly RichTextBox messageTextBox;
11+
public DialogResult Result { get; private set; }
12+
private readonly TableLayoutPanel panel;
13+
private readonly Button okButton;
14+
private readonly Button cpyButton;
15+
private readonly Button logButton;
16+
public ErrorMessageDialog(string title, string message, string? logPath = null)
17+
{
18+
panel = new();
19+
messageTextBox = new RichTextBox();
20+
okButton = new Button();
21+
cpyButton = new Button();
22+
logButton = new Button();
23+
24+
InitializeComponent();
25+
SetupDialog(title, message, logPath);
26+
}
27+
private void InitializeComponent()
28+
{
29+
SuspendLayout();
30+
31+
Text = string.Empty;
32+
Size = new Size(450, 200);
33+
StartPosition = FormStartPosition.CenterParent;
34+
FormBorderStyle = FormBorderStyle.FixedDialog;
35+
MaximizeBox = false;
36+
MinimizeBox = false;
37+
ShowIcon = false;
38+
ShowInTaskbar = false;
39+
Dock = DockStyle.Fill;
40+
41+
// panel
42+
panel.Anchor = AnchorStyles.Top;
43+
panel.Size = new Size(300, 150);
44+
panel.BorderStyle = BorderStyle.None;
45+
panel.ColumnCount = 3;
46+
panel.RowCount = 2;
47+
panel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
48+
panel.RowStyles.Add(new RowStyle(SizeType.Absolute, 44));
49+
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
50+
panel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
51+
panel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
52+
panel.Dock = DockStyle.Fill;
53+
54+
// Message TextBox
55+
messageTextBox.ReadOnly = true;
56+
messageTextBox.BorderStyle = BorderStyle.None;
57+
messageTextBox.BackColor = BackColor;
58+
messageTextBox.ScrollBars = RichTextBoxScrollBars.Vertical;
59+
messageTextBox.TabStop = false;
60+
61+
// ok button
62+
okButton.Text = "OK";
63+
okButton.Size = new Size(75, 23);
64+
okButton.DialogResult = DialogResult.OK;
65+
okButton.Click += (s, e) => { Result = DialogResult.OK; Close(); };
66+
67+
// copy button
68+
cpyButton.Text = "Copy error";
69+
cpyButton.Size = new Size(100, 23);
70+
cpyButton.Click += CopyButton_Click;
71+
72+
// log button
73+
logButton.Text = "Open Log Folder";
74+
logButton.Size = new Size(120, 23);
75+
logButton.Click += LogButton_Click;
76+
77+
panel.Controls.Add(messageTextBox, 0, 0);
78+
panel.SetColumnSpan(messageTextBox, 3);
79+
panel.Controls.Add(okButton, 0, 1);
80+
panel.Controls.Add(cpyButton, 1, 1);
81+
panel.Controls.Add(logButton, 2, 1);
82+
83+
Controls.Add(panel);
84+
85+
// Set default button and cancel button
86+
AcceptButton = okButton;
87+
CancelButton = okButton;
88+
89+
ResumeLayout();
90+
}
91+
private void SetupDialog(string title, string message, string? logPath = null)
92+
{
93+
Text = title;
94+
messageTextBox.Rtf = message;
95+
messageTextBox.AutoSize = true;
96+
97+
Size size = messageTextBox.GetPreferredSize(new Size(800, 0)) + new Size(0, 50);
98+
int newHeight = size.Height + 100;
99+
int newWidth = Math.Max(450, size.Width + 50);
100+
101+
messageTextBox.Size = new Size(size.Width, size.Height);
102+
Size = new Size(newWidth, newHeight);
103+
104+
// Hide log button if no path provided
105+
if (string.IsNullOrEmpty(logPath))
106+
{
107+
logButton.Visible = false;
108+
okButton.Location = new Point(340, 125); // Center the OK button
109+
}
110+
else
111+
{
112+
logButton.Tag = logPath; // Store the log path
113+
}
114+
}
115+
private void LogButton_Click(object? sender, EventArgs e)
116+
{
117+
try
118+
{
119+
string? logPath = logButton.Tag?.ToString();
120+
if (!string.IsNullOrEmpty(logPath))
121+
{
122+
System.Diagnostics.Process.Start("explorer.exe", logPath);
123+
}
124+
}
125+
catch (Exception ex)
126+
{
127+
MessageBox.Show($"Could not open log folder: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
128+
}
129+
130+
// nifty trick: use Retry to indicate log button was clicked
131+
Result = DialogResult.Retry;
132+
Close();
133+
}
134+
private void CopyButton_Click(object? sender, EventArgs e)
135+
{
136+
Clipboard.SetText(messageTextBox.Text);
137+
}
138+
public static DialogResult Show(string title, string message, string? logPath = null)
139+
{
140+
using var dialog = new ErrorMessageDialog(title, message, logPath);
141+
dialog.ShowDialog();
142+
return dialog.Result;
143+
}
144+
}
145+
}

Main.xaml.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public partial class Main : Window
3838
public static IntPtr handle;
3939
public string mslVersion;
4040
public string utmtlibVersion;
41+
public readonly string logPath = "logs";
4142
private const double DefaultWidth = 960; // Исходная ширина
4243
private const double DefaultHeight = 800; // Исходная высота
4344
private const double AspectRatio = DefaultWidth / DefaultHeight; // Соотношение сторон
@@ -61,7 +62,7 @@ public Main()
6162
// create File and Console (controlledby a switch) sinks
6263
LoggerConfiguration logger = new LoggerConfiguration()
6364
.MinimumLevel.Debug()
64-
.WriteTo.File(string.Format("logs/log_{0}.txt", DateTime.Now.ToString("yyyyMMdd_HHmm")))
65+
.WriteTo.File(string.Format("{0}/log_{1}.txt", logPath, DateTime.Now.ToString("yyyyMMdd_HHmm")))
6566
.WriteTo.Logger(log => log
6667
.MinimumLevel.ControlledBy(lls)
6768
.WriteTo.Console()

ModShardLauncher.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
4141
<PackageReference Include="SharpZipLib" Version="1.3.3" />
4242
<PackageReference Include="System.Drawing.Common" Version="8.0.1" />
43+
<PackageReference Include="System.Windows.Forms" Version="4.0.0" />
4344
<PackageReference Include="XamlAnimatedGif" Version="2.2.0" />
4445
<PackageReference Include="xunit" Version="2.6.5" />
4546
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">

0 commit comments

Comments
 (0)