From ae876f709d5e774e5076d4428af5bb872c8d3b8b Mon Sep 17 00:00:00 2001 From: data-bomb Date: Wed, 20 Mar 2024 07:20:49 -0700 Subject: [PATCH] Changes Timers for AFK Mod - Changes away from System.Timers for the AFK mod --- Si_AFKManager/Si_AFK.cs | 44 ++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/Si_AFKManager/Si_AFK.cs b/Si_AFKManager/Si_AFK.cs index 2947b69..9bfe424 100644 --- a/Si_AFKManager/Si_AFK.cs +++ b/Si_AFKManager/Si_AFK.cs @@ -28,14 +28,14 @@ You should have received a copy of the GNU General Public License using HarmonyLib; using MelonLoader; using Si_AFKManager; -using System.Timers; using System.Collections.Generic; using System.Linq; using SilicaAdminMod; using System; using System.Collections; +using UnityEngine; -[assembly: MelonInfo(typeof(AwayFromKeyboard), "AFK Manager", "1.2.6", "databomb", "https://github.com/data-bomb/Silica")] +[assembly: MelonInfo(typeof(AwayFromKeyboard), "AFK Manager", "1.2.7", "databomb", "https://github.com/data-bomb/Silica")] [assembly: MelonGame("Bohemia Interactive", "Silica")] [assembly: MelonOptionalDependencies("Admin Mod")] @@ -59,10 +59,9 @@ public uint Minutes } } - static Timer afkTimer = null!; + static float Timer_AFKCheck = HelperMethods.Timer_Inactive; static bool AdminModAvailable = false; static List AFKTracker = null!; - static bool oneMinuteCheckTime; static bool skippedFirstCheck; static MelonPreferences_Category _modCategory = null!; @@ -78,6 +77,8 @@ public override void OnInitializeMelon() public override void OnLateInitializeMelon() { + HelperMethods.StartTimer(Timer_AFKCheck); + AdminModAvailable = RegisteredMelons.Any(m => m.Info.Name == "Admin Mod"); AFKTracker = new List(); @@ -87,12 +88,6 @@ public override void OnLateInitializeMelon() HelperMethods.RegisterAdminCommand("kick", kickCallback, Power.Kick); HelperMethods.RegisterAdminCommand("afk", afkCallback, Power.Kick); - double interval = 60000.0f; - afkTimer = new System.Timers.Timer(interval); - afkTimer.Elapsed += new ElapsedEventHandler(TimerCallbackOneMinute); - afkTimer.AutoReset = true; - afkTimer.Enabled = true; - #if NET6_0 bool QListLoaded = RegisteredMelons.Any(m => m.Info.Name == "QList"); if (!QListLoaded) @@ -234,11 +229,6 @@ public static void Command_AFK(Player callerPlayer, String args) } } - private static void TimerCallbackOneMinute(object? source, ElapsedEventArgs e) - { - oneMinuteCheckTime = true; - } - #if NET6_0 [HarmonyPatch(typeof(MusicJukeboxHandler), nameof(MusicJukeboxHandler.Update))] #else @@ -256,11 +246,22 @@ private static void Postfix(MusicJukeboxHandler __instance) } // check if timer expired while the game is in-progress - if (GameMode.CurrentGameMode.GameOngoing == true && oneMinuteCheckTime == true && skippedFirstCheck == true) + Timer_AFKCheck += Time.deltaTime; + if (Timer_AFKCheck >= 60.0f) { - //MelonLogger.Msg("AFK Timer fired"); + Timer_AFKCheck = 0.0f; - oneMinuteCheckTime = false; + if (!GameMode.CurrentGameMode.GameOngoing) + { + return; + } + + // skip the first timer expiration so we're at least a minute into the round + if (!skippedFirstCheck) + { + skippedFirstCheck = true; + return; + } // track if any players need to be removed from the AFKTracker list after we've finished iterating // we can't kick inside the foreach Players iterator because it modifies the list @@ -334,13 +335,6 @@ private static void Postfix(MusicJukeboxHandler __instance) AFKTracker.RemoveAt(indexToKick); } } - - // skip the first timer expiration so we're at least a minute into the round - if (GameMode.CurrentGameMode.GameOngoing == true && oneMinuteCheckTime == true && skippedFirstCheck == false) - { - oneMinuteCheckTime = false; - skippedFirstCheck = true; - } } catch (Exception exception) {