From 97de1b4622316f262eba5573c89297397c8d8adf Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Mon, 14 Oct 2019 21:20:59 +0100 Subject: [PATCH] Moved Platform version determination to static --- .../EnvironmentInfo/IPlatformInfo.cs | 50 ------ .../EnvironmentInfo/PlatformInfo.cs | 158 ++++++++++++++++++ .../MonoPlatformInfoFixture.cs | 4 +- .../EnvironmentInfo/MonoPlatformInfo.cs | 45 ----- .../DotNetPlatformInfoFixture.cs | 4 +- .../EnvironmentInfo/DotNetPlatformInfo.cs | 85 ---------- 6 files changed, 162 insertions(+), 184 deletions(-) delete mode 100644 src/NzbDrone.Common/EnvironmentInfo/IPlatformInfo.cs create mode 100644 src/NzbDrone.Common/EnvironmentInfo/PlatformInfo.cs delete mode 100644 src/NzbDrone.Mono/EnvironmentInfo/MonoPlatformInfo.cs delete mode 100644 src/NzbDrone.Windows/EnvironmentInfo/DotNetPlatformInfo.cs diff --git a/src/NzbDrone.Common/EnvironmentInfo/IPlatformInfo.cs b/src/NzbDrone.Common/EnvironmentInfo/IPlatformInfo.cs deleted file mode 100644 index 5136f90d2..000000000 --- a/src/NzbDrone.Common/EnvironmentInfo/IPlatformInfo.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; - -namespace NzbDrone.Common.EnvironmentInfo -{ - - public enum PlatformType - { - DotNet = 0, - Mono = 1 - } - - public interface IPlatformInfo - { - Version Version { get; } - } - - public abstract class PlatformInfo : IPlatformInfo - { - static PlatformInfo() - { - if (Type.GetType("Mono.Runtime") != null) - { - Platform = PlatformType.Mono; - } - else - { - Platform = PlatformType.DotNet; - } - } - - public static PlatformType Platform { get; } - public static bool IsMono => Platform == PlatformType.Mono; - public static bool IsDotNet => Platform == PlatformType.DotNet; - - public static string PlatformName - { - get - { - if (IsDotNet) - { - return ".NET"; - } - - return "Mono"; - } - } - - public abstract Version Version { get; } - } -} diff --git a/src/NzbDrone.Common/EnvironmentInfo/PlatformInfo.cs b/src/NzbDrone.Common/EnvironmentInfo/PlatformInfo.cs new file mode 100644 index 000000000..c8469ef09 --- /dev/null +++ b/src/NzbDrone.Common/EnvironmentInfo/PlatformInfo.cs @@ -0,0 +1,158 @@ +using System; +using System.Reflection; +using System.Text.RegularExpressions; +using Microsoft.Win32; + +namespace NzbDrone.Common.EnvironmentInfo +{ + + public enum PlatformType + { + DotNet = 0, + Mono = 1 + } + + public interface IPlatformInfo + { + Version Version { get; } + } + + public class PlatformInfo : IPlatformInfo + { + private static readonly Regex MonoVersionRegex = new Regex(@"(?<=\W|^)(?\d+\.\d+(\.\d+)?(\.\d+)?)(?=\W)", RegexOptions.Compiled | RegexOptions.IgnoreCase); + + private static PlatformType _platform; + private static Version _version; + + static PlatformInfo() + { + if (Type.GetType("Mono.Runtime") != null) + { + _platform = PlatformType.Mono; + _version = GetMonoVersion(); + } + else + { + _platform = PlatformType.DotNet; + _version = GetDotNetVersion(); + } + } + + public static PlatformType Platform => _platform; + public static bool IsMono => Platform == PlatformType.Mono; + public static bool IsDotNet => Platform == PlatformType.DotNet; + + public static string PlatformName + { + get + { + if (IsDotNet) + { + return ".NET"; + } + + return "Mono"; + } + } + + public Version Version => _version; + + public static Version GetVersion() + { + return _version; + } + + private static Version GetMonoVersion() + { + try + { + var type = Type.GetType("Mono.Runtime"); + + if (type != null) + { + var displayNameMethod = type.GetMethod("GetDisplayName", BindingFlags.NonPublic | BindingFlags.Static); + if (displayNameMethod != null) + { + var displayName = displayNameMethod.Invoke(null, null).ToString(); + var versionMatch = MonoVersionRegex.Match(displayName); + + if (versionMatch.Success) + { + return new Version(versionMatch.Groups["version"].Value); + } + } + } + } + catch (Exception ex) + { + Console.WriteLine("Couldnt get Mono version: " + ex.ToString()); + } + + return new Version(); + } + + private static Version GetDotNetVersion() + { + try + { + const string subkey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\"; + using (var ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey)) + { + if (ndpKey == null) + { + return new Version(4, 0); + } + + var releaseKey = (int)ndpKey.GetValue("Release"); + + if (releaseKey >= 528040) + { + return new Version(4, 8, 0); + } + if (releaseKey >= 461808) + { + return new Version(4, 7, 2); + } + if (releaseKey >= 461308) + { + return new Version(4, 7, 1); + } + if (releaseKey >= 460798) + { + return new Version(4, 7); + } + if (releaseKey >= 394802) + { + return new Version(4, 6, 2); + } + if (releaseKey >= 394254) + { + return new Version(4, 6, 1); + } + if (releaseKey >= 393295) + { + return new Version(4, 6); + } + if (releaseKey >= 379893) + { + return new Version(4, 5, 2); + } + if (releaseKey >= 378675) + { + return new Version(4, 5, 1); + } + if (releaseKey >= 378389) + { + return new Version(4, 5); + } + } + } + catch (Exception ex) + { + Console.WriteLine("Couldnt get .NET framework version: " + ex.ToString()); + } + + return new Version(4, 0); + } + } +} diff --git a/src/NzbDrone.Mono.Test/EnvironmentInfo/MonoPlatformInfoFixture.cs b/src/NzbDrone.Mono.Test/EnvironmentInfo/MonoPlatformInfoFixture.cs index 80fce9cc1..a2c0ffd5c 100644 --- a/src/NzbDrone.Mono.Test/EnvironmentInfo/MonoPlatformInfoFixture.cs +++ b/src/NzbDrone.Mono.Test/EnvironmentInfo/MonoPlatformInfoFixture.cs @@ -1,14 +1,14 @@ using System; using FluentAssertions; using NUnit.Framework; -using NzbDrone.Mono.EnvironmentInfo; +using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Test.Common; namespace NzbDrone.Mono.Test.EnvironmentInfo { [TestFixture] [Platform("Mono")] - public class MonoPlatformInfoFixture : TestBase + public class MonoPlatformInfoFixture : TestBase { [Test] public void should_get_framework_version() diff --git a/src/NzbDrone.Mono/EnvironmentInfo/MonoPlatformInfo.cs b/src/NzbDrone.Mono/EnvironmentInfo/MonoPlatformInfo.cs deleted file mode 100644 index ca487eedd..000000000 --- a/src/NzbDrone.Mono/EnvironmentInfo/MonoPlatformInfo.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System; -using System.Reflection; -using System.Text.RegularExpressions; -using NLog; -using NzbDrone.Common.EnvironmentInfo; - -namespace NzbDrone.Mono.EnvironmentInfo -{ - public class MonoPlatformInfo : PlatformInfo - { - private static readonly Regex VersionRegex = new Regex(@"(?<=\W|^)(?\d+\.\d+(\.\d+)?(\.\d+)?)(?=\W)", RegexOptions.Compiled | RegexOptions.IgnoreCase); - public override Version Version { get; } - - public MonoPlatformInfo(Logger logger) - { - var runTimeVersion = new Version(); - - try - { - var type = Type.GetType("Mono.Runtime"); - - if (type != null) - { - var displayNameMethod = type.GetMethod("GetDisplayName", BindingFlags.NonPublic | BindingFlags.Static); - if (displayNameMethod != null) - { - var displayName = displayNameMethod.Invoke(null, null).ToString(); - var versionMatch = VersionRegex.Match(displayName); - - if (versionMatch.Success) - { - runTimeVersion = new Version(versionMatch.Groups["version"].Value); - } - } - } - } - catch (Exception ex) - { - logger.Error(ex, "Unable to get mono version"); - } - - Version = runTimeVersion; - } - } -} diff --git a/src/NzbDrone.Windows.Test/EnvironmentInfo/DotNetPlatformInfoFixture.cs b/src/NzbDrone.Windows.Test/EnvironmentInfo/DotNetPlatformInfoFixture.cs index 7bfeb70e1..1f575a3f8 100644 --- a/src/NzbDrone.Windows.Test/EnvironmentInfo/DotNetPlatformInfoFixture.cs +++ b/src/NzbDrone.Windows.Test/EnvironmentInfo/DotNetPlatformInfoFixture.cs @@ -1,13 +1,13 @@ using FluentAssertions; using NUnit.Framework; +using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Test.Common; -using NzbDrone.Windows.EnvironmentInfo; namespace NzbDrone.Windows.Test.EnvironmentInfo { [TestFixture] [Platform("Win")] - public class DotNetPlatformInfoFixture : TestBase + public class DotNetPlatformInfoFixture : TestBase { [Test] public void should_get_framework_version() diff --git a/src/NzbDrone.Windows/EnvironmentInfo/DotNetPlatformInfo.cs b/src/NzbDrone.Windows/EnvironmentInfo/DotNetPlatformInfo.cs deleted file mode 100644 index 64a2bf90b..000000000 --- a/src/NzbDrone.Windows/EnvironmentInfo/DotNetPlatformInfo.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System; -using Microsoft.Win32; -using NLog; -using NzbDrone.Common.EnvironmentInfo; - -namespace NzbDrone.Windows.EnvironmentInfo -{ - public class DotNetPlatformInfo : PlatformInfo - { - private readonly Logger _logger; - - public DotNetPlatformInfo(Logger logger) - { - _logger = logger; - var version = GetFrameworkVersion(); - Version = version; - } - - public override Version Version { get; } - - private Version GetFrameworkVersion() - { - try - { - const string subkey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\"; - using (var ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey)) - { - if (ndpKey == null) - { - return new Version(4, 0); - } - - var releaseKey = (int)ndpKey.GetValue("Release"); - - if (releaseKey >= 528040) - { - return new Version(4, 8, 0); - } - if (releaseKey >= 461808) - { - return new Version(4, 7, 2); - } - if (releaseKey >= 461308) - { - return new Version(4, 7, 1); - } - if (releaseKey >= 460798) - { - return new Version(4, 7); - } - if (releaseKey >= 394802) - { - return new Version(4, 6, 2); - } - if (releaseKey >= 394254) - { - return new Version(4, 6, 1); - } - if (releaseKey >= 393295) - { - return new Version(4, 6); - } - if (releaseKey >= 379893) - { - return new Version(4, 5, 2); - } - if (releaseKey >= 378675) - { - return new Version(4, 5, 1); - } - if (releaseKey >= 378389) - { - return new Version(4, 5); - } - } - } - catch (Exception e) - { - _logger.Error(e, "Couldnt get .NET framework version"); - } - - return new Version(4, 0); - } - } -}