2021-08-10 11:43:36 +02:00
// _ _ _ ____ _ _____
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
// |
2022-01-06 20:22:38 +01:00
// Copyright 2015-2022 Łukasz "JustArchi" Domeradzki
2021-08-10 11:43:36 +02:00
// 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 ;
2021-11-10 21:23:24 +01:00
namespace ArchiSteamFarm.Core ;
2021-08-10 11:43:36 +02:00
2021-11-10 21:23:24 +01:00
internal static class AprilFools {
private static readonly object LockObject = new ( ) ;
2021-08-10 11:43:36 +02:00
2021-11-10 21:23:24 +01:00
// 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 ;
2021-08-10 11:43:36 +02:00
2021-11-10 21:23:24 +01:00
private static readonly Timer Timer = new ( Init ) ;
2021-08-10 11:43:36 +02:00
2021-11-10 21:23:24 +01:00
internal static void Init ( object? state = null ) {
DateTime now = DateTime . Now ;
2021-08-10 11:43:36 +02:00
2022-12-15 19:23:46 +01:00
if ( now is { Month : 4 , Day : 1 } ) {
2021-08-10 11:43:36 +02:00
try {
2021-11-10 21:23:24 +01:00
CultureInfo . DefaultThreadCurrentCulture = CultureInfo . DefaultThreadCurrentUICulture = CultureInfo . CreateSpecificCulture ( SharedInfo . LolcatCultureName ) ;
2021-08-10 11:43:36 +02:00
} catch ( Exception e ) {
ASF . ArchiLogger . LogGenericDebuggingException ( e ) ;
return ;
}
2021-11-10 21:23:24 +01:00
TimeSpan aprilFoolsEnd = TimeSpan . FromDays ( 1 ) - now . TimeOfDay ;
2021-08-10 11:43:36 +02:00
lock ( LockObject ) {
2021-11-10 21:23:24 +01:00
Timer . Change ( aprilFoolsEnd + TimeSpan . FromMilliseconds ( 100 ) , Timeout . InfiniteTimeSpan ) ;
2021-08-10 11:43:36 +02:00
}
2021-11-10 21:23:24 +01:00
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 ) ;
2021-08-10 11:43:36 +02:00
}
}
}