From a8ab73ac65b950adff2a4f33896bd7d7e1be973c Mon Sep 17 00:00:00 2001 From: PopSlime Date: Tue, 15 Nov 2022 17:29:10 +0800 Subject: [PATCH] Fix chart player dispose. --- Assets/Cryville/Crtr/ChartPlayer.cs | 218 ++++++++++--------- Assets/Cryville/Crtr/Event/ContainerState.cs | 2 +- 2 files changed, 112 insertions(+), 108 deletions(-) diff --git a/Assets/Cryville/Crtr/ChartPlayer.cs b/Assets/Cryville/Crtr/ChartPlayer.cs index f250f69..1dc1d89 100644 --- a/Assets/Cryville/Crtr/ChartPlayer.cs +++ b/Assets/Cryville/Crtr/ChartPlayer.cs @@ -68,15 +68,6 @@ namespace Cryville.Crtr { InputProxy inputProxy; - ~ChartPlayer() { - Dispose(); - } - - public void Dispose() { - if (loadThread != null) loadThread.Abort(); - if (texLoader != null) texLoader.Dispose(); - } - #region MonoBehaviour void Start() { var logobj = GameObject.Find("Logs"); @@ -99,6 +90,19 @@ namespace Cryville.Crtr { // Camera.main.RenderToCubemap(); } + void OnDestroy() { + if (cbus != null) cbus.Dispose(); + if (bbus != null) bbus.Dispose(); + if (tbus != null) tbus.Dispose(); + if (nbus != null) nbus.Dispose(); + if (loadThread != null) loadThread.Abort(); + if (texLoader != null) texLoader.Dispose(); + if (inputProxy != null) inputProxy.Dispose(); + if (texs != null) foreach (var t in texs) Texture.Destroy(t.Value); + Camera.onPostRender -= OnCameraPostRender; + GC.Collect(); + } + bool texloaddone; diag::Stopwatch texloadtimer = new diag::Stopwatch(); bool firstFrame; @@ -110,58 +114,58 @@ namespace Cryville.Crtr { else Game.MainLogger.Enumerate((level, module, msg) => { }); } void GameUpdate() { - try { - if (Screen.width != screenSize.x || Screen.height != screenSize.y) - throw new InvalidOperationException("Window resized while playing"); - float dt = firstFrame + try { + if (Screen.width != screenSize.x || Screen.height != screenSize.y) + throw new InvalidOperationException("Window resized while playing"); + float dt = firstFrame ? 1f / Application.targetFrameRate : Time.deltaTime; - firstFrame = false; - inputProxy.ForceTick(); - cbus.ForwardByTime(dt); - bbus.ForwardByTime(dt); - UnityEngine.Profiling.Profiler.BeginSample("ChartPlayer.Forward"); - UnityEngine.Profiling.Profiler.BeginSample("EventBus.Copy"); - bbus.CopyTo(2, tbus); - bbus.CopyTo(3, nbus); - UnityEngine.Profiling.Profiler.EndSample(); - float step = autoRenderStep ? ( firstFrame + firstFrame = false; + inputProxy.ForceTick(); + cbus.ForwardByTime(dt); + bbus.ForwardByTime(dt); + UnityEngine.Profiling.Profiler.BeginSample("ChartPlayer.Forward"); + UnityEngine.Profiling.Profiler.BeginSample("EventBus.Copy"); + bbus.CopyTo(2, tbus); + bbus.CopyTo(3, nbus); + UnityEngine.Profiling.Profiler.EndSample(); + float step = autoRenderStep ? ( firstFrame ? 1f / Application.targetFrameRate : Time.smoothDeltaTime ) : renderStep; - actualRenderStep = step; + actualRenderStep = step; - nbus.ForwardStepByTime(clippingDist, step); - nbus.BroadcastEndUpdate(); - nbus.Anchor(); + nbus.ForwardStepByTime(clippingDist, step); + nbus.BroadcastEndUpdate(); + nbus.Anchor(); - tbus.ForwardStepByTime(clippingDist, step); - tbus.ForwardStepByTime(renderDist, step); - tbus.BroadcastEndUpdate(); - UnityEngine.Profiling.Profiler.EndSample(); - } - catch (Exception ex) { - Game.LogException("Game", "An error occured while playing", ex); - Popup.CreateException(ex); - Stop(); - } + tbus.ForwardStepByTime(clippingDist, step); + tbus.ForwardStepByTime(renderDist, step); + tbus.BroadcastEndUpdate(); + UnityEngine.Profiling.Profiler.EndSample(); } + catch (Exception ex) { + Game.LogException("Game", "An error occured while playing", ex); + Popup.CreateException(ex); + Stop(); + } + } void LoadUpdate() { - if (texLoader != null) { - string url = texLoader.url; - string name = StringUtils.TrimExt(url.Substring(url.LastIndexOfAny(new char[] {'/', '\\'}) + 1)); + if (texLoader != null) { + string url = texLoader.url; + string name = StringUtils.TrimExt(url.Substring(url.LastIndexOfAny(new char[] {'/', '\\'}) + 1)); #if UNITY_5_4_OR_NEWER - if (texHandler.isDone) { - var tex = texHandler.texture; - texs.Add(name, tex); - Logger.Log("main", 0, "Load/MainThread", "Loaded texture {0} ({1} bytes)", name, texLoader.downloadedBytes); - texLoader.Dispose(); - texHandler.Dispose(); - texLoader = null; - } - else if (texLoader.downloadProgress != 0) { - Logger.Log("main", 0, "Load/MainThread", "Loading texture {0} {1:P0}", name, texLoader.downloadProgress); - } + if (texHandler.isDone) { + var tex = texHandler.texture; + texs.Add(name, tex); + Logger.Log("main", 0, "Load/MainThread", "Loaded texture {0} ({1} bytes)", name, texLoader.downloadedBytes); + texLoader.Dispose(); + texHandler.Dispose(); + texLoader = null; + } + else if (texLoader.downloadProgress != 0) { + Logger.Log("main", 0, "Load/MainThread", "Loading texture {0} {1:P0}", name, texLoader.downloadProgress); + } #else if (texLoader.isDone) { var tex = texLoader.texture; @@ -174,58 +178,58 @@ namespace Cryville.Crtr { Logger.Log("main", 0, "Load/MainThread", "Loading texture {0} {1:P0}", name, texLoader.progress); } #endif - } - if (texLoader == null) - if (texLoadQueue.Count > 0) { + } + if (texLoader == null) + if (texLoadQueue.Count > 0) { #if UNITY_5_4_OR_NEWER - texHandler = new DownloadHandlerTexture(); - texLoader = new UnityWebRequest(Game.FileProtocolPrefix + texLoadQueue.Dequeue(), "GET", texHandler, null); - texLoader.SendWebRequest(); + texHandler = new DownloadHandlerTexture(); + texLoader = new UnityWebRequest(Game.FileProtocolPrefix + texLoadQueue.Dequeue(), "GET", texHandler, null); + texLoader.SendWebRequest(); #else texLoader = new WWW(Game.FileProtocolPrefix + texLoadQueue.Dequeue()); #endif - } - else if (!texloaddone) { - texloaddone = true; - texloadtimer.Stop(); - Logger.Log("main", 1, "Load/MainThread", "Main thread done ({0}ms)", texloadtimer.Elapsed.TotalMilliseconds); - } - if (!loadThread.IsAlive) { + } + else if (!texloaddone) { + texloaddone = true; + texloadtimer.Stop(); + Logger.Log("main", 1, "Load/MainThread", "Main thread done ({0}ms)", texloadtimer.Elapsed.TotalMilliseconds); + } + if (!loadThread.IsAlive) { if (threadException != null) { - Logger.Log("main", 4, "Load/MainThread", "Load failed"); - loadThread = null; + Logger.Log("main", 4, "Load/MainThread", "Load failed"); + loadThread = null; Popup.CreateException(threadException); #if BUILD - ReturnToMenu(); + ReturnToMenu(); #endif - } - else if (texLoader == null) { - Prehandle(); - loadThread = null; - } + } + else if (texLoader == null) { + Prehandle(); + loadThread = null; } } + } string timetext = string.Empty; void LogUpdate() { - string _logs = logs.text; - Game.MainLogger.Enumerate((level, module, msg) => { - string color; - switch (level) { - case 0: color = "#888888"; break; - case 1: color = "#bbbbbb"; break; - case 2: color = "#0088ff"; break; - case 3: color = "#ffff00"; break; - case 4: color = "#ff0000"; break; - case 5: color = "#bb0000"; break; - default: color = "#ff00ff"; break; - } - _logs += string.Format( - "\r\n<{2}> {3}", - DateTime.UtcNow.ToString("s"), color, module, msg - ); - }); - logs.text = _logs.Substring(Mathf.Max(0, _logs.IndexOf('\n', Mathf.Max(0, _logs.Length - 4096)))); - var sttext = string.Format( + string _logs = logs.text; + Game.MainLogger.Enumerate((level, module, msg) => { + string color; + switch (level) { + case 0: color = "#888888"; break; + case 1: color = "#bbbbbb"; break; + case 2: color = "#0088ff"; break; + case 3: color = "#ffff00"; break; + case 4: color = "#ff0000"; break; + case 5: color = "#bb0000"; break; + default: color = "#ff00ff"; break; + } + _logs += string.Format( + "\r\n<{2}> {3}", + DateTime.UtcNow.ToString("s"), color, module, msg + ); + }); + logs.text = _logs.Substring(Mathf.Max(0, _logs.IndexOf('\n', Mathf.Max(0, _logs.Length - 4096)))); + var sttext = string.Format( "FPS: i{0:0} / s{1:0}\nSMem: {2:N0} / {3:N0}\nIMem: {4:N0} / {5:N0}", 1 / Time.deltaTime, 1 / Time.smoothDeltaTime, @@ -247,13 +251,13 @@ namespace Cryville.Crtr { } void OnCameraPostRender(Camera cam) { if (started) timetext = string.Format( - "\nSTime: {0:R}\nATime: {1:R}\nITime: {2:R}", - cbus.Time, - Game.AudioClient.Position - atime0, - inputProxy.GetTimestampAverage() - ); + "\nSTime: {0:R}\nATime: {1:R}\nITime: {2:R}", + cbus.Time, + Game.AudioClient.Position - atime0, + inputProxy.GetTimestampAverage() + ); else timetext = string.Empty; - } + } #endregion #region Triggers @@ -394,15 +398,11 @@ namespace Cryville.Crtr { public void Stop() { try { Logger.Log("main", 1, "Game", "Stopping"); - chart = null; Game.AudioSession = Game.AudioSequencer.NewSession(); - if (cbus != null) cbus.Dispose(); - if (bbus != null) bbus.Dispose(); - if (tbus != null) tbus.Dispose(); - if (nbus != null) nbus.Dispose(); - inputProxy.Deactivate(); - foreach (var t in texs) Texture.Destroy(t.Value); - Camera.onPostRender -= OnCameraPostRender; + if (cbus != null) { cbus.Dispose(); cbus = null; } + if (bbus != null) { bbus.Dispose(); bbus = null; } + if (tbus != null) { tbus.Dispose(); tbus = null; } + if (nbus != null) { nbus.Dispose(); nbus = null; } Logger.Log("main", 1, "Game", "Stopped"); } catch (Exception ex) { @@ -511,11 +511,15 @@ namespace Cryville.Crtr { } } Logger.Log("main", 0, "Load/WorkerThread", "Prehandling (iteration 1)"); - cbus.Clone(16).Forward(); + using (var pbus = cbus.Clone(16)) { + pbus.Forward(); + } Logger.Log("main", 0, "Load/WorkerThread", "Patching events"); cbus.DoPatch(); Logger.Log("main", 0, "Load/WorkerThread", "Prehandling (iteration 2)"); - cbus.Clone(17).Forward(); + using (var pbus = cbus.Clone(17)) { + pbus.Forward(); + } Logger.Log("main", 0, "Load/WorkerThread", "Cloning states (type 1)"); bbus = cbus.Clone(1, -clippingDist); diff --git a/Assets/Cryville/Crtr/Event/ContainerState.cs b/Assets/Cryville/Crtr/Event/ContainerState.cs index 802cb7c..7ea9fd5 100644 --- a/Assets/Cryville/Crtr/Event/ContainerState.cs +++ b/Assets/Cryville/Crtr/Event/ContainerState.cs @@ -196,7 +196,7 @@ namespace Cryville.Crtr.Event { public void Dispose() { if (Disposed) return; Disposed = true; - if (Handler != null) Handler.Dispose(); + if (CloneType < 16 && Handler != null) Handler.Dispose(); foreach (var s in Children) s.Value.Dispose(); RMVPool.ReturnAll();