Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 18 additions & 21 deletions Controls/ModInfos.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using ModShardLauncher.Core.Errors;
using ModShardLauncher.Core.UI;
using ModShardLauncher.Mods;
using Serilog;

Expand Down Expand Up @@ -36,38 +38,33 @@ private async void Save_Click(object sender, EventArgs e)
return;
}

bool patchSucess = false;
MSLDiagnostic? diag = ModLoader.PatchFile();

Stopwatch watch = Stopwatch.StartNew();
try
if (diag is null)
{
ModLoader.PatchFile();
long elapsedMs = watch.ElapsedMilliseconds;
Main.Instance.LogModListStatus();
Log.Information("Patching lasts {{{0}}} ms", elapsedMs);
Log.Information("Successfully patch vanilla");
patchSucess = true;

Task<bool> save = DataLoader.DoSaveDialog();
await save;
if (save.Result)
{
LootUtils.SaveLootTables(Msl.ThrowIfNull(Path.GetDirectoryName(DataLoader.savedDataPath)));
}
else
{
Log.Information("Saved cancelled.");
}
}
catch (Exception ex)
else
{
Main.Instance.LogModListStatus();
Log.Error(ex, "Something went wrong");
Log.Information("Failed patching vanilla");
MessageBox.Show(ex.ToString(), Application.Current.FindResource("SaveDataWarning").ToString());
}

// attempt to save the patched data
if (patchSucess)
{
Task<bool> save = DataLoader.DoSaveDialog();
await save;
if (!save.Result) Log.Information("Saved cancelled.");
// copy the dataloot.json in the stoneshard directory
LootUtils.SaveLootTables(Msl.ThrowIfNull(Path.GetDirectoryName(DataLoader.savedDataPath)));
ErrorMessageDialog.Show(diag.Title(), diag.MessageDialog(), Main.Instance.logPath);
}

// reload the data
await DataLoader.LoadFile(DataLoader.dataPath, true);
await DataLoader.LoadFile(DataLoader.dataPath);
Main.Instance.Refresh();
}

Expand Down
2 changes: 1 addition & 1 deletion Controls/SourceBar.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private void CompileButton_Click(object sender, RoutedEventArgs e)
Log.Error(ex, "Something went wrong");
}

Msl.ThrowIfNull(Main.Instance.Viewer.Content as UserControl).UpdateLayout();
Msl.ThrowIfNull((UserControl)Main.Instance.Viewer.Content).UpdateLayout();
Main.Instance.Refresh();
}

Expand Down
81 changes: 81 additions & 0 deletions Core/Errors/MSLDiagnostic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.Drawing;
using System.Windows.Forms;
using Serilog;

namespace ModShardLauncher.Core.Errors
{
public class MSLDiagnostic
{
public string ModName;
public string? FileName;
public string? PatchingMethod;
public Exception exception;
public MSLDiagnostic(Exception ex, ModFile modFile)
{
if (ex.Data.Contains("fileName"))
{
object? fileName = ex.Data["fileName"];
FileName = $"{fileName}";
}
if (ex.Data.Contains("patchingWay"))
{
object? patchingWay = ex.Data["patchingWay"];
PatchingMethod = $"{patchingWay}";
}
ModName = modFile.Name;
exception = ex;
}
public void ToLog()
{
string extraInformation = " for {{{0}}}";
if (FileName is not null)
{
extraInformation += " in file {{{1}}}";
}
if (PatchingMethod is not null)
{
extraInformation += " while patching by {{{2}}}";
}
Log.Error(exception, "Something went wrong" + extraInformation, ModName, FileName, PatchingMethod);
}
public string Title()
{
return $"Patching {ModName} failed";
}
public string MessageDialog()
{
RichTextBox message = new();

message.SelectionColor = Color.Black;
message.AppendText("An error was encountered while patching the mod ");
message.SelectionColor = Color.Blue;
message.AppendText(ModName);
message.SelectionColor = Color.Black;
message.AppendText(":\n");

if (FileName is not null)
{
message.SelectionColor = Color.Black;
message.AppendText("In file ");
message.SelectionColor = Color.Blue;
message.AppendText(FileName);
}
if (PatchingMethod is not null)
{
message.SelectionColor = Color.Black;
message.AppendText(" by ");
message.SelectionColor = Color.Blue;
message.AppendText(PatchingMethod);
message.SelectionColor = Color.Black;
message.AppendText(":\n");
}

message.SelectionColor = Color.Black;
message.AppendText("\n");
message.SelectionFont = new Font(message.Font, FontStyle.Bold);
message.AppendText(exception.ToString());
return message.Rtf;
}
}
}
145 changes: 145 additions & 0 deletions Core/UI/ErrorMessageDialog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
using System;
using System.Drawing;
using System.Windows.Forms;
using Serilog;

