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;
using DG.Tweening;
using MDPro3.Duel.YGOSharp;
using MDPro3.Utility;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
......@@ -101,8 +102,7 @@ namespace MDPro3.UI
protected override void OnDisable()
{
base.OnDisable();
cts?.Cancel();
cts?.Dispose();
DisposeRefreshCts();
}
public void SetConfigDeck(string hint)
......@@ -147,9 +147,15 @@ namespace MDPro3.UI
{
refreshed = false;
cts = new();
await UniTask.WaitUntil(() => Items.initialized, cancellationToken : cts.Token);
await UniTask.WaitWhile(() => TextureManager.container == null, cancellationToken: cts.Token);
DisposeRefreshCts();
var refreshCts = new CancellationTokenSource();
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;
ImageCard0.color =Color.clear;
......@@ -162,7 +168,7 @@ namespace MDPro3.UI
if (card0 == 0)
{
ImageCard0.texture = null;
ImageCard0.material = await ABLoader.LoadProtectorMaterial(protector.ToString(), cts.Token);
ImageCard0.material = await ABLoader.LoadProtectorMaterial(protector.ToString(), token);
}
else
{
......@@ -174,7 +180,7 @@ namespace MDPro3.UI
if (card1 == 0)
{
ImageCard1.texture = null;
ImageCard1.material = await ABLoader.LoadProtectorMaterial(protector.ToString(), cts.Token);
ImageCard1.material = await ABLoader.LoadProtectorMaterial(protector.ToString(), token);
}
else
{
......@@ -186,7 +192,7 @@ namespace MDPro3.UI
if (card2 == 0)
{
ImageCard2.texture = null;
ImageCard2.material = await ABLoader.LoadProtectorMaterial(protector.ToString(), cts.Token);
ImageCard2.material = await ABLoader.LoadProtectorMaterial(protector.ToString(), token);
}
else
{
......@@ -197,6 +203,49 @@ namespace MDPro3.UI
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()
{
......
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