|
| 1 | +using System.Diagnostics; |
| 2 | +using Microsoft.UI; |
| 3 | +using UniGetUI.Core.Data; |
| 4 | +using UniGetUI.Core.Language; |
| 5 | +using UniGetUI.Core.Tools; |
| 6 | + |
| 7 | +namespace UniGetUI; |
| 8 | + |
| 9 | +public static class CrashHandler |
| 10 | +{ |
| 11 | + private const uint MB_ICONSTOP = 0x00000010; |
| 12 | + |
| 13 | + [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode)] |
| 14 | + private static extern int MessageBox(IntPtr hWnd, string lpText, string lpCaption, uint uType); |
| 15 | + |
| 16 | + private static bool _reportMissingFiles() |
| 17 | + { |
| 18 | + try |
| 19 | + { |
| 20 | + var errorMessage = "UniGetUI has detected that some required files are missing." |
| 21 | + + "\n\nThis might be caused by an incomplete installation or corrupted files. Please reinstall UniGetUI." |
| 22 | + + "\n\nRun UniGetUI with the parameter '--no-corrupt-dialog' to get more details about the crash."; |
| 23 | + |
| 24 | + var title = "UniGetUI - Missing Files"; |
| 25 | + |
| 26 | + MessageBox(IntPtr.Zero, errorMessage, title, MB_ICONSTOP); |
| 27 | + return true; |
| 28 | + } |
| 29 | + catch |
| 30 | + { |
| 31 | + return false; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public static void ReportFatalException(Exception e) |
| 36 | + { |
| 37 | + Debugger.Break(); |
| 38 | + |
| 39 | + |
| 40 | + if (!Environment.GetCommandLineArgs().Contains(CLIHandler.NO_CORRUPT_DIALOG)) |
| 41 | + { |
| 42 | + Exception? fileEx = e; |
| 43 | + while (fileEx is not null) |
| 44 | + { |
| 45 | + if (fileEx.ToString().Contains("Could not load file or assembly")) |
| 46 | + { |
| 47 | + if (_reportMissingFiles()) |
| 48 | + { |
| 49 | + Environment.Exit(1); |
| 50 | + } |
| 51 | + } |
| 52 | + fileEx = fileEx.InnerException; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + string LangName = "Unknown"; |
| 57 | + try |
| 58 | + { |
| 59 | + LangName = CoreTools.GetCurrentLocale(); |
| 60 | + } |
| 61 | + catch |
| 62 | + { |
| 63 | + // ignored |
| 64 | + } |
| 65 | + |
| 66 | + string Error_String = $@" |
| 67 | + Windows version: {Environment.OSVersion.VersionString} |
| 68 | + Language: {LangName} |
| 69 | + APP Version: {CoreData.VersionName} |
| 70 | + APP Build number: {CoreData.BuildNumber} |
| 71 | + Executable: {Environment.ProcessPath} |
| 72 | +
|
| 73 | +Crash HResult: 0x{(uint)e.HResult:X} ({(uint)e.HResult}, {e.HResult}) |
| 74 | +Crash Message: {e.Message} |
| 75 | +
|
| 76 | +Crash Traceback: |
| 77 | +{e.StackTrace}"; |
| 78 | + |
| 79 | + try |
| 80 | + { |
| 81 | + int i = 0; |
| 82 | + while (e.InnerException is not null) |
| 83 | + { |
| 84 | + i++; |
| 85 | + e = e.InnerException; |
| 86 | + Error_String += $@" |
| 87 | +
|
| 88 | +
|
| 89 | +--------------------- |
| 90 | +Inner exception ({i}): |
| 91 | +Crash HResult: 0x{(uint)e.HResult:X} ({(uint)e.HResult}, {e.HResult}) |
| 92 | +Crash Message: {e.Message} |
| 93 | +
|
| 94 | +Crash Traceback: |
| 95 | +{e.StackTrace}"; |
| 96 | + } |
| 97 | + |
| 98 | + if (i == 0) |
| 99 | + { |
| 100 | + Error_String += $"\n\n\nNo inner exceptions found"; |
| 101 | + } |
| 102 | + } catch |
| 103 | + { |
| 104 | + // ignore |
| 105 | + } |
| 106 | + |
| 107 | + Console.WriteLine(Error_String); |
| 108 | + |
| 109 | + string ErrorBody = "https://www.marticliment.com/error-report/?appName=UniGetUI^&errorBody=" + |
| 110 | + Uri.EscapeDataString(Error_String.Replace("\n", "{l}")); |
| 111 | + |
| 112 | + Console.WriteLine(ErrorBody); |
| 113 | + |
| 114 | + using Process cmd = new(); |
| 115 | + cmd.StartInfo.FileName = "cmd.exe"; |
| 116 | + cmd.StartInfo.RedirectStandardInput = true; |
| 117 | + cmd.StartInfo.RedirectStandardOutput = true; |
| 118 | + cmd.StartInfo.CreateNoWindow = true; |
| 119 | + cmd.StartInfo.UseShellExecute = false; |
| 120 | + cmd.Start(); |
| 121 | + cmd.StandardInput.WriteLine("start " + ErrorBody); |
| 122 | + cmd.StandardInput.WriteLine("exit"); |
| 123 | + cmd.WaitForExit(); |
| 124 | + Environment.Exit(1); |
| 125 | + } |
| 126 | +} |
0 commit comments