2
0
Эх сурвалжийг харах

Bit of previous refactoring

CPKreuz 1 жил өмнө
parent
commit
73ecd2738d

+ 46 - 23
src/PixiEditor/Models/DataHolders/CrashReport.cs

@@ -91,40 +91,46 @@ internal class CrashReport : IDisposable
 
     public int GetDocumentCount() => ZipFile.Entries.Where(x => x.FullName.EndsWith(".pixi")).Count();
 
-    public List<(CrashFilePathInfo originalPath, byte[] dotPixiBytes)> RecoverDocuments()
+    public bool TryRecoverDocuments(out List<RecoveredPixi> list)
     {
-        // Load .pixi files
-        Dictionary<string, byte[]> recoveredDocuments = new();
-        foreach (ZipArchiveEntry entry in ZipFile.Entries.Where(x => x.FullName.EndsWith(".pixi")))
+        try
         {
-            using Stream stream = entry.Open();
-            using MemoryStream memStream = new();
-            stream.CopyTo(memStream);
-            recoveredDocuments.Add(entry.FullName["Documents/".Length..], memStream.ToArray());
+            list = RecoverDocuments();
+        }
+        catch (Exception e)
+        {
+            list = null;
+            CrashHelper.SendExceptionInfoToWebhook(e);
+            return false;
         }
 
+        return true;
+    }
+    
+    public List<RecoveredPixi> RecoverDocuments()
+    {
         var originalPathsEntry = ZipFile.Entries.First(entry => entry.FullName == "DocumentInfo.json");
-        
+
         // Load original paths
-        Dictionary<string, CrashFilePathInfo> originalPaths;
+        Dictionary<string, string> paths;
         {
             using Stream stream = originalPathsEntry.Open();
             using StreamReader reader = new(stream);
             string json = reader.ReadToEnd();
-            originalPaths = JsonConvert.DeserializeObject<Dictionary<string, CrashFilePathInfo>>(json);
+            paths = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
         }
 
-        var list = new List<(CrashFilePathInfo originalPath, byte[] dotPixiBytes)>();
-
-        foreach (var document in recoveredDocuments)
+        // Load .pixi files
+        List<RecoveredPixi> recoveredDocuments = new();
+        foreach (var path in paths)
         {
-            var originalPath = originalPaths[document.Key];
-            list.Add((originalPath, document.Value));
+            recoveredDocuments.Add(new RecoveredPixi(path.Value, ZipFile.GetEntry($"Documents/{path.Key}")));
         }
 
-        return list;
+        return recoveredDocuments;
     }
 
+
     public void Dispose()
     {
         ZipFile.Dispose();
@@ -172,8 +178,8 @@ internal class CrashReport : IDisposable
 
         // Write the documents into zip
         int counter = 0;
-        Dictionary<string, CrashFilePathInfo> originalPaths = new();
-        foreach (DocumentViewModel document in vm.DocumentManagerSubViewModel.Documents)
+        var originalPaths = new Dictionary<string, string>();
+        foreach (var document in vm.DocumentManagerSubViewModel.Documents)
         {
             try
             {
@@ -187,7 +193,7 @@ internal class CrashReport : IDisposable
                 using Stream documentStream = archive.CreateEntry($"Documents/{nameInZip}").Open();
                 documentStream.Write(serialized);
 
-                originalPaths.Add(nameInZip, new CrashFilePathInfo(document.FullFilePath, null));
+                originalPaths.Add(nameInZip, document.FullFilePath);
             }
             catch { }
             counter++;
@@ -214,10 +220,27 @@ internal class CrashReport : IDisposable
         ReportText = Encoding.UTF8.GetString(encodedReport);
     }
 
-    internal class CrashReportUserMessage
+
+    public class RecoveredPixi
     {
-        public string Message { get; set; }
+        public string? Path { get; }
+        
+        public ZipArchiveEntry RecoveredEntry { get; }
+        
+        public byte[] GetRecoveredBytes()
+        {
+            var buffer = new byte[RecoveredEntry.Length];
+            using var stream = RecoveredEntry.Open();
 
-        public string Mail { get; set; }
+            stream.ReadExactly(buffer);
+            
+            return buffer;
+        }
+        
+        public RecoveredPixi(string? path, ZipArchiveEntry recoveredEntry)
+        {
+            Path = path;
+            RecoveredEntry = recoveredEntry;
+        }
     }
 }

+ 24 - 15
src/PixiEditor/Views/MainWindow.xaml.cs

@@ -109,31 +109,26 @@ internal partial class MainWindow : Window
 
     public static MainWindow CreateWithRecoveredDocuments(CrashReport report, out bool showMissingFilesDialog)
     {
-        var app = (App)Application.Current;
-        MainWindow window = new(app.InitApp());
-        FileViewModel fileVM = window.services.GetRequiredService<FileViewModel>();
-        var documents = report.RecoverDocuments();
+        var window = GetMainWindow();
+        var fileVM = window.services.GetRequiredService<FileViewModel>();
+
+        if (!report.TryRecoverDocuments(out var documents))
+        {
+            showMissingFilesDialog = true;
+            return window;
+        }
 
         var i = 0;
 
-        foreach (var (path, bytes) in documents)
+        foreach (var document in documents)
         {
             try
             {
-                fileVM.OpenRecoveredDotPixi(path.OriginalPath, bytes);
+                fileVM.OpenRecoveredDotPixi(document.Path, document.GetRecoveredBytes());
                 i++;
             }
             catch (Exception e)
             {
-                try
-                {
-                    fileVM.OpenFromPath(path.AutosavePath, false);
-                }
-                catch (Exception deepE)
-                {
-                    CrashHelper.SendExceptionInfoToWebhook(deepE);
-                }
-                
                 CrashHelper.SendExceptionInfoToWebhook(e);
             }
         }
@@ -141,6 +136,20 @@ internal partial class MainWindow : Window
         showMissingFilesDialog = documents.Count != i;
 
         return window;
+
+        MainWindow GetMainWindow()
+        {
+            try
+            {
+                var app = (App)Application.Current;
+                return new MainWindow(app.InitApp());
+            }
+            catch (Exception e)
+            {
+                Task.Run(() => CrashHelper.SendExceptionInfoToWebhook(e)).Wait();
+                throw;
+            }
+        }
     }
 
     /// <summary>Brings main window to foreground.</summary>