From 7e1a9019305de196cbe97fe7bb6bf5db052ec0ab Mon Sep 17 00:00:00 2001 From: dplochcoder Date: Sun, 10 Aug 2025 22:17:54 -0400 Subject: [PATCH 1/2] Allow clients to react to HKMP's time scale changes --- HKMP/Api/Client/IClientManager.cs | 10 ++++++++++ HKMP/Game/Client/ClientManager.cs | 16 +++++++++++++--- HKMP/Game/Client/PauseManager.cs | 15 ++++++++++++--- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/HKMP/Api/Client/IClientManager.cs b/HKMP/Api/Client/IClientManager.cs index 53ed9f15..9d84272b 100644 --- a/HKMP/Api/Client/IClientManager.cs +++ b/HKMP/Api/Client/IClientManager.cs @@ -90,4 +90,14 @@ public interface IClientManager { /// Event that is called when another player leaves the local scene. /// event Action PlayerLeaveSceneEvent; + + /// + /// Event that is called when HKMP modifies the game's time scale. + /// + delegate void SetTimeScale(ref float timeScale); + + /// + /// Event that is called when HKMP modifies the game's time scale. + /// + event SetTimeScale OnSetTimeScale; } diff --git a/HKMP/Game/Client/ClientManager.cs b/HKMP/Game/Client/ClientManager.cs index 6ac6a69c..cc048088 100644 --- a/HKMP/Game/Client/ClientManager.cs +++ b/HKMP/Game/Client/ClientManager.cs @@ -74,6 +74,11 @@ internal class ClientManager : IClientManager { /// private readonly EntityManager _entityManager; + /// + /// The pause manager instance. + /// + private readonly PauseManager _pauseManager; + /// /// The client addon manager instance. /// @@ -128,6 +133,9 @@ public string Username { /// public event Action PlayerLeaveSceneEvent; + /// + public event IClientManager.SetTimeScale OnSetTimeScale; + /// public Team Team => _playerManager.LocalPlayerTeam; @@ -180,7 +188,9 @@ ModSettings modSettings _entityManager = new EntityManager(netClient); - new PauseManager(netClient).RegisterHooks(); + _pauseManager = new PauseManager(netClient, (ref float timeScale) => OnSetTimeScale?.Invoke(ref timeScale)); + _pauseManager.RegisterHooks(); + new FsmPatcher().RegisterHooks(); _commandManager = new ClientCommandManager(); @@ -327,7 +337,7 @@ private void InternalDisconnect() { // Check whether the game is in the pause menu and reset timescale to 0 in that case if (UIManager.instance.uiState.Equals(UIState.PAUSED)) { - PauseManager.SetTimeScale(0); + _pauseManager.SetTimeScale(0); } try { @@ -485,7 +495,7 @@ private void OnClientConnect(LoginResponse loginResponse) { // Since we are probably in the pause menu when we connect, set the timescale so the game // is running while paused - PauseManager.SetTimeScale(1.0f); + _pauseManager.SetTimeScale(1.0f); UiManager.InternalChatBox.AddMessage("You are connected to the server"); } diff --git a/HKMP/Game/Client/PauseManager.cs b/HKMP/Game/Client/PauseManager.cs index 89fda0fe..da50ddc1 100644 --- a/HKMP/Game/Client/PauseManager.cs +++ b/HKMP/Game/Client/PauseManager.cs @@ -1,6 +1,7 @@ using System.Collections; using System.Reflection; using GlobalEnums; +using Hkmp.Api.Client; using Hkmp.Networking.Client; using Modding; using UnityEngine; @@ -15,9 +16,15 @@ internal class PauseManager { /// The net client instance. /// private readonly NetClient _netClient; + + /// + /// Hook for time scale changes. + /// + private readonly IClientManager.SetTimeScale _onSetTimeScale; - public PauseManager(NetClient netClient) { + public PauseManager(NetClient netClient, IClientManager.SetTimeScale onSetTimeScale) { _netClient = netClient; + _onSetTimeScale = onSetTimeScale; } /// @@ -194,7 +201,9 @@ private static void ImmediateUnpauseIfPaused() { /// Sets the time scale similarly to the method GameManager#SetTimeScale. /// /// The new time scale. - public static void SetTimeScale(float timeScale) { - TimeController.GenericTimeScale = timeScale > 0.00999999977648258 ? timeScale : 0.0f; + public void SetTimeScale(float timeScale) { + timeScale = timeScale > 0.00999999977648258 ? timeScale : 0.0f; + _onSetTimeScale(ref timeScale); + TimeController.GenericTimeScale = timeScale; } } From bd679558f969cfa18adae0adcb1ef078e63d6d62 Mon Sep 17 00:00:00 2001 From: dplochcoder Date: Mon, 11 Aug 2025 00:01:05 -0400 Subject: [PATCH 2/2] Make time scale hooks readonly --- HKMP/Api/Client/IClientManager.cs | 2 +- HKMP/Game/Client/ClientManager.cs | 2 +- HKMP/Game/Client/PauseManager.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/HKMP/Api/Client/IClientManager.cs b/HKMP/Api/Client/IClientManager.cs index 9d84272b..18805b51 100644 --- a/HKMP/Api/Client/IClientManager.cs +++ b/HKMP/Api/Client/IClientManager.cs @@ -94,7 +94,7 @@ public interface IClientManager { /// /// Event that is called when HKMP modifies the game's time scale. /// - delegate void SetTimeScale(ref float timeScale); + delegate void SetTimeScale(float timeScale); /// /// Event that is called when HKMP modifies the game's time scale. diff --git a/HKMP/Game/Client/ClientManager.cs b/HKMP/Game/Client/ClientManager.cs index cc048088..f5bdc98d 100644 --- a/HKMP/Game/Client/ClientManager.cs +++ b/HKMP/Game/Client/ClientManager.cs @@ -188,7 +188,7 @@ ModSettings modSettings _entityManager = new EntityManager(netClient); - _pauseManager = new PauseManager(netClient, (ref float timeScale) => OnSetTimeScale?.Invoke(ref timeScale)); + _pauseManager = new PauseManager(netClient, timeScale => OnSetTimeScale?.Invoke(timeScale)); _pauseManager.RegisterHooks(); new FsmPatcher().RegisterHooks(); diff --git a/HKMP/Game/Client/PauseManager.cs b/HKMP/Game/Client/PauseManager.cs index da50ddc1..cab60a9b 100644 --- a/HKMP/Game/Client/PauseManager.cs +++ b/HKMP/Game/Client/PauseManager.cs @@ -203,7 +203,7 @@ private static void ImmediateUnpauseIfPaused() { /// The new time scale. public void SetTimeScale(float timeScale) { timeScale = timeScale > 0.00999999977648258 ? timeScale : 0.0f; - _onSetTimeScale(ref timeScale); TimeController.GenericTimeScale = timeScale; + _onSetTimeScale(timeScale); } }