diff --git a/FuckMetrics/FuckMetrics.cs b/FuckMetrics/FuckMetrics.cs index 01593e9..d02169d 100644 --- a/FuckMetrics/FuckMetrics.cs +++ b/FuckMetrics/FuckMetrics.cs @@ -1,5 +1,8 @@ using ABI_RC.Core.InteractionSystem; using ABI_RC.Core.IO; +using cohtml; +using HarmonyLib; +using UnityEngine; namespace NAK.Melons.FuckMetrics { @@ -30,5 +33,26 @@ namespace NAK.Melons.FuckMetrics SchedulerSystem.RemoveJob(job); } } + + public static void CohtmlAdvanceView(CohtmlView cohtmlView, Traverse menuOpenTraverse) + { + if (!FuckMetricsMod.EntryDisableCohtmlViewOnIdle.Value) return; + + // Don't execute if menu is open + if (cohtmlView == null || menuOpenTraverse.GetValue()) return; + + // Disable cohtmlView (opening should enable) + cohtmlView.enabled = false; + + // Death + try + { + if (cohtmlView.m_UISystem != null) cohtmlView.View.Advance(cohtmlView.m_UISystem.Id, (double)Time.unscaledTime * 1000.0); + } + catch (Exception e) + { + FuckMetricsMod.Logger.Error($"An exception was thrown while calling CohtmlView.Advance(). Error message: {e.Message}"); + } + } } } diff --git a/FuckMetrics/FuckMetrics.csproj b/FuckMetrics/FuckMetrics.csproj index 10c4ff7..a930aa0 100644 --- a/FuckMetrics/FuckMetrics.csproj +++ b/FuckMetrics/FuckMetrics.csproj @@ -23,7 +23,7 @@ ..\..\..\..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\ChilloutVR\ChilloutVR_Data\Managed\cohtml.Net.dll - ..\..\..\..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\ChilloutVR\ChilloutVR_Data\Managed\Cohtml.Runtime.dll + ManagedLibs\Cohtml.Runtime.dll C:\Program Files (x86)\Steam\steamapps\common\ChilloutVR\MelonLoader\MelonLoader.dll diff --git a/FuckMetrics/HarmonyPatches.cs b/FuckMetrics/HarmonyPatches.cs index 1cf1233..6b91273 100644 --- a/FuckMetrics/HarmonyPatches.cs +++ b/FuckMetrics/HarmonyPatches.cs @@ -1,4 +1,6 @@ using ABI_RC.Core.InteractionSystem; +using ABI_RC.Core.IO; +using cohtml; using HarmonyLib; namespace NAK.Melons.FuckMetrics.HarmonyPatches; @@ -59,4 +61,30 @@ class ViewManagerPatches CVR_MenuManager.Instance.SendCoreUpdate(); } } +} + +public static class CohtmlViewPatches +{ + private static CohtmlView _quickMenuView; + private static CohtmlView _gameMenuView; + private static Traverse _quickMenuOpenTraverse; + private static Traverse _gameMenuOpenTraverse; + + [HarmonyPostfix] + [HarmonyPatch(typeof(CVR_MenuManager), "Start")] + private static void Postfix_CVR_MenuManager_Start(ref CVR_MenuManager __instance, ref CohtmlView ___quickMenu) + { + _quickMenuView = ___quickMenu; + _quickMenuOpenTraverse = Traverse.Create(__instance).Field("_quickMenuOpen"); + SchedulerSystem.AddJob(new SchedulerSystem.Job(() => FuckMetrics.CohtmlAdvanceView(_quickMenuView, _quickMenuOpenTraverse)), 15f, 6f, -1); + } + + [HarmonyPostfix] + [HarmonyPatch(typeof(ViewManager), "Start")] + private static void Postfix_ViewManager_Start(ref ViewManager __instance, ref CohtmlView ___gameMenuView) + { + _gameMenuView = ___gameMenuView; + _gameMenuOpenTraverse = Traverse.Create(__instance).Field("_gameMenuOpen"); + SchedulerSystem.AddJob(new SchedulerSystem.Job(() => FuckMetrics.CohtmlAdvanceView(_gameMenuView, _gameMenuOpenTraverse)), 12f, 6f, -1); + } } \ No newline at end of file diff --git a/FuckMetrics/Main.cs b/FuckMetrics/Main.cs index 457b211..31ab736 100644 --- a/FuckMetrics/Main.cs +++ b/FuckMetrics/Main.cs @@ -6,9 +6,13 @@ namespace NAK.Melons.FuckMetrics; public class FuckMetricsMod : MelonMod { + public static MelonLogger.Instance Logger; public const string SettingsCategory = "FuckMetrics"; public static readonly MelonPreferences_Category CategoryFuckMetrics = MelonPreferences.CreateCategory(SettingsCategory); + public static readonly MelonPreferences_Entry EntryDisableCohtmlViewOnIdle = + CategoryFuckMetrics.CreateEntry("Disable CohtmlView On Idle", false, description: "Disables CohtmlView on the menus when idle. This can give a huge performance boost."); + public static readonly MelonPreferences_Entry EntryDisableMetrics = CategoryFuckMetrics.CreateEntry("Menu Metrics", SettingState.MenuOnly, description: "Disables menu metrics (FPS & Ping). Updates once on menu open if disabled."); @@ -20,7 +24,6 @@ public class FuckMetricsMod : MelonMod public static readonly MelonPreferences_Entry EntryCoreUpdateRate = CategoryFuckMetrics.CreateEntry("Core Update Rate", 2f, description: "Sets the update rate for the menu core updates. Default is 0.1f. Recommended to be 2f or higher."); - public enum SettingState { Always, @@ -30,10 +33,14 @@ public class FuckMetricsMod : MelonMod public override void OnInitializeMelon() { + Logger = LoggerInstance; EntryDisableMetrics.OnEntryValueChangedUntyped.Subscribe(OnDisableMetrics); EntryDisableCoreUpdates.OnEntryValueChangedUntyped.Subscribe(OnDisableCoreUpdates); + EntryMetricsUpdateRate.OnEntryValueChangedUntyped.Subscribe(OnChangeMetricsUpdateRate); + EntryCoreUpdateRate.OnEntryValueChangedUntyped.Subscribe(OnChangeCoreUpdateRate); ApplyPatches(typeof(HarmonyPatches.CVR_MenuManagerPatches)); ApplyPatches(typeof(HarmonyPatches.ViewManagerPatches)); + ApplyPatches(typeof(HarmonyPatches.CohtmlViewPatches)); MelonCoroutines.Start(WaitForLocalPlayer()); } @@ -60,6 +67,24 @@ public class FuckMetricsMod : MelonMod FuckMetrics.ToggleCoreUpdates(EntryDisableCoreUpdates.Value == SettingState.Always); } + private void OnChangeMetricsUpdateRate(object arg1, object arg2) + { + if (EntryDisableMetrics.Value != SettingState.Disabled) + { + FuckMetrics.ToggleMetrics(false); + FuckMetrics.ToggleMetrics(true); + } + } + + private void OnChangeCoreUpdateRate(object arg1, object arg2) + { + if (EntryDisableCoreUpdates.Value != SettingState.Disabled) + { + FuckMetrics.ToggleCoreUpdates(false); + FuckMetrics.ToggleCoreUpdates(true); + } + } + private void ApplyPatches(Type type) { try @@ -68,8 +93,8 @@ public class FuckMetricsMod : MelonMod } catch (Exception e) { - LoggerInstance.Msg($"Failed while patching {type.Name}!"); - LoggerInstance.Error(e); + Logger.Msg($"Failed while patching {type.Name}!"); + Logger.Error(e); } } } \ No newline at end of file