From e6784b967e2b43058d3c58b962a8ef5a77e26022 Mon Sep 17 00:00:00 2001 From: NotAKidoS <37721153+NotAKidOnSteam@users.noreply.github.com> Date: Fri, 14 Oct 2022 02:00:05 -0500 Subject: [PATCH] Initial Release Added UIExpansionKit support, moved Harmony patches to own cs, added accidental feature that disables automatic state change, added option to lower FPS while in sleep state. --- Blackout/Blackout.csproj | 3 +++ Blackout/BlackoutController.cs | 28 ++++++++++++-------- Blackout/HarmonyPatches.cs | 19 ++++++++++++++ Blackout/Main.cs | 45 ++++++++++++++++++--------------- Blackout/UIExpansionKitAddon.cs | 15 +++++++++++ 5 files changed, 80 insertions(+), 30 deletions(-) create mode 100644 Blackout/HarmonyPatches.cs create mode 100644 Blackout/UIExpansionKitAddon.cs diff --git a/Blackout/Blackout.csproj b/Blackout/Blackout.csproj index 6b9471a..60c5870 100644 --- a/Blackout/Blackout.csproj +++ b/Blackout/Blackout.csproj @@ -38,6 +38,9 @@ C:\Program Files (x86)\Steam\steamapps\common\ChilloutVR\ChilloutVR_Data\Managed\SteamVR.dll + + ..\..\..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\ChilloutVR\Mods\UIExpansionKit.dll + ..\..\..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\ChilloutVR\ChilloutVR_Data\Managed\UnityEngine.AnimationModule.dll diff --git a/Blackout/BlackoutController.cs b/Blackout/BlackoutController.cs index a583775..6906192 100644 --- a/Blackout/BlackoutController.cs +++ b/Blackout/BlackoutController.cs @@ -1,5 +1,6 @@ using ABI_RC.Core.Player; using ABI_RC.Core.UI; +using ABI_RC.Core.Savior; using MelonLoader; using UnityEngine; @@ -47,6 +48,9 @@ public class BlackoutController : MonoBehaviour //this is uh, not work well- might rewrite now that i know how this should work public bool HudMessages = false; + //lower FPS while in sleep mode + public bool DropFPSOnSleep = false; + public enum BlackoutState { Awake = 0, @@ -60,6 +64,7 @@ public class BlackoutController : MonoBehaviour private float lastAwakeTime = 0f; private int nextUpdate = 1; private Animator blackoutAnimator; + private int targetFPS; public void ChangeBlackoutState(BlackoutState newState) { @@ -91,6 +96,7 @@ public class BlackoutController : MonoBehaviour BlackoutState prevState = CurrentState; CurrentState = newState; SendHUDMessage($"Exiting {prevState} and entering {newState} state."); + ChangeTargetFPS(); } void Update() @@ -144,18 +150,10 @@ public class BlackoutController : MonoBehaviour SetupBlackoutInstance(); } } - - void OnEnabled() - { - if (!blackoutAnimator) return; - blackoutAnimator.gameObject.SetActive(true); - } - - void OnDisabled() + + void OnDisable() { ChangeBlackoutState(BlackoutState.Awake); - if (!blackoutAnimator) return; - blackoutAnimator.gameObject.SetActive(false); } public void SetupBlackoutInstance() @@ -189,6 +187,16 @@ public class BlackoutController : MonoBehaviour CohtmlHud.Instance.ViewDropTextImmediate("Blackout", message, GetNextStateTimer().ToString() + " seconds till next state change."); } + private void ChangeTargetFPS() + { + if (!DropFPSOnSleep) return; + + //store target FPS to restore, i check each time just in case it changed + targetFPS = MetaPort.Instance.settings.GetSettingInt("GraphicsFramerateTarget", 0); + + Application.targetFrameRate = (CurrentState == BlackoutState.Sleeping) ? 5 : targetFPS; + } + private void HandleAwakeState() { //small movement should reset sleep timer diff --git a/Blackout/HarmonyPatches.cs b/Blackout/HarmonyPatches.cs new file mode 100644 index 0000000..00dd3e8 --- /dev/null +++ b/Blackout/HarmonyPatches.cs @@ -0,0 +1,19 @@ +using HarmonyLib; +using ABI_RC.Core.Player; + +namespace Blackout; + +[HarmonyPatch] +internal class HarmonyPatches +{ + //Support for changing VRMode during runtime. + [HarmonyPostfix] + [HarmonyPatch(typeof(PlayerSetup), "CalibrateAvatar")] + private static void CheckVRModeOnSwitch() + { + if (Blackout.inVR != PlayerSetup.Instance._inVr) + { + BlackoutController.Instance.SetupBlackoutInstance(); + } + } +} \ No newline at end of file diff --git a/Blackout/Main.cs b/Blackout/Main.cs index e4fa78a..2f8f7f0 100644 --- a/Blackout/Main.cs +++ b/Blackout/Main.cs @@ -7,11 +7,13 @@ namespace Blackout; public class Blackout : MelonMod { - private static bool inVR; - private static MelonPreferences_Category m_categoryBlackout; - private static MelonPreferences_Entry m_entryEnabled, m_entryHudMessages; - //private static MelonPreferences_Entry m_entryVROnly; - private static MelonPreferences_Entry + public const string SettingsCategory = "Blackout"; + + internal static bool inVR; + internal static MelonPreferences_Category m_categoryBlackout; + internal static MelonPreferences_Entry m_entryEnabled, m_entryHudMessages, m_entryDropFPSOnSleep; + //internal static MelonPreferences_Entry m_entryVROnly; + internal static MelonPreferences_Entry m_entryDrowsyThreshold, m_entryAwakeThreshold, m_entryDrowsyModeTimer, m_entrySleepModeTimer, m_entryDrowsyDimStrength; @@ -19,8 +21,9 @@ public class Blackout : MelonMod public override void OnApplicationStart() { m_categoryBlackout = MelonPreferences.CreateCategory(nameof(Blackout)); - m_entryEnabled = m_categoryBlackout.CreateEntry("Enabled", true, description: "Dim screen when sleeping."); + m_entryEnabled = m_categoryBlackout.CreateEntry("Automatic State Change", true, description: "Dim screen when there is no movement for a while."); m_entryHudMessages = m_categoryBlackout.CreateEntry("Hud Messages", false, description: "Notify on state change."); + m_entryDropFPSOnSleep = m_categoryBlackout.CreateEntry("Lower FPS While Sleep", false, description: "Lowers FPS to 5 while in Sleep State."); m_entryDrowsyThreshold = m_categoryBlackout.CreateEntry("Drowsy Threshold", 1f, description: "Degrees of movement to return partial vision."); m_entryAwakeThreshold = m_categoryBlackout.CreateEntry("Awake Threshold", 12f, description: "Degrees of movement to return full vision."); m_entryDrowsyModeTimer = m_categoryBlackout.CreateEntry("Enter Drowsy Time", 3f, description: "How many minutes without movement until enter drowsy mode."); @@ -29,8 +32,12 @@ public class Blackout : MelonMod //m_entryVROnly = m_categoryBlackout.CreateEntry("VR Only", false, description: "Only enable mod in VR."); m_categoryBlackout.SaveToFile(false); + //please tell me a better way to do this + //this is fucking + //gross pleas etell me how to do this but not like this m_entryEnabled.OnValueChangedUntyped += OnEnabled; m_entryHudMessages.OnValueChangedUntyped += OnUpdateSettings; + m_entryDropFPSOnSleep.OnValueChangedUntyped += OnUpdateSettings; m_entryDrowsyThreshold.OnValueChangedUntyped += OnUpdateSettings; m_entryAwakeThreshold.OnValueChangedUntyped += OnUpdateSettings; m_entryDrowsyModeTimer.OnValueChangedUntyped += OnUpdateSettings; @@ -38,6 +45,13 @@ public class Blackout : MelonMod m_entryDrowsyDimStrength.OnValueChangedUntyped += OnUpdateSettings; //m_entryVROnly.OnValueChangedUntyped += OnUpdateSettings; MelonLoader.MelonCoroutines.Start(WaitForLocalPlayer()); + + //UIExpansionKit addon + if (MelonHandler.Mods.Any(it => it.Info.Name == "UI Expansion Kit")) + { + MelonLogger.Msg("Initializing UIExpansionKit support."); + UiExtensionsAddon.Init(); + } } System.Collections.IEnumerator WaitForLocalPlayer() @@ -70,20 +84,11 @@ public class Blackout : MelonMod BlackoutController.Instance.SleepModeTimer = m_entrySleepModeTimer.Value; BlackoutController.Instance.DrowsyDimStrength = m_entryDrowsyDimStrength.Value; BlackoutController.Instance.HudMessages = m_entryHudMessages.Value; + BlackoutController.Instance.DropFPSOnSleep = m_entryDropFPSOnSleep.Value; } - //Support for changing VRMode during runtime. - [HarmonyPatch] - private class HarmonyPatches - { - [HarmonyPostfix] - [HarmonyPatch(typeof(PlayerSetup), "CalibrateAvatar")] - private static void CheckVRModeSwitch() - { - if (inVR != PlayerSetup.Instance._inVr) - { - BlackoutController.Instance.SetupBlackoutInstance(); - } - } - } + //UIExpansionKit actions + public static void AwakeState() => BlackoutController.Instance.ChangeBlackoutState(BlackoutController.BlackoutState.Awake); + public static void DrowsyState() => BlackoutController.Instance.ChangeBlackoutState(BlackoutController.BlackoutState.Drowsy); + public static void SleepingState() => BlackoutController.Instance.ChangeBlackoutState(BlackoutController.BlackoutState.Sleeping); } \ No newline at end of file diff --git a/Blackout/UIExpansionKitAddon.cs b/Blackout/UIExpansionKitAddon.cs new file mode 100644 index 0000000..f38bccb --- /dev/null +++ b/Blackout/UIExpansionKitAddon.cs @@ -0,0 +1,15 @@ +using System.Runtime.CompilerServices; +using UIExpansionKit.API; + +namespace Blackout; +public static class UiExtensionsAddon +{ + [MethodImpl(MethodImplOptions.NoInlining)] + public static void Init() + { + var settings = ExpansionKitApi.GetSettingsCategory(Blackout.SettingsCategory); + settings.AddSimpleButton("Awake State", Blackout.AwakeState); + settings.AddSimpleButton("Drowsy State", Blackout.DrowsyState); + settings.AddSimpleButton("Sleep State", Blackout.SleepingState); + } +} \ No newline at end of file