From 558030f72a6c36cd84aa81b4e33bc490692ec0d5 Mon Sep 17 00:00:00 2001 From: Clansty Date: Tue, 22 Apr 2025 22:08:29 +0800 Subject: [PATCH] [+] AntiLag --- AquaMai.Mods/Utils/AntiLag.cs | 44 +++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 AquaMai.Mods/Utils/AntiLag.cs diff --git a/AquaMai.Mods/Utils/AntiLag.cs b/AquaMai.Mods/Utils/AntiLag.cs new file mode 100644 index 00000000..a5ef174a --- /dev/null +++ b/AquaMai.Mods/Utils/AntiLag.cs @@ -0,0 +1,44 @@ +using System.Runtime.InteropServices; +using AquaMai.Config.Attributes; +using HarmonyLib; +using Main; +using MelonLoader; +using UnityEngine; + +namespace AquaMai.Mods.Utils; + +[ConfigSection( + en: "Some tricks to prevent the system from lagging", + zh: "奇妙的防掉帧,如果你有莫名其妙的掉帧,可以试试这个")] +public class AntiLag : MonoBehaviour +{ + [HarmonyPostfix] + [HarmonyPatch(typeof(GameMainObject), "Awake")] + public static void OnGameMainObjectAwake() + { + var go = new GameObject("妙妙防掉帧"); + go.AddComponent(); + } + + private void Awake() + { + InvokeRepeating(nameof(OnTimer), 10f, 10f); + } + + [DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)] + private static extern void keybd_event(uint bVk, byte bScan, uint dwFlags, uint dwExtraInfo); + + const int KEYEVENTF_KEYDOWN = 0x0000; + const int KEYEVENTF_KEYUP = 0x0002; + const int CTRL = 17; + + private void OnTimer() + { + if (!Application.isFocused) return; +#if DEBUG + MelonLogger.Msg("[AntiLag] Trigger"); +#endif + keybd_event(CTRL, 0, KEYEVENTF_KEYDOWN, 0); + keybd_event(CTRL, 0, KEYEVENTF_KEYUP, 0); + } +} \ No newline at end of file