123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using Godot;
- using TBL.GodotSharp.IO.File;
- using Zio;
- namespace TBL.GodotSharp.Content.FileSystem
- {
- /// <summary>
- /// Godot 文件系统
- /// </summary>
- public class GodotFileSystem : Zio.FileSystems.FileSystem
- {
- /// <summary>
- /// 根目录
- /// </summary>
- private readonly string _rootPath;
- /// <summary>
- /// 目录工具
- /// </summary>
- 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<UPath> 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<FileSystemItem> 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();
- }
- }
- }
|