This repository has been archived by the owner on Oct 16, 2020. It is now read-only.
/
SharpDevelopMain.cs
228 lines (200 loc) · 7.09 KB
/
SharpDevelopMain.cs
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
// <version>$Revision$</version>
// </file>
using System;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.SharpDevelop.Project;
using ICSharpCode.SharpDevelop.Sda;
namespace ICSharpCode.SharpDevelop
{
/// <summary>
/// This Class is the Core main class, it starts the program.
/// </summary>
public class SharpDevelopMain
{
static string[] commandLineArgs = null;
public static string[] CommandLineArgs {
get {
return commandLineArgs;
}
}
static bool UseExceptionBox {
get {
#if DEBUG
if (Debugger.IsAttached) return false;
#endif
foreach(string arg in commandLineArgs) {
if (arg.Contains("noExceptionBox")) return false;
}
return true;
}
}
/// <summary>
/// Starts the core of SharpDevelop.
/// </summary>
[STAThread()]
public static void Main(string[] args)
{
commandLineArgs = args; // Needed by UseExceptionBox
// Do not use LoggingService here (see comment in Run(string[]))
if (UseExceptionBox) {
try {
Run();
} catch (Exception ex) {
try {
HandleMainException(ex);
} catch (Exception loadError) {
// HandleMainException can throw error when log4net is not found
MessageBox.Show(loadError.ToString(), "Critical error (Logging service defect?)");
}
}
} else {
Run();
}
}
static void HandleMainException(Exception ex)
{
LoggingService.Fatal(ex);
try {
Application.Run(new ExceptionBox(ex, "Unhandled exception terminated SharpDevelop", true));
} catch {
MessageBox.Show(ex.ToString(), "Critical error (cannot use ExceptionBox)");
}
}
static void Run()
{
// DO NOT USE LoggingService HERE!
// LoggingService requires ICSharpCode.Core.dll and log4net.dll
// When a method containing a call to LoggingService is JITted, the
// libraries are loaded.
// We want to show the SplashScreen while those libraries are loading, so
// don't call LoggingService.
#if DEBUG
Control.CheckForIllegalCrossThreadCalls = true;
#endif
bool noLogo = false;
Application.SetCompatibleTextRenderingDefault(false);
SplashScreenForm.SetCommandLineArgs(commandLineArgs);
foreach (string parameter in SplashScreenForm.GetParameterList()) {
if ("nologo".Equals(parameter, StringComparison.OrdinalIgnoreCase))
noLogo = true;
}
if (!CheckEnvironment())
return;
if (!noLogo) {
SplashScreenForm.ShowSplashScreen();
}
try {
RunApplication();
} finally {
if (SplashScreenForm.SplashScreen != null) {
SplashScreenForm.SplashScreen.Dispose();
}
}
}
static bool CheckEnvironment()
{
// Safety check: our setup already checks that .NET 4 is installed, but we manually check the .NET version in case SharpDevelop is
// used on another machine than it was installed on (e.g. "SharpDevelop on USB stick")
if (Environment.Version < new Version(4, 0, 30319)) {
MessageBox.Show("This version of SharpDevelop requires .NET 4.0. You are using: " + Environment.Version, "SharpDevelop");
return false;
}
return true;
}
static void RunApplication()
{
// The output encoding differs based on whether SharpDevelop is a console app (debug mode)
// or Windows app (release mode). Because this flag also affects the default encoding
// when reading from other processes' standard output, we explicitly set the encoding to get
// consistent behaviour in debug and release builds of SharpDevelop.
#if DEBUG
// Console apps use the system's OEM codepage, windows apps the ANSI codepage.
// We'll always use the Windows (ANSI) codepage.
try {
Console.OutputEncoding = System.Text.Encoding.Default;
} catch (IOException) {
// can happen if SharpDevelop doesn't have a console
}
#endif
LoggingService.Info("Starting SharpDevelop...");
try {
StartupSettings startup = new StartupSettings();
#if DEBUG
startup.UseSharpDevelopErrorHandler = UseExceptionBox;
#endif
Assembly exe = typeof(SharpDevelopMain).Assembly;
startup.ApplicationRootPath = Path.Combine(Path.GetDirectoryName(exe.Location), "..");
startup.AllowUserAddIns = true;
string configDirectory = ConfigurationManager.AppSettings["settingsPath"];
if (String.IsNullOrEmpty(configDirectory)) {
startup.ConfigDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"ICSharpCode/SharpDevelop" + RevisionClass.Major + "." + RevisionClass.Minor);
} else {
startup.ConfigDirectory = Path.Combine(Path.GetDirectoryName(exe.Location), configDirectory);
}
startup.DomPersistencePath = ConfigurationManager.AppSettings["domPersistencePath"];
if (string.IsNullOrEmpty(startup.DomPersistencePath)) {
startup.DomPersistencePath = Path.Combine(Path.GetTempPath(), "SharpDevelop" + RevisionClass.Major + "." + RevisionClass.Minor);
#if DEBUG
startup.DomPersistencePath = Path.Combine(startup.DomPersistencePath, "Debug");
#endif
} else if (startup.DomPersistencePath == "none") {
startup.DomPersistencePath = null;
}
startup.AddAddInsFromDirectory(Path.Combine(startup.ApplicationRootPath, "AddIns"));
// allows testing addins without having to install them
foreach (string parameter in SplashScreenForm.GetParameterList()) {
if (parameter.StartsWith("addindir:", StringComparison.OrdinalIgnoreCase)) {
startup.AddAddInsFromDirectory(parameter.Substring(9));
}
}
SharpDevelopHost host = new SharpDevelopHost(AppDomain.CurrentDomain, startup);
string[] fileList = SplashScreenForm.GetRequestedFileList();
if (fileList.Length > 0) {
if (LoadFilesInPreviousInstance(fileList)) {
LoggingService.Info("Aborting startup, arguments will be handled by previous instance");
return;
}
}
host.BeforeRunWorkbench += delegate {
if (SplashScreenForm.SplashScreen != null) {
SplashScreenForm.SplashScreen.BeginInvoke(new MethodInvoker(SplashScreenForm.SplashScreen.Dispose));
SplashScreenForm.SplashScreen = null;
}
};
WorkbenchSettings workbenchSettings = new WorkbenchSettings();
workbenchSettings.RunOnNewThread = false;
for (int i = 0; i < fileList.Length; i++) {
workbenchSettings.InitialFileList.Add(fileList[i]);
}
host.RunWorkbench(workbenchSettings);
} finally {
LoggingService.Info("Leaving RunApplication()");
}
}
static bool LoadFilesInPreviousInstance(string[] fileList)
{
try {
foreach (string file in fileList) {
if (ProjectService.HasProjectLoader(file)) {
return false;
}
}
return SingleInstanceHelper.OpenFilesInPreviousInstance(fileList);
} catch (Exception ex) {
LoggingService.Error(ex);
return false;
}
}
}
}