feat(storage): 实现 Godot 文件存储删除功能

- 添加 Godot 命名空间引用
- 实现 Delete 方法的具体逻辑
- 支持 Godot 路径和普通文件路径的删除操作
- 添加文件存在性检查避免删除不存在的文件
- 实现线程安全的锁机制防止并发冲突
- 添加删除完成后的锁清理防止内存泄漏
- 提供详细的错误处理和异常抛出机制
This commit is contained in:
GeWuYou 2026-01-19 20:12:20 +08:00
parent 48c962f874
commit 766a73f2a9

View File

@ -3,6 +3,7 @@ using System.Text;
using GFramework.Core.Abstractions.storage;
using GFramework.Game.Abstractions.serializer;
using GFramework.Godot.extensions;
using Godot;
using FileAccess = Godot.FileAccess;
namespace GFramework.Godot.storage;
@ -37,7 +38,31 @@ public sealed class GodotFileStorage : IStorage
/// <param name="key">存储键</param>
public void Delete(string key)
{
throw new NotImplementedException();
var path = ToAbsolutePath(key);
var keyLock = GetLock(path);
lock (keyLock)
{
if (path.IsGodotPath())
{
if (FileAccess.FileExists(path))
{
var err = DirAccess.RemoveAbsolute(path);
if (err != Error.Ok)
throw new IOException($"Failed to delete Godot file: {path}, error: {err}");
}
}
else
{
if (File.Exists(path))
{
File.Delete(path);
}
}
}
// 删除完成后尝试移除锁,防止锁字典无限增长
_keyLocks.TryRemove(path, out _);
}
#endregion