/
SubsystemVersion.cs
208 lines (179 loc) · 6.92 KB
/
SubsystemVersion.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis
{
/// <summary>
/// Represents subsystem version, see /subsystemversion command line
/// option for details and valid values.
///
/// The following table lists common subsystem versions of Windows.
///
/// Windows version Subsystem version
/// - Windows 2000 5.00
/// - Windows XP 5.01
/// - Windows Vista 6.00
/// - Windows 7 6.01
/// - Windows 8 Release Preview 6.02
/// </summary>
public struct SubsystemVersion : IEquatable<SubsystemVersion>
{
/// <summary>
/// Major subsystem version
/// </summary>
public int Major { get; }
/// <summary>
/// Minor subsystem version
/// </summary>
public int Minor { get; }
/// <summary>
/// Subsystem version not specified
/// </summary>
public static SubsystemVersion None => new SubsystemVersion();
/// <summary>
/// Subsystem version: Windows 2000
/// </summary>
public static SubsystemVersion Windows2000 => new SubsystemVersion(5, 0);
/// <summary>
/// Subsystem version: Windows XP
/// </summary>
public static SubsystemVersion WindowsXP => new SubsystemVersion(5, 1);
/// <summary>
/// Subsystem version: Windows Vista
/// </summary>
public static SubsystemVersion WindowsVista => new SubsystemVersion(6, 0);
/// <summary>
/// Subsystem version: Windows 7
/// </summary>
public static SubsystemVersion Windows7 => new SubsystemVersion(6, 1);
/// <summary>
/// Subsystem version: Windows 8
/// </summary>
public static SubsystemVersion Windows8 => new SubsystemVersion(6, 2);
private SubsystemVersion(int major, int minor)
{
this.Major = major;
this.Minor = minor;
}
/// <summary>
/// Try parse subsystem version in "x.y" format. Note, no spaces are allowed in string representation.
/// </summary>
/// <param name="str">String to parse</param>
/// <param name="version">the value if successfully parsed or None otherwise</param>
/// <returns>true if parsed successfully, false otherwise</returns>
public static bool TryParse(string str, out SubsystemVersion version)
{
version = SubsystemVersion.None;
if (!string.IsNullOrWhiteSpace(str))
{
string major;
string? minor;
int index = str.IndexOf('.');
//found a dot
if (index >= 0)
{
//if there's a dot and no following digits, it's an error in the native compiler.
if (str.Length == index + 1)
return false;
major = str.Substring(0, index);
minor = str.Substring(index + 1);
}
else
{
major = str;
minor = null;
}
int majorValue;
if (major != major.Trim() ||
!int.TryParse(major, NumberStyles.None, CultureInfo.InvariantCulture, out majorValue) ||
majorValue >= 65356 || majorValue < 0)
{
return false;
}
int minorValue = 0;
//it's fine to have just a single number specified for the subsystem.
if (minor != null)
{
if (minor != minor.Trim() ||
!int.TryParse(minor, NumberStyles.None, CultureInfo.InvariantCulture, out minorValue) ||
minorValue >= 65356 || minorValue < 0)
{
return false;
}
}
version = new SubsystemVersion(majorValue, minorValue);
return true;
}
return false;
}
/// <summary>
/// Create a new instance of subsystem version with specified major and minor values.
/// </summary>
/// <param name="major">major subsystem version</param>
/// <param name="minor">minor subsystem version</param>
/// <returns>subsystem version with provided major and minor</returns>
public static SubsystemVersion Create(int major, int minor)
{
return new SubsystemVersion(major, minor);
}
/// <summary>
/// Subsystem version default for the specified output kind and platform combination
/// </summary>
/// <param name="outputKind">Output kind</param>
/// <param name="platform">Platform</param>
/// <returns>Subsystem version</returns>
internal static SubsystemVersion Default(OutputKind outputKind, Platform platform)
{
if (platform == Platform.Arm)
return Windows8;
switch (outputKind)
{
case OutputKind.ConsoleApplication:
case OutputKind.DynamicallyLinkedLibrary:
case OutputKind.NetModule:
case OutputKind.WindowsApplication:
return new SubsystemVersion(4, 0);
case OutputKind.WindowsRuntimeApplication:
case OutputKind.WindowsRuntimeMetadata:
return Windows8;
default:
throw new ArgumentOutOfRangeException(CodeAnalysisResources.OutputKindNotSupported, "outputKind");
}
}
/// <summary>
/// True if the subsystem version has a valid value
/// </summary>
public bool IsValid
{
get
{
return this.Major >= 0 &&
this.Minor >= 0 &&
this.Major < 65536 && this.Minor < 65536;
}
}
public override bool Equals(object? obj)
{
return obj is SubsystemVersion && Equals((SubsystemVersion)obj);
}
public override int GetHashCode()
{
return Hash.Combine(this.Minor.GetHashCode(), this.Major.GetHashCode());
}
public bool Equals(SubsystemVersion other)
{
return this.Major == other.Major && this.Minor == other.Minor;
}
public override string ToString()
{
return string.Format("{0}.{1:00}", this.Major, this.Minor);
}
}
}