-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
193 lines (162 loc) Β· 7.35 KB
/
Program.cs
File metadata and controls
193 lines (162 loc) Β· 7.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using BugSplatDotNetStandard;
using CommandLine;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using static BugSplatDotNetStandard.BugSplat;
namespace BugSplatCrashHandler
{
static class Program
{
public static IniFile CrashIni { get; private set; } = new IniFile();
public static readonly string USER_CREDS_REGISTRY_KEY_PATH = "Software\\BugSplat\\UserCredentials";
public static readonly string USER_NAME_REGISTRY_KEY = "UserName";
public static readonly string USER_EMAIL_REGISTRY_KEY = "UserEmail";
public class Options
{
[Option('i', "iniFile", Required = false, HelpText = "Configuration file.")]
public string IniFile { get; set; }
[Option('q', "quietMode", Required = false, HelpText = "Don't prompt for user input.")]
public bool QuietMode { get; set; }
[Option('z', "zipFile", Required = false, HelpText = "Zipped crash report to upload.")]
public bool ZipFile { get; set; }
// TODO DP handle 'f' (MDSF_LOGFILE)
[Option('f', "logToFile", Required = false, HelpText = "Write BugSplatCrashHandler log statements to a file.")]
public bool LogToFile { get; set; }
// TODO DP handle 'l' (MDSF_LOGCONSOLE)
[Option('l', "logToConsole", Required = false, HelpText = "Write BugSplatCrashHandler log statements to standard output.")]
public bool LogToConsole { get; set; }
}
static void RunOptions(Options opts)
{
// We won't get here if IniFile is null, but the compiler can't figure that out
if (opts.IniFile != null)
{
CrashIni = new IniFile(opts.IniFile);
}
var options = new BugSplatPostOptions();
// User entered credentials saved here
var userCredsKey = Registry.CurrentUser.CreateSubKey(USER_CREDS_REGISTRY_KEY_PATH);
// Allow either Database or Vendor (legacy) for database property
var database = CrashIni.Read("Database");
database = string.IsNullOrEmpty(database) ? CrashIni.Read("Vendor") : database;
if (string.IsNullOrEmpty(database))
{
MessageBox.Show("No database property found!", "Error");
Environment.Exit(1);
}
var application = CrashIni.Read("Application", true);
var version = CrashIni.Read("Version", true);
var crashType = CrashIni.Read("CrashType", false);
var minidumpPath = CrashIni.Read("MiniDump", false);
var xmlReportPath = CrashIni.Read("XmlReport", false);
if (string.IsNullOrEmpty(minidumpPath) && string.IsNullOrEmpty(xmlReportPath))
{
MessageBox.Show($"Either MiniDump or XmlReport parameter is required");
Application.Exit();
}
var crashReportFile = string.IsNullOrEmpty(xmlReportPath) ? new FileInfo(minidumpPath) : new FileInfo(xmlReportPath);
options.CrashTypeId = crashReportFile.Extension.ToLower().Equals(".xml") ? XmlNameToXmlCrashTypeId(crashReportFile.Name) : CrashTypeStringToCrashTypeId(crashType);
// ToDo: We need API support for the Notes field
var notes = CrashIni.Read("Notes", false);
// Read default user/email from ini
options.User = CrashIni.Read("User", false);
options.Email = CrashIni.Read("Email", false);
// If defaults for user/email are empty, get last user-entered values
if (options.User.Length == 0)
{
var rval = userCredsKey?.GetValue(USER_NAME_REGISTRY_KEY, null);
options.User = rval?.ToString();
}
if (options.Email.Length == 0)
{
var rval = userCredsKey?.GetValue(USER_EMAIL_REGISTRY_KEY, null);
options.Email = rval?.ToString();
}
var userDescription = CrashIni.Read("UserDescription", false);
options.Description = userDescription;
// Add each file attachment
var logFilePath = CrashIni.Read("LogFilePath", false);
if (logFilePath.Length > 0)
{
var logFile = new FileInfo(logFilePath);
if (logFile.Exists)
{
options.Attachments.Add(logFile);
}
}
var attachmentIndex = 0;
while (true)
{
var fname = CrashIni.Read("AdditionalFile" + attachmentIndex++, false);
if (fname.Length <= 0)
{
break;
}
var item = new FileInfo(fname);
if (item.Exists)
{
options.Attachments.Add(item);
}
}
var bugsplat = new BugSplat(database, application, version);
if (opts.QuietMode && !crashReportFile.Exists)
{
Environment.Exit(1);
}
if (opts.QuietMode && crashReportFile.Exists)
{
var poster = new CrashPoster(bugsplat);
poster.PostCrashAndDisplaySupportResponseIfAvailable(crashReportFile, options);
Environment.Exit(0);
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new CrashDialogForm(bugsplat, crashReportFile, options));
}
static void HandleParseError(IEnumerable<Error> errs)
{
MessageBox.Show(errs.ToString(), "BugSplat Crash Handler", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
var args = Environment.GetCommandLineArgs();
new Parser(with => { with.IgnoreUnknownArguments = true; })
.ParseArguments<Options>(args)
.WithParsed(RunOptions)
.WithNotParsed(HandleParseError);
}
private static int CrashTypeStringToCrashTypeId(string typestr)
{
if (typestr.Equals("Windows.Native", StringComparison.OrdinalIgnoreCase))
{
return (int)MinidumpTypeId.WindowsNative;
}
else if (typestr.Equals("Windows.NET", StringComparison.OrdinalIgnoreCase))
{
return (int)MinidumpTypeId.DotNet;
}
else if (typestr.Equals("UnityNativeWindows", StringComparison.OrdinalIgnoreCase))
{
return (int)MinidumpTypeId.UnityNativeWindows;
}
return (int)MinidumpTypeId.Unknown;
}
private static int XmlNameToXmlCrashTypeId(string name)
{
var asan = new Regex("bsAsanReport.*\\.xml");
if (asan.IsMatch(name))
{
return (int)XmlTypeId.Asan;
}
return (int)XmlTypeId.Xml;
}
}
}