namespace ModShardLauncher.Core.UI
{
public class ErrorMessageDialog : Form
{
private readonly RichTextBox messageTextBox;
public DialogResult Result { get; private set; }
private readonly TableLayoutPanel panel;
private readonly Button okButton;
private readonly Button cpyButton;
private readonly Button logButton;
public ErrorMessageDialog(string title, string message, string? logPath = null)
{
panel = new();
messageTextBox = new RichTextBox();
okButton = new Button();
cpyButton = new Button();
logButton = new Button();

InitializeComponent();
SetupDialog(title, message, logPath);
}
private void InitializeComponent()
{
SuspendLayout();

Text = string.Empty;
Size = new Size(450, 200);
StartPosition = FormStartPosition.CenterParent;
FormBorderStyle = FormBorderStyle.FixedDialog;
MaximizeBox = false;
MinimizeBox = false;
ShowIcon = false;
ShowInTaskbar = false;
Dock = DockStyle.Fill;

// panel
panel.Anchor = AnchorStyles.Top;
panel.Size = new Size(300, 150);
panel.BorderStyle = BorderStyle.None;
panel.ColumnCount = 3;
panel.RowCount = 2;
panel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
panel.RowStyles.Add(new RowStyle(SizeType.Absolute, 44));
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
panel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
panel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
panel.Dock = DockStyle.Fill;

// Message TextBox
messageTextBox.ReadOnly = true;
messageTextBox.BorderStyle = BorderStyle.None;
messageTextBox.BackColor = BackColor;
messageTextBox.ScrollBars = RichTextBoxScrollBars.Vertical;
messageTextBox.TabStop = false;

// ok button
okButton.Text = "OK";
okButton.Size = new Size(75, 23);
okButton.DialogResult = DialogResult.OK;
okButton.Click += (s, e) => { Result = DialogResult.OK; Close(); };

// copy button
cpyButton.Text = "Copy error";
cpyButton.Size = new Size(100, 23);
cpyButton.Click += CopyButton_Click;

// log button
logButton.Text = "Open Log Folder";
logButton.Size = new Size(120, 23);
logButton.Click += LogButton_Click;

panel.Controls.Add(messageTextBox, 0, 0);
panel.SetColumnSpan(messageTextBox, 3);
panel.Controls.Add(okButton, 0, 1);
panel.Controls.Add(cpyButton, 1, 1);
panel.Controls.Add(logButton, 2, 1);

Controls.Add(panel);

// Set default button and cancel button
AcceptButton = okButton;
CancelButton = okButton;

ResumeLayout();
}
private void SetupDialog(string title, string message, string? logPath = null)
{
Text = title;
messageTextBox.Rtf = message;
messageTextBox.AutoSize = true;

Size size = messageTextBox.GetPreferredSize(new Size(800, 0)) + new Size(0, 50);
int newHeight = size.Height + 100;
int newWidth = Math.Max(450, size.Width + 50);

messageTextBox.Size = new Size(size.Width, size.Height);
Size = new Size(newWidth, newHeight);

// Hide log button if no path provided
if (string.IsNullOrEmpty(logPath))
{
logButton.Visible = false;
okButton.Location = new Point(340, 125); // Center the OK button
}
else
{
logButton.Tag = logPath; // Store the log path
}
}
private void LogButton_Click(object? sender, EventArgs e)
{
try
{
string? logPath = logButton.Tag?.ToString();
if (!string.IsNullOrEmpty(logPath))
{
System.Diagnostics.Process.Start("explorer.exe", logPath);
}
}
catch (Exception ex)
{
MessageBox.Show($"Could not open log folder: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}

// nifty trick: use Retry to indicate log button was clicked
Result = DialogResult.Retry;
Close();
}
private void CopyButton_Click(object? sender, EventArgs e)
{
Clipboard.SetText(messageTextBox.Text);
}
public static DialogResult Show(string title, string message, string? logPath = null)
{
using var dialog = new ErrorMessageDialog(title, message, logPath);
dialog.ShowDialog();
return dialog.Result;
}
}
}
Loading
Loading