ILuaFileSystem.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using Lua.Internal;
  2. namespace Lua.IO;
  3. public interface ILuaFileSystem
  4. {
  5. public bool IsReadable(string path);
  6. public ValueTask<LuaFileContent> ReadFileContentAsync(string path, CancellationToken cancellationToken);
  7. public ILuaIOStream? Open(string path, LuaFileOpenMode mode, bool throwError);
  8. public void Rename(string oldName, string newName);
  9. public void Remove(string path);
  10. public string DirectorySeparator { get; }
  11. public string GetTempFileName();
  12. public ILuaIOStream OpenTempFileStream();
  13. }
  14. public interface ILuaIOStream : IDisposable
  15. {
  16. public ValueTask<string?> ReadLineAsync(CancellationToken cancellationToken);
  17. public ValueTask<string> ReadToEndAsync(CancellationToken cancellationToken);
  18. public ValueTask<string?> ReadStringAsync(int count, CancellationToken cancellationToken);
  19. public ValueTask WriteAsync(ReadOnlyMemory<char> buffer, CancellationToken cancellationToken);
  20. public ValueTask FlushAsync(CancellationToken cancellationToken);
  21. public void SetVBuf(LuaFileBufferingMode mode, int size);
  22. public long Seek(long offset, SeekOrigin origin);
  23. }
  24. public sealed class FileSystem : ILuaFileSystem
  25. {
  26. public static readonly FileSystem Instance = new();
  27. public static (FileMode, FileAccess access) GetFileMode(LuaFileOpenMode luaFileOpenMode)
  28. {
  29. return luaFileOpenMode switch
  30. {
  31. LuaFileOpenMode.Read => (FileMode.Open, FileAccess.Read),
  32. LuaFileOpenMode.Write => (FileMode.Create, FileAccess.Write),
  33. LuaFileOpenMode.Append => (FileMode.Append, FileAccess.Write),
  34. LuaFileOpenMode.ReadWriteOpen => (FileMode.Open, FileAccess.ReadWrite),
  35. LuaFileOpenMode.ReadWriteCreate => (FileMode.Create, FileAccess.ReadWrite),
  36. LuaFileOpenMode.ReadAppend => (FileMode.Append, FileAccess.ReadWrite),
  37. _ => throw new ArgumentOutOfRangeException(nameof(luaFileOpenMode), luaFileOpenMode, null)
  38. };
  39. }
  40. public bool IsReadable(string path)
  41. {
  42. if (!File.Exists(path)) return false;
  43. try
  44. {
  45. File.Open(path, FileMode.Open, FileAccess.Read).Dispose();
  46. return true;
  47. }
  48. catch (Exception)
  49. {
  50. return false;
  51. }
  52. }
  53. public ValueTask<LuaFileContent> ReadFileContentAsync(string path, CancellationToken cancellationToken)
  54. {
  55. var bytes = File.ReadAllBytes(path);
  56. return new(new LuaFileContent(bytes));
  57. }
  58. public ILuaIOStream? Open(string path, LuaFileOpenMode luaMode, bool throwError)
  59. {
  60. if (luaMode == LuaFileOpenMode.ReadAppend)
  61. {
  62. throw new NotSupportedException("a+ mode is not supported.");
  63. }
  64. var (mode, access) = GetFileMode(luaMode);
  65. try
  66. {
  67. return new LuaIOStreamWrapper(File.Open(path, mode, access));
  68. }
  69. catch (Exception)
  70. {
  71. if (throwError)
  72. {
  73. throw;
  74. }
  75. return null;
  76. }
  77. }
  78. public void Rename(string oldName, string newName)
  79. {
  80. if (oldName == newName) return;
  81. File.Move(oldName, newName);
  82. File.Delete(oldName);
  83. }
  84. public void Remove(string path)
  85. {
  86. File.Delete(path);
  87. }
  88. static readonly string directorySeparator = Path.DirectorySeparatorChar.ToString();
  89. public string DirectorySeparator => directorySeparator;
  90. public string GetTempFileName()
  91. {
  92. return Path.GetTempFileName();
  93. }
  94. public ILuaIOStream OpenTempFileStream()
  95. {
  96. return new LuaIOStreamWrapper(File.Open(Path.GetTempFileName(), FileMode.OpenOrCreate, FileAccess.ReadWrite));
  97. }
  98. }
  99. public sealed class LuaIOStreamWrapper(Stream innerStream) : ILuaIOStream
  100. {
  101. StreamReader? reader;
  102. StreamWriter? writer;
  103. public ValueTask<string?> ReadLineAsync(CancellationToken cancellationToken)
  104. {
  105. ThrowIfNotReadable();
  106. reader ??= new(innerStream);
  107. return new(reader.ReadLine());
  108. }
  109. public ValueTask<string> ReadToEndAsync(CancellationToken cancellationToken)
  110. {
  111. ThrowIfNotReadable();
  112. reader ??= new(innerStream);
  113. return new(reader.ReadToEnd());
  114. }
  115. public ValueTask<string?> ReadStringAsync(int count, CancellationToken cancellationToken)
  116. {
  117. ThrowIfNotReadable();
  118. reader ??= new(innerStream);
  119. using var byteBuffer = new PooledArray<char>(count);
  120. var span = byteBuffer.AsSpan();
  121. var ret = reader.Read(span);
  122. if (ret != span.Length)
  123. {
  124. return new(default(string));
  125. }
  126. return new(span.ToString());
  127. }
  128. public ValueTask WriteAsync(ReadOnlyMemory<char> buffer, CancellationToken cancellationToken)
  129. {
  130. ThrowIfNotWritable();
  131. writer ??= new(innerStream);
  132. writer.Write(buffer.Span);
  133. return new();
  134. }
  135. public ValueTask FlushAsync(CancellationToken cancellationToken)
  136. {
  137. ThrowIfNotWritable();
  138. writer?.Flush();
  139. return new();
  140. }
  141. public void SetVBuf(LuaFileBufferingMode mode, int size)
  142. {
  143. writer ??= new(innerStream);
  144. // Ignore size parameter
  145. writer.AutoFlush = mode is LuaFileBufferingMode.NoBuffering or LuaFileBufferingMode.LineBuffering;
  146. }
  147. public long Seek(long offset, SeekOrigin origin) => innerStream.Seek(offset, origin);
  148. public bool CanRead => innerStream.CanRead;
  149. public bool CanSeek => innerStream.CanSeek;
  150. public bool CanWrite => innerStream.CanWrite;
  151. void ThrowIfNotReadable()
  152. {
  153. if (!innerStream.CanRead)
  154. {
  155. throw new IOException("Stream is not readable.");
  156. }
  157. }
  158. void ThrowIfNotWritable()
  159. {
  160. if (!innerStream.CanWrite)
  161. {
  162. throw new IOException("Stream is not writable.");
  163. }
  164. }
  165. public void Dispose() => innerStream.Dispose();
  166. }