diff --git a/Source/DynamicOpenVR.BeatSaber/Plugin.cs b/Source/DynamicOpenVR.BeatSaber/Plugin.cs index 1d1f379..ccd1599 100644 --- a/Source/DynamicOpenVR.BeatSaber/Plugin.cs +++ b/Source/DynamicOpenVR.BeatSaber/Plugin.cs @@ -45,7 +45,9 @@ public class Plugin : IBeatSaberPlugin private HarmonyInstance _harmonyInstance; - private readonly HashSet pauseEvents = new HashSet(new [] { EVREventType.VREvent_InputFocusCaptured, EVREventType.VREvent_DashboardActivated, EVREventType.VREvent_OverlayShown }); + private readonly HashSet _pauseEvents = new HashSet(new [] { EVREventType.VREvent_InputFocusCaptured, EVREventType.VREvent_DashboardActivated, EVREventType.VREvent_OverlayShown }); + + private bool _initialized; public void Init(Logger logger) { @@ -68,6 +70,8 @@ public void OnApplicationStart() AddManifestToSteamConfig(); RegisterActionSet(); ApplyHarmonyPatches(); + + _initialized = true; } catch (Exception ex) { @@ -79,13 +83,13 @@ public void OnApplicationStart() public void OnApplicationQuit() { // not really necessary here, just following good practices - leftTriggerValue.Dispose(); - rightTriggerValue.Dispose(); - menu.Dispose(); - leftSlice.Dispose(); - rightSlice.Dispose(); - leftHandPose.Dispose(); - rightHandPose.Dispose(); + leftTriggerValue?.Dispose(); + rightTriggerValue?.Dispose(); + menu?.Dispose(); + leftSlice?.Dispose(); + rightSlice?.Dispose(); + leftHandPose?.Dispose(); + rightHandPose?.Dispose(); } private void AddManifestToSteamConfig() @@ -248,10 +252,13 @@ public void OnSceneUnloaded(Scene scene) { } public void OnUpdate() { - VREvent_t evt = default; - if (OpenVR.System.PollNextEvent(ref evt, (uint)Marshal.SizeOf(typeof(VREvent_t))) && pauseEvents.Contains((EVREventType) evt.eventType)) + if (_initialized) { - Resources.FindObjectsOfTypeAll().FirstOrDefault()?.Pause(); + VREvent_t evt = default; + if (OpenVR.System.PollNextEvent(ref evt, (uint)Marshal.SizeOf(typeof(VREvent_t))) && _pauseEvents.Contains((EVREventType) evt.eventType)) + { + Resources.FindObjectsOfTypeAll().FirstOrDefault()?.Pause(); + } } } }