com.alicizax.unity.framework/Runtime/UI/Constant/UIMetadata.cs

95 lines
2.8 KiB
C#
Raw Normal View History

2025-09-05 19:46:30 +08:00
using System;
using System.Runtime.CompilerServices;
using System.Threading;
2025-09-05 19:46:30 +08:00
using AlicizaX;
using Cysharp.Threading.Tasks;
namespace AlicizaX.UI.Runtime
{
internal sealed class UIMetadata
{
public UIBase View { get; private set; }
public readonly UIMetaRegistry.UIMetaInfo MetaInfo;
public readonly UIResRegistry.UIResInfo ResInfo;
public readonly Type UILogicType;
public bool InCache = false;
private CancellationTokenSource _cancellationTokenSource;
public CancellationToken CancellationToken => _cancellationTokenSource?.Token ?? CancellationToken.None;
2025-09-05 19:46:30 +08:00
public UIState State
{
get
{
if (View == null) return UIState.Uninitialized;
return View.State;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void CreateUI()
{
if (View is null)
{
2025-12-24 20:44:36 +08:00
if (!UIStateMachine.ValidateTransition(UILogicType.Name, UIState.Uninitialized, UIState.CreatedUI))
return;
2025-09-05 19:46:30 +08:00
View = (UIBase)InstanceFactory.CreateInstanceOptimized(UILogicType);
EnsureCancellationToken();
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void EnsureCancellationToken()
{
if (_cancellationTokenSource == null)
{
_cancellationTokenSource = new CancellationTokenSource();
2025-09-05 19:46:30 +08:00
}
}
public void CancelAsyncOperations()
{
_cancellationTokenSource?.Cancel();
_cancellationTokenSource?.Dispose();
_cancellationTokenSource = null;
}
2025-09-05 19:46:30 +08:00
public void Dispose()
{
DisposeAsync().Forget();
}
internal async UniTask DisposeAsync()
2025-09-05 19:46:30 +08:00
{
CancelAsyncOperations();
2025-09-05 19:46:30 +08:00
if (State != UIState.Uninitialized && State != UIState.Destroying)
{
await View.InternalDestroy();
View = null;
}
}
public UIMetadata(Type uiType)
{
if (uiType == null)
{
throw new ArgumentNullException(nameof(uiType));
}
2025-09-05 19:46:30 +08:00
UILogicType = uiType;
if (!UIMetaRegistry.TryGet(UILogicType.TypeHandle, out MetaInfo))
{
throw new InvalidOperationException($"[UI] Metadata not registered for {UILogicType.FullName}");
}
2025-09-05 19:46:30 +08:00
if (!UIResRegistry.TryGet(MetaInfo.HolderRuntimeTypeHandle, out ResInfo))
{
throw new InvalidOperationException($"[UI] Resource metadata not registered for holder of {UILogicType.FullName}");
}
2025-09-05 19:46:30 +08:00
}
}
}