IFile.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using LibHac;
  2. using LibHac.Fs;
  3. using System;
  4. namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy
  5. {
  6. class IFile : IpcService, IDisposable
  7. {
  8. private LibHac.Fs.IFile _baseFile;
  9. public IFile(LibHac.Fs.IFile baseFile)
  10. {
  11. _baseFile = baseFile;
  12. }
  13. [Command(0)]
  14. // Read(u32 readOption, u64 offset, u64 size) -> (u64 out_size, buffer<u8, 0x46, 0> out_buf)
  15. public ResultCode Read(ServiceCtx context)
  16. {
  17. long position = context.Request.ReceiveBuff[0].Position;
  18. ReadOption readOption = (ReadOption)context.RequestData.ReadInt32();
  19. context.RequestData.BaseStream.Position += 4;
  20. long offset = context.RequestData.ReadInt64();
  21. long size = context.RequestData.ReadInt64();
  22. byte[] data = new byte[size];
  23. int readSize;
  24. try
  25. {
  26. readSize = _baseFile.Read(data, offset, readOption);
  27. }
  28. catch (HorizonResultException ex)
  29. {
  30. return (ResultCode)ex.ResultValue.Value;
  31. }
  32. context.Memory.WriteBytes(position, data);
  33. context.ResponseData.Write((long)readSize);
  34. return ResultCode.Success;
  35. }
  36. [Command(1)]
  37. // Write(u32 writeOption, u64 offset, u64 size, buffer<u8, 0x45, 0>)
  38. public ResultCode Write(ServiceCtx context)
  39. {
  40. long position = context.Request.SendBuff[0].Position;
  41. WriteOption writeOption = (WriteOption)context.RequestData.ReadInt32();
  42. context.RequestData.BaseStream.Position += 4;
  43. long offset = context.RequestData.ReadInt64();
  44. long size = context.RequestData.ReadInt64();
  45. byte[] data = context.Memory.ReadBytes(position, size);
  46. try
  47. {
  48. _baseFile.Write(data, offset, writeOption);
  49. }
  50. catch (HorizonResultException ex)
  51. {
  52. return (ResultCode)ex.ResultValue.Value;
  53. }
  54. return ResultCode.Success;
  55. }
  56. [Command(2)]
  57. // Flush()
  58. public ResultCode Flush(ServiceCtx context)
  59. {
  60. try
  61. {
  62. _baseFile.Flush();
  63. }
  64. catch (HorizonResultException ex)
  65. {
  66. return (ResultCode)ex.ResultValue.Value;
  67. }
  68. return ResultCode.Success;
  69. }
  70. [Command(3)]
  71. // SetSize(u64 size)
  72. public ResultCode SetSize(ServiceCtx context)
  73. {
  74. try
  75. {
  76. long size = context.RequestData.ReadInt64();
  77. _baseFile.SetSize(size);
  78. }
  79. catch (HorizonResultException ex)
  80. {
  81. return (ResultCode)ex.ResultValue.Value;
  82. }
  83. return ResultCode.Success;
  84. }
  85. [Command(4)]
  86. // GetSize() -> u64 fileSize
  87. public ResultCode GetSize(ServiceCtx context)
  88. {
  89. try
  90. {
  91. context.ResponseData.Write(_baseFile.GetSize());
  92. }
  93. catch (HorizonResultException ex)
  94. {
  95. return (ResultCode)ex.ResultValue.Value;
  96. }
  97. return ResultCode.Success;
  98. }
  99. public void Dispose()
  100. {
  101. Dispose(true);
  102. }
  103. protected virtual void Dispose(bool disposing)
  104. {
  105. if (disposing)
  106. {
  107. _baseFile?.Dispose();
  108. }
  109. }
  110. }
  111. }