GodotFileSystem.cs 6.1 KB

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