Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make error on load show on UI thread #986

Closed
wants to merge 1 commit into from
Closed
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
14 changes: 7 additions & 7 deletions UndertaleModTool/Editors/UndertaleCodeEditor.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ private async Task DecompileCode(UndertaleCode code, bool first, LoaderDialog ex
}
catch (Exception exc)
{
mainWindow.ShowError(exc.ToString());
mainWindow.ShowErrorInvoke(exc.ToString());
}

if (decompiled != null)
Expand Down Expand Up @@ -595,14 +595,14 @@ private async void GraphCode(UndertaleCode code)
catch (Exception e)
{
Debug.WriteLine(e.ToString());
if (mainWindow.ShowQuestion("Unable to execute GraphViz: " + e.Message + "\nMake sure you have downloaded it and set the path in settings.\nDo you want to open the download page now?", MessageBoxImage.Error) == MessageBoxResult.Yes)
if (mainWindow.ShowQuestionInvoke("Unable to execute GraphViz: " + e.Message + "\nMake sure you have downloaded it and set the path in settings.\nDo you want to open the download page now?", MessageBoxImage.Error) == MessageBoxResult.Yes)
MainWindow.OpenBrowser("https://graphviz.gitlab.io/_pages/Download/Download_windows.html");
}
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
mainWindow.ShowError(e.Message, "Graph generation failed");
mainWindow.ShowErrorInvoke(e.Message, "Graph generation failed");
}

Dispatcher.Invoke(() =>
Expand Down Expand Up @@ -692,21 +692,21 @@ private async Task DecompiledLostFocusBody(object sender, RoutedEventArgs e)
if (compileContext == null)
{
dialog.TryClose();
mainWindow.ShowError("Compile context was null for some reason...", "This shouldn't happen");
mainWindow.ShowErrorInvoke("Compile context was null for some reason...", "This shouldn't happen");
return;
}

if (compileContext.HasError)
{
dialog.TryClose();
mainWindow.ShowError(Truncate(compileContext.ResultError, 512), "Compiler error");
mainWindow.ShowErrorInvoke(Truncate(compileContext.ResultError, 512), "Compiler error");
return;
}

if (!compileContext.SuccessfulCompile)
{
dialog.TryClose();
mainWindow.ShowError("(unknown error message)", "Compile failed");
mainWindow.ShowErrorInvoke("(unknown error message)", "Compile failed");
return;
}

Expand All @@ -733,7 +733,7 @@ private async Task DecompiledLostFocusBody(object sender, RoutedEventArgs e)
}
catch (Exception exc)
{
mainWindow.ShowError("Error during writing of GML code to profile:\n" + exc);
mainWindow.ShowErrorInvoke("Error during writing of GML code to profile:\n" + exc);
}
}

Expand Down
38 changes: 15 additions & 23 deletions UndertaleModTool/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ private async void Window_Loaded(object sender, RoutedEventArgs e)
}

