ILuaFileSystem.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. namespace Lua.IO;
  2. public interface ILuaFileSystem
  3. {
  4. public bool IsReadable(string path);
  5. public ValueTask<LuaFileContent> ReadFileContentAsync(string path, CancellationToken cancellationToken);
  6. public IStream Open(string path, FileMode mode, FileAccess access);
  7. public void Rename(string oldName, string newName);
  8. public void Remove(string path);
  9. }
  10. public interface IStream : IDisposable
  11. {
  12. public IStreamReader? Reader { get; }
  13. public IStreamWriter? Writer { get; }
  14. public long Seek(long offset, SeekOrigin origin);
  15. public void SetLength(long value);
  16. public bool CanRead { get; }
  17. public bool CanSeek { get; }
  18. public bool CanWrite { get; }
  19. public long Length { get; }
  20. public long Position { get; set; }
  21. }
  22. public interface IStreamReader : IDisposable
  23. {
  24. public ValueTask<string?> ReadLineAsync(CancellationToken cancellationToken);
  25. public ValueTask<string> ReadToEndAsync(CancellationToken cancellationToken);
  26. public ValueTask<int> ReadByteAsync(CancellationToken cancellationToken);
  27. }
  28. public interface IStreamWriter : IDisposable
  29. {
  30. public ValueTask WriteAsync(ReadOnlyMemory<char> buffer, CancellationToken cancellationToken);
  31. public ValueTask FlushAsync(CancellationToken cancellationToken);
  32. public void SetVBuf(string mode, int size);
  33. }
  34. public sealed class FileSystem : ILuaFileSystem
  35. {
  36. public static readonly FileSystem Instance = new();
  37. public bool IsReadable(string path)
  38. {
  39. if (!File.Exists(path)) return false;
  40. try
  41. {
  42. File.Open(path, FileMode.Open, FileAccess.Read).Dispose();
  43. return true;
  44. }
  45. catch (Exception)
  46. {
  47. return false;
  48. }
  49. }
  50. public ValueTask<LuaFileContent> ReadFileContentAsync(string path, CancellationToken cancellationToken)
  51. {
  52. var bytes = File.ReadAllBytes(path);
  53. return new(new LuaFileContent(bytes));
  54. }
  55. public IStream Open(string path, FileMode mode, FileAccess access)
  56. {
  57. return new StreamWrapper(File.Open(path, mode, access));
  58. }
  59. public void Rename(string oldName, string newName)
  60. {
  61. if (oldName == newName) return;
  62. File.Move(oldName, newName);
  63. File.Delete(oldName);
  64. }
  65. public void Remove(string path)
  66. {
  67. File.Delete(path);
  68. }
  69. }
  70. public sealed class StreamReaderWrapper(StreamReader streamReader) : IStreamReader
  71. {
  72. public ValueTask<string?> ReadLineAsync(CancellationToken cancellationToken)
  73. {
  74. return new(streamReader.ReadLine());
  75. }
  76. public ValueTask<string> ReadToEndAsync(CancellationToken cancellationToken)
  77. {
  78. return new(streamReader.ReadToEnd());
  79. }
  80. public ValueTask<int> ReadByteAsync(CancellationToken cancellationToken)
  81. {
  82. return new(streamReader.Read());
  83. }
  84. public void Dispose()
  85. {
  86. streamReader.Dispose();
  87. }
  88. }
  89. public sealed class StreamWriterWrapper(StreamWriter streamWriter) : IStreamWriter
  90. {
  91. public ValueTask WriteAsync(ReadOnlyMemory<char> buffer, CancellationToken cancellationToken)
  92. {
  93. streamWriter.Write(buffer.Span);
  94. return new();
  95. }
  96. public ValueTask FlushAsync(CancellationToken cancellationToken)
  97. {
  98. streamWriter.Flush();
  99. return new();
  100. }
  101. public void SetVBuf(string mode, int size)
  102. {
  103. // Ignore size parameter
  104. streamWriter.AutoFlush = mode is "no" or "line";
  105. }
  106. public void Dispose()
  107. {
  108. streamWriter.Dispose();
  109. }
  110. }
  111. public sealed class StreamWrapper(Stream fileStream) : IStream
  112. {
  113. public IStreamReader? Reader => fileStream.CanRead ? new StreamReaderWrapper(new(fileStream)) : null;
  114. public IStreamWriter? Writer => fileStream.CanWrite ? new StreamWriterWrapper(new(fileStream)) : null;
  115. public long Seek(long offset, SeekOrigin origin) => fileStream.Seek(offset, origin);
  116. public void SetLength(long value) => fileStream.SetLength(value);
  117. public bool CanRead => fileStream.CanRead;
  118. public bool CanSeek => fileStream.CanSeek;
  119. public bool CanWrite => fileStream.CanWrite;
  120. public long Length => fileStream.Length;
  121. public long Position
  122. {
  123. get => fileStream.Position;
  124. set => fileStream.Position = value;
  125. }
  126. public void Dispose() => fileStream.Dispose();
  127. }