Update ASF-ui digest to 51952a3
[ArchiSteamFarm.git] / ArchiSteamFarm / Core / AprilFools.cs
blob3579e73fa346c10af9d150bd9a54f0a6a682e529
1 // ----------------------------------------------------------------------------------------------
2 // _ _ _ ____ _ _____
3 // / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
4 // / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
5 // / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
6 // /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
7 // ----------------------------------------------------------------------------------------------
8 // |
9 // Copyright 2015-2024 Łukasz "JustArchi" Domeradzki
10 // Contact: JustArchi@JustArchi.net
11 // |
12 // Licensed under the Apache License, Version 2.0 (the "License");
13 // you may not use this file except in compliance with the License.
14 // You may obtain a copy of the License at
15 // |
16 // http://www.apache.org/licenses/LICENSE-2.0
17 // |
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the License is distributed on an "AS IS" BASIS,
20 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 // See the License for the specific language governing permissions and
22 // limitations under the License.
24 using System;
25 using System.Globalization;
26 using System.Threading;
28 namespace ArchiSteamFarm.Core;
30 internal static class AprilFools {
31 private static readonly object LockObject = new();
33 // We don't care about CurrentCulture global config property, because April Fools are never initialized in this case
34 private static readonly CultureInfo OriginalCulture = CultureInfo.CurrentCulture;
36 private static readonly Timer Timer = new(Init);
38 internal static void Init(object? state = null) {
39 DateTime now = DateTime.Now;
41 if (now is (_, 4, 1)) {
42 try {
43 CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.CreateSpecificCulture(SharedInfo.LolcatCultureName);
44 } catch (Exception e) {
45 ASF.ArchiLogger.LogGenericDebuggingException(e);
47 return;
50 TimeSpan aprilFoolsEnd = TimeSpan.FromDays(1) - now.TimeOfDay;
52 lock (LockObject) {
53 Timer.Change(aprilFoolsEnd + TimeSpan.FromMilliseconds(100), Timeout.InfiniteTimeSpan);
56 return;
59 try {
60 CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = OriginalCulture;
61 } catch (Exception e) {
62 ASF.ArchiLogger.LogGenericDebuggingException(e);
64 return;
67 // 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
68 DateTime nextAprilFools = new(now.Month >= 4 ? now.Year + 1 : now.Year, 4, 1, 0, 0, 0, DateTimeKind.Local);
70 TimeSpan aprilFoolsStart = nextAprilFools - now;
72 // Timer can accept only dueTimes up to 2^32 - 2
73 uint dueTime = (uint) Math.Min(uint.MaxValue - 1, (ulong) aprilFoolsStart.TotalMilliseconds + 100);
75 lock (LockObject) {
76 Timer.Change(dueTime, Timeout.Infinite);