Forráskód Böngészése

Don't autosave all files on crash

CPKreuz 1 éve
szülő
commit
4fd89f8543

+ 2 - 0
src/PixiEditor.Extensions/Common/UserPreferences/PreferencesConstants.cs

@@ -25,4 +25,6 @@ public static class PreferencesConstants
     
     public const string SaveSessionStateEnabled = nameof(SaveSessionStateEnabled);
     public const bool SaveSessionStateDefault = true;
+
+    public const string LastCrashFile = nameof(LastCrashFile);
 }

+ 2 - 1
src/PixiEditor/Data/Localization/Languages/en.json

@@ -634,5 +634,6 @@
   "LOGOFF_TITLE": "Sign out?",
   "SHUTDOWN_TITLE": "Shut down?",
   
-  "BACK_UP": "Backup"
+  "BACK_UP": "Backup",
+  "SEND": "Send"
 }

+ 11 - 0
src/PixiEditor/Helpers/CrashHelper.cs

@@ -6,6 +6,7 @@ using System.Text;
 using System.Windows.Input;
 using ByteSizeLib;
 using Hardware.Info;
+using PixiEditor.Extensions.Common.UserPreferences;
 using PixiEditor.Models.DataHolders;
 using Mouse = System.Windows.Input.Mouse;
 
@@ -28,6 +29,16 @@ internal class CrashHelper
         
         var report = CrashReport.Generate(exception);
         report.TrySave();
+
+        try
+        {
+            IPreferences.Current?.UpdateLocalPreference(PreferencesConstants.LastCrashFile, report.FilePath);
+        }
+        catch
+        {
+            // ignored
+        }
+        
         report.RestartToCrashReport();
     }
 

+ 19 - 14
src/PixiEditor/ViewModels/CrashReportViewModel.cs

@@ -56,22 +56,27 @@ internal class CrashReportViewModel : ViewModelBase
 
         if (showMissingFilesDialog)
         {
-            var dialog = new OptionsDialog<LocalizedString>(
-                "CRASH_NOT_ALL_DOCUMENTS_RECOVERED_TITLE",
-                new LocalizedString("CRASH_NOT_ALL_DOCUMENTS_RECOVERED"))
+            ShowMissingFilesDialog(CrashReport);
+        }
+    }
+
+    public static void ShowMissingFilesDialog(CrashReport crashReport)
+    {
+        var dialog = new OptionsDialog<LocalizedString>(
+            "CRASH_NOT_ALL_DOCUMENTS_RECOVERED_TITLE",
+            new LocalizedString("CRASH_NOT_ALL_DOCUMENTS_RECOVERED"))
+        {
             {
+                "SEND", _ =>
                 {
-                    "SEND", _ =>
-                    {
-                        var sendReportDialog = new SendCrashReportWindow(CrashReport);
-                        sendReportDialog.ShowDialog();
-                    }
-                },
-                "CLOSE"
-            };
-
-            dialog.ShowDialog(true);
-        }
+                    var sendReportDialog = new SendCrashReportWindow(crashReport);
+                    sendReportDialog.ShowDialog();
+                }
+            },
+            "CLOSE"
+        };
+
+        dialog.ShowDialog(true);
     }
 
     [Conditional("DEBUG")]

+ 77 - 3
src/PixiEditor/ViewModels/SubViewModels/Main/FileViewModel.cs

@@ -100,9 +100,35 @@ internal class FileViewModel : SubViewModel<ViewModelMain>
         List<string> args = StartupArgs.Args;
         string file = args.FirstOrDefault(x => Importer.IsSupportedFile(x) && File.Exists(x));
 
-        if (!args.Contains("--crash"))
+        var preferences = IPreferences.Current;
+
+        try
+        {
+            if (!args.Contains("--crash"))
+            {
+                var lastCrash = preferences!.GetLocalPreference<string>(PreferencesConstants.LastCrashFile);
+
+                if (lastCrash == null)
+                {
+                    ReopenTempFiles();
+                }
+                else
+                {
+                    preferences.UpdateLocalPreference<string>(PreferencesConstants.LastCrashFile, null);
+
+                    var report = CrashReport.Parse(lastCrash);
+                    OpenFromReport(report, out bool showMissingFilesDialog);
+
+                    if (showMissingFilesDialog)
+                    {
+                        CrashReportViewModel.ShowMissingFilesDialog(report);
+                    }
+                }
+            }
+        }
+        catch (Exception exc)
         {
-            ReopenTempFiles();
+            CrashHelper.SendExceptionInfoToWebhook(exc);
         }
         
         if (file != null)
