GodotFileSystem.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Godot;
  5. using TBL.GodotSharp.IO.File;
  6. using Zio;
  7. namespace TBL.GodotSharp.Content.FileSystem;
  8. /// <summary>
  9. /// Godot 文件系统
  10. /// </summary>
  11. public class GodotFileSystem : Zio.FileSystems.FileSystem
  12. {
  13. /// <summary>
  14. /// 根目录
  15. /// </summary>
  16. private readonly string _rootPath;
  17. /// <summary>
  18. /// 目录工具
  19. /// </summary>
  20. private readonly Godot.Directory _directoryTool;
  21. public GodotFileSystem(string rootPath)
  22. {
  23. _rootPath = rootPath;
  24. _directoryTool = new Godot.Directory();
  25. }
  26. private string GetFullPath(UPath uPath) => $"{_rootPath}{uPath}";
  27. protected override void CreateDirectoryImpl(UPath path)
  28. {
  29. _directoryTool.MakeDirRecursive(GetFullPath(path));
  30. }
  31. protected override bool DirectoryExistsImpl(UPath path) => _directoryTool.DirExists(GetFullPath(path));
  32. protected override bool FileExistsImpl(UPath path) => _directoryTool.FileExists(GetFullPath(path));
  33. protected override Stream OpenFileImpl(UPath path, FileMode mode, FileAccess access, FileShare share)
  34. {
  35. if (mode == FileMode.Open && access == FileAccess.Read)
  36. {
  37. return new GodotFileStream(GetFullPath(path), Godot.File.ModeFlags.Read);
  38. }
  39. throw new FileNotFoundException();
  40. }
  41. protected override IEnumerable<UPath> EnumeratePathsImpl(
  42. UPath path,
  43. string searchPattern,
  44. SearchOption searchOption,
  45. SearchTarget searchTarget
  46. )
  47. {
  48. var directoryTool = new Godot.Directory();
  49. var err = directoryTool.Open(GetFullPath(path));
  50. if (err != Error.Ok)
  51. throw new IOException($"{err}");
  52. directoryTool.ListDirBegin();
  53. for (var current = directoryTool.GetNext();; current = directoryTool.GetNext())
  54. {
  55. if (string.IsNullOrEmpty(current))
  56. break;
  57. if (current is ".." or ".")
  58. continue;
  59. if ((searchTarget == SearchTarget.Both || searchTarget == SearchTarget.File) &&
  60. !directoryTool.CurrentIsDir())
  61. yield return System.IO.Path.Combine(path.FullName, current);
  62. if ((searchTarget == SearchTarget.Both || searchTarget == SearchTarget.Directory) &&
  63. directoryTool.CurrentIsDir())
  64. yield return System.IO.Path.Combine(path.FullName, current);
  65. if (directoryTool.CurrentIsDir() && searchOption == SearchOption.AllDirectories)
  66. {
  67. foreach (var sub in EnumeratePathsImpl(System.IO.Path.Combine(path.FullName, current),
  68. searchPattern, searchOption, searchTarget))
  69. {
  70. yield return sub;
  71. }
  72. }
  73. }
  74. directoryTool.ListDirEnd();
  75. }
  76. protected override void MoveDirectoryImpl(UPath srcPath, UPath destPath)
  77. {
  78. throw new NotImplementedException();
  79. }
  80. protected override void DeleteDirectoryImpl(UPath path, bool isRecursive)
  81. {
  82. throw new NotImplementedException();
  83. }
  84. protected override void CopyFileImpl(UPath srcPath, UPath destPath, bool overwrite)
  85. {
  86. throw new NotImplementedException();
  87. }
  88. protected override void ReplaceFileImpl(UPath srcPath, UPath destPath, UPath destBackupPath, bool ignoreMetadataErrors)
  89. {
  90. throw new NotImplementedException();
  91. }
  92. protected override long GetFileLengthImpl(UPath path)
  93. {
  94. throw new NotImplementedException();
  95. }
  96. protected override void MoveFileImpl(UPath srcPath, UPath destPath)
  97. {
  98. throw new NotImplementedException();
  99. }
  100. protected override void DeleteFileImpl(UPath path)
  101. {
  102. throw new NotImplementedException();
  103. }
  104. protected override FileAttributes GetAttributesImpl(UPath path)
  105. {
  106. throw new NotImplementedException();
  107. }
  108. protected override void SetAttributesImpl(UPath path, FileAttributes attributes)
  109. {
  110. throw new NotImplementedException();
  111. }
  112. protected override DateTime GetCreationTimeImpl(UPath path)
  113. {
  114. throw new NotImplementedException();
  115. }
  116. protected override void SetCreationTimeImpl(UPath path, DateTime time)
  117. {
  118. throw new NotImplementedException();
  119. }
  120. protected override DateTime GetLastAccessTimeImpl(UPath path)
  121. {
  122. throw new NotImplementedException();
  123. }
  124. protected override void SetLastAccessTimeImpl(UPath path, DateTime time)
  125. {
  126. throw new NotImplementedException();
  127. }
  128. protected override DateTime GetLastWriteTimeImpl(UPath path)
  129. {
  130. throw new NotImplementedException();
  131. }
  132. protected override void SetLastWriteTimeImpl(UPath path, DateTime time)
  133. {
  134. throw new NotImplementedException();
  135. }
  136. protected override IEnumerable<FileSystemItem> EnumerateItemsImpl(UPath path, SearchOption searchOption, SearchPredicate searchPredicate)
  137. {
  138. throw new NotImplementedException();
  139. }
  140. protected override IFileSystemWatcher WatchImpl(UPath path)
  141. {
  142. throw new NotImplementedException();
  143. }
  144. protected override string ConvertPathToInternalImpl(UPath path)
  145. {
  146. throw new NotImplementedException();
  147. }
  148. protected override UPath ConvertPathFromInternalImpl(string innerPath)
  149. {
  150. throw new NotImplementedException();
  151. }
  152. }