Fix chart player dispose.

This commit is contained in:
2022-11-15 17:29:10 +08:00
parent 35ac57bfba
commit a8ab73ac65
2 changed files with 112 additions and 108 deletions

View File

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

View File

@@ -196,7 +196,7 @@ namespace Cryville.Crtr.Event {
public void Dispose() { public void Dispose() {
if (Disposed) return; if (Disposed) return;
Disposed = true; Disposed = true;
if (Handler != null) Handler.Dispose(); if (CloneType < 16 && Handler != null) Handler.Dispose();
foreach (var s in Children) foreach (var s in Children)
s.Value.Dispose(); s.Value.Dispose();
RMVPool.ReturnAll(); RMVPool.ReturnAll();