ILuaStream.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. namespace Lua.IO
  2. {
  3. public interface ILuaStream : IDisposable
  4. {
  5. public LuaFileOpenMode Mode { get; }
  6. public ValueTask<string> ReadAllAsync(CancellationToken cancellationToken)
  7. {
  8. Mode.ThrowIfNotReadable();
  9. // Default implementation using ReadStringAsync
  10. throw new NotImplementedException($"ReadAllAsync must be implemented by {GetType().Name}");
  11. }
  12. public ValueTask<string?> ReadLineAsync(CancellationToken cancellationToken)
  13. {
  14. Mode.ThrowIfNotReadable();
  15. // Default implementation using ReadStringAsync
  16. throw new NotImplementedException($"ReadLineAsync must be implemented by {GetType().Name}");
  17. }
  18. public ValueTask<string?> ReadStringAsync(int count, CancellationToken cancellationToken)
  19. {
  20. Mode.ThrowIfNotReadable();
  21. // Default implementation using ReadAllAsync
  22. throw new NotImplementedException($"ReadStringAsync must be implemented by {GetType().Name}");
  23. }
  24. public ValueTask WriteAsync(ReadOnlyMemory<char> content, CancellationToken cancellationToken)
  25. {
  26. Mode.ThrowIfNotWritable();
  27. throw new NotImplementedException($"WriteAsync must be implemented by {GetType().Name}");
  28. }
  29. public ValueTask WriteAsync(string content, CancellationToken cancellationToken) => WriteAsync(content.AsMemory(), cancellationToken);
  30. public ValueTask FlushAsync(CancellationToken cancellationToken)
  31. {
  32. // Default implementation does nothing (no buffering)
  33. return default;
  34. }
  35. public void SetVBuf(LuaFileBufferingMode mode, int size)
  36. {
  37. // Default implementation does nothing (no configurable buffering)
  38. }
  39. public long Seek(long offset, SeekOrigin origin)
  40. {
  41. throw new NotSupportedException($"Seek is not supported by {GetType().Name}");
  42. }
  43. public static ILuaStream CreateStreamWrapper(Stream stream, LuaFileOpenMode openMode)
  44. {
  45. return new TextLuaStream(openMode, stream);
  46. }
  47. public static ILuaStream CreateFromFileString(string content)
  48. {
  49. return new StringStream(content);
  50. }
  51. public static ILuaStream CreateFromMemory(ReadOnlyMemory<char> content)
  52. {
  53. return new CharMemoryStream(content);
  54. }
  55. public void Close()
  56. {
  57. Dispose();
  58. }
  59. }
  60. }