IFile.cs 2.8 KB

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