if (!deleted)
this.ShowWarning($"The updater temp folder can't be deleted.\nError - {exMessage}.");
this.ShowWarningInvoke($"The updater temp folder can't be deleted.\nError - {exMessage}.");
}
});
}
Expand Down Expand Up @@ -624,7 +624,7 @@ public async Task<bool> MakeNewDataFile()
{
if (Data != null)
{
if (this.ShowQuestion("Warning: you currently have a project open.\nAre you sure you want to make a new project?") == MessageBoxResult.No)
if (this.ShowQuestionInvoke("Warning: you currently have a project open.\nAre you sure you want to make a new project?") == MessageBoxResult.No)
return false;
}
this.Dispatcher.Invoke(() =>
Expand Down Expand Up @@ -923,7 +923,7 @@ private async Task LoadFile(string filename, bool preventClose = false)
{
data = UndertaleIO.Read(stream, warning =>
{
this.ShowWarning(warning, "Loading warning");
this.ShowWarningInvoke(warning, "Loading warning");
hadWarnings = true;
}, message =>
{
Expand All @@ -935,7 +935,7 @@ private async Task LoadFile(string filename, bool preventClose = false)
}
catch (Exception e)
{
this.ShowError("An error occured while trying to load:\n" + e.Message, "Load error");
this.ShowErrorInvoke("An error occured while trying to load:\n" + e.Message, "Load error");
}

Dispatcher.Invoke(async () =>
Expand Down Expand Up @@ -1153,10 +1153,7 @@ private async Task SaveFile(string filename, bool suppressDebug = false)
catch { }
}

Dispatcher.Invoke(() =>
{
this.ShowError("An error occured while trying to save:\n" + e.Message, "Save error");
});
this.ShowErrorInvoke("An error occured while trying to save:\n" + e.Message, "Save error");

SaveSucceeded = false;
}
Expand Down Expand Up @@ -1189,10 +1186,7 @@ private async Task SaveFile(string filename, bool suppressDebug = false)
}
catch (Exception exc)
{
Dispatcher.Invoke(() =>
{
this.ShowError("An error occured while trying to save:\n" + exc.Message, "Save error");
});
this.ShowErrorInvoke("An error occured while trying to save:\n" + exc.Message, "Save error");

SaveSucceeded = false;
}
Expand Down Expand Up @@ -1257,8 +1251,7 @@ await Task.Run(() => {

if (!File.Exists(Path.Combine(cacheDirPath, num.ToString())))
{
this.ShowWarning("Decompiled code cache file for open data is missing, but its name present in the index.");

this.ShowWarningInvoke("Decompiled code cache file for open data is missing, but its name present in the index.");
return;
}

Expand All @@ -1269,7 +1262,7 @@ await Task.Run(() => {
string prevHash = fs.ReadLine();

if (!Regex.IsMatch(prevHash, "^[0-9a-fA-F]{32}$")) //if first 32 bytes of cache file are not a valid MD5
this.ShowWarning("Decompiled code cache for open file is broken.\nThe cache will be generated again.");
this.ShowWarningInvoke("Decompiled code cache for open file is broken.\nThe cache will be generated again.");
else
{
if (hash == prevHash)
Expand All @@ -1288,7 +1281,7 @@ await Task.Run(() => {
}
catch
{
this.ShowWarning("Decompiled code cache for open file is broken.\nThe cache will be generated again.");
this.ShowWarningInvoke("Decompiled code cache for open file is broken.\nThe cache will be generated again.");

Data.GMLCache = null;
Data.GMLCacheFailed = null;
Expand All @@ -1300,7 +1293,7 @@ await Task.Run(() => {
string[] invalidNames = Data.GMLCache.Keys.Except(codeNames).ToArray();
if (invalidNames.Length > 0)
{
this.ShowWarning($"Decompiled code cache for open file contains one or more non-existent code names (first - \"{invalidNames[0]}\").\nThe cache will be generated again.");
this.ShowWarningInvoke($"Decompiled code cache for open file contains one or more non-existent code names (first - \"{invalidNames[0]}\").\nThe cache will be generated again.");

Data.GMLCache = null;

Expand All @@ -1312,7 +1305,7 @@ await Task.Run(() => {
Data.GMLCacheWasSaved = true;
}
else
this.ShowWarning("Open file differs from the one the cache was generated for.\nThat decompiled code cache will be generated again.");
this.ShowWarningInvoke("Open file differs from the one the cache was generated for.\nThat decompiled code cache will be generated again.");
}
}
}
Expand Down Expand Up @@ -2341,7 +2334,7 @@ private async Task RunScriptNow(string path)
{
Console.WriteLine(exc.ToString());
Dispatcher.Invoke(() => CommandBox.Text = exc.Message);
this.ShowError(exc.Message, "Script compile error");
this.ShowErrorInvoke(exc.Message, "Script compile error");
ScriptExecutionSuccess = false;
ScriptErrorMessage = exc.Message;
ScriptErrorType = "CompilationErrorException";
Expand All @@ -2358,7 +2351,7 @@ private async Task RunScriptNow(string path)

Console.WriteLine(exc.ToString());
Dispatcher.Invoke(() => CommandBox.Text = exc.Message);
this.ShowError(isScriptException ? exc.Message : excString, "Script error");
this.ShowErrorInvoke(isScriptException ? exc.Message : excString, "Script error");
ScriptExecutionSuccess = false;
ScriptErrorMessage = exc.Message;
ScriptErrorType = "Exception";
Expand Down Expand Up @@ -2626,8 +2619,7 @@ public async void UpdateApp(SettingsWindow window)
"the Nightly builds if you don't have a GitHub account, or compile UTMT yourself.\n" +
"For any questions or more information, ask in the Underminers Discord server.");
window.UpdateButtonEnabled = true;
return;

return;
}

string sysDriveLetter = Path.GetTempPath()[0].ToString();
Expand Down Expand Up @@ -3209,7 +3201,7 @@ private async void MenuItem_OffsetMap_Click(object sender, RoutedEventArgs e)
}
catch (Exception ex)
{
this.ShowError("An error occured while trying to load:\n" + ex.Message, "Load error");
this.ShowErrorInvoke("An error occured while trying to load:\n" + ex.Message, "Load error");
}

Dispatcher.Invoke(() =>
Expand Down
53 changes: 53 additions & 0 deletions UndertaleModTool/MessageBoxExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,59 @@ public static MessageBoxResult ShowError(this Window window, string messageBoxTe
return ShowCore(window, messageBoxText, title, MessageBoxButton.OK, MessageBoxImage.Error);
}

/// <summary>
/// Invokes <paramref name="window"/> to show an informational <see cref="MessageBox"/> with <paramref name="window"/> as the parent.
/// </summary>
/// <param name="window">The parent from which the <see cref="MessageBox"/> will show.</param>
/// <param name="messageBoxText">A <see cref="string"/> that specifies the text to display.</param>
/// <param name="title">A <see cref="string"/> that specifies the title bar caption to display.</param>
/// <returns><see cref="MessageBoxResult.OK"/> or <see cref="MessageBoxResult.None"/> if
/// the <see cref="MessageBox"/> was cancelled.</returns>
public static MessageBoxResult ShowMessageInvoke(this Window window, string messageBoxText, string title = "UndertaleModTool")
{
return window.Dispatcher.Invoke(() => ShowCore(window, messageBoxText, title, MessageBoxButton.OK, MessageBoxImage.Information));
}

/// <summary>
/// Invokes <paramref name="window"/> to show a <see cref="MessageBox"/> prompting for a yes/no question with <paramref name="window"/> as the parent.
/// </summary>
/// <param name="window">The parent from which the <see cref="MessageBox"/> will show.</param>
/// <param name="messageBoxText">A <see cref="string"/> that specifies the text to display.</param>
/// <param name="icon">The <see cref="MessageBoxImage"/> to display.</param>
/// <param name="title">A <see cref="string"/> that specifies the title bar caption to display.</param>
/// <returns><see cref="MessageBoxResult.Yes"/> or <see cref="MessageBoxResult.No"/> depending on the users' answer.
/// <see cref="MessageBoxResult.None"/> if the <see cref="MessageBox"/> was cancelled.</returns>
public static MessageBoxResult ShowQuestionInvoke(this Window window, string messageBoxText, MessageBoxImage icon = MessageBoxImage.Question, string title = "UndertaleModTool")
{
return window.Dispatcher.Invoke(() => ShowCore(window, messageBoxText, title, MessageBoxButton.YesNo, icon));
}

/// <summary>
/// Invokes <paramref name="window"/> to show a warning <see cref="MessageBox"/> with <paramref name="window"/> as the parent.
/// </summary>
/// <param name="window">The parent from which the <see cref="MessageBox"/> will show.</param>
/// <param name="messageBoxText">A <see cref="string"/> that specifies the text to display.</param>
/// <param name="title">A <see cref="string"/> that specifies the title bar caption to display.</param>
/// <returns><see cref="MessageBoxResult.OK"/> or <see cref="MessageBoxResult.None"/> if
/// the <see cref="MessageBox"/> was cancelled.</returns>
public static MessageBoxResult ShowWarningInvoke(this Window window, string messageBoxText, string title = "Warning")
{
return window.Dispatcher.Invoke(() => ShowCore(window, messageBoxText, title, MessageBoxButton.OK, MessageBoxImage.Warning));
}

/// <summary>
/// Invokes <paramref name="window"/> to show an error <see cref="MessageBox"/> with <paramref name="window"/> as the parent.
/// </summary>
/// <param name="window">The parent from which the <see cref="MessageBox"/> will show.</param>
/// <param name="messageBoxText">A <see cref="string"/> that specifies the text to display.</param>
/// <param name="title">A <see cref="string"/> that specifies the title bar caption to display.</param>
/// <returns><see cref="MessageBoxResult.OK"/> or <see cref="MessageBoxResult.None"/> if
/// the <see cref="MessageBox"/> was cancelled.</returns>
public static MessageBoxResult ShowErrorInvoke(this Window window, string messageBoxText, string title = "Error")
{
return window.Dispatcher.Invoke(() => ShowCore(window, messageBoxText, title, MessageBoxButton.OK, MessageBoxImage.Error));
}

/// <summary>
/// The wrapper for the extensions to directly call <see cref="MessageBox.Show(Window, string, string, MessageBoxButton, MessageBoxImage)"/>.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions UndertaleModTool/ProfileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ await Task.Run(() =>

if (!SettingsWindow.ProfileMessageShown)
{
this.ShowMessage(@"The profile for your game loaded successfully!
this.ShowMessageInvoke(@"The profile for your game loaded successfully!

UndertaleModTool now uses the ""Profile"" system by default for code.
Using the profile system, many new features are available to you!
Expand Down Expand Up @@ -383,7 +383,7 @@ await Task.Run(() =>
Directory.CreateDirectory(profDir);
Directory.CreateDirectory(Path.Combine(profDir, "Main"));
Directory.CreateDirectory(Path.Combine(profDir, "Temp"));
this.ShowMessage("Profile saved successfully to " + ProfileHash);
this.ShowMessageInvoke("Profile saved successfully to " + ProfileHash);
}
if (SettingsWindow.DeleteOldProfileOnSave && copyProfile)
{
Expand Down