diff --git a/ArchiSteamFarm/ArchiSteamFarm.csproj b/ArchiSteamFarm/ArchiSteamFarm.csproj index 8c9cb2700..fb9ca8e65 100644 --- a/ArchiSteamFarm/ArchiSteamFarm.csproj +++ b/ArchiSteamFarm/ArchiSteamFarm.csproj @@ -30,6 +30,7 @@ + diff --git a/ArchiSteamFarm/Core/AprilFools.cs b/ArchiSteamFarm/Core/AprilFools.cs new file mode 100644 index 000000000..60f04f1a7 --- /dev/null +++ b/ArchiSteamFarm/Core/AprilFools.cs @@ -0,0 +1,83 @@ +// _ _ _ ____ _ _____ +// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___ +// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \ +// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | | +// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_| +// | +// Copyright 2015-2021 Łukasz "JustArchi" Domeradzki +// Contact: JustArchi@JustArchi.net +// | +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// | +// http://www.apache.org/licenses/LICENSE-2.0 +// | +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Globalization; +using System.Threading; + +namespace ArchiSteamFarm.Core { + internal static class AprilFools { + private static readonly object LockObject = new(); + + // We don't care about CurrentCulture global config property, because April Fools are never initialized in this case + private static readonly CultureInfo OriginalCulture = CultureInfo.CurrentCulture; + + private static readonly Timer Timer = new(Init); + + internal static void Init(object? state = null) { + DateTime now = DateTime.Now; + + if ((now.Month == 4) && (now.Day == 1)) { + try { + CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.CreateSpecificCulture("qps-Ploc"); + } catch (Exception e) { + ASF.ArchiLogger.LogGenericDebuggingException(e); + + return; + } + + TimeSpan aprilFoolsEnd = TimeSpan.FromDays(1) - now.TimeOfDay; + + lock (LockObject) { + Timer.Change(aprilFoolsEnd + TimeSpan.FromMilliseconds(100), Timeout.InfiniteTimeSpan); + } + + return; + } + + try { + CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = OriginalCulture; + } catch (Exception e) { + ASF.ArchiLogger.LogGenericDebuggingException(e); + + return; + } + + // Since we already verified that it's not April Fools right now, either we're in months 1-3 before 1st April this year, or 4-12 already after the 1st April + DateTime nextAprilFools = new(now.Month >= 4 ? now.Year + 1 : now.Year, 4, 1, 0, 0, 0, DateTimeKind.Local); + + TimeSpan aprilFoolsStart = nextAprilFools - now; + + // Timer can accept only dueTimes up to 2^32 - 2 + uint dueTime = (uint) Math.Min(uint.MaxValue - 1, (ulong) aprilFoolsStart.TotalMilliseconds + 100); + + lock (LockObject) { + Timer.Change(dueTime, Timeout.Infinite); + } + } + + internal static void OnTimeChanged(object? sender, EventArgs e) { + lock (LockObject) { + Timer.Change(TimeSpan.Zero, Timeout.InfiniteTimeSpan); + } + } + } +} diff --git a/ArchiSteamFarm/Program.cs b/ArchiSteamFarm/Program.cs index 7e494c975..7ba0b5fc9 100644 --- a/ArchiSteamFarm/Program.cs +++ b/ArchiSteamFarm/Program.cs @@ -19,6 +19,9 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if NETFRAMEWORK +using System.Runtime.InteropServices; +#endif using System; using System.Collections; using System.Collections.Generic; @@ -39,6 +42,7 @@ using ArchiSteamFarm.NLog.Targets; using ArchiSteamFarm.Steam; using ArchiSteamFarm.Storage; using ArchiSteamFarm.Web; +using Microsoft.Win32; using Newtonsoft.Json; using NLog; using NLog.Targets; @@ -267,15 +271,15 @@ namespace ArchiSteamFarm { ASF.ArchiLogger.LogGenericError(Strings.ErrorInvalidCurrentCulture); } } else { - // April Fools easter egg - DateTime now = DateTime.Now; + // April Fools easter egg logic + AprilFools.Init(); - if ((now.Month == 4) && (now.Day == 1)) { - try { - CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.CreateSpecificCulture("qps-Ploc"); - } catch (Exception e) { - ASF.ArchiLogger.LogGenericDebuggingException(e); - } +#if NETFRAMEWORK + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { +#else + if (OperatingSystem.IsWindows()) { +#endif + SystemEvents.TimeChanged += AprilFools.OnTimeChanged; } } diff --git a/Directory.Packages.props b/Directory.Packages.props index 4ffc7fb5f..7a320141c 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -23,6 +23,7 @@ +