public static async Task SendToDiscord(CrashInfo a_CrashInfo, string a_CrashDescription, string a_AppName = "") { string filenamePrefix = a_AppName != string.Empty ? a_AppName + "-" : ""; string versionString = a_CrashInfo.m_BuildVersion != string.Empty ? a_CrashInfo.m_BuildVersion + "-" : ""; string fileName = filenamePrefix + versionString + "Crash-" + DateTime.UtcNow.ToString("yyyy-MM-dd--HH-mm") + ".zip"; if (s_CrashReportEmbedColor == string.Empty) { s_CrashReportEmbedColor = "16745472"; } string embedStr = string.Empty; if (a_CrashDescription != string.Empty) { a_CrashDescription = a_CrashDescription.Replace("\r", "").Replace("\n", "\\n"); embedStr = "{" + "\"embeds\": " + "[" + "{" + "\"title\": \"Crash Report\"," + "\"description\": \"" + a_CrashDescription + "\"," + "\"color\": \"" + s_CrashReportEmbedColor + "\"" + "}" + "]" + "}"; } try { if (embedStr == string.Empty && a_CrashInfo == null) { return; } var content = new MultipartFormDataContent(s_MultipartBoundary); if (a_CrashInfo != null) { ByteArrayContent crashContent = new ByteArrayContent(a_CrashInfo.ZipCrashInfo()); content.Add(crashContent, fileName, fileName); } if (embedStr != string.Empty) { content.Add(new StringContent(embedStr), "payload_json"); } await s_HttpClient.PostAsync(s_WebhookUrl, content); } catch (HttpRequestException e) { //Console.WriteLine(e.Message); } }
private async void SendAndClose_Click(object sender, RoutedEventArgs e) { btnSendAndClose.IsEnabled = false; var ci = CrashInfo.GetCrashInfo(IncludeCrashLog.IsEnabled); await Discord.SendToDiscord(ci, CrashDescription.Text, s_AppName); Close(); }
public static CrashInfo GetCrashInfo(bool a_IncludeLog) { // Check if crash data can be found DirectoryInfo crashDir = null; if (s_CrashReportLocation != string.Empty) { crashDir = new DirectoryInfo(s_CrashReportLocation); crashDir.Refresh(); } var dir = crashDir; if (crashDir != null && !crashDir.Exists) { string path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); path += "\\" + s_AppName + "\\Saved\\Crashes"; crashDir = new DirectoryInfo(path); if (!crashDir.Exists) { return(null); } else { // Get the newest folder in the crash directory dir = crashDir.GetDirectories().OrderByDescending(d => d.LastWriteTimeUtc).First(); } } var info = new CrashInfo(); dir.Refresh(); FileInfo[] fileBuffer; if (a_IncludeLog) { // Get the log file fileBuffer = dir.GetFiles("*.log"); if (fileBuffer.Length > 0) { FileInfo logFile = fileBuffer.First(); info.m_LogContent = File.ReadAllText(logFile.FullName); } } // Get the xml log file fileBuffer = dir.GetFiles("*.runtime-xml"); if (fileBuffer.Length > 0) { FileInfo xmlFile = fileBuffer.First(); info.m_XmlContent = File.ReadAllText(xmlFile.FullName); } // Get the minidump fileBuffer = dir.GetFiles("*.dmp"); if (fileBuffer.Length > 0) { FileInfo dumpFile = fileBuffer.First(); info.m_MiniDump = File.ReadAllBytes(dumpFile.FullName); } // Determine build version if (Properties.Resources.RelativeVersionFilePath != string.Empty) { string gameDir = string.Empty; if (s_CrashReportLocation.Contains("/AppData/Local/")) { // Get the game's base directory based on the current working directory string workingDirectory = Directory.GetCurrentDirectory().Replace("\\", "/"); gameDir = workingDirectory.Substring(0, workingDirectory.LastIndexOf("/Binaries/")); gameDir = gameDir.Substring(0, gameDir.LastIndexOf("/")); } else { // The crash report is (most likely) located in the game's folder gameDir = s_CrashReportLocation.Substring(0, s_CrashReportLocation.LastIndexOf("/Saved/Crashes/")); gameDir = gameDir.Substring(0, gameDir.LastIndexOf("/")); } info.m_BuildVersion = GetBuildVersion(gameDir); } return(info); }