// Copyright (c) 2026 GeWuYou
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using GFramework.Core.Abstractions.bases;
using Godot;
namespace GFramework.Godot.data;
///
/// Godot资源仓储实现类,用于管理Godot资源的存储和加载。
/// 实现了IResourceRepository接口,提供基于键的资源存取功能。
///
/// 资源键的类型
/// 资源类型,必须继承自Godot.Resource并实现IHasKey接口
public class GodotResourceRepository
: IResourceRepository
where TResource : Resource, IHasKey
where TKey : notnull
{
///
/// 内部存储字典,用于保存键值对形式的资源
///
private readonly Dictionary _storage = new();
///
/// 向仓储中添加资源
///
/// 资源的键
/// 要添加的资源对象
/// 当键已存在时抛出异常
public void Add(TKey key, TResource value)
{
if (!_storage.TryAdd(key, value))
throw new InvalidOperationException($"Duplicate key detected: {key}");
}
///
/// 根据键获取资源
///
/// 资源的键
/// 对应的资源对象
/// 当键不存在时抛出异常
public TResource Get(TKey key)
{
if (!_storage.TryGetValue(key, out var value))
throw new KeyNotFoundException($"Resource with key '{key}' not found.");
return value;
}
///
/// 尝试根据键获取资源
///
/// 资源的键
/// 输出参数,返回找到的资源对象
/// 如果找到资源返回true,否则返回false
public bool TryGet(TKey key, out TResource value)
=> _storage.TryGetValue(key, out value!);
///
/// 获取所有资源的只读集合
///
/// 包含所有资源的只读集合
public IReadOnlyCollection GetAll()
=> _storage.Values;
///
/// 检查是否包含指定键的资源
///
/// 要检查的键
/// 如果包含该键返回true,否则返回false
public bool Contains(TKey key)
=> _storage.ContainsKey(key);
///
/// 从仓储中移除指定键的资源
///
/// 要移除的资源键
/// 当键不存在时抛出异常
public void Remove(TKey key)
{
if (!_storage.Remove(key))
throw new KeyNotFoundException($"Resource with key '{key}' not found.");
}
///
/// 清空仓储中的所有资源
///
public void Clear()
=> _storage.Clear();
///
/// 从指定路径集合加载资源到仓储中
///
/// 资源文件路径的集合
/// 是否递归加载子目录中的资源
public void LoadFromPath(IEnumerable paths, bool recursive = false)
{
foreach (var path in paths)
{
LoadSinglePath(path, recursive);
}
}
///
/// 从指定路径数组加载资源到仓储中
/// 提供便捷的参数数组重载方法
///
/// 是否递归加载子目录中的资源
/// 资源文件路径的参数数组
public void LoadFromPath(bool recursive = false, params string[] paths)
{
LoadFromPath(paths, recursive);
}
///
/// 从单个路径加载资源
/// 遍历目录中的所有.tres和.res文件并加载为资源
///
/// 要加载资源的目录路径
/// 是否递归加载子目录中的资源
private void LoadSinglePath(string path, bool recursive)
{
// 打开目录访问对象
var dir = DirAccess.Open(path);
if (dir == null)
{
GD.PushWarning($"Path not found: {path}");
return;
}
// 开始遍历目录
dir.ListDirBegin();
while (true)
{
var entry = dir.GetNext();
if (string.IsNullOrEmpty(entry))
break;
var fullPath = $"{path}/{entry}";
// 处理目录项
if (dir.CurrentIsDir())
{
// 递归处理子目录(排除.和..目录)
if (recursive && entry != "." && entry != "..")
{
LoadSinglePath(fullPath, true);
}
continue;
}
// 只处理.tres和.res文件
if (!entry.EndsWith(".tres") && !entry.EndsWith(".res"))
continue;
// 加载资源文件
var resource = GD.Load(fullPath);
if (resource == null)
{
GD.PushWarning($"Failed to load resource: {fullPath}");
continue;
}
Add(resource.Key, resource);
}
// 结束目录遍历
dir.ListDirEnd();
}
}