GFramework/GFramework.Game/assets/AbstractAssetCatalogSystem.cs
GeWuYou 2db09e72d7 refactor(assets): 重构资产目录系统以支持多种资源类型
- 将 AbstractAssetCatalogSystem 从 GFramework.Godot 移动到 GFramework.Game
- 引入 GameUnitId、TemplateId 和 AssetId 替代原有的 SceneId 和 ResourceId
- 更新注册与查询接口以区分不同类型资源
- 修改相关系统类以适配新的资产标识符类型
- 调整项目引用依赖关系,确保正确的程序集链接
- 扩展资源工厂系统以处理新增的资源类别
- [no tag]
2025-12-20 13:45:49 +08:00

167 lines
7.2 KiB
C#
Raw 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 GFramework.Core.system;
namespace GFramework.Game.assets;
/// <summary>
/// 资源目录系统抽象基类,用于管理和注册游戏中的场景和资源。
/// 提供了统一的接口来注册和查询不同类型的资产(如游戏单元、模板、普通资源)。
/// 子类需要实现 <see cref="RegisterAssets"/> 方法以完成具体资产的注册逻辑。
/// </summary>
public abstract class AbstractAssetCatalogSystem : AbstractSystem, IAssetCatalogSystem
{
private readonly Dictionary<string, AssetCatalog.GameUnitId> _gameUnits = new();
private readonly Dictionary<string, AssetCatalog.TemplateId> _templates = new();
private readonly Dictionary<string, AssetCatalog.AssetId> _assets = new();
/// <summary>
/// 系统初始化时调用,用于触发资产注册流程。
/// 此方法会调用抽象方法 <see cref="RegisterAssets"/>,由子类提供实际注册逻辑。
/// </summary>
protected override void OnInit()
{
RegisterAssets();
}
/// <summary>
/// 抽象方法,必须在子类中重写。用于定义具体的资产注册逻辑。
/// 在此方法中应通过调用各种 Register 方法将资产信息添加到系统中。
/// </summary>
protected abstract void RegisterAssets();
#region Register or Module 使
/// <summary>
/// 注册一个游戏单元GameUnit使用指定的键和路径。
/// </summary>
/// <param name="key">唯一标识该游戏单元的字符串键。</param>
/// <param name="path">该游戏单元对应的资源路径。</param>
/// <exception cref="InvalidOperationException">当键已存在时抛出异常。</exception>
public void RegisterGameUnit(string key, string path)
{
if (!_gameUnits.TryAdd(key, new AssetCatalog.GameUnitId(path)))
throw new InvalidOperationException($"GameUnit key duplicated: {key}");
}
/// <summary>
/// 根据映射对象注册一个游戏单元GameUnit
/// </summary>
/// <param name="mapping">包含键与ID映射关系的对象。</param>
/// <exception cref="InvalidOperationException">
/// 当映射ID不是 <see cref="AssetCatalog.GameUnitId"/> 类型或键重复时抛出异常。
/// </exception>
public void RegisterGameUnit(AssetCatalog.AssetCatalogMapping mapping)
{
if (mapping.Id is not AssetCatalog.GameUnitId sceneId)
throw new InvalidOperationException("Mapping ID is not a GameUnitId");
if (!_gameUnits.TryAdd(mapping.Key, sceneId))
throw new InvalidOperationException($"Scene key duplicated: {mapping.Key}");
}
/// <summary>
/// 注册一个模板资源Template使用指定的键和路径。
/// </summary>
/// <param name="key">唯一标识该模板的字符串键。</param>
/// <param name="path">该模板对应的资源路径。</param>
/// <exception cref="InvalidOperationException">当键已存在时抛出异常。</exception>
public void RegisterTemplate(string key, string path)
{
if (!_templates.TryAdd(key, new AssetCatalog.TemplateId(path)))
throw new InvalidOperationException($"Template key duplicated: {key}");
}
/// <summary>
/// 根据映射对象注册一个模板资源Template
/// </summary>
/// <param name="mapping">包含键与ID映射关系的对象。</param>
/// <exception cref="InvalidOperationException">
/// 当映射ID不是 <see cref="AssetCatalog.TemplateId"/> 类型或键重复时抛出异常。
/// </exception>
public void RegisterTemplate(AssetCatalog.AssetCatalogMapping mapping)
{
if (mapping.Id is not AssetCatalog.TemplateId templateId)
throw new InvalidOperationException("Mapping ID is not a TemplateId");
if (!_templates.TryAdd(mapping.Key, templateId))
throw new InvalidOperationException($"Template key duplicated: {mapping.Key}");
}
/// <summary>
/// 注册一个通用资源Asset使用指定的键和路径。
/// </summary>
/// <param name="key">唯一标识该资源的字符串键。</param>
/// <param name="path">该资源对应的资源路径。</param>
/// <exception cref="InvalidOperationException">当键已存在时抛出异常。</exception>
public void RegisterAsset(string key, string path)
{
if (!_assets.TryAdd(key, new AssetCatalog.AssetId(path)))
throw new InvalidOperationException($"Asset key duplicated: {key}");
}
/// <summary>
/// 根据映射对象注册一个通用资源Asset
/// </summary>
/// <param name="mapping">包含键与ID映射关系的对象。</param>
/// <exception cref="InvalidOperationException">
/// 当映射ID不是 <see cref="AssetCatalog.AssetId"/> 类型或键重复时抛出异常。
/// </exception>
public void RegisterAsset(AssetCatalog.AssetCatalogMapping mapping)
{
if (mapping.Id is not AssetCatalog.AssetId assetId)
throw new InvalidOperationException("Mapping ID is not a AssetId");
if (!_assets.TryAdd(mapping.Key, assetId))
throw new InvalidOperationException($"Asset key duplicated: {mapping.Key}");
}
#endregion
#region Query
/// <summary>
/// 获取指定键对应的游戏单元ID。
/// </summary>
/// <param name="key">要查找的游戏单元键。</param>
/// <returns>对应的游戏单元ID。</returns>
/// <exception cref="KeyNotFoundException">如果未找到指定键则抛出异常。</exception>
public AssetCatalog.GameUnitId GetGameUnit(string key) => _gameUnits[key];
/// <summary>
/// 获取指定键对应的模板资源ID。
/// </summary>
/// <param name="key">要查找的模板资源键。</param>
/// <returns>对应的模板资源ID。</returns>
/// <exception cref="KeyNotFoundException">如果未找到指定键则抛出异常。</exception>
public AssetCatalog.TemplateId GetTemplate(string key) => _templates[key];
/// <summary>
/// 获取指定键对应的通用资源ID。
/// </summary>
/// <param name="key">要查找的通用资源键。</param>
/// <returns>对应的通用资源ID。</returns>
/// <exception cref="KeyNotFoundException">如果未找到指定键则抛出异常。</exception>
public AssetCatalog.AssetId GetAsset(string key) => _assets[key];
/// <summary>
/// 判断是否存在指定键的游戏单元。
/// </summary>
/// <param name="key">要检查的游戏单元键。</param>
/// <returns>若存在返回 true否则返回 false。</returns>
public bool HasGameUnit(string key) => _gameUnits.ContainsKey(key);
/// <summary>
/// 判断是否存在指定键的模板资源。
/// </summary>
/// <param name="key">要检查的模板资源键。</param>
/// <returns>若存在返回 true否则返回 false。</returns>
public bool HasTemplate(string key) => _templates.ContainsKey(key);
/// <summary>
/// 判断是否存在指定键的通用资源。
/// </summary>
/// <param name="key">要检查的通用资源键。</param>
/// <returns>若存在返回 true否则返回 false。</returns>
public bool HasAsset(string key) => _assets.ContainsKey(key);
#endregion
}