ILuaFileManager.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. namespace Lua;
  2. public interface ILuaFileManager
  3. {
  4. public bool IsReadable(string path);
  5. public ValueTask<LuaFile> LoadModuleAsync(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 SystemFileManager : ILuaFileManager
  35. {
  36. public static readonly SystemFileManager 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<LuaFile> LoadModuleAsync(string path, CancellationToken cancellationToken)
  51. {
  52. var bytes = File.ReadAllBytes(path);
  53. return new(new LuaFile(bytes));
  54. }
  55. public IStream Open(string path, FileMode mode, FileAccess access)
  56. {
  57. return new SystemStream(File.Open(path, mode, access));
  58. }
  59. public ValueTask WriteAllTextAsync(string path, string text, CancellationToken cancellationToken)
  60. {
  61. File.WriteAllText(path, text);
  62. return new();
  63. }
  64. public void Rename(string oldName, string newName)
  65. {
  66. if (oldName == newName) return;
  67. File.Move(oldName, newName);
  68. File.Delete(oldName);
  69. }
  70. public void Remove(string path)
  71. {
  72. File.Delete(path);
  73. }
  74. }
  75. public sealed class SystemStreamReader(StreamReader streamReader) : IStreamReader
  76. {
  77. public ValueTask<string?> ReadLineAsync(CancellationToken cancellationToken)
  78. {
  79. return new(streamReader.ReadLine());
  80. }
  81. public ValueTask<string> ReadToEndAsync(CancellationToken cancellationToken)
  82. {
  83. return new(streamReader.ReadToEnd());
  84. }
  85. public ValueTask<int> ReadByteAsync(CancellationToken cancellationToken)
  86. {
  87. return new(streamReader.Read());
  88. }
  89. public void Dispose()
  90. {
  91. streamReader.Dispose();
  92. }
  93. }
  94. public sealed class SystemStreamWriter(StreamWriter streamWriter) : IStreamWriter
  95. {
  96. public ValueTask WriteAsync(ReadOnlyMemory<char> buffer, CancellationToken cancellationToken)
  97. {
  98. streamWriter.Write(buffer.Span);
  99. return new();
  100. }
  101. public ValueTask WriteLineAsync(ReadOnlyMemory<char> buffer, CancellationToken cancellationToken)
  102. {
  103. streamWriter.WriteLine(buffer.Span);
  104. return new();
  105. }
  106. public ValueTask FlushAsync(CancellationToken cancellationToken)
  107. {
  108. streamWriter.Flush();
  109. return new();
  110. }
  111. public void SetVBuf(string mode, int size)
  112. {
  113. // Ignore size parameter
  114. streamWriter.AutoFlush = mode is "no" or "line";
  115. }
  116. public void Dispose()
  117. {
  118. streamWriter.Dispose();
  119. }
  120. }
  121. public sealed class SystemStream(Stream fileStream) : IStream
  122. {
  123. public IStreamReader? Reader => fileStream.CanRead ? new SystemStreamReader(new(fileStream)) : null;
  124. public IStreamWriter? Writer => fileStream.CanWrite ? new SystemStreamWriter(new(fileStream)) : null;
  125. public long Seek(long offset, SeekOrigin origin) => fileStream.Seek(offset, origin);
  126. public void SetLength(long value) => fileStream.SetLength(value);
  127. public bool CanRead => fileStream.CanRead;
  128. public bool CanSeek => fileStream.CanSeek;
  129. public bool CanWrite => fileStream.CanWrite;
  130. public long Length => fileStream.Length;
  131. public long Position
  132. {
  133. get => fileStream.Position;
  134. set => fileStream.Position = value;
  135. }
  136. public void Dispose() => fileStream.Dispose();
  137. }