From 2719c69753f16ee72b8178328726d578bcd0b5bf Mon Sep 17 00:00:00 2001 From: originalnicodr Date: Sun, 23 Feb 2025 20:27:50 -0300 Subject: [PATCH] Added new safeguards when adding and removing event handlers to the rendering pipeline. Also changed the "reset camera on new freecam sessions" option default value to true, as its more intuitive. --- src/Config/ConfigManager.cs | 2 +- src/UI/Panels/FreeCamPanel.cs | 135 +++++++++++++++++++--------------- 2 files changed, 76 insertions(+), 61 deletions(-) diff --git a/src/Config/ConfigManager.cs b/src/Config/ConfigManager.cs index c95b363..b9605e5 100644 --- a/src/Config/ConfigManager.cs +++ b/src/Config/ConfigManager.cs @@ -191,7 +191,7 @@ namespace UnityExplorer.Config Reset_Camera_Transform = new("Reset Camera transform on freecam disable", "Reset the camera position and rotation between freecam sessions, so the freecam always starts from the gameplay position and rotation.", - false); + true); Arrow_Size = new("Visualizers arrows size", "Cam Paths nodes and Lights Manager lights visualizers' arrow size (must be positive) (needs visualizer toggled to reflect changes).", diff --git a/src/UI/Panels/FreeCamPanel.cs b/src/UI/Panels/FreeCamPanel.cs index b91b216..65349bc 100644 --- a/src/UI/Panels/FreeCamPanel.cs +++ b/src/UI/Panels/FreeCamPanel.cs @@ -1068,40 +1068,48 @@ namespace UnityExplorer.UI.Panels ExplorerCore.LogWarning($"Failed to listen to BeforeRender: {exception}"); } #endif - // These doesn't exist for Unity <2017 nor when using HDRP - Type renderPipelineManagerType = ReflectionUtility.GetTypeByName("RenderPipelineManager"); - if (renderPipelineManagerType != null){ - EventInfo beginFrameRenderingEvent = renderPipelineManagerType.GetEvent("beginFrameRendering"); - if (beginFrameRenderingEvent != null) { - beginFrameRenderingEvent.AddEventHandler(null, OnBeforeEvent); - } - EventInfo endFrameRenderingEvent = renderPipelineManagerType.GetEvent("endFrameRendering"); - if (endFrameRenderingEvent != null) { - endFrameRenderingEvent.AddEventHandler(null, OnAfterEvent); + + try + { + // These doesn't exist for Unity <2017 nor when using HDRP + Type renderPipelineManagerType = ReflectionUtility.GetTypeByName("RenderPipelineManager"); + if (renderPipelineManagerType != null){ + EventInfo beginFrameRenderingEvent = renderPipelineManagerType.GetEvent("beginFrameRendering"); + if (beginFrameRenderingEvent != null) { + beginFrameRenderingEvent.AddEventHandler(null, OnBeforeEvent); + } + EventInfo endFrameRenderingEvent = renderPipelineManagerType.GetEvent("endFrameRendering"); + if (endFrameRenderingEvent != null) { + endFrameRenderingEvent.AddEventHandler(null, OnAfterEvent); + } + + EventInfo beginCameraRenderingEvent = renderPipelineManagerType.GetEvent("beginCameraRendering"); + if (beginCameraRenderingEvent != null) { + beginCameraRenderingEvent.AddEventHandler(null, OnBeforeEvent); + } + EventInfo endCameraRenderingEvent = renderPipelineManagerType.GetEvent("endCameraRendering"); + if (endCameraRenderingEvent != null) { + endCameraRenderingEvent.AddEventHandler(null, OnAfterEvent); + } + + EventInfo beginContextRenderingEvent = renderPipelineManagerType.GetEvent("beginContextRendering"); + if (beginContextRenderingEvent != null) { + beginContextRenderingEvent.AddEventHandler(null, OnBeforeEvent); + } + EventInfo endContextRenderingEvent = renderPipelineManagerType.GetEvent("endContextRendering"); + if (endContextRenderingEvent != null) { + endContextRenderingEvent.AddEventHandler(null, OnAfterEvent); + } } - EventInfo beginCameraRenderingEvent = renderPipelineManagerType.GetEvent("beginCameraRendering"); - if (beginCameraRenderingEvent != null) { - beginCameraRenderingEvent.AddEventHandler(null, OnBeforeEvent); - } - EventInfo endCameraRenderingEvent = renderPipelineManagerType.GetEvent("endCameraRendering"); - if (endCameraRenderingEvent != null) { - endCameraRenderingEvent.AddEventHandler(null, OnAfterEvent); - } - - EventInfo beginContextRenderingEvent = renderPipelineManagerType.GetEvent("beginContextRendering"); - if (beginContextRenderingEvent != null) { - beginContextRenderingEvent.AddEventHandler(null, OnBeforeEvent); - } - EventInfo endContextRenderingEvent = renderPipelineManagerType.GetEvent("endContextRendering"); - if (endContextRenderingEvent != null) { - endContextRenderingEvent.AddEventHandler(null, OnAfterEvent); + EventInfo onBeforeRenderEvent = typeof(Application).GetEvent("onBeforeRender"); + if (onBeforeRenderEvent != null) { + onBeforeRenderEvent.AddEventHandler(null, onBeforeRenderAction); } } - - EventInfo onBeforeRenderEvent = typeof(Application).GetEvent("onBeforeRender"); - if (onBeforeRenderEvent != null) { - onBeforeRenderEvent.AddEventHandler(null, onBeforeRenderAction); + catch (Exception exception) + { + ExplorerCore.LogWarning($"Failed to add event handler to rendering pipeline: {exception}"); } } @@ -1117,41 +1125,48 @@ namespace UnityExplorer.UI.Panels ExplorerCore.LogWarning($"Failed to unlisten from BeforeRender: {exception}"); } #endif - // These doesn't exist for Unity <2017 nor when using HDRP - Type renderPipelineManagerType = ReflectionUtility.GetTypeByName("RenderPipelineManager"); - if (renderPipelineManagerType != null){ - EventInfo beginFrameRenderingEvent = renderPipelineManagerType.GetEvent("beginFrameRendering"); - if (beginFrameRenderingEvent != null) { - beginFrameRenderingEvent.RemoveEventHandler(null, OnBeforeEvent); - } - EventInfo endFrameRenderingEvent = renderPipelineManagerType.GetEvent("endFrameRendering"); - if (endFrameRenderingEvent != null) { - endFrameRenderingEvent.RemoveEventHandler(null, OnAfterEvent); + try + { + // These doesn't exist for Unity <2017 nor when using HDRP + Type renderPipelineManagerType = ReflectionUtility.GetTypeByName("RenderPipelineManager"); + if (renderPipelineManagerType != null){ + EventInfo beginFrameRenderingEvent = renderPipelineManagerType.GetEvent("beginFrameRendering"); + if (beginFrameRenderingEvent != null) { + beginFrameRenderingEvent.RemoveEventHandler(null, OnBeforeEvent); + } + EventInfo endFrameRenderingEvent = renderPipelineManagerType.GetEvent("endFrameRendering"); + if (endFrameRenderingEvent != null) { + endFrameRenderingEvent.RemoveEventHandler(null, OnAfterEvent); + } + + EventInfo beginCameraRenderingEvent = renderPipelineManagerType.GetEvent("beginCameraRendering"); + if (beginCameraRenderingEvent != null) { + beginCameraRenderingEvent.RemoveEventHandler(null, OnBeforeEvent); + } + EventInfo endCameraRenderingEvent = renderPipelineManagerType.GetEvent("endCameraRendering"); + if (endCameraRenderingEvent != null) { + endCameraRenderingEvent.RemoveEventHandler(null, OnAfterEvent); + } + + EventInfo beginContextRenderingEvent = renderPipelineManagerType.GetEvent("beginContextRendering"); + if (beginContextRenderingEvent != null) { + beginContextRenderingEvent.RemoveEventHandler(null, OnBeforeEvent); + } + EventInfo endContextRenderingEvent = renderPipelineManagerType.GetEvent("endContextRendering"); + if (endContextRenderingEvent != null) { + endContextRenderingEvent.RemoveEventHandler(null, OnAfterEvent); + } } - EventInfo beginCameraRenderingEvent = renderPipelineManagerType.GetEvent("beginCameraRendering"); - if (beginCameraRenderingEvent != null) { - beginCameraRenderingEvent.RemoveEventHandler(null, OnBeforeEvent); - } - EventInfo endCameraRenderingEvent = renderPipelineManagerType.GetEvent("endCameraRendering"); - if (endCameraRenderingEvent != null) { - endCameraRenderingEvent.RemoveEventHandler(null, OnAfterEvent); - } - - EventInfo beginContextRenderingEvent = renderPipelineManagerType.GetEvent("beginContextRendering"); - if (beginContextRenderingEvent != null) { - beginContextRenderingEvent.RemoveEventHandler(null, OnBeforeEvent); - } - EventInfo endContextRenderingEvent = renderPipelineManagerType.GetEvent("endContextRendering"); - if (endContextRenderingEvent != null) { - endContextRenderingEvent.RemoveEventHandler(null, OnAfterEvent); + EventInfo onBeforeRenderEvent = typeof(Application).GetEvent("onBeforeRender"); + if (onBeforeRenderEvent != null) { + onBeforeRenderEvent.RemoveEventHandler(null, onBeforeRenderAction); } } - - EventInfo onBeforeRenderEvent = typeof(Application).GetEvent("onBeforeRender"); - if (onBeforeRenderEvent != null) { - onBeforeRenderEvent.RemoveEventHandler(null, onBeforeRenderAction); + catch (Exception exception) + { + ExplorerCore.LogWarning($"Failed to remove event handler from rendering pipeline: {exception}"); } }