Fix Render State Cleanup on Initialization Failure

This commit is contained in:
copecone committed 2026-10-11 16:08:00 +09:00
1 parent 7eec3b4660
commit dee3b2dbcf
1 file changed
+25 -8
+25 -8
View File
@@ -145,7 +145,7 @@ internal class RenderManager : MonoBehaviour
public int frameCountBase; public int frameCountBase;
public OfflineAudioMixer? AudioMixer { get; private set; } public OfflineAudioMixer? AudioMixer { get; private set; }
private AudioSyncCalculator SyncCalculator { get; set; } = null!; private AudioSyncCalculator? SyncCalculator { get; set; }
private FrameReader? _frameReader; private FrameReader? _frameReader;
private FrameHookPipeline? _frameHookPipeline; private FrameHookPipeline? _frameHookPipeline;
@@ -498,7 +498,10 @@ internal class RenderManager : MonoBehaviour
{ {
yield return new WaitForSecondsRealtime(1); yield return new WaitForSecondsRealtime(1);
} }
// Cancelled or failed during the countdown: do not start the encoder
if (stage != RenderStage.OnScene) yield break;
frameCount = 0; framesSent = 0; frameCount = 0; framesSent = 0;
_renderStopwatch.Restart(); _renderStopwatch.Restart();
@@ -521,13 +524,20 @@ internal class RenderManager : MonoBehaviour
catch (Exception ex) catch (Exception ex)
{ {
Melon<RenderMod>.Logger.Error($"Encoder initialization error: {ex}"); Melon<RenderMod>.Logger.Error($"Encoder initialization error: {ex}");
ResetFrameHookState();
// Release the encoder/thread and restore time, input offset, stage and hooks before showing the dialog
MarkFailed($"failed to initialize the video encoder: {ex.Message}");
ResetRenderState(success: false);
StopFFmpeg(false);
RestoreRenderEnvironment();
CommonWarningDialogUI.ShowDialog( CommonWarningDialogUI.ShowDialog(
"Encoder Error", "Encoder Error",
$"Failed to initialize video encoder.\nError: {ex.Message}", $"Failed to initialize video encoder.\nError: {ex.Message}",
new DialogButton("OK", () => new DialogButton("OK", () =>
{ {
ADOBase.loader.LoadSceneWithTransition(WipeDirection.StartsFromRight, "scnEditor"); ADOBase.loader.LoadSceneWithTransition(WipeDirection.StartsFromRight, "scnEditor");
Cursor.visible = true;
}) })
); );
yield break; yield break;
@@ -691,14 +701,14 @@ internal class RenderManager : MonoBehaviour
} }
} }
if (Configuration.AudioConfig.RenderAudio) if (Configuration.AudioConfig.RenderAudio && SyncCalculator is { } syncCalculator)
{ {
var frameIdx = (int)(passCount - 1); var frameIdx = (int)(passCount - 1);
var effectiveVideoFps = Math.Min(Configuration.VideoFPS, Configuration.RenderFPS); var effectiveVideoFps = Math.Min(Configuration.VideoFPS, Configuration.RenderFPS);
var frameTime = frameIdx / effectiveVideoFps; var frameTime = frameIdx / effectiveVideoFps;
var channels = Configuration.AudioConfig.AudioChannels; var channels = Configuration.AudioConfig.AudioChannels;
var samplesNeeded = SyncCalculator.GetNextFrameSamples(); var samplesNeeded = syncCalculator.GetNextFrameSamples();
var audioBuffer = ArrayPool<float>.Shared.Rent(samplesNeeded * channels); var audioBuffer = ArrayPool<float>.Shared.Rent(samplesNeeded * channels);
try try
@@ -888,6 +898,15 @@ internal class RenderManager : MonoBehaviour
} }
private void EndRender() private void EndRender()
{
RestoreRenderEnvironment();
ADOBase.loader.LoadSceneWithTransition(WipeDirection.StartsFromRight, "scnEditor");
Cursor.visible = true;
}
// Undoes what the render setup changed (time, input offset, auto, queues, session hooks), without leaving the scene.
private void RestoreRenderEnvironment()
{ {
Time.timeScale = 1f; Time.timeScale = 1f;
Time.captureDeltaTime = 0f; Time.captureDeltaTime = 0f;
@@ -922,13 +941,11 @@ internal class RenderManager : MonoBehaviour
_frameReader?.Dispose(); _frameReader?.Dispose();
ResetFrameHookState(); ResetFrameHookState();
AudioMixer?.Reset(); AudioMixer = null; AudioMixer?.Reset(); AudioMixer = null;
SyncCalculator.Reset(); SyncCalculator = null;
AudioSpectrumGenerator.Clear(); AudioSpectrumGenerator.Clear();
RenderContext.Reset(); RenderContext.Reset();
ADOBase.loader.LoadSceneWithTransition(WipeDirection.StartsFromRight, "scnEditor");
Cursor.visible = true;
} }
private RenderResult BuildResult(bool success, string? errorMessage = null) private RenderResult BuildResult(bool success, string? errorMessage = null)