diff --git a/src/Acoustics.Shared/AppConfigHelper.cs b/src/Acoustics.Shared/AppConfigHelper.cs index ebab31cdb..90e1f63ee 100644 --- a/src/Acoustics.Shared/AppConfigHelper.cs +++ b/src/Acoustics.Shared/AppConfigHelper.cs @@ -21,15 +21,6 @@ namespace Acoustics.Shared public static class AppConfigHelper { - private static readonly KeyValueConfigurationCollection SharedSettings; - - private static readonly ILog Log = LogManager.GetLogger(nameof(AppConfigHelper)); - - private static readonly string ExecutingAssemblyPath = - (Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly()).Location; - - public static readonly string ExecutingAssemblyDirectory = Path.GetDirectoryName(ExecutingAssemblyPath); - public const string DefaultTargetSampleRateKey = "DefaultTargetSampleRate"; public static int DefaultTargetSampleRate => GetInt(DefaultTargetSampleRateKey); @@ -45,12 +36,63 @@ public static class AppConfigHelper public const string StandardDateFormatUtcWithFractionalSeconds = "yyyyMMdd-HHmmss.FFFZ"; + public static readonly string ExecutingAssemblyDirectory = Path.GetDirectoryName(ExecutingAssemblyPath); + + private static readonly KeyValueConfigurationCollection SharedSettings; + + private static readonly ILog Log = LogManager.GetLogger(nameof(AppConfigHelper)); + + private static readonly string ExecutingAssemblyPath = + (Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly()).Location; + + private static readonly bool IsLinuxValue; + private static readonly bool IsWindowsValue; + private static readonly bool IsMacOsXValue; + static AppConfigHelper() { ExeConfigurationFileMap exeConfigurationFileMap = new ExeConfigurationFileMap(); exeConfigurationFileMap.ExeConfigFilename = Path.Combine(ExecutingAssemblyDirectory, "AP.Settings.Config"); var sharedConfig = ConfigurationManager.OpenMappedExeConfiguration(exeConfigurationFileMap, ConfigurationUserLevel.None); SharedSettings = sharedConfig.AppSettings.Settings; + + IsMono = Type.GetType("Mono.Runtime") != null; + CheckOs(ref IsWindowsValue, ref IsLinuxValue, ref IsMacOsXValue); + + } + + /// + /// Adapted from https://stackoverflow.com/a/38795621/224512 + /// + private static void CheckOs(ref bool isWindows, ref bool isLinux, ref bool isMacOsX) + { + string windir = Environment.GetEnvironmentVariable("windir"); + if (!string.IsNullOrEmpty(windir) && windir.Contains(@"\") && Directory.Exists(windir)) + { + isWindows = true; + } + else if (File.Exists(@"/proc/sys/kernel/ostype")) + { + string osType = File.ReadAllText(@"/proc/sys/kernel/ostype"); + if (osType.StartsWith("Linux", StringComparison.OrdinalIgnoreCase)) + { + // Note: Android gets here too + isLinux = true; + } + else + { + throw new PlatformNotSupportedException(osType); + } + } + else if (File.Exists(@"/System/Library/CoreServices/SystemVersion.plist")) + { + // Note: iOS gets here too + isMacOsX = true; + } + else + { + throw new PlatformNotSupportedException("Unkown platform"); + } } public static string FileDateFormatUtc @@ -281,29 +323,13 @@ public static DirectoryInfo AssemblyDir } } - public static bool IsMono - { - get - { - return Type.GetType("Mono.Runtime") != null; - } - } + public static bool IsMono { get; } - public static bool IsLinux - { - get - { - return Environment.OSVersion.Platform == PlatformID.Unix; - } - } + public static bool IsLinux => IsLinuxValue; - public static bool IsMacOsX - { - get - { - return Environment.OSVersion.Platform == PlatformID.MacOSX; - } - } + public static bool IsWindows => IsWindowsValue; + + public static bool IsMacOsX => IsMacOsXValue; public static string GetString(string key) { @@ -517,14 +543,15 @@ private static string GetExeFile(string appConfigKey) { string path = null; string key = null; - if (IsLinux) + + if (IsMacOsX) { - key = appConfigKey + "Linux"; + key = appConfigKey + "MacOsX"; path = GetString(key); } - else if (IsMacOsX) + else if (IsLinux) { - key = appConfigKey + "MacOsX"; + key = appConfigKey + "Linux"; path = GetString(key); } else diff --git a/src/Acoustics.Tools/Acoustics.Tools.csproj b/src/Acoustics.Tools/Acoustics.Tools.csproj index 45f8dab84..375229c61 100644 --- a/src/Acoustics.Tools/Acoustics.Tools.csproj +++ b/src/Acoustics.Tools/Acoustics.Tools.csproj @@ -152,7 +152,7 @@ AP.Settings.config Always - + Designer