Skip to content

Commit 1c39aae

Browse files
MarLoerstam
authored andcommitted
CSHARP-1875: Steer clear of code that needs ntdll.dll
And making sure it compile/works on .NET Standard / Core as well
1 parent bf69902 commit 1c39aae

File tree

1 file changed

+109
-46
lines changed

1 file changed

+109
-46
lines changed

src/MongoDB.Driver.Core/Core/Connections/ClientDocumentHelper.cs

Lines changed: 109 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System;
1717
using System.Reflection;
1818
using System.Runtime.InteropServices;
19+
using System.Text.RegularExpressions;
1920
using MongoDB.Bson;
2021

2122
namespace MongoDB.Driver.Core.Connections
@@ -68,57 +69,119 @@ internal static BsonDocument CreateDriverDocument(string driverVersion)
6869
internal static BsonDocument CreateOSDocument()
6970
{
7071
string osType;
71-
switch (Environment.OSVersion.Platform)
72+
string architecture;
73+
string osName;
74+
string osVersion;
75+
76+
#if NET45
77+
if (Type.GetType("Mono.Runtime") != null)
78+
{
79+
Console.WriteLine("Hello Mono!!!");
80+
switch (Environment.OSVersion.Platform)
81+
{
82+
case PlatformID.Win32S:
83+
case PlatformID.Win32Windows:
84+
case PlatformID.Win32NT:
85+
case PlatformID.WinCE:
86+
osType = "Windows";
87+
break;
88+
89+
case PlatformID.Unix:
90+
osType = "Linux";
91+
break;
92+
93+
case PlatformID.Xbox:
94+
osType = "XBox";
95+
break;
96+
97+
case PlatformID.MacOSX:
98+
osType = "macOS";
99+
break;
100+
101+
default:
102+
osType = "Unknown";
103+
break;
104+
}
105+
106+
107+
PortableExecutableKinds peKind;
108+
ImageFileMachine machine;
109+
typeof(object).Module.GetPEKind(out peKind, out machine);
110+
switch (machine)
111+
{
112+
case ImageFileMachine.I386:
113+
architecture = "x86_32";
114+
break;
115+
case ImageFileMachine.IA64:
116+
case ImageFileMachine.AMD64:
117+
architecture = "x86_64";
118+
break;
119+
case ImageFileMachine.ARM:
120+
architecture = "arm" + (Environment.Is64BitProcess ? "64" : "");
121+
break;
122+
default:
123+
architecture = null;
124+
break;
125+
}
126+
127+
osName = Environment.OSVersion.VersionString;
128+
129+
osVersion = Environment.OSVersion.Version.ToString();
130+
131+
return CreateOSDocument(osType, osName, architecture, osVersion);
132+
}
133+
else
134+
#endif
72135
{
73-
case PlatformID.Win32S:
74-
case PlatformID.Win32Windows:
75-
case PlatformID.Win32NT:
76-
case PlatformID.WinCE:
136+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
137+
{
77138
osType = "Windows";
78-
break;
79-
80-
case PlatformID.Unix:
139+
}
140+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
141+
{
81142
osType = "Linux";
82-
break;
83-
84-
case PlatformID.Xbox:
85-
osType = "XBox";
86-
break;
87-
88-
case PlatformID.MacOSX:
143+
}
144+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
145+
{
89146
osType = "macOS";
90-
break;
91-
92-
default:
93-
osType = "Unknown";
94-
break;
147+
}
148+
else
149+
{
150+
osType = "unknown";
151+
}
152+
153+
osName = RuntimeInformation.OSDescription.Trim();
154+
155+
switch (RuntimeInformation.ProcessArchitecture)
156+
{
157+
case Architecture.Arm:
158+
architecture = "arm";
159+
break;
160+
case Architecture.Arm64:
161+
architecture = "arm64";
162+
break;
163+
case Architecture.X64:
164+
architecture = "x86_64";
165+
break;
166+
case Architecture.X86:
167+
architecture = "x86_32";
168+
break;
169+
default:
170+
architecture = null;
171+
break;
172+
}
173+
174+
var match = Regex.Match(osName, @" (?<version>\d+\.\d[^ ]*)");
175+
if (match.Success)
176+
{
177+
osVersion = match.Groups["version"].Value;
178+
}
179+
else
180+
{
181+
osVersion = null;
182+
}
95183
}
96184

97-
string architecture;
98-
PortableExecutableKinds peKind;
99-
ImageFileMachine machine;
100-
typeof(object).Module.GetPEKind(out peKind, out machine);
101-
switch (machine)
102-
{
103-
case ImageFileMachine.I386:
104-
architecture = "x86_32";
105-
break;
106-
case ImageFileMachine.IA64:
107-
case ImageFileMachine.AMD64:
108-
architecture = "x86_64";
109-
break;
110-
case ImageFileMachine.ARM:
111-
architecture = "arm" + (Environment.Is64BitProcess ? "64" : "");
112-
break;
113-
default:
114-
architecture = null;
115-
break;
116-
}
117-
118-
var osName = Environment.OSVersion.VersionString;
119-
120-
string osVersion = Environment.OSVersion.Version.ToString();
121-
122185
return CreateOSDocument(osType, osName, architecture, osVersion);
123186
}
124187

@@ -182,7 +245,7 @@ internal static BsonDocument RemoveOptionalFieldsUntilDocumentIsLessThan512Bytes
182245

183246
return clientDocument;
184247
}
185-
#endregion
248+
#endregion
186249

187250
}
188251
}

0 commit comments

Comments
 (0)