-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathEngineLauncherPackage.cs
More file actions
197 lines (154 loc) · 6.15 KB
/
EngineLauncherPackage.cs
File metadata and controls
197 lines (154 loc) · 6.15 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using EngineLauncher.Model;
using EngineLauncher.Utilities;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using static System.FormattableString;
namespace EngineLauncher
{
[PackageRegistration(UseManagedResourcesOnly = true)]
[InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
[Guid(EngineLauncherPackage.PackageGuidString)]
[ProvideMenuResource("Menus.ctmenu", 1)]
public sealed class EngineLauncherPackage : Package, IVsSolutionEvents
{
#region Constants
public const string PackageGuidString = "ff5205bf-e1fd-4fb4-8105-91825e6834f0";
private const string SelectedEngineKey = "SelectedEngine";
private const string SelectedConfigurationKey = "SelectedConfiguration";
private const string AdditionalLaunchConfigurationsKey = "AdditionalLaunchConfigurations";
#endregion
#region Constructor
public EngineLauncherPackage()
{
this.Model = new EngineLauncherModel(this);
this.AddOptionKey(SelectedEngineKey);
this.AddOptionKey(SelectedConfigurationKey);
this.AddOptionKey(AdditionalLaunchConfigurationsKey);
this.ResetSettings();
}
#endregion
#region Package Members
internal Guid? SelectedEngine { get; set; }
internal string SelectedConfiguration { get; set; }
internal EngineLauncherModel Model { get; }
private void ResetSettings()
{
this.SelectedEngine = null;
this.SelectedConfiguration = null;
this.Model.Reset();
}
protected override void Initialize()
{
base.Initialize();
LaunchEngineCommand.Initialize(this);
// Register for solution events so we can receive notification when a solution is closed.
IVsSolution sln = (IVsSolution)Package.GetGlobalService(typeof(SVsSolution));
uint cookie;
sln.AdviseSolutionEvents(this, out cookie);
}
protected override void OnLoadOptions(string key, Stream stream)
{
using (StreamReader reader = new StreamReader(stream))
{
string rawValue = reader.ReadToEnd();
switch (key)
{
case SelectedEngineKey:
this.SelectedEngine = GuidHelper.TryParseGuid(rawValue);
break;
case SelectedConfigurationKey:
this.SelectedConfiguration = rawValue;
break;
case AdditionalLaunchConfigurationsKey:
foreach (string configPath in rawValue.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries))
{
this.Model.CreateUserLaunchConfiguration(configPath);
}
break;
default:
Debug.Fail(Invariant($"Unknown storage key '{key}'!"));
break;
}
}
base.OnLoadOptions(key, stream);
}
protected override void OnSaveOptions(string key, Stream stream)
{
using (StreamWriter writer = new StreamWriter(stream))
{
string rawValue = null;
switch (key)
{
case SelectedEngineKey:
rawValue = this.SelectedEngine?.ToString("B");
break;
case SelectedConfigurationKey:
rawValue = this.SelectedConfiguration;
break;
case AdditionalLaunchConfigurationsKey:
rawValue = String.Join("|", this.Model.LaunchConfigurations.Where(c => c.IsUserPath).Select(c => c.Path));
break;
default:
Debug.Fail(Invariant($"Unknown storage key '{key}'!"));
break;
}
writer.Write(rawValue);
}
base.OnSaveOptions(key, stream);
}
#endregion
#region IVsSolutionEvents Implementation
public int OnAfterOpenProject(IVsHierarchy pHierarchy, int fAdded)
{
return VSConstants.S_OK;
}
public int OnQueryCloseProject(IVsHierarchy pHierarchy, int fRemoving, ref int pfCancel)
{
return VSConstants.S_OK;
}
public int OnBeforeCloseProject(IVsHierarchy pHierarchy, int fRemoved)
{
return VSConstants.S_OK;
}
public int OnAfterLoadProject(IVsHierarchy pStubHierarchy, IVsHierarchy pRealHierarchy)
{
return VSConstants.S_OK;
}
public int OnQueryUnloadProject(IVsHierarchy pRealHierarchy, ref int pfCancel)
{
return VSConstants.S_OK;
}
public int OnBeforeUnloadProject(IVsHierarchy pRealHierarchy, IVsHierarchy pStubHierarchy)
{
return VSConstants.S_OK;
}
public int OnAfterOpenSolution(object pUnkReserved, int fNewSolution)
{
return VSConstants.S_OK;
}
public int OnQueryCloseSolution(object pUnkReserved, ref int pfCancel)
{
return VSConstants.S_OK;
}
public int OnBeforeCloseSolution(object pUnkReserved)
{
return VSConstants.S_OK;
}
public int OnAfterCloseSolution(object pUnkReserved)
{
// Reset settings when the solution is closed. This ensures that configuration options will not be reused if a solution
// is closed, and then another solution that does not already have our setting keys present in its SUO is opened.
this.ResetSettings();
return VSConstants.S_OK;
}
#endregion
}
}