Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 15f6d20

Browse files
committed
Refactor platform detection
1 parent 85b1ed7 commit 15f6d20

File tree

2 files changed

+50
-47
lines changed

2 files changed

+50
-47
lines changed

src/ServiceStack.Text/Env.cs

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,7 @@ static Env()
1414
if (PclExport.Instance == null)
1515
throw new ArgumentException("PclExport.Instance needs to be initialized");
1616

17-
var platformName = PclExport.Instance.PlatformName;
18-
19-
#if NETSTANDARD2_0
20-
IsUWP = IsRunningAsUwp();
21-
#endif
22-
23-
if (!IsUWP)
24-
{
25-
IsMono = AssemblyUtils.FindType("Mono.Runtime") != null;
26-
27-
IsIOS = AssemblyUtils.FindType("MonoTouch.Foundation.NSObject") != null
28-
|| AssemblyUtils.FindType("Foundation.NSObject") != null;
29-
30-
IsAndroid = AssemblyUtils.FindType("Android.Manifest") != null;
31-
32-
try
33-
{
34-
IsOSX = AssemblyUtils.FindType("Mono.AppKit") != null;
35-
#if NET45
36-
IsWindows = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("windir"));
37-
if (File.Exists(@"/System/Library/CoreServices/SystemVersion.plist"))
38-
IsOSX = true;
39-
string osType = File.Exists(@"/proc/sys/kernel/ostype") ? File.ReadAllText(@"/proc/sys/kernel/ostype") : null;
40-
IsLinux = osType?.IndexOf("Linux", StringComparison.OrdinalIgnoreCase) >= 0;
41-
#endif
42-
}
43-
catch (Exception) {}
44-
}
45-
46-
#if NETSTANDARD2_0
17+
#if NETSTANDARD2_0 || NETCORE2_1
4718
IsNetStandard = true;
4819
try
4920
{
@@ -52,30 +23,64 @@ static Env()
5223
IsOSX = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX);
5324

5425
var fxDesc = System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription;
55-
if (!IsIOS && IsOSX && fxDesc.Contains("Mono"))
56-
{
57-
var runtimeDir = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory();
58-
//iOS detection no longer trustworthy so assuming iOS based on some current heuristics. TODO: improve iOS detection
59-
IsIOS = runtimeDir.StartsWith("/private/var") ||
60-
runtimeDir.Contains("/CoreSimulator/Devices/");
61-
}
26+
IsMono = fxDesc.Contains("Mono");
6227
IsNetCore = fxDesc.StartsWith(".NET Core", StringComparison.OrdinalIgnoreCase);
6328
}
6429
catch (Exception) {} //throws PlatformNotSupportedException in AWS lambda
6530
IsUnix = IsOSX || IsLinux;
6631
HasMultiplePlatformTargets = true;
32+
IsUWP = IsRunningAsUwp();
6733
#elif NET45
6834
IsNetFramework = true;
35+
switch (Environment.OSVersion.Platform)
36+
{
37+
case PlatformID.Win32NT:
38+
case PlatformID.Win32S:
39+
case PlatformID.Win32Windows:
40+
case PlatformID.WinCE:
41+
IsWindows = true;
42+
break;
43+
}
44+
6945
var platform = (int)Environment.OSVersion.Platform;
7046
IsUnix = platform == 4 || platform == 6 || platform == 128;
71-
IsLinux = IsUnix;
72-
if (Environment.GetEnvironmentVariable("OS")?.IndexOf("Windows", StringComparison.OrdinalIgnoreCase) >= 0)
73-
IsWindows = true;
47+
48+
if (File.Exists(@"/System/Library/CoreServices/SystemVersion.plist"))
49+
IsOSX = true;
50+
var osType = File.Exists(@"/proc/sys/kernel/ostype")
51+
? File.ReadAllText(@"/proc/sys/kernel/ostype")
52+
: null;
53+
IsLinux = osType?.IndexOf("Linux", StringComparison.OrdinalIgnoreCase) >= 0;
54+
try
55+
{
56+
IsMono = AssemblyUtils.FindType("Mono.Runtime") != null;
57+
}
58+
catch (Exception) {}
59+
7460
SupportsDynamic = true;
75-
#elif NETCORE2_1
61+
#endif
62+
63+
#if NETCORE2_1
64+
IsNetStandard = false;
7665
IsNetCore = true;
7766
SupportsDynamic = true;
7867
#endif
68+
69+
if (!IsUWP)
70+
{
71+
try
72+
{
73+
IsAndroid = AssemblyUtils.FindType("Android.Manifest") != null;
74+
if (IsOSX && IsMono)
75+
{
76+
var runtimeDir = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory();
77+
//iOS detection no longer trustworthy so assuming iOS based on some current heuristics. TODO: improve iOS detection
78+
IsIOS = runtimeDir.StartsWith("/private/var") ||
79+
runtimeDir.Contains("/CoreSimulator/Devices/");
80+
}
81+
}
82+
catch (Exception) {}
83+
}
7984

8085
SupportsExpressions = true;
8186
SupportsEmit = !(IsUWP || IsIOS);
@@ -87,8 +92,10 @@ static Env()
8792

8893
ServerUserAgent = "ServiceStack/" +
8994
ServiceStackVersion + " "
90-
+ platformName
91-
+ (IsMono ? "/Mono" : "/.NET");
95+
+ PclExport.Instance.PlatformName
96+
+ (IsMono ? "/Mono" : "")
97+
+ (IsLinux ? "/Linux" : IsOSX ? "/OSX" : IsUnix ? "/Unix" : IsWindows ? "/Windows" : "/UnknownOS")
98+
+ (IsIOS ? "/iOS" : IsAndroid ? "/Android" : IsUWP ? "/UWP" : "");
9299

93100
VersionString = ServiceStackVersion.ToString(CultureInfo.InvariantCulture);
94101

src/ServiceStack.Text/PclExport.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ public abstract class PclExport
2121
{
2222
public static class Platforms
2323
{
24-
public const string Uwp = "UWP";
25-
public const string Android = "Android";
26-
public const string IOS = "IOS";
27-
public const string Mac = "MAC";
2824
public const string NetStandard = "NETStandard";
2925
public const string NetCore = "NetCore";
3026
public const string Net45 = "Net45";

0 commit comments

Comments
 (0)