using System;
using System.Collections.Generic;
using System.IO;
using Godot;
using TBL.GodotSharp.IO.File;
using Zio;
namespace TBL.GodotSharp.Content.FileSystem
{
///
/// Godot 文件系统
///
public class GodotFileSystem : Zio.FileSystems.FileSystem
{
///
/// 根目录
///
private readonly string _rootPath;
///
/// 目录工具
///
private readonly Godot.Directory _directoryTool;
public GodotFileSystem(string rootPath)
{
_rootPath = rootPath;
_directoryTool = new Godot.Directory();
}
private string GetFullPath(UPath uPath) => $"{_rootPath}{uPath}";
protected override void CreateDirectoryImpl(UPath path)
{
_directoryTool.MakeDirRecursive(GetFullPath(path));
}
protected override bool DirectoryExistsImpl(UPath path) => _directoryTool.DirExists(GetFullPath(path));
protected override bool FileExistsImpl(UPath path) => _directoryTool.FileExists(GetFullPath(path));
protected override Stream OpenFileImpl(UPath path, FileMode mode, FileAccess access, FileShare share)
{
if (mode == FileMode.Open && access == FileAccess.Read)
{
return new GodotFileStream(GetFullPath(path), Godot.File.ModeFlags.Read);
}
throw new FileNotFoundException();
}
protected override IEnumerable EnumeratePathsImpl(
UPath path,
string searchPattern,
SearchOption searchOption,
SearchTarget searchTarget
)
{
var directoryTool = new Godot.Directory();
var err = directoryTool.Open(GetFullPath(path));
if (err != Error.Ok)
throw new IOException($"{err}");
directoryTool.ListDirBegin();
var searchPatternMatcher = SearchPattern.Parse(ref path, ref searchPattern);
for (var current = directoryTool.GetNext();; current = directoryTool.GetNext())
{
if (string.IsNullOrEmpty(current))
break;
if (current == ".." || current == ".")
continue;
if ((searchTarget == SearchTarget.Both || searchTarget == SearchTarget.File) &&
!directoryTool.CurrentIsDir() && searchPatternMatcher.Match(current))
yield return System.IO.Path.Combine(path.FullName, current);
if ((searchTarget == SearchTarget.Both || searchTarget == SearchTarget.Directory) &&
directoryTool.CurrentIsDir() && searchPatternMatcher.Match(current))
yield return System.IO.Path.Combine(path.FullName, current);
if (directoryTool.CurrentIsDir() && searchOption == SearchOption.AllDirectories)
{
foreach (var sub in EnumeratePathsImpl(System.IO.Path.Combine(path.FullName, current),
searchPattern, searchOption, searchTarget))
{
yield return sub;
}
}
}
directoryTool.ListDirEnd();
}
protected override void MoveDirectoryImpl(UPath srcPath, UPath destPath)
{
throw new NotImplementedException();
}
protected override void DeleteDirectoryImpl(UPath path, bool isRecursive)
{
throw new NotImplementedException();
}
protected override void CopyFileImpl(UPath srcPath, UPath destPath, bool overwrite)
{
throw new NotImplementedException();
}
protected override void ReplaceFileImpl(UPath srcPath, UPath destPath, UPath destBackupPath, bool ignoreMetadataErrors)
{
throw new NotImplementedException();
}
protected override long GetFileLengthImpl(UPath path)
{
throw new NotImplementedException();
}
protected override void MoveFileImpl(UPath srcPath, UPath destPath)
{
throw new NotImplementedException();
}
protected override void DeleteFileImpl(UPath path)
{
throw new NotImplementedException();
}
protected override FileAttributes GetAttributesImpl(UPath path)
{
throw new NotImplementedException();
}
protected override void SetAttributesImpl(UPath path, FileAttributes attributes)
{
throw new NotImplementedException();
}
protected override DateTime GetCreationTimeImpl(UPath path)
{
throw new NotImplementedException();
}
protected override void SetCreationTimeImpl(UPath path, DateTime time)
{
throw new NotImplementedException();
}
protected override DateTime GetLastAccessTimeImpl(UPath path)
{
throw new NotImplementedException();
}
protected override void SetLastAccessTimeImpl(UPath path, DateTime time)
{
throw new NotImplementedException();
}
protected override DateTime GetLastWriteTimeImpl(UPath path)
{
throw new NotImplementedException();
}
protected override void SetLastWriteTimeImpl(UPath path, DateTime time)
{
throw new NotImplementedException();
}
protected override IEnumerable EnumerateItemsImpl(UPath path, SearchOption searchOption, SearchPredicate searchPredicate)
{
throw new NotImplementedException();
}
protected override IFileSystemWatcher WatchImpl(UPath path)
{
throw new NotImplementedException();
}
protected override string ConvertPathToInternalImpl(UPath path)
{
throw new NotImplementedException();
}
protected override UPath ConvertPathFromInternalImpl(string innerPath)
{
throw new NotImplementedException();
}
}
}