IFile.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. Result result = _baseFile.Read(out long bytesRead, offset, data, readOption);
  24. context.Memory.WriteBytes(position, data);
  25. context.ResponseData.Write(bytesRead);
  26. return (ResultCode)result.Value;
  27. }
  28. [Command(1)]
  29. // Write(u32 writeOption, u64 offset, u64 size, buffer<u8, 0x45, 0>)
  30. public ResultCode Write(ServiceCtx context)
  31. {
  32. long position = context.Request.SendBuff[0].Position;
  33. WriteOption writeOption = (WriteOption)context.RequestData.ReadInt32();
  34. context.RequestData.BaseStream.Position += 4;
  35. long offset = context.RequestData.ReadInt64();
  36. long size = context.RequestData.ReadInt64();
  37. byte[] data = context.Memory.ReadBytes(position, size);
  38. return (ResultCode)_baseFile.Write(offset, data, writeOption).Value;
  39. }
  40. [Command(2)]
  41. // Flush()
  42. public ResultCode Flush(ServiceCtx context)
  43. {
  44. return (ResultCode)_baseFile.Flush().Value;
  45. }
  46. [Command(3)]
  47. // SetSize(u64 size)
  48. public ResultCode SetSize(ServiceCtx context)
  49. {
  50. long size = context.RequestData.ReadInt64();
  51. return (ResultCode)_baseFile.SetSize(size).Value;
  52. }
  53. [Command(4)]
  54. // GetSize() -> u64 fileSize
  55. public ResultCode GetSize(ServiceCtx context)
  56. {
  57. Result result = _baseFile.GetSize(out long size);
  58. context.ResponseData.Write(size);
  59. return (ResultCode)result.Value;
  60. }
  61. public void Dispose()
  62. {
  63. Dispose(true);
  64. }
  65. protected virtual void Dispose(bool disposing)
  66. {
  67. if (disposing)
  68. {
  69. _baseFile?.Dispose();
  70. }
  71. }
  72. }
  73. }