@@ -111,13 +137,61 @@ internal class FileViewModel : SubViewModel<ViewModelMain>
         }
         else if ((Owner.DocumentManagerSubViewModel.Documents.Count == 0 && !args.Contains("--crash")) && !args.Contains("--openedInExisting"))
         {
-            if (IPreferences.Current.GetPreference("ShowStartupWindow", true))
+            if (preferences!.GetPreference("ShowStartupWindow", true))
             {
                 OpenHelloTherePopup();
             }
         }
     }
 
+    public void OpenFromReport(CrashReport report, out bool showMissingFilesDialog)
+    {
+        var documents = report.RecoverDocuments();
+        
+        var i = 0;
+
+        Exception firstException = null;
+        Exception secondException = null;
+        Exception thirdException = null;
+        
+        foreach (var document in documents)
+        {
+            try
+            {
+                OpenRecoveredDotPixi(document.Path.OriginalPath, document.Path.AutosavePath, document.Path.GetAutosaveGuid(), document.GetRecoveredBytes());
+                i++;
+            }
+            catch (Exception e)
+            {
+                firstException = e;
+                
+                try
+                {
+                    OpenFromPath(document.Path.AutosavePath, false);
+                    
+                }
+                catch (Exception deepE)
+                {
+                    secondException = deepE;
+                    
+                    try
+                    {
+                        OpenRecoveredDotPixi(document.Path.OriginalPath, document.Path.AutosavePath, document.Path.GetAutosaveGuid(), document.GetAutosaveBytes());
+                    }
+                    catch (Exception veryDeepE)
+                    {
+                        thirdException = veryDeepE;
+                    }
+                }
+            }
+
+            var exceptions = new[] { firstException, secondException, thirdException };
+            CrashHelper.SendExceptionInfoToWebhook(new AggregateException(exceptions.Where(x => x != null).ToArray()));
+        }
+
+        showMissingFilesDialog = documents.Count != i;
+    }
+
     private void ReopenTempFiles()
     {
         var preferences = Owner.Preferences;

+ 6 - 51
src/PixiEditor/Views/MainWindow.xaml.cs

@@ -115,45 +115,11 @@ internal partial class MainWindow : Window
     {
         var app = (App)Application.Current;
         MainWindow window = new(app.InitApp());
-        FileViewModel fileVM = window.services.GetRequiredService<FileViewModel>();
-        var documents = report.RecoverDocuments();
-
-        var i = 0;
-
-        foreach (var document in documents)
-        {
-            try
-            {
-                fileVM.OpenRecoveredDotPixi(document.Path.OriginalPath, document.Path.AutosavePath, document.Path.GetAutosaveGuid(), document.GetRecoveredBytes());
-                i++;
-            }
-            catch (Exception e)
-            {
-                try
-                {
-                    fileVM.OpenFromPath(document.Path.AutosavePath, false);
-                    
-                }
-                catch (Exception deepE)
-                {
-                    try
-                    {
-                        fileVM.OpenRecoveredDotPixi(document.Path.OriginalPath, document.Path.AutosavePath, document.Path.GetAutosaveGuid(), document.GetAutosaveBytes());
-                    }
-                    catch (Exception veryDeepE)
-                    {
-                        CrashHelper.SendExceptionInfoToWebhook(veryDeepE);
-                    }
-                    
-                    CrashHelper.SendExceptionInfoToWebhook(deepE);
-                }
-                
-                CrashHelper.SendExceptionInfoToWebhook(e);
-            }
-        }
-
-        showMissingFilesDialog = documents.Count != i;
+        var files = window.services.GetRequiredService<FileViewModel>();
+        var preferences = window.services.GetRequiredService<IPreferences>();
 
+        files.OpenFromReport(report, out showMissingFilesDialog);
+        
         return window;
 
         MainWindow GetMainWindow()
@@ -275,20 +241,9 @@ internal partial class MainWindow : Window
         }
     }
 
-    private void MainWindow_Initialized(object sender, EventArgs e)
+    private void MainWindow_Initialized(object sender, EventArgs _)
     {
-        AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
-        {
-            try
-            {
-                DataContext.AutosaveAllForNextSession();
-            }
-            finally
-            {
-                CrashHelper.SaveCrashInfo((Exception)e.ExceptionObject);
-
-            }
-        };
+        AppDomain.CurrentDomain.UnhandledException += (_, e) => CrashHelper.SaveCrashInfo((Exception)e.ExceptionObject);
     }
 
     private void MainWindow_Drop(object sender, DragEventArgs e)