[smuxi] Common, Frontend-GNOME, Engine-JabbR: move platform detection to Common



commit 7934271913b9b0a244cd2584413b744f99d502e8
Author: Mirco Bauer <meebey meebey net>
Date:   Thu Feb 4 00:28:58 2021 +0800

    Common, Frontend-GNOME, Engine-JabbR: move platform detection to Common
    
    The platform specific detections should live in Common and reused elsewhere.

 src/Common/Platform.cs                   | 187 +++++++++++++++++--------------
 src/Engine-JabbR/JabbrProtocolManager.cs |   2 +-
 src/Frontend-GNOME/Frontend.cs           |  15 ++-
 3 files changed, 109 insertions(+), 95 deletions(-)
---
diff --git a/src/Common/Platform.cs b/src/Common/Platform.cs
index b90f3845..ffbe60a7 100644
--- a/src/Common/Platform.cs
+++ b/src/Common/Platform.cs
@@ -1,6 +1,6 @@
 // This file is part of Smuxi and is licensed under the terms of MIT/X11
 // 
-// Copyright (c) 2010-2012 Mirco Bauer <meebey meebey net>
+// Copyright (c) 2010-2013, 2021 Mirco Bauer <meebey meebey net>
 //
 // Permission is hereby granted, free of charge, to any person obtaining a copy
 // of this software and associated documentation files (the "Software"), to deal
