using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Net.Http; using System.Runtime.InteropServices; using System.Threading.Tasks; using Avalonia; using Avalonia.Controls.ApplicationLifetimes; using MsBox.Avalonia.Enums; using Lumafly.Models; using Lumafly.Services; using Lumafly.ViewModels; using Lumafly.Views.Windows; namespace Lumafly.Util; public static class DisplayErrors { public static async Task DisplayHashMismatch(HashMismatchException e) { await MessageBoxUtil.GetMessageBoxStandardWindow ( title: Resources.MLVM_DisplayHashMismatch_Msgbox_Title, text: string.Format(Resources.MLVM_DisplayHashMismatch_Msgbox_Text, e.Name, e.Actual, e.Expected), icon: Icon.Error ).ShowWindowDialogAsync(AvaloniaUtils.GetMainWindow()); } public static async Task DisplayGenericError(string action, string name, Exception e) { Trace.TraceError(e.ToString()); string additionalInfo = string.Empty; if (e is ReadableError readableException) additionalInfo = $" {readableException.Message}"; await DisplayGenericError(string.Format(Resources.MVVM_ExceptionOccur + additionalInfo, action, name), e); } public static async Task DisplayGenericError(string errorText, Exception? e = null) { if (e != null) Trace.TraceError(e.ToString()); await new ErrorPopup() { DataContext = new ErrorPopupViewModel(errorText, e) }.ShowDialog(AvaloniaUtils.GetMainWindow()); } public static async Task DisplayNetworkError(string name, HttpRequestException e) { Trace.WriteLine($"Failed to download {name}, {e}"); await MessageBoxUtil.GetMessageBoxStandardWindow ( title: Resources.MLVM_DisplayNetworkError_Msgbox_Title, text: string.Format(Resources.MLVM_DisplayNetworkError_Msgbox_Text, name), icon: Icon.Error ).ShowWindowDialogAsync(AvaloniaUtils.GetMainWindow()); } // asks user for confirmation on whether or not they want to uninstall/disable mod. // returns whether or not user presses yes on the message box public static async Task DisplayHasDependentsWarning(string modName, IEnumerable dependents) { var dependentsString = string.Join(", ", dependents.Select(x => x.Name)); var result = await MessageBoxUtil.GetMessageBoxStandardWindow ( title: Resources.MVVM_DependentsWarning_Header, text: string.Format(Resources.MVVM_DependentsWarning_Body, modName, dependentsString), icon: Icon.Stop, @enum: ButtonEnum.YesNo ).ShowWindowDialogAsync(AvaloniaUtils.GetMainWindow()); // return whether or not yes was clicked. Also don't remove mod when box is closed with the x return result.HasFlag(ButtonResult.Yes) && !result.HasFlag(ButtonResult.None); } // asks user for confirmation on whether or not they want to install dependencies when enabling the mod. // returns whether or not user presses yes on the message box public static async Task DisplayHasNotInstalledDependenciesWarning(string modName, IEnumerable dependencies) { var dependenciesString = string.Join(", ", dependencies.Select(x => x.Name)); var result = await MessageBoxUtil.GetMessageBoxStandardWindow ( title: Resources.MVVM_DependenciesNotInstalledWarning_Header, text: string.Format(Resources.MVVM_DependenciesNotInstalledWarning_Body, dependenciesString, modName), icon: Icon.Stop, @enum: ButtonEnum.YesNo ).ShowWindowDialogAsync(AvaloniaUtils.GetMainWindow()); // return whether or not yes was clicked. Also don't remove mod when box is closed with the x return result.HasFlag(ButtonResult.Yes) && !result.HasFlag(ButtonResult.None); } public static Task DisplayUninstallDependenciesConfirmation(List> options, bool hasExternalMods) { var window = new UninstallDependenciesConfirmationWindow { DataContext = new UninstallDependenciesConfirmationWindowViewModel(options, hasExternalMods) }; return window.ShowDialog(AvaloniaUtils.GetMainWindow()); } public static async Task DisplayAreYouSureWarning(string warningText) { var result = await MessageBoxUtil.GetMessageBoxStandardWindow ( title: Resources.MVVM_AreYouSure, text: warningText, icon: Icon.Stop, @enum: ButtonEnum.YesNo ).ShowWindowDialogAsync(AvaloniaUtils.GetMainWindow()); // return whether or not yes was clicked. Also don't remove mod when box is closed with the x return result.HasFlag(ButtonResult.Yes) && !result.HasFlag(ButtonResult.None); } public static async Task DoActionAfterConfirmation(bool shouldAskForConfirmation, Func> warningPopupDisplayer, Func action) { if (shouldAskForConfirmation) { if (await warningPopupDisplayer()) { await action(); } } else { await action(); } } private enum IOErrorType { LockedFile, WriteProtected, Other } public static async Task HandleIOExceptionWhenDownloading(Exception e, string action, ModItem? item = null) { var errorType = e switch { _ when e.Message.Contains("The process cannot access the file") => IOErrorType.LockedFile, _ when e.Message.Contains("The media is write protected") => IOErrorType.WriteProtected, _ => IOErrorType.Other }; string additionalText = ""; additionalText = errorType switch { IOErrorType.LockedFile => GiveMoreDetailsOnLockedFileError(e), IOErrorType.WriteProtected => GiveMoreDetailsOnWriteProtectedError(e), _ => "" }; if (item != null) { item.CallOnPropertyChanged(nameof(ModItem.InstallingButtonAccessible)); } await DisplayGenericError( $"Unable to {action} {item?.Name}.\n" + $"{Resources.MVVM_SystemIOException_GeneralReason}.\n" + additionalText, e); } private static string GetFileNameFromError(string message, string start) { int indexOfErrorStart = message.IndexOf(start, StringComparison.Ordinal); string filePath = message[indexOfErrorStart..].Split('\'')[1]; return filePath; } private static string GiveMoreDetailsOnWriteProtectedError(Exception e) { var additionalText = $"{Resources.MVVM_SystemIOException_ProtectedLocation}.\n"; try { string file = GetFileNameFromError(e.Message, "The media is write protected."); additionalText += $"{Resources.MVVM_SystemIOException_LumaflyCantAccessFile} {file}\n"; } catch (Exception) { // ignored as its not a requirement } additionalText += $"\n{Resources.MVVM_RunAsAdmin}"; return additionalText; } private static string GiveMoreDetailsOnLockedFileError(Exception e) { var additionalText = $"{Resources.MVVM_SystemIOException_GeneralFileLock}\n"; try { string filePath = GetFileNameFromError(e.Message, "The process cannot access the file"); if (OperatingSystem.IsWindows()) { var processes = FileAccessLookup.WhoIsLocking(filePath); if (processes.Count > 0) { var listOfProcesses = $"{string.Join("\n-", processes.Select(x => x.ProcessName))}"; Trace.WriteLine($"Following processes is locking the file {listOfProcesses}"); additionalText = $"\n{Resources.MVVM_SystemIOException_LockingProcessesList}\n{listOfProcesses}"; } } } catch (Exception) { //ignored as its not a requirement } return additionalText; } /// /// Ask the user if they want to run the program as administrator. If yes then start new instance as admin and shutdown current instance. /// /// The message stating why a restart is required public static async Task AskForAdminReload(string message) { if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return; // windows only if (AvaloniaUtils.IsAdministrator()) return; // already admin var res = await MessageBoxUtil.GetMessageBoxStandardWindow ( title: "Requesting running as administrator", text: message + " Do you want to reload as administrator and try again?", icon: Icon.Info, @enum: ButtonEnum.YesNo ).ShowAsPopupAsync(AvaloniaUtils.GetMainWindow()); await Task.Delay(500); // without this stuff breaks if (res == ButtonResult.Yes) { var exeName = Process.GetCurrentProcess().MainModule?.FileName; if (exeName != null) { Process.Start(new ProcessStartInfo(exeName) { Verb = "runas", // the way to run as admin UseShellExecute = true }); ((IClassicDesktopStyleApplicationLifetime?)Application.Current?.ApplicationLifetime)!.Shutdown(); await Task.Delay(500); // give it 0.5 seconds to restart so no extra actions happen and cause errors } } } } /// /// An generic error that should display additional information to the user /// public class ReadableError : Exception { public ReadableError(string additionalText) : base(additionalText) { } }