mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 19:03:29 +08:00
- 引入基于键的资源工厂注册机制,替换原有的类型唯一注册方式 - 更新资源工厂接口,新增 GetFactory 方法支持键名参数 - 修改内部注册表结构,使用 (Type, string) 元组作为工厂字典键 - 在注册场景和资源时传入键名,确保资源可按名称区分 - 增强资源工厂条目信息,添加 ResourceType 和 Key 属性 - 完善工厂解析逻辑,通过类型和键双重条件匹配目标工厂 - 添加对空键值的校验,防止无效键导致运行时错误 - 优化预加载流程,仅执行明确标记为预加载的资源工厂
110 lines
4.0 KiB
C#
110 lines
4.0 KiB
C#
using GFramework.Core.events;
|
||
using GFramework.Core.extensions;
|
||
using GFramework.Core.system;
|
||
using Godot;
|
||
|
||
namespace GFramework.Core.Godot.system;
|
||
|
||
|
||
/// <summary>
|
||
/// 资源工厂系统抽象基类,用于统一管理各类资源的创建与预加载逻辑。
|
||
/// 提供注册场景和资源的方法,并通过依赖的资源加载系统和资产目录系统完成实际资源的获取与构造。
|
||
/// </summary>
|
||
public abstract class AbstractResourceFactorySystem : AbstractSystem, IResourceFactorySystem
|
||
{
|
||
private ResourceFactory.Registry? _registry;
|
||
private IResourceLoadSystem? _resourceLoadSystem;
|
||
private IAssetCatalogSystem? _assetCatalogSystem;
|
||
|
||
/// <summary>
|
||
/// 系统初始化方法,在系统启动时执行一次。
|
||
/// 初始化资源注册表,并获取依赖的资源加载系统和资产目录系统。
|
||
/// 最后执行所有已注册资源的预加载操作。
|
||
/// </summary>
|
||
protected override void OnInit()
|
||
{
|
||
_registry = new ResourceFactory.Registry();
|
||
_resourceLoadSystem = this.GetSystem<IResourceLoadSystem>();
|
||
_assetCatalogSystem = this.GetSystem<IAssetCatalogSystem>();
|
||
// 监听架构初始化事件
|
||
this.RegisterEvent<ArchitectureEvents.ArchitectureInitializedEvent>(_ =>
|
||
{
|
||
// 注册资源
|
||
RegisterResources();
|
||
// 预加载所有资源
|
||
_registry.PreloadAll();
|
||
});
|
||
}
|
||
/// <summary>
|
||
/// 注册系统所需的各种资源类型。由子类实现具体注册逻辑。
|
||
/// </summary>
|
||
protected abstract void RegisterResources();
|
||
|
||
|
||
/// <summary>
|
||
/// 根据指定的键获取资源工厂函数。
|
||
/// </summary>
|
||
/// <typeparam name="T">资源类型</typeparam>
|
||
/// <param name="key">资源键</param>
|
||
/// <returns>返回创建指定类型资源的工厂函数</returns>
|
||
public Func<T> GetFactory<T>(string key)=>_registry!.ResolveFactory<T>(key);
|
||
|
||
/// <summary>
|
||
/// 根据资产目录映射信息获取资源工厂函数。
|
||
/// </summary>
|
||
/// <typeparam name="T">资源类型</typeparam>
|
||
/// <param name="mapping">资产目录映射信息</param>
|
||
/// <returns>返回创建指定类型资源的工厂函数</returns>
|
||
public Func<T> GetFactory<T>(AssetCatalog.AssetCatalogMapping mapping) => _registry!.ResolveFactory<T>(mapping.Key);
|
||
|
||
|
||
|
||
#region Register Helpers(声明式)
|
||
|
||
/// <summary>
|
||
/// 注册场景资源工厂。
|
||
/// 根据场景键名获取场景ID,并将场景加载工厂注册到注册表中。
|
||
/// </summary>
|
||
/// <typeparam name="T">场景节点类型,必须继承自Node</typeparam>
|
||
/// <param name="sceneKey">场景在资产目录中的键名</param>
|
||
/// <param name="preload">是否需要预加载该场景资源</param>
|
||
protected void RegisterScene<T>(
|
||
string sceneKey,
|
||
bool preload = false)
|
||
where T : Node
|
||
{
|
||
var id = _assetCatalogSystem!.GetScene(sceneKey);
|
||
|
||
_registry!.Register(
|
||
sceneKey,
|
||
_resourceLoadSystem!.GetOrRegisterSceneFactory<T>(id),
|
||
preload
|
||
);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 注册普通资源工厂。
|
||
/// 根据资源键名获取资源ID,并将资源加载工厂注册到注册表中。
|
||
/// </summary>
|
||
/// <typeparam name="T">资源类型,必须继承自Resource</typeparam>
|
||
/// <param name="resourceKey">资源在资产目录中的键名</param>
|
||
/// <param name="duplicate">是否需要复制资源实例</param>
|
||
/// <param name="preload">是否需要预加载该资源</param>
|
||
protected void RegisterResource<T>(
|
||
string resourceKey,
|
||
bool duplicate = false,
|
||
bool preload = false)
|
||
where T : Resource
|
||
{
|
||
var id = _assetCatalogSystem!.GetResource(resourceKey);
|
||
|
||
_registry!.Register(
|
||
resourceKey,
|
||
_resourceLoadSystem!.GetOrRegisterResourceFactory<T>(id, duplicate),
|
||
preload
|
||
);
|
||
}
|
||
|
||
#endregion
|
||
}
|