From 078f5c2102eece5e1ba435d7d770b1c0be4e70ee Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Sun, 18 Jan 2026 20:14:33 +0800 Subject: [PATCH] =?UTF-8?q?refactor(asset):=20=E7=A7=BB=E9=99=A4=E6=8A=BD?= =?UTF-8?q?=E8=B1=A1=E8=B5=84=E6=BA=90=E7=9B=AE=E5=BD=95=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E5=B9=B6=E9=87=8D=E6=9E=84=E6=B3=A8=E5=86=8C=E8=A1=A8=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 删除 AbstractAssetCatalogUtility 类及其所有实现逻辑 - 移除 AssetCatalog 相关结构体和接口定义 - 删除 IAssetCatalogUtility 和 IResourceFactoryUtility 接口 - 移除 AbstractResourceFactoryUtility 和 ResourceLoadUtility 实现 - 重命名 IUiRegistry 接口为 IAssetRegistry 并更新泛型参数说明 - 重命名 GodotUiRegistry 为 GodotAssetRegistry 并更新接口引用 - 创建新的 IGodotAssetRegistry 接口用于PackedScene资源管理 - 创建新的 IGodotSceneRegistry 接口并更新 GodotSceneRegistry 实现 - 更新 GodotUiFactory 中的依赖注入从 IUiRegistry 改为 IAssetRegistry --- .../assets/AssetCatalog.cs | 45 ---- .../assets/IAssetCatalogUtility.cs | 94 -------- .../assets/IResourceFactoryUtility.cs | 27 --- .../assets/ResourceFactory.cs | 132 ------------ .../ui/IAssetRegistry.cs | 10 + .../ui/IUiRegistry.cs | 10 - .../assets/AbstractAssetCatalogUtility.cs | 187 ---------------- .../assets/AbstractResourceFactoryUtility.cs | 125 ----------- .../assets/IResourceLoadUtility.cs | 83 ------- .../assets/ResourceLoadUtility.cs | 202 ------------------ GFramework.Godot/scene/GodotSceneRegistry.cs | 5 +- GFramework.Godot/scene/IGodotSceneRegistry.cs | 6 + ...dotUiRegistry.cs => GodotAssetRegistry.cs} | 5 +- GFramework.Godot/ui/GodotUiFactory.cs | 4 +- GFramework.Godot/ui/IGodotAssetRegistry.cs | 10 + 15 files changed, 32 insertions(+), 913 deletions(-) delete mode 100644 GFramework.Game.Abstractions/assets/AssetCatalog.cs delete mode 100644 GFramework.Game.Abstractions/assets/IAssetCatalogUtility.cs delete mode 100644 GFramework.Game.Abstractions/assets/IResourceFactoryUtility.cs delete mode 100644 GFramework.Game.Abstractions/assets/ResourceFactory.cs create mode 100644 GFramework.Game.Abstractions/ui/IAssetRegistry.cs delete mode 100644 GFramework.Game.Abstractions/ui/IUiRegistry.cs delete mode 100644 GFramework.Game/assets/AbstractAssetCatalogUtility.cs delete mode 100644 GFramework.Godot/assets/AbstractResourceFactoryUtility.cs delete mode 100644 GFramework.Godot/assets/IResourceLoadUtility.cs delete mode 100644 GFramework.Godot/assets/ResourceLoadUtility.cs create mode 100644 GFramework.Godot/scene/IGodotSceneRegistry.cs rename GFramework.Godot/ui/{GodotUiRegistry.cs => GodotAssetRegistry.cs} (68%) create mode 100644 GFramework.Godot/ui/IGodotAssetRegistry.cs diff --git a/GFramework.Game.Abstractions/assets/AssetCatalog.cs b/GFramework.Game.Abstractions/assets/AssetCatalog.cs deleted file mode 100644 index 84d6349..0000000 --- a/GFramework.Game.Abstractions/assets/AssetCatalog.cs +++ /dev/null @@ -1,45 +0,0 @@ -namespace GFramework.Game.Abstractions.assets; - -/// -/// 资源目录类,用于定义和管理游戏中的场景和资源标识符 -/// -public static class AssetCatalog -{ - /// - /// 资源标识符接口,定义了资源路径的访问接口 - /// - public interface IAssetId - { - /// - /// 获取资源的路径 - /// - string Path { get; } - } - - /// - /// 资源目录映射结构体,用于存储资源目录的键值对映射关系 - /// - /// 资源目录的键 - /// 资源标识符 - public readonly record struct AssetCatalogMapping(string Key, IAssetId Id); - - - /// - /// 场景页面资源标识符结构体,用于标识场景页面资源 - /// - /// 场景页面资源路径 - public readonly record struct ScenePageId(string Path) : IAssetId; - - - /// - /// 场景单元资源标识符结构体,用于标识场景单元资源 - /// - /// 场景单元资源路径 - public readonly record struct SceneUnitId(string Path) : IAssetId; - - /// - /// 通用资源标识符结构体,实现IAssetId接口 - /// - /// 资源路径 - public readonly record struct AssetId(string Path) : IAssetId; -} \ No newline at end of file diff --git a/GFramework.Game.Abstractions/assets/IAssetCatalogUtility.cs b/GFramework.Game.Abstractions/assets/IAssetCatalogUtility.cs deleted file mode 100644 index a022360..0000000 --- a/GFramework.Game.Abstractions/assets/IAssetCatalogUtility.cs +++ /dev/null @@ -1,94 +0,0 @@ -using GFramework.Core.Abstractions.utility; - -namespace GFramework.Game.Abstractions.assets; - -/// -/// 资产目录工具接口,提供对场景单元、场景页面和普通资产的管理功能 -/// 继承自IUtility接口,用于处理资产目录相关的操作 -/// -public interface IAssetCatalogUtility : IContextUtility -{ - /// - /// 根据指定的键获取场景单元标识符 - /// - /// 用于查找场景单元的键值 - /// 返回与指定键对应的场景单元标识符 - AssetCatalog.SceneUnitId GetSceneUnit(string key); - - /// - /// 根据指定的键获取场景页面标识符 - /// - /// 用于查找场景页面的键值 - /// 返回与指定键对应的场景页面标识符 - AssetCatalog.ScenePageId GetScenePage(string key); - - - /// - /// 根据指定的键获取资源ID - /// - /// 用于查找资源的键值 - /// 返回对应的资源ID,如果未找到则返回默认值 - AssetCatalog.AssetId GetAsset(string key); - - /// - /// 注册场景单元到资产目录中 - /// - /// 场景单元的唯一标识键 - /// 场景单元资源的路径 - public void RegisterSceneUnit(string key, string path); - - /// - /// 通过资产目录映射注册场景单元 - /// - /// 包含场景单元信息的资产目录映射对象 - public void RegisterSceneUnit(AssetCatalog.AssetCatalogMapping mapping); - - /// - /// 注册场景页面模板 - /// - /// 场景页面的唯一标识键 - /// 场景页面资源路径 - void RegisterScenePage(string key, string path); - - /// - /// 通过资产目录映射注册场景页面 - /// - /// 包含场景页面信息的资产目录映射对象 - void RegisterScenePage(AssetCatalog.AssetCatalogMapping mapping); - - /// - /// 注册普通资产资源到资产目录中 - /// - /// 资产的唯一标识键值 - /// 资产资源的路径 - void RegisterAsset(string key, string path); - - /// - /// 根据映射配置注册普通资产资源到资产目录中 - /// - /// 包含键值和路径映射关系的配置对象 - void RegisterAsset(AssetCatalog.AssetCatalogMapping mapping); - - - /// - /// 检查是否存在指定键的场景单元 - /// - /// 用于查找场景单元的键值 - /// 存在返回true,否则返回false - bool HasSceneUnit(string key); - - - /// - /// 检查是否存在指定键的场景页面 - /// - /// 用于查找场景页面的键值 - /// 存在返回true,否则返回false - bool HasScenePage(string key); - - /// - /// 检查是否存在指定键的资源 - /// - /// 用于查找资源的键值 - /// 存在返回true,否则返回false - bool HasAsset(string key); -} \ No newline at end of file diff --git a/GFramework.Game.Abstractions/assets/IResourceFactoryUtility.cs b/GFramework.Game.Abstractions/assets/IResourceFactoryUtility.cs deleted file mode 100644 index 0898c94..0000000 --- a/GFramework.Game.Abstractions/assets/IResourceFactoryUtility.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using GFramework.Core.Abstractions.utility; - -namespace GFramework.Game.Abstractions.assets; - -/// -/// 资源工厂工具接口,提供根据键名或资产目录映射获取资源创建函数的功能 -/// 继承自IContextUtility接口,用于在游戏框架中管理资源创建工厂 -/// -public interface IResourceFactoryUtility : IContextUtility -{ - /// - /// 根据指定键名获取指定类型T的资源创建函数 - /// - /// 要获取创建函数的资源类型 - /// 用于标识资源的键名 - /// 返回一个创建T类型实例的函数委托 - Func GetFactory(string key); - - /// - /// 根据资产目录映射获取指定类型T的资源创建函数 - /// - /// 要获取创建函数的资源类型 - /// 资产目录映射信息 - /// 返回一个创建T类型实例的函数委托 - Func GetFactory(AssetCatalog.AssetCatalogMapping mapping); -} \ No newline at end of file diff --git a/GFramework.Game.Abstractions/assets/ResourceFactory.cs b/GFramework.Game.Abstractions/assets/ResourceFactory.cs deleted file mode 100644 index 40176be..0000000 --- a/GFramework.Game.Abstractions/assets/ResourceFactory.cs +++ /dev/null @@ -1,132 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -namespace GFramework.Game.Abstractions.assets; - -/// -/// 资源工厂类,用于注册和解析各种资源的创建工厂 -/// -public static class ResourceFactory -{ - /// - /// 可预加载条目接口,定义了是否需要预加载以及执行工厂的方法 - /// - private interface IPreloadableEntry - { - /// - /// 获取一个值,表示该资源是否需要预加载 - /// - bool Preload { get; } - - /// - /// 获取资源类型 - /// - Type ResourceType { get; } - - /// - /// 获取资源键值 - /// - string Key { get; } - - /// - /// 执行与该条目关联的工厂方法 - /// - void ExecuteFactory(); - } - - - /// - /// 表示一个具体的资源工厂条目,实现 IPreloadableEntry 接口 - /// - /// 资源类型 - private sealed class Entry(string key, Func factory, bool preload) : IPreloadableEntry - { - /// - /// 获取用于创建资源的工厂函数 - /// - public Func Factory { get; } = factory; - - /// - /// 获取一个值,表示该资源是否需要预加载 - /// - public bool Preload { get; } = preload; - - /// - /// 执行工厂函数以创建资源实例 - /// - public void ExecuteFactory() - { - Factory(); - } - - /// - /// 获取资源的类型 - /// - public Type ResourceType => typeof(T); - - /// - /// 获取资源的键值 - /// - public string Key { get; } = key; - } - - - /// - /// 工厂注册表,管理所有已注册的资源工厂 - /// - public sealed class Registry - { - /// - /// 存储所有已注册的工厂函数,键为资源类型,值为对应的工厂条目对象 - /// - private readonly Dictionary<(Type type, string key), IPreloadableEntry> _factories = new(); - - /// - /// 注册指定类型的资源工厂 - /// - /// 要注册的资源类型 - /// 键 - /// 创建该类型资源的工厂函数 - /// 是否需要预加载该资源,默认为false - public void Register(string key, Func factory, bool preload = false) - { - if (string.IsNullOrWhiteSpace(key)) - throw new ArgumentException("Resource key cannot be null or empty.", nameof(key)); - - var dictKey = (typeof(T), key); - - _factories[dictKey] = new Entry(key, factory, preload); - } - - /// - /// 解析并获取指定类型的工厂函数 - /// - /// 要获取工厂函数的资源类型 - /// 资源键 - /// 指定类型的工厂函数 - /// 当指定类型的工厂未注册时抛出异常 - public Func ResolveFactory(string key) - { - var dictKey = (typeof(T), key); - - if (_factories.TryGetValue(dictKey, out var entry) - && entry is Entry typed) - return typed.Factory; - - throw new InvalidOperationException( - $"Factory not registered: {typeof(T).Name} with key '{key}'"); - } - - /// - /// 预加载所有标记为需要预加载的资源 - /// - public void PreloadAll() - { - // 遍历所有已注册的工厂 - foreach (var entry in _factories.Values.Where(entry => entry.Preload)) - // 执行其工厂方法进行预加载 - entry.ExecuteFactory(); - } - } -} \ No newline at end of file diff --git a/GFramework.Game.Abstractions/ui/IAssetRegistry.cs b/GFramework.Game.Abstractions/ui/IAssetRegistry.cs new file mode 100644 index 0000000..048dc20 --- /dev/null +++ b/GFramework.Game.Abstractions/ui/IAssetRegistry.cs @@ -0,0 +1,10 @@ +using GFramework.Core.Abstractions.utility; +using GFramework.Game.Abstractions.registry; + +namespace GFramework.Game.Abstractions.ui; + +/// +/// 资源注册表接口,用于管理指定类型T的资源注册和查找 +/// +/// 资源的类型 +public interface IAssetRegistry : IUtility, IRegistry; \ No newline at end of file diff --git a/GFramework.Game.Abstractions/ui/IUiRegistry.cs b/GFramework.Game.Abstractions/ui/IUiRegistry.cs deleted file mode 100644 index bddb3e2..0000000 --- a/GFramework.Game.Abstractions/ui/IUiRegistry.cs +++ /dev/null @@ -1,10 +0,0 @@ -using GFramework.Core.Abstractions.utility; -using GFramework.Game.Abstractions.registry; - -namespace GFramework.Game.Abstractions.ui; - -/// -/// UI注册表接口,用于根据UI键获取对应的UI实例 -/// -/// UI实例的类型参数,使用协变修饰符out -public interface IUiRegistry : IUtility, IRegistry; \ No newline at end of file diff --git a/GFramework.Game/assets/AbstractAssetCatalogUtility.cs b/GFramework.Game/assets/AbstractAssetCatalogUtility.cs deleted file mode 100644 index bf2d5dc..0000000 --- a/GFramework.Game/assets/AbstractAssetCatalogUtility.cs +++ /dev/null @@ -1,187 +0,0 @@ -using GFramework.Core.utility; -using GFramework.Game.Abstractions.assets; - -namespace GFramework.Game.assets; - -/// -/// 资源目录系统抽象基类,用于管理和注册游戏中的场景和资源。 -/// 提供了统一的接口来注册和查询不同类型的资产(如游戏单元、模板、普通资源)。 -/// 子类需要实现 方法以完成具体资产的注册逻辑。 -/// -public abstract class AbstractAssetCatalogUtility : AbstractContextUtility, IAssetCatalogUtility -{ - private readonly Dictionary _assets = new(); - private readonly Dictionary _scenePages = new(); - private readonly Dictionary _sceneUnits = new(); - - - /// - /// 系统初始化时调用,用于触发资产注册流程。 - /// 此方法会调用抽象方法 ,由子类提供实际注册逻辑。 - /// - protected override void OnInit() - { - RegisterAssets(); - } - - /// - /// 抽象方法,必须在子类中重写。用于定义具体的资产注册逻辑。 - /// 在此方法中应通过调用各种 Register 方法将资产信息添加到系统中。 - /// - protected abstract void RegisterAssets(); - - #region Register(内部 or Module 使用) - - /// - /// 注册场景单元到资产目录中 - /// - /// 场景单元的唯一标识键 - /// 场景单元资源的路径 - /// 当指定的键已存在时抛出异常 - public void RegisterSceneUnit(string key, string path) - { - // 尝试添加场景单元,如果键已存在则抛出异常 - if (!_sceneUnits.TryAdd(key, new AssetCatalog.SceneUnitId(path))) - throw new InvalidOperationException($"SceneUnit key duplicated: {key}"); - } - - /// - /// 通过资产目录映射注册场景单元 - /// - /// 包含场景单元信息的资产目录映射对象 - /// 当映射ID不是SceneUnitId类型或键已存在时抛出异常 - public void RegisterSceneUnit(AssetCatalog.AssetCatalogMapping mapping) - { - // 验证映射ID是否为SceneUnitId类型 - if (mapping.Id is not AssetCatalog.SceneUnitId sceneId) - throw new InvalidOperationException("Mapping ID is not a SceneUnitId"); - - // 尝试添加场景单元,如果键已存在则抛出异常 - if (!_sceneUnits.TryAdd(mapping.Key, sceneId)) - throw new InvalidOperationException($"Scene key duplicated: {mapping.Key}"); - } - - /// - /// 注册场景页面模板 - /// - /// 场景页面的唯一标识键 - /// 场景页面资源路径 - /// 当键已存在时抛出异常 - public void RegisterScenePage(string key, string path) - { - if (!_scenePages.TryAdd(key, new AssetCatalog.ScenePageId(path))) - throw new InvalidOperationException($"Template key duplicated: {key}"); - } - - /// - /// 通过资产目录映射注册场景页面 - /// - /// 包含场景页面信息的资产目录映射对象 - /// 当映射ID不是ScenePageId类型或键已存在时抛出异常 - public void RegisterScenePage(AssetCatalog.AssetCatalogMapping mapping) - { - // 验证映射ID是否为ScenePageId类型 - if (mapping.Id is not AssetCatalog.ScenePageId templateId) - throw new InvalidOperationException("Mapping ID is not a ScenePageId"); - - // 尝试添加场景页面,如果键已存在则抛出异常 - if (!_scenePages.TryAdd(mapping.Key, templateId)) - throw new InvalidOperationException($"Template key duplicated: {mapping.Key}"); - } - - - /// - /// 注册一个通用资源(Asset),使用指定的键和路径。 - /// - /// 唯一标识该资源的字符串键。 - /// 该资源对应的资源路径。 - /// 当键已存在时抛出异常。 - public void RegisterAsset(string key, string path) - { - if (!_assets.TryAdd(key, new AssetCatalog.AssetId(path))) - throw new InvalidOperationException($"Asset key duplicated: {key}"); - } - - /// - /// 根据映射对象注册一个通用资源(Asset)。 - /// - /// 包含键与ID映射关系的对象。 - /// - /// 当映射ID不是 类型或键重复时抛出异常。 - /// - 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(对外) - - /// - /// 根据指定的键获取场景单元标识符 - /// - /// 用于查找场景单元的键值 - /// 返回与指定键对应的场景单元标识符 - public AssetCatalog.SceneUnitId GetSceneUnit(string key) - { - return _sceneUnits[key]; - } - - /// - /// 根据指定的键获取场景页面标识符 - /// - /// 用于查找场景页面的键值 - /// 返回与指定键对应的场景页面标识符 - public AssetCatalog.ScenePageId GetScenePage(string key) - { - return _scenePages[key]; - } - - /// - /// 获取指定键对应的通用资源ID。 - /// - /// 要查找的通用资源键。 - /// 对应的通用资源ID。 - /// 如果未找到指定键则抛出异常。 - public AssetCatalog.AssetId GetAsset(string key) - { - return _assets[key]; - } - - /// - /// 检查是否存在指定键的场景单元 - /// - /// 用于查找场景单元的键值 - /// 存在返回true,否则返回false - public bool HasSceneUnit(string key) - { - return _sceneUnits.ContainsKey(key); - } - - /// - /// 检查是否存在指定键的场景页面 - /// - /// 用于查找场景页面的键值 - /// 存在返回true,否则返回false - public bool HasScenePage(string key) - { - return _scenePages.ContainsKey(key); - } - - /// - /// 判断是否存在指定键的通用资源。 - /// - /// 要检查的通用资源键。 - /// 若存在返回 true,否则返回 false。 - public bool HasAsset(string key) - { - return _assets.ContainsKey(key); - } - - #endregion -} \ No newline at end of file diff --git a/GFramework.Godot/assets/AbstractResourceFactoryUtility.cs b/GFramework.Godot/assets/AbstractResourceFactoryUtility.cs deleted file mode 100644 index f88f35c..0000000 --- a/GFramework.Godot/assets/AbstractResourceFactoryUtility.cs +++ /dev/null @@ -1,125 +0,0 @@ -using GFramework.Core.extensions; -using GFramework.Core.utility; -using GFramework.Game.Abstractions.assets; -using Godot; - -namespace GFramework.Godot.assets; - -/// -/// 资源工厂系统抽象基类,用于统一管理各类资源的创建与预加载逻辑。 -/// 提供注册场景和资源的方法,并通过依赖的资源加载系统和资产目录系统完成实际资源的获取与构造。 -/// -public abstract class AbstractResourceFactoryUtility : AbstractContextUtility, IResourceFactoryUtility -{ - private IAssetCatalogUtility? _assetCatalogUtility; - private ResourceFactory.Registry? _registry; - private IResourceLoadUtility? _resourceLoadUtility; - - /// - /// 根据指定的键获取资源工厂函数。 - /// - /// 资源类型 - /// 资源键 - /// 返回创建指定类型资源的工厂函数 - public Func GetFactory(string key) - { - return _registry!.ResolveFactory(key); - } - - - /// - /// 根据资产目录映射信息获取资源工厂函数。 - /// - /// 资源类型 - /// 资产目录映射信息 - /// 返回创建指定类型资源的工厂函数 - public Func GetFactory(AssetCatalog.AssetCatalogMapping mapping) - { - return _registry!.ResolveFactory(mapping.Key); - } - - /// - /// 系统初始化方法,在系统启动时执行一次。 - /// 初始化资源注册表,并获取依赖的资源加载系统和资产目录系统。 - /// 最后执行所有已注册资源的预加载操作。 - /// - protected override void OnInit() - { - _registry = new ResourceFactory.Registry(); - _resourceLoadUtility = this.GetUtility(); - _assetCatalogUtility = this.GetUtility(); - RegisterResources(); - _registry!.PreloadAll(); - } - - /// - /// 注册系统所需的各种资源类型。由子类实现具体注册逻辑。 - /// - protected abstract void RegisterResources(); - - - #region Register Helpers(声明式) - - /// - /// 注册场景单元到资源管理系统中 - /// - /// 场景单元类型,必须继承自Node - /// 场景单元键值,用于标识特定的场景单元资源 - /// 是否预加载该资源,默认为false - public void RegisterSceneUnit( - string sceneUnitKey, - bool preload = false) - where T : Node - { - var id = _assetCatalogUtility!.GetSceneUnit(sceneUnitKey); - - _registry!.Register( - sceneUnitKey, - _resourceLoadUtility!.GetOrRegisterGameUnitFactory(id), - preload - ); - } - - - /// - /// 注册场景页面到资源管理系统中 - /// - /// 场景页面类型,必须继承自Node - /// 场景页面键值,用于标识特定的场景页面资源 - /// 是否预加载该资源,默认为false - public void RegisterScenePage( - string scenePageKey, - bool preload = false) - where T : Node - { - var id = _assetCatalogUtility!.GetScenePage(scenePageKey); - - _registry!.Register( - scenePageKey, - _resourceLoadUtility!.GetOrRegisterTemplateFactory(id), - preload - ); - } - - /// - /// 注册通用资产资源到资源管理系统中 - /// - /// 资产类型,必须继承自Resource - /// 资产键值,用于标识特定的资产资源 - /// 是否预加载该资源,默认为false - public void RegisterAsset( - string assetKey, - bool preload = false) - where T : Resource - { - var id = _assetCatalogUtility!.GetAsset(assetKey); - - _registry!.Register( - assetKey, - _resourceLoadUtility!.GetOrRegisterAssetFactory(id), - preload - ); - } - - #endregion -} \ No newline at end of file diff --git a/GFramework.Godot/assets/IResourceLoadUtility.cs b/GFramework.Godot/assets/IResourceLoadUtility.cs deleted file mode 100644 index 862a871..0000000 --- a/GFramework.Godot/assets/IResourceLoadUtility.cs +++ /dev/null @@ -1,83 +0,0 @@ -using GFramework.Core.Abstractions.utility; -using GFramework.Game.Abstractions.assets; -using Godot; - -namespace GFramework.Godot.assets; - -/// -/// 资源加载系统接口,提供资源和场景的加载、实例化、预加载等功能 -/// -public interface IResourceLoadUtility : IContextUtility -{ - /// - /// 加载指定路径的资源 - /// - /// 资源类型,必须继承自Resource - /// 资源路径 - /// 加载的资源实例 - public T? LoadResource(string path) where T : Resource; - - /// - /// 获取场景加载器,用于延迟加载场景 - /// - /// 场景路径 - /// 场景的延迟加载包装器 - public Lazy GetSceneLoader(string path); - - /// - /// 创建指定路径场景的实例 - /// - /// 节点类型,必须继承自Node - /// 场景路径 - /// 场景实例化的节点对象 - public T? CreateInstance(string path) where T : Node; - - /// - /// 获取或注册游戏单位工厂函数 - /// - /// 节点类型,必须继承自Node - /// 场景资源标识符 - /// 创建场景实例的工厂函数 - Func GetOrRegisterGameUnitFactory( - AssetCatalog.SceneUnitId id - ) where T : Node; - - /// - /// 获取或注册模板资源工厂函数 - /// - /// 节点类型,必须继承自Node - /// 模板资源标识符 - /// 创建模板实例的工厂函数 - Func GetOrRegisterTemplateFactory( - AssetCatalog.ScenePageId id - ) where T : Node; - - /// - /// 获取或注册通用资产工厂函数 - /// - /// 资源类型,必须继承自Resource - /// 资产资源标识符 - /// 是否对原始资源进行复制操作,默认为false - /// 创建资产实例的工厂函数 - Func GetOrRegisterAssetFactory( - AssetCatalog.AssetId id, - bool duplicate = false - ) where T : Resource; - - /// - /// 预加载指定路径的多个资源 - /// - /// 需要预加载的资源路径集合 - public void Preload(IEnumerable paths); - - /// - /// 卸载指定路径的资源 - /// - /// 需要卸载的资源路径 - public void Unload(string path); - - /// - /// 清除所有已加载的资源 - /// - public void ClearAll(); -} \ No newline at end of file diff --git a/GFramework.Godot/assets/ResourceLoadUtility.cs b/GFramework.Godot/assets/ResourceLoadUtility.cs deleted file mode 100644 index 0aaccf0..0000000 --- a/GFramework.Godot/assets/ResourceLoadUtility.cs +++ /dev/null @@ -1,202 +0,0 @@ -using GFramework.Core.utility; -using GFramework.Game.Abstractions.assets; -using Godot; - -namespace GFramework.Godot.assets; - -/// -/// 资源加载系统,用于统一管理和缓存Godot资源(如场景、纹理等)的加载与实例化。 -/// 提供基础加载、场景实例化、资源工厂注册以及缓存管理功能。 -/// -public class ResourceLoadUtility : AbstractContextUtility, IResourceLoadUtility -{ - /// - /// 已加载的资源缓存字典,键为资源路径,值为已加载的Resource对象。 - /// - private readonly Dictionary _loadedResources = new(); - - /// - /// 资源获取/复制工厂委托缓存,键为资源路径,值为获取或复制资源的Func委托。 - /// - private readonly Dictionary _resourceFactories = new(); - - /// - /// 场景实例化工厂委托缓存,键为场景路径,值为创建该场景实例的Func委托。 - /// - private readonly Dictionary _sceneFactories = new(); - - /// - /// 场景懒加载器缓存,键为场景路径,值为延迟加载的PackedScene对象。 - /// - private readonly Dictionary> _sceneLoaders = new(); - - /// - /// 根据给定路径加载场景,并创建其节点实例。 - /// - /// 期望返回的节点类型,必须是Node的子类。 - /// 场景文件的相对路径。 - /// 新创建的场景根节点实例;如果加载失败则返回null。 - public T? CreateInstance(string path) where T : Node - { - var scene = GetSceneLoader(path).Value; - return scene.Instantiate(); - } - - public Func GetOrRegisterGameUnitFactory(AssetCatalog.SceneUnitId id) where T : Node - { - var path = id.Path; - if (_sceneFactories.TryGetValue(path, out var d)) - return d as Func ?? - throw new InvalidCastException($"Factory for path '{path}' is not of type Func<{typeof(T)}>"); - - var factory = () => - { - var scene = GetSceneLoader(path).Value - ?? throw new InvalidOperationException($"Scene not loaded: {path}"); - - return scene.Instantiate() - ?? throw new InvalidOperationException($"Instantiate failed: {path}"); - }; - - _sceneFactories[path] = factory; - return factory; - } - - public Func GetOrRegisterTemplateFactory(AssetCatalog.ScenePageId id) where T : Node - { - var path = id.Path; - if (_sceneFactories.TryGetValue(path, out var d)) - return d as Func ?? - throw new InvalidCastException($"Factory for path '{path}' is not of type Func<{typeof(T)}>"); - - var factory = () => - { - var scene = GetSceneLoader(path).Value - ?? throw new InvalidOperationException($"Scene not loaded: {path}"); - - return scene.Instantiate() - ?? throw new InvalidOperationException($"Instantiate failed: {path}"); - }; - - _sceneFactories[path] = factory; - return factory; - } - - public Func GetOrRegisterAssetFactory(AssetCatalog.AssetId id, bool duplicate = false) where T : Resource - { - var path = id.Path; - if (_resourceFactories.TryGetValue(path, out var d)) - return d as Func ?? - throw new InvalidCastException($"Factory for path '{path}' is not of type Func<{typeof(T)}>"); - - var factory = () => - { - var res = LoadResource(path) - ?? throw new InvalidOperationException($"Load failed: {path}"); - - if (!duplicate) return res; - - return res.Duplicate() as T ?? res; - }; - - _resourceFactories[path] = factory; - return factory; - } - - /// - /// 初始化方法,在系统初始化时打印日志信息。 - /// - protected override void OnInit() - { - } - - #region 基础加载 - - /// - /// 加载指定类型的资源并进行缓存。如果资源已经加载过则直接从缓存中返回。 - /// - /// 要加载的资源类型,必须继承自Resource。 - /// 资源在项目中的相对路径。 - /// 成功加载的资源对象;若路径无效或加载失败则返回null。 - public T? LoadResource(string path) where T : Resource - { - if (string.IsNullOrEmpty(path)) - return null; - - if (_loadedResources.TryGetValue(path, out var cached)) - return cached as T; - - var res = GD.Load(path); - if (res == null) - { - GD.PrintErr($"[ResourceLoadUtility] Load failed: {path}"); - return null; - } - - _loadedResources[path] = res; - return res; - } - - /// - /// 获取一个场景的懒加载器,用于按需加载PackedScene资源。 - /// 若对应路径尚未注册加载器,则会自动创建一个新的Lazy实例。 - /// - /// 场景文件的相对路径。 - /// 表示该场景懒加载逻辑的Lazy<PackedScene>对象。 - public Lazy GetSceneLoader(string path) - { - if (_sceneLoaders.TryGetValue(path, out var loader)) - return loader; - - loader = new Lazy(() => - { - var scene = LoadResource(path); - return scene ?? throw new InvalidOperationException($"Failed to load scene: {path}"); - }); - - _sceneLoaders[path] = loader; - return loader; - } - - #endregion - - #region 缓存管理 - - /// - /// 预加载一组资源和场景到内存中以提升后续访问速度。 - /// - /// 待预加载的资源路径集合。 - public void Preload(IEnumerable paths) - { - foreach (var path in paths) - { - GetSceneLoader(path); - LoadResource(path); - } - } - - /// - /// 清除指定路径的所有相关缓存数据,包括资源、场景加载器及各类工厂。 - /// - /// 要卸载的资源路径。 - public void Unload(string path) - { - _loadedResources.Remove(path); - _sceneLoaders.Remove(path); - _sceneFactories.Remove(path); - _resourceFactories.Remove(path); - } - - /// - /// 清空所有当前系统的资源缓存、加载器和工厂列表。 - /// - public void ClearAll() - { - _loadedResources.Clear(); - _sceneLoaders.Clear(); - _sceneFactories.Clear(); - _resourceFactories.Clear(); - } - - #endregion -} \ No newline at end of file diff --git a/GFramework.Godot/scene/GodotSceneRegistry.cs b/GFramework.Godot/scene/GodotSceneRegistry.cs index 17efa8e..bd0e8a4 100644 --- a/GFramework.Godot/scene/GodotSceneRegistry.cs +++ b/GFramework.Godot/scene/GodotSceneRegistry.cs @@ -1,5 +1,4 @@ using GFramework.Core.Abstractions.registries; -using GFramework.Game.Abstractions.ui; using Godot; namespace GFramework.Godot.scene; @@ -9,5 +8,5 @@ namespace GFramework.Godot.scene; /// 继承自KeyValueRegistryBase,使用字符串作为键,PackedScene作为值进行存储 /// 实现IUiRegistry接口,提供UI场景的注册和管理功能 /// -public class GodotSceneRegistry() : KeyValueRegistryBase(StringComparer.Ordinal), - IUiRegistry; \ No newline at end of file +public class GodotSceneRegistry() + : KeyValueRegistryBase(StringComparer.Ordinal), IGodotSceneRegistry; \ No newline at end of file diff --git a/GFramework.Godot/scene/IGodotSceneRegistry.cs b/GFramework.Godot/scene/IGodotSceneRegistry.cs new file mode 100644 index 0000000..44dcc05 --- /dev/null +++ b/GFramework.Godot/scene/IGodotSceneRegistry.cs @@ -0,0 +1,6 @@ +using GFramework.Game.Abstractions.ui; +using Godot; + +namespace GFramework.Godot.scene; + +public interface IGodotSceneRegistry : IAssetRegistry; \ No newline at end of file diff --git a/GFramework.Godot/ui/GodotUiRegistry.cs b/GFramework.Godot/ui/GodotAssetRegistry.cs similarity index 68% rename from GFramework.Godot/ui/GodotUiRegistry.cs rename to GFramework.Godot/ui/GodotAssetRegistry.cs index dab8072..923df57 100644 --- a/GFramework.Godot/ui/GodotUiRegistry.cs +++ b/GFramework.Godot/ui/GodotAssetRegistry.cs @@ -1,5 +1,4 @@ using GFramework.Core.Abstractions.registries; -using GFramework.Game.Abstractions.ui; using Godot; namespace GFramework.Godot.ui; @@ -9,5 +8,5 @@ namespace GFramework.Godot.ui; /// 继承自KeyValueRegistryBase,使用字符串作为键,PackedScene作为值进行存储 /// 实现IUiRegistry接口,提供UI场景的注册和管理功能 /// -public class GodotUiRegistry() : KeyValueRegistryBase(StringComparer.Ordinal), - IUiRegistry; \ No newline at end of file +public class GodotAssetRegistry() + : KeyValueRegistryBase(StringComparer.Ordinal), IGodotAssetRegistry; \ No newline at end of file diff --git a/GFramework.Godot/ui/GodotUiFactory.cs b/GFramework.Godot/ui/GodotUiFactory.cs index c80985c..b7d494f 100644 --- a/GFramework.Godot/ui/GodotUiFactory.cs +++ b/GFramework.Godot/ui/GodotUiFactory.cs @@ -14,7 +14,7 @@ public class GodotUiFactory : AbstractContextUtility, IUiFactory /// /// UI注册表,用于存储和获取PackedScene类型的UI资源 /// - private IUiRegistry _registry = null!; + private IAssetRegistry _registry = null!; /// /// 根据指定的UI键创建UI页面实例 @@ -44,6 +44,6 @@ public class GodotUiFactory : AbstractContextUtility, IUiFactory /// protected override void OnInit() { - _registry = this.GetUtility>()!; + _registry = this.GetUtility>()!; } } \ No newline at end of file diff --git a/GFramework.Godot/ui/IGodotAssetRegistry.cs b/GFramework.Godot/ui/IGodotAssetRegistry.cs new file mode 100644 index 0000000..ee3522b --- /dev/null +++ b/GFramework.Godot/ui/IGodotAssetRegistry.cs @@ -0,0 +1,10 @@ +using GFramework.Game.Abstractions.ui; +using Godot; + +namespace GFramework.Godot.ui; + +/// +/// Godot UI注册表接口,用于管理PackedScene类型的UI资源注册和管理 +/// 继承自通用UI注册表接口,专门针对Godot引擎的PackedScene资源类型 +/// +public interface IGodotAssetRegistry : IAssetRegistry; \ No newline at end of file