Files
ft/Client/Assets/Scripts/Activity/ActivityScopeManager.cs
2026-06-29 21:18:33 +08:00

128 lines
4.3 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using System.Linq;
using asap.core;
using UnityEngine;
namespace Activity
{
/// <summary>
/// 活动Scope生命周期管理器
/// 负责管理每个活动ID独立的DI容器Scope
/// </summary>
public class ActivityScopeManager : IDisposable
{
private readonly Dictionary<(int,int),Container.IScope> _activityScopes = new();
private readonly Dictionary<Type, (int EventType, int EventSubType)> _activeRegistry = new();
/// <summary>
/// 销毁所有Scope
/// </summary>
public void Dispose()
{
foreach (var scopesValue in _activityScopes.Values)
scopesValue?.Dispose();
_activityScopes.Clear();
_activeRegistry.Clear();
Debug.Log("[ActivityScopeManager] Disposed all activity scopes");
}
/// <summary>
/// 为指定活动ID创建Scope
/// </summary>
public Container.IScope CreateActivityScope((int type,int subtype) typeKey)
{
if (_activityScopes.TryGetValue(typeKey, out var activityScope))
{
Debug.LogWarning($"[ActivityScopeManager] Activity scope for activity {typeKey} already exists");
return activityScope;
}
var scope = GContext.container.CreateScope();
if (scope == null)
{
Debug.LogError($"[ActivityScopeManager] Failed to create scope for activity {typeKey}");
return null;
}
_activityScopes.Add(typeKey, scope);
return scope;
}
/// <summary>
/// 获取指定活动ID的Scope如果不存在则创建
/// </summary>
public Container.IScope GetActivityScope((int type,int subtype) typeKey)
{
if (_activityScopes.TryGetValue(typeKey, out var scope)) return scope;
scope = CreateActivityScope(typeKey);
return scope;
}
/// <summary>
/// 销毁指定活动ID的Scope
/// </summary>
public void DestroyActivityScope((int type,int subtype) typeKey)
{
if (!_activityScopes.TryGetValue(typeKey, out var scope)) return;
scope.Dispose();
_activityScopes.Remove(typeKey);
// 清理所有指向该 typeKey 的注册项,避免 Resolve<TM>() 取到已销毁 Scope 的 typeKey
var keysToRemove = _activeRegistry
.Where(x => x.Value == typeKey)
.Select(x => x.Key)
.ToList();
foreach (var key in keysToRemove)
_activeRegistry.Remove(key);
Debug.Log($"[ActivityScopeManager] Destroyed scope for activity {typeKey}, registry entries removed: {keysToRemove.Count}");
}
/// <summary>
/// 检查指定活动ID是否有活跃的Scope
/// </summary>
public bool HasActiveScope((int type,int subtype) typeKey)
{
return _activityScopes.ContainsKey(typeKey);
}
/// <summary>
/// 检查类型是否已注册
/// </summary>
public bool IsRegistered<TM>() where TM : AEventDataManager
{
return _activeRegistry.ContainsKey(typeof(TM));
}
/// <summary>
/// 获取或注册实例
/// </summary>
public TM GetOrRegisterInstance<TM>((int type, int subtype) typeKey) where TM : AEventDataManager
{
var scope = GetActivityScope(typeKey);
if (scope == null) return null;
// 如果未注册,先注册到 GContext.container
if (!_activeRegistry.ContainsKey(typeof(TM)))
{
GContext.container.Register<TM>(typeof(TM).Name).PerScope();
_activeRegistry.Add(typeof(TM), typeKey);
}
return scope.GetInstance((typeof(TM), typeof(TM).Name)) as TM;
}
/// <summary>
/// 根据 TM 类型获取已注册的 typeKey
/// </summary>
public (int EventType, int EventSubType)? GetRegisteredTypeKey<TM>() where TM : AEventDataManager
{
if (_activeRegistry.TryGetValue(typeof(TM), out var typeKey))
return typeKey;
return null;
}
}
}