Commit 4fa42886 authored by ElderLich's avatar ElderLich

Bug Fix: Fix deck selector CancellationTokenSource disposed exception on tab switch

Updated SelectionButton_DeckSelector cancellation lifecycle to safely cancel/dispose refresh tokens, prevent reuse of disposed CTS, and handle expected async cancellation during rapid Online toggle/page disable flows.
parent c62b4d2b
...@@ -2,6 +2,7 @@ using Cysharp.Threading.Tasks; ...@@ -2,6 +2,7 @@ using Cysharp.Threading.Tasks;
using DG.Tweening; using DG.Tweening;
using MDPro3.Duel.YGOSharp; using MDPro3.Duel.YGOSharp;
using MDPro3.Utility; using MDPro3.Utility;
using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
...@@ -101,8 +102,7 @@ namespace MDPro3.UI ...@@ -101,8 +102,7 @@ namespace MDPro3.UI
protected override void OnDisable() protected override void OnDisable()
{ {
base.OnDisable(); base.OnDisable();
cts?.Cancel(); DisposeRefreshCts();
cts?.Dispose();
} }
public void SetConfigDeck(string hint) public void SetConfigDeck(string hint)
...@@ -147,9 +147,15 @@ namespace MDPro3.UI ...@@ -147,9 +147,15 @@ namespace MDPro3.UI
{ {
refreshed = false; refreshed = false;
cts = new(); DisposeRefreshCts();
await UniTask.WaitUntil(() => Items.initialized, cancellationToken : cts.Token); var refreshCts = new CancellationTokenSource();
await UniTask.WaitWhile(() => TextureManager.container == null, cancellationToken: cts.Token); cts = refreshCts;
var token = refreshCts.Token;
try
{
await UniTask.WaitUntil(() => Items.initialized, cancellationToken: token);
await UniTask.WaitWhile(() => TextureManager.container == null, cancellationToken: token);
ImageDeck.color = Color.clear; ImageDeck.color = Color.clear;
ImageCard0.color =Color.clear; ImageCard0.color =Color.clear;
...@@ -162,7 +168,7 @@ namespace MDPro3.UI ...@@ -162,7 +168,7 @@ namespace MDPro3.UI
if (card0 == 0) if (card0 == 0)
{ {
ImageCard0.texture = null; ImageCard0.texture = null;
ImageCard0.material = await ABLoader.LoadProtectorMaterial(protector.ToString(), cts.Token); ImageCard0.material = await ABLoader.LoadProtectorMaterial(protector.ToString(), token);
} }
else else
{ {
...@@ -174,7 +180,7 @@ namespace MDPro3.UI ...@@ -174,7 +180,7 @@ namespace MDPro3.UI
if (card1 == 0) if (card1 == 0)
{ {
ImageCard1.texture = null; ImageCard1.texture = null;
ImageCard1.material = await ABLoader.LoadProtectorMaterial(protector.ToString(), cts.Token); ImageCard1.material = await ABLoader.LoadProtectorMaterial(protector.ToString(), token);
} }
else else
{ {
...@@ -186,7 +192,7 @@ namespace MDPro3.UI ...@@ -186,7 +192,7 @@ namespace MDPro3.UI
if (card2 == 0) if (card2 == 0)
{ {
ImageCard2.texture = null; ImageCard2.texture = null;
ImageCard2.material = await ABLoader.LoadProtectorMaterial(protector.ToString(), cts.Token); ImageCard2.material = await ABLoader.LoadProtectorMaterial(protector.ToString(), token);
} }
else else
{ {
...@@ -197,6 +203,49 @@ namespace MDPro3.UI ...@@ -197,6 +203,49 @@ namespace MDPro3.UI
refreshed = true; refreshed = true;
} }
catch (OperationCanceledException)
{
}
finally
{
if (ReferenceEquals(cts, refreshCts))
{
cts = null;
try
{
refreshCts.Dispose();
}
catch (ObjectDisposedException)
{
}
}
}
}
private void DisposeRefreshCts()
{
var refreshCts = cts;
if (refreshCts == null)
return;
cts = null;
try
{
refreshCts.Cancel();
}
catch (ObjectDisposedException)
{
}
try
{
refreshCts.Dispose();
}
catch (ObjectDisposedException)
{
}
}
protected override void CallHoverOnEvent() protected override void CallHoverOnEvent()
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment