IFile.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using Ryujinx.HLE.OsHle.Ipc;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. namespace Ryujinx.HLE.OsHle.Services.FspSrv
  6. {
  7. class IFile : IpcService, IDisposable
  8. {
  9. private Dictionary<int, ServiceProcessRequest> m_Commands;
  10. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
  11. private Stream BaseStream;
  12. public event EventHandler<EventArgs> Disposed;
  13. public string HostPath { get; private set; }
  14. public IFile(Stream BaseStream, string HostPath)
  15. {
  16. m_Commands = new Dictionary<int, ServiceProcessRequest>()
  17. {
  18. { 0, Read },
  19. { 1, Write },
  20. { 2, Flush },
  21. { 3, SetSize },
  22. { 4, GetSize }
  23. };
  24. this.BaseStream = BaseStream;
  25. this.HostPath = HostPath;
  26. }
  27. public long Read(ServiceCtx Context)
  28. {
  29. long Position = Context.Request.ReceiveBuff[0].Position;
  30. long Zero = Context.RequestData.ReadInt64();
  31. long Offset = Context.RequestData.ReadInt64();
  32. long Size = Context.RequestData.ReadInt64();
  33. byte[] Data = new byte[Size];
  34. BaseStream.Seek(Offset, SeekOrigin.Begin);
  35. int ReadSize = BaseStream.Read(Data, 0, (int)Size);
  36. Context.Memory.WriteBytes(Position, Data);
  37. Context.ResponseData.Write((long)ReadSize);
  38. return 0;
  39. }
  40. public long Write(ServiceCtx Context)
  41. {
  42. long Position = Context.Request.SendBuff[0].Position;
  43. long Zero = Context.RequestData.ReadInt64();
  44. long Offset = Context.RequestData.ReadInt64();
  45. long Size = Context.RequestData.ReadInt64();
  46. byte[] Data = Context.Memory.ReadBytes(Position, Size);
  47. BaseStream.Seek(Offset, SeekOrigin.Begin);
  48. BaseStream.Write(Data, 0, (int)Size);
  49. return 0;
  50. }
  51. public long Flush(ServiceCtx Context)
  52. {
  53. BaseStream.Flush();
  54. return 0;
  55. }
  56. public long SetSize(ServiceCtx Context)
  57. {
  58. long Size = Context.RequestData.ReadInt64();
  59. BaseStream.SetLength(Size);
  60. return 0;
  61. }
  62. public long GetSize(ServiceCtx Context)
  63. {
  64. Context.ResponseData.Write(BaseStream.Length);
  65. return 0;
  66. }
  67. public void Dispose()
  68. {
  69. Dispose(true);
  70. }
  71. protected virtual void Dispose(bool disposing)
  72. {
  73. if (disposing && BaseStream != null)
  74. {
  75. BaseStream.Dispose();
  76. Disposed?.Invoke(this, EventArgs.Empty);
  77. }
  78. }
  79. }
  80. }