CrashReport.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using PixiEditor.Helpers;
  2. using PixiEditor.Helpers.Extensions;
  3. using PixiEditor.Parser;
  4. using PixiEditor.ViewModels;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.IO.Compression;
  10. using System.Linq;
  11. using System.Reflection;
  12. using System.Text;
  13. namespace PixiEditor.Models.DataHolders
  14. {
  15. public class CrashReport : IDisposable
  16. {
  17. public static CrashReport Generate(Exception exception)
  18. {
  19. StringBuilder builder = new();
  20. DateTime currentTime = DateTime.Now;
  21. builder
  22. .AppendLine($"PixiEditor {VersionHelpers.GetCurrentAssemblyVersionString()} crashed on {currentTime:yyyy.MM.dd} at {currentTime:HH:mm:ss}\n")
  23. .AppendLine("-----System Information----")
  24. .AppendLine("General:")
  25. .AppendLine($" OS: {Environment.OSVersion.VersionString}")
  26. .AppendLine();
  27. CrashHelper helper = new();
  28. try
  29. {
  30. helper.GetCPUInformation(builder);
  31. }
  32. catch (Exception cpuE)
  33. {
  34. builder.AppendLine($"Error ({cpuE.GetType().FullName}: {cpuE.Message}) while gathering CPU information, skipping...");
  35. }
  36. try
  37. {
  38. helper.GetGPUInformation(builder);
  39. }
  40. catch (Exception gpuE)
  41. {
  42. builder.AppendLine($"Error ({gpuE.GetType().FullName}: {gpuE.Message}) while gathering GPU information, skipping...");
  43. }
  44. try
  45. {
  46. helper.GetMemoryInformation(builder);
  47. }
  48. catch (Exception memE)
  49. {
  50. builder.AppendLine($"Error ({memE.GetType().FullName}: {memE.Message}) while gathering memory information, skipping...");
  51. }
  52. CrashHelper.AddExceptionMessage(builder, exception);
  53. string filename = $"crash-{currentTime:yyyy-MM-dd_HH-mm-ss_fff}.zip";
  54. string path = Path.Combine(
  55. Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
  56. "PixiEditor",
  57. "crash_logs");
  58. Directory.CreateDirectory(path);
  59. CrashReport report = new();
  60. report.FilePath = Path.Combine(path, filename);
  61. report.ReportText = builder.ToString();
  62. return report;
  63. }
  64. public static CrashReport Parse(string path)
  65. {
  66. CrashReport report = new();
  67. report.FilePath = path;
  68. report.ZipFile = System.IO.Compression.ZipFile.Open(path, ZipArchiveMode.Read);
  69. report.ExtractReport();
  70. return report;
  71. }
  72. public string FilePath { get; set; }
  73. public string ReportText { get; set; }
  74. private ZipArchive ZipFile { get; set; }
  75. public int GetDocumentCount() => ZipFile.Entries.Where(x => x.FullName.EndsWith(".pixi")).Count();
  76. public List<Document> RecoverDocuments()
  77. {
  78. List<Document> documents = new();
  79. foreach (ZipArchiveEntry entry in ZipFile.Entries.Where(x => x.FullName.EndsWith(".pixi")))
  80. {
  81. using Stream stream = entry.Open();
  82. Document document;
  83. try
  84. {
  85. document = PixiParser.Deserialize(stream).ToDocument();
  86. document.ChangesSaved = false;
  87. }
  88. catch
  89. {
  90. continue;
  91. }
  92. documents.Add(document);
  93. }
  94. return documents;
  95. }
  96. public void Dispose()
  97. {
  98. ZipFile.Dispose();
  99. }
  100. public void RestartToCrashReport()
  101. {
  102. Process process = new();
  103. process.StartInfo = new()
  104. {
  105. FileName = Path.ChangeExtension(Assembly.GetExecutingAssembly().Location, "exe"),
  106. Arguments = $"--crash \"{Path.GetFullPath(FilePath)}\""
  107. };
  108. process.Start();
  109. }
  110. public bool TrySave()
  111. {
  112. try
  113. {
  114. Save();
  115. return true;
  116. }
  117. catch
  118. {
  119. return false;
  120. }
  121. }
  122. public void Save()
  123. {
  124. using FileStream zipStream = new(FilePath, FileMode.Create, FileAccess.Write);
  125. using ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Create);
  126. using (Stream reportStream = archive.CreateEntry("report.txt").Open())
  127. {
  128. reportStream.Write(Encoding.UTF8.GetBytes(ReportText));
  129. }
  130. foreach (Document document in ViewModelMain.Current.BitmapManager.Documents)
  131. {
  132. try
  133. {
  134. string documentPath =
  135. $"{(string.IsNullOrWhiteSpace(document.DocumentFilePath) ? "Unsaved" : Path.GetFileNameWithoutExtension(document.DocumentFilePath))}-{document.OpenedUTC}.pixi".Replace(':', '_');
  136. byte[] serialized = PixiParser.Serialize(document.ToSerializable());
  137. using Stream documentStream = archive.CreateEntry($"Documents/{documentPath}").Open();
  138. documentStream.Write(serialized);
  139. }
  140. catch { }
  141. }
  142. }
  143. private void ExtractReport()
  144. {
  145. ZipArchiveEntry entry = ZipFile.GetEntry("report.txt");
  146. using Stream stream = entry.Open();
  147. byte[] encodedReport = new byte[entry.Length];
  148. stream.Read(encodedReport);
  149. ReportText = Encoding.UTF8.GetString(encodedReport);
  150. }
  151. public class CrashReportUserMessage
  152. {
  153. public string Message { get; set; }
  154. public string Mail { get; set; }
  155. }
  156. }
  157. }