@@ -28,92 +28,12 @@ namespace Smuxi.Common
 {
     public static class Platform
     {
-        public static string OperatingSystem {
-            get {
-                if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
-                    return Environment.OSVersion.Platform.ToString();
-                }
-
-                // uname present?
-                try {
-                    var pinfo = new ProcessStartInfo("uname");
-                    pinfo.UseShellExecute = false;
-                    pinfo.RedirectStandardOutput = true;
-                    pinfo.RedirectStandardError = true;
-                    Process.Start(pinfo).WaitForExit();
-                } catch (Exception) {
-                    // fall back to runtime detector
-                    return Environment.OSVersion.Platform.ToString();
-                }
-
-                string os = null;
-                // GNU/Linux
-                // GNU/kFreeBSD
-                // Cygwin
-                var info = new ProcessStartInfo("uname", "-o");
-                info.UseShellExecute = false;
-                info.RedirectStandardOutput = true;
-                info.RedirectStandardError = true;
-                var process = Process.Start(info);
-                process.WaitForExit();
-                if (process.ExitCode == 0) {
-                    os = process.StandardOutput.ReadLine();
-                    // HACK: if Cygwin was installed on Windows and is in PATH
-                    // we should not trust uname and ask the runtime instead
-                    if (os == "Cygwin") {
-                        return Environment.OSVersion.Platform.ToString();
-                    }
-                }
-
-                if (String.IsNullOrEmpty(os)) {
-                    // not all operating systems support -o so lets fallback to -s
-                    // Linux
-                    // FreeBSD
-                    // Darwin
-                    info = new ProcessStartInfo("uname", "-s");
-                    info.UseShellExecute = false;
-                    info.RedirectStandardOutput = true;
-                    info.RedirectStandardError = true;
-                    process = Process.Start(info);
-                    process.WaitForExit();
-                    if (process.ExitCode == 0) {
-                        os = process.StandardOutput.ReadLine();
-                    }
-                }
-
-                if (String.IsNullOrEmpty(os)) {
-                    return "Unknown";
-                }
+        public static string OperatingSystem { get; private set; }
+        public static bool IsWindows { get; private set; }
+        public static bool IsMacOSX { get; private set; }
+        public static bool IsLinux { get; private set; }
+        public static bool IsMono { get; private set; }
 
-                string distro = null;
-                try {
-                    info = new ProcessStartInfo("lsb_release", "-i");
-                    info.UseShellExecute = false;
-                    info.RedirectStandardOutput = true;
-                    info.RedirectStandardError = true;
-                    process = Process.Start(info);
-                    process.WaitForExit();
-                    if (process.ExitCode == 0) {
-                        distro = process.StandardOutput.ReadLine();
-                        var match = Regex.Match(distro,
-                                                @"^Distributor ID:\s+(.+)");
-                        if (match.Success && match.Groups.Count > 1) {
-                            distro = match.Groups[1].Value;
-                        } else {
-                            distro = null;
-                        }
-                    }
-                } catch (Exception) {
-                }
-
-                if (String.IsNullOrEmpty(distro)) {
-                    return os;
-                }
-
-                return String.Format("{0} ({1})", os, distro);
-            }
-        }
-        
         public static string Architecture {
             get {
                 if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
@@ -192,6 +112,101 @@ namespace Smuxi.Common
             }
         }
 
+        static Platform()
+        {
+            OperatingSystem = GetOperatingSystem();
+            IsWindows = Environment.OSVersion.Platform == PlatformID.Win32NT;
+            IsMacOSX = OperatingSystem == "Darwin";
+            // OS might return GNU/Linux or Linux
+            IsLinux = OperatingSystem.Contains("Linux");
+            IsMono = Type.GetType("Mono.Runtime") != null;
+        }
+
+        public static string GetOperatingSystem()
+        {
+            if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
+                return Environment.OSVersion.Platform.ToString();
+            }
+
+            // uname present?
+            try {
+                var pinfo = new ProcessStartInfo("uname");
+                pinfo.UseShellExecute = false;
+                pinfo.RedirectStandardOutput = true;
+                pinfo.RedirectStandardError = true;
+                Process.Start(pinfo).WaitForExit();
+            } catch (Exception) {
+                // fall back to runtime detector
+                return Environment.OSVersion.Platform.ToString();
+            }
+
+            string os = null;
+            // GNU/Linux
+            // GNU/kFreeBSD
+            // Cygwin
+            var info = new ProcessStartInfo("uname", "-o");
+            info.UseShellExecute = false;
+            info.RedirectStandardOutput = true;
+            info.RedirectStandardError = true;
+            var process = Process.Start(info);
+            process.WaitForExit();
+            if (process.ExitCode == 0) {
+                os = process.StandardOutput.ReadLine();
+                // HACK: if Cygwin was installed on Windows and is in PATH
+                // we should not trust uname and ask the runtime instead
+                if (os == "Cygwin") {
+                    return Environment.OSVersion.Platform.ToString();
+                }
+            }
+
+            if (String.IsNullOrEmpty(os)) {
+                // not all operating systems support -o so lets fallback to -s
+                // Linux
+                // FreeBSD
+                // Darwin
+                info = new ProcessStartInfo("uname", "-s");
+                info.UseShellExecute = false;
+                info.RedirectStandardOutput = true;
+                info.RedirectStandardError = true;
+                process = Process.Start(info);
+                process.WaitForExit();
+                if (process.ExitCode == 0) {
+                    os = process.StandardOutput.ReadLine();
+                }
+            }
+
+            if (String.IsNullOrEmpty(os)) {
+                return "Unknown";
+            }
+
+            string distro = null;
+            try {
+                info = new ProcessStartInfo("lsb_release", "-i");
+                info.UseShellExecute = false;
+                info.RedirectStandardOutput = true;
+                info.RedirectStandardError = true;
+                process = Process.Start(info);
+                process.WaitForExit();
+                if (process.ExitCode == 0) {
+                    distro = process.StandardOutput.ReadLine();
+                    var match = Regex.Match(distro,
+                                            @"^Distributor ID:\s+(.+)");
+                    if (match.Success && match.Groups.Count > 1) {
+                        distro = match.Groups[1].Value;
+                    } else {
+                        distro = null;
+                    }
+                }
+            } catch (Exception) {
+            }
+
+            if (String.IsNullOrEmpty(distro)) {
+                return os;
+            }
+
+            return String.Format("{0} ({1})", os, distro);
+        }
+
         public static string GetBuffersPath(string username)
         {
             var dbPath = GetBuffersBasePath();
diff --git a/src/Engine-JabbR/JabbrProtocolManager.cs b/src/Engine-JabbR/JabbrProtocolManager.cs
index 1439890c..e74d38f0 100644
--- a/src/Engine-JabbR/JabbrProtocolManager.cs
+++ b/src/Engine-JabbR/JabbrProtocolManager.cs
@@ -200,7 +200,7 @@ namespace Smuxi.Engine
                 // for some reason and then fallbacks to LongPollingTransport
                 // this takes 10 seconds though, so let's go LP directly
                 Func<IClientTransport> transport = null;
-                if (Type.GetType("Mono.Runtime") == null) {
+                if (!Platform.IsMono) {
                     transport = () => new AutoTransport(new DefaultHttpClient());
                 } else {
                     transport = () => new LongPollingTransport();
diff --git a/src/Frontend-GNOME/Frontend.cs b/src/Frontend-GNOME/Frontend.cs
index 3314e5be..8085b1ce 100644
--- a/src/Frontend-GNOME/Frontend.cs
+++ b/src/Frontend-GNOME/Frontend.cs
@@ -64,9 +64,11 @@ namespace Smuxi.Frontend.Gnome
         public static bool IsDisconnecting { get; private set; }
         public static bool IsGtkInitialized { get; private set; }
         public static bool InGtkApplicationRun { get; private set; }
-        public static bool IsWindows { get; private set; }
+        public static bool IsWindows => Platform.IsWindows;
         public static bool IsUnity { get; private set; }
-        public static bool IsMacOSX { get; private set; }
+        public static bool IsMacOSX => Platform.IsMacOSX;
+        public static bool IsLinux => Platform.IsLinux;
+        public static bool IsMono => Platform.IsMono;
         public static Version EngineAssemblyVersion { get; set; }
         public static Version EngineProtocolVersion { get; set; }
 
@@ -172,8 +174,6 @@ namespace Smuxi.Frontend.Gnome
 
         static Frontend()
         {
-            IsWindows = Environment.OSVersion.Platform == PlatformID.Win32NT;
-            IsMacOSX = Platform.OperatingSystem == "Darwin";
             var desktop = Environment.GetEnvironmentVariable("XDG_CURRENT_DESKTOP");
             if (!String.IsNullOrEmpty(desktop) && desktop.ToLower().Contains("unity")) {
 #if LOG4NET
@@ -299,7 +299,7 @@ namespace Smuxi.Frontend.Gnome
             _FrontendManager.Sync();
 
             // MS .NET doesn't like this with Remoting?
-            if (Type.GetType("Mono.Runtime") != null) {
+            if (Frontend.IsMono) {
                 // when are running on Mono, all should be good
                 if (_UserConfig.IsCaching) {
                     // when our UserConfig is cached, we need to invalidate the cache
@@ -630,8 +630,7 @@ namespace Smuxi.Frontend.Gnome
 
             // we are using a remote engine, we are not running on Mono and an
             // IConvertible issue happened
-            if (!Frontend.IsLocalEngine &&
-                Type.GetType("Mono.Runtime") == null &&
+            if (!Frontend.IsLocalEngine && !IsMono &&
                 ex is InvalidCastException &&
                 ex.Message.Contains("IConvertible")) {
                 var msg = _(
@@ -1090,7 +1089,7 @@ namespace Smuxi.Frontend.Gnome
 #else
             // with GTK# 2.8 we can do this better, see above
             // GTK# 2.7.1 for MS .NET doesn't support that though.
-            if (Type.GetType("Mono.Runtime") == null) {
+            if (!IsMono) {
                 // when we don't run on Mono, we need to initialize glib ourself
                 GLib.Thread.Init();
             }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]