From 1fe5cfff490fe8d2d207b9dadd67615d922c70df Mon Sep 17 00:00:00 2001 From: JustArchi Date: Tue, 28 Jun 2016 10:36:28 +0200 Subject: [PATCH] Fix broken Mono after #268 --- ArchiSteamFarm.sln.DotSettings | 2 +- ArchiSteamFarm/ArchiSteamFarm.csproj | 2 +- ArchiSteamFarm/GlobalConfig.cs | 2 +- ArchiSteamFarm/Program.cs | 4 +- ArchiSteamFarm/{Mono.cs => Runtime.cs} | 53 +++++++++++++++++++++----- 5 files changed, 48 insertions(+), 15 deletions(-) rename ArchiSteamFarm/{Mono.cs => Runtime.cs} (57%) diff --git a/ArchiSteamFarm.sln.DotSettings b/ArchiSteamFarm.sln.DotSettings index fd6581351..5a9b16887 100644 --- a/ArchiSteamFarm.sln.DotSettings +++ b/ArchiSteamFarm.sln.DotSettings @@ -20,7 +20,7 @@ WTF XML <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb"><ExtraRule Prefix="_" Suffix="" Style="AaBb" /><ExtraRule Prefix="_" Suffix="" Style="aaBb" /></Policy> - <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb"><ExtraRule Prefix="_" Suffix="" Style="aaBb" /></Policy> + <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb"><ExtraRule Prefix="_" Suffix="" Style="AaBb" /><ExtraRule Prefix="_" Suffix="" Style="aaBb" /></Policy> True True True \ No newline at end of file diff --git a/ArchiSteamFarm/ArchiSteamFarm.csproj b/ArchiSteamFarm/ArchiSteamFarm.csproj index a631e4ba9..baa1891f7 100644 --- a/ArchiSteamFarm/ArchiSteamFarm.csproj +++ b/ArchiSteamFarm/ArchiSteamFarm.csproj @@ -117,7 +117,7 @@ - + diff --git a/ArchiSteamFarm/GlobalConfig.cs b/ArchiSteamFarm/GlobalConfig.cs index 55ba9ec40..2ccb7ba74 100644 --- a/ArchiSteamFarm/GlobalConfig.cs +++ b/ArchiSteamFarm/GlobalConfig.cs @@ -165,7 +165,7 @@ namespace ArchiSteamFarm { globalConfig.FarmingDelay = DefaultFarmingDelay; } - if ((globalConfig.FarmingDelay > 5) && Mono.RequiresWorkaroundForBug41701()) { + if ((globalConfig.FarmingDelay > 5) && Runtime.RequiresWorkaroundForMonoBug41701()) { Logging.LogGenericWarning("Your Mono runtime is affected by bug 41701, FarmingDelay of " + globalConfig.FarmingDelay + " is not possible - value of 5 will be used instead"); globalConfig.FarmingDelay = 5; } diff --git a/ArchiSteamFarm/Program.cs b/ArchiSteamFarm/Program.cs index 00d0b05e2..214921895 100644 --- a/ArchiSteamFarm/Program.cs +++ b/ArchiSteamFarm/Program.cs @@ -277,7 +277,7 @@ namespace ArchiSteamFarm { return null; } - if (GlobalConfig.Headless || !Environment.UserInteractive) { + if (GlobalConfig.Headless || !Runtime.IsUserInteractive) { Logging.LogGenericWarning("Received a request for user input, but process is running in headless mode!"); return null; } @@ -540,7 +540,7 @@ namespace ArchiSteamFarm { } private static void Main(string[] args) { - if (Environment.UserInteractive) { + if (Runtime.IsUserInteractive) { // App Init(args); diff --git a/ArchiSteamFarm/Mono.cs b/ArchiSteamFarm/Runtime.cs similarity index 57% rename from ArchiSteamFarm/Mono.cs rename to ArchiSteamFarm/Runtime.cs index da46d4438..9846baa9f 100644 --- a/ArchiSteamFarm/Mono.cs +++ b/ArchiSteamFarm/Runtime.cs @@ -23,27 +23,60 @@ */ using System; +using System.IO; using System.Reflection; namespace ArchiSteamFarm { - internal static class Mono { - internal static bool RequiresWorkaroundForBug41701() { - // https://bugzilla.xamarin.com/show_bug.cgi?id=41701 - Version version = GetMonoVersion(); - if (version == null) { + internal static class Runtime { + private static readonly Type MonoRuntime = Type.GetType("Mono.Runtime"); + private static bool IsRunningOnMono => MonoRuntime != null; + + private static bool? _IsUserInteractive; + internal static bool IsUserInteractive { + get { + if (_IsUserInteractive.HasValue) { + return _IsUserInteractive.Value; + } + + if (Environment.UserInteractive) { + _IsUserInteractive = true; + return true; + } + + // If it's non-Mono, we can trust the result + if (!IsRunningOnMono) { + _IsUserInteractive = false; + return false; + } + + // Otherwise use workaround for Mono, as Environment.UserInteractive is always false + // Please close your eyes, there is really no better way for now + _IsUserInteractive = Console.In is StreamReader; + return _IsUserInteractive.Value; + } + } + + internal static bool RequiresWorkaroundForMonoBug41701() { + // Mono only, https://bugzilla.xamarin.com/show_bug.cgi?id=41701 + if (!IsRunningOnMono) { return false; } - return version >= new Version(4, 4); + Version monoVersion = GetMonoVersion(); + if (monoVersion == null) { + return false; + } + + return monoVersion >= new Version(4, 4); } private static Version GetMonoVersion() { - Type type = Type.GetType("Mono.Runtime"); - if (type == null) { - return null; // OK, not Mono + if (MonoRuntime == null) { + Logging.LogNullError(nameof(MonoRuntime)); + return null; } - MethodInfo displayName = type.GetMethod("GetDisplayName", BindingFlags.NonPublic | BindingFlags.Static); + MethodInfo displayName = MonoRuntime.GetMethod("GetDisplayName", BindingFlags.NonPublic | BindingFlags.Static); if (displayName == null) { Logging.LogNullError(nameof(displayName)); return null;