This repository was archived by the owner on Oct 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 807
Expand file tree
/
Copy pathCallHelper.cs
More file actions
292 lines (262 loc) · 10.4 KB
/
Copy pathCallHelper.cs
File metadata and controls
292 lines (262 loc) · 10.4 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Threading;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using ICSharpCode.Core;
using ICSharpCode.Core.WinForms;
using ICSharpCode.SharpDevelop.Commands;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.SharpDevelop.Workbench;
using ICSharpCode.SharpDevelop.Logging;
using ICSharpCode.SharpDevelop.Parser;
namespace ICSharpCode.SharpDevelop.Sda
{
internal sealed class CallHelper : MarshalByRefObject
{
SharpDevelopHost.CallbackHelper callback;
bool useSharpDevelopErrorHandler;
public override object InitializeLifetimeService()
{
return null;
}
#region Initialize Core
public void InitSharpDevelopCore(SharpDevelopHost.CallbackHelper callback, StartupSettings properties)
{
// Initialize the most important services:
var container = new SharpDevelopServiceContainer();
container.AddFallbackProvider(ServiceSingleton.FallbackServiceProvider);
container.AddService(typeof(IMessageService), new SDMessageService());
container.AddService(typeof(ILoggingService), new log4netLoggingService());
ServiceSingleton.ServiceProvider = container;
LoggingService.Info("InitSharpDevelop...");
this.callback = callback;
CoreStartup startup = new CoreStartup(properties.ApplicationName);
if (properties.UseSharpDevelopErrorHandler) {
this.useSharpDevelopErrorHandler = true;
ExceptionBox.RegisterExceptionBoxForUnhandledExceptions();
}
string configDirectory = properties.ConfigDirectory;
string dataDirectory = properties.DataDirectory;
string propertiesName;
if (properties.PropertiesName != null) {
propertiesName = properties.PropertiesName;
} else {
propertiesName = properties.ApplicationName + "Properties";
}
if (properties.ApplicationRootPath != null) {
FileUtility.ApplicationRootPath = properties.ApplicationRootPath;
}
if (configDirectory == null)
configDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
properties.ApplicationName);
var propertyService = new PropertyService(
DirectoryName.Create(configDirectory),
DirectoryName.Create(dataDirectory ?? Path.Combine(FileUtility.ApplicationRootPath, "data")),
propertiesName);
startup.StartCoreServices(propertyService);
Assembly exe = Assembly.Load(properties.ResourceAssemblyName);
SD.ResourceService.RegisterNeutralStrings(new ResourceManager("ICSharpCode.SharpDevelop.Resources.StringResources", exe));
SD.ResourceService.RegisterNeutralImages(new ResourceManager("ICSharpCode.SharpDevelop.Resources.BitmapResources", exe));
CommandWrapper.LinkCommandCreator = (link => new LinkCommand(link));
CommandWrapper.WellKnownCommandCreator = Core.Presentation.MenuService.GetKnownCommand;
CommandWrapper.RegisterConditionRequerySuggestedHandler = (eh => CommandManager.RequerySuggested += eh);
CommandWrapper.UnregisterConditionRequerySuggestedHandler = (eh => CommandManager.RequerySuggested -= eh);
StringParser.RegisterStringTagProvider(new SharpDevelopStringTagProvider());
LoggingService.Info("Looking for AddIns...");
foreach (string file in properties.addInFiles) {
startup.AddAddInFile(file);
}
foreach (string dir in properties.addInDirectories) {
startup.AddAddInsFromDirectory(dir);
}
if (properties.AllowAddInConfigurationAndExternalAddIns) {
startup.ConfigureExternalAddIns(Path.Combine(configDirectory, "AddIns.xml"));
}
if (properties.AllowUserAddIns) {
startup.ConfigureUserAddIns(Path.Combine(configDirectory, "AddInInstallTemp"),
Path.Combine(configDirectory, "AddIns"));
}
LoggingService.Info("Loading AddInTree...");
startup.RunInitialization();
((AssemblyParserService)SD.AssemblyParserService).DomPersistencePath = properties.DomPersistencePath;
// Register events to marshal back
Project.ProjectService.BuildStarted += delegate { this.callback.StartBuild(); };
Project.ProjectService.BuildFinished += delegate { this.callback.EndBuild(); };
Project.ProjectService.SolutionLoaded += delegate { this.callback.SolutionLoaded(); };
Project.ProjectService.SolutionClosed += delegate { this.callback.SolutionClosed(); };
FileUtility.FileLoaded += delegate(object sender, FileNameEventArgs e) { this.callback.FileLoaded(e.FileName); };
FileUtility.FileSaved += delegate(object sender, FileNameEventArgs e) { this.callback.FileSaved(e.FileName); };
LoggingService.Info("InitSharpDevelop finished");
}
#endregion
#region Initialize and run Workbench
public void RunWorkbench(WorkbenchSettings settings)
{
if (settings.RunOnNewThread) {
Thread t = new Thread(RunWorkbenchInternal);
t.SetApartmentState(ApartmentState.STA);
t.Name = "SDmain";
t.Start(settings);
} else {
RunWorkbenchInternal(settings);
}
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
void RunWorkbenchInternal(object settings)
{
WorkbenchSettings wbSettings = (WorkbenchSettings)settings;
WorkbenchStartup wbc = new WorkbenchStartup();
LoggingService.Info("Initializing workbench...");
wbc.InitializeWorkbench();
RunWorkbenchInitializedCommands();
LoggingService.Info("Starting workbench...");
Exception exception = null;
// finally start the workbench.
try {
callback.BeforeRunWorkbench();
if (Debugger.IsAttached) {
wbc.Run(wbSettings.InitialFileList);
} else {
try {
wbc.Run(wbSettings.InitialFileList);
} catch (Exception ex) {
exception = ex;
}
}
} finally {
LoggingService.Info("Unloading services...");
try {
// see IShutdownService.Shutdown for a description of the shut down procedure
WorkbenchSingleton.OnWorkbenchUnloaded();
var propertyService = SD.PropertyService;
var shutdownService = (ShutdownService)SD.ShutdownService;
shutdownService.WaitForBackgroundTasks();
((IDisposable)SD.Services).Dispose(); // dispose all services
propertyService.Save();
} catch (Exception ex) {
LoggingService.Warn("Exception during unloading", ex);
if (exception == null) {
exception = ex;
}
}
}
LoggingService.Info("Finished running workbench.");
callback.WorkbenchClosed();
if (exception != null) {
const string errorText = "Unhandled exception terminated the workbench";
LoggingService.Fatal(exception);
if (useSharpDevelopErrorHandler) {
System.Windows.Forms.Application.Run(new ExceptionBox(exception, errorText, true));
} else {
throw new RunWorkbenchException(errorText, exception);
}
}
}
void RunWorkbenchInitializedCommands()
{
foreach (ICommand command in AddInTree.BuildItems<ICommand>("/SharpDevelop/Workbench/AutostartAfterWorkbenchInitialized", null, false)) {
try {
command.Execute(null);
} catch (Exception ex) {
// allow startup to continue if some commands fail
MessageService.ShowException(ex);
}
}
}
#endregion
public List<Document> OpenDocuments {
get {
List<Document> l = new List<Document>();
SD.MainThread.InvokeIfRequired(() => GetOpenDocuments(l));
return l;
}
}
void GetOpenDocuments(List<Document> l)
{
foreach (IViewContent vc in SD.Workbench.ViewContentCollection) {
Document d = Document.FromWindow(vc);
if (d != null) {
l.Add(d);
}
}
}
/// <summary>
/// Opens the document with the specified file name.
/// </summary>
public Document OpenDocument(string fileName)
{
return SD.MainThread.InvokeIfRequired(() => OpenDocumentInternal(fileName));
}
Document OpenDocumentInternal(string fileName)
{
return Document.FromWindow(FileService.OpenFile(fileName));
}
public void OpenProject(string fileName)
{
SD.MainThread.InvokeIfRequired(() => SD.ProjectService.OpenSolutionOrProject(FileName.Create(fileName)));
}
public bool IsSolutionOrProject(string fileName)
{
return SD.ProjectService.IsSolutionOrProjectFile(FileName.Create(fileName));
}
public bool CloseWorkbench(bool force)
{
return SD.MainThread.InvokeIfRequired(() => CloseWorkbenchInternal(force));
}
bool CloseWorkbenchInternal(bool force)
{
foreach (IWorkbenchWindow window in SD.Workbench.WorkbenchWindowCollection.ToArray()) {
if (!window.CloseWindow(force))
return false;
}
SD.Workbench.MainWindow.Close();
return true;
}
[SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "needs to be run in correct AppDomain")]
public void KillWorkbench()
{
Dispatcher.CurrentDispatcher.BeginInvokeShutdown(DispatcherPriority.Normal);
}
public bool WorkbenchVisible {
get {
return SD.MainThread.InvokeIfRequired<bool>(GetWorkbenchVisibleInternal);
}
set {
SD.MainThread.InvokeIfRequired(() => SetWorkbenchVisibleInternal(value));
}
}
bool GetWorkbenchVisibleInternal()
{
return SD.Workbench.MainWindow.Visibility == Visibility.Visible;
}
void SetWorkbenchVisibleInternal(bool value)
{
SD.Workbench.MainWindow.Visibility = value ? Visibility.Visible : Visibility.Hidden;
}
}
}