IStorageAccessor.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. namespace Ryujinx.HLE.HOS.Services.Am
  3. {
  4. class IStorageAccessor : IpcService
  5. {
  6. private IStorage _storage;
  7. public IStorageAccessor(IStorage storage)
  8. {
  9. _storage = storage;
  10. }
  11. [Command(0)]
  12. // GetSize() -> u64
  13. public ResultCode GetSize(ServiceCtx context)
  14. {
  15. context.ResponseData.Write((long)_storage.Data.Length);
  16. return ResultCode.Success;
  17. }
  18. [Command(10)]
  19. // Write(u64, buffer<bytes, 0x21>)
  20. public ResultCode Write(ServiceCtx context)
  21. {
  22. // TODO: Error conditions.
  23. long writePosition = context.RequestData.ReadInt64();
  24. (long position, long size) = context.Request.GetBufferType0x21();
  25. if (size > 0)
  26. {
  27. long maxSize = _storage.Data.Length - writePosition;
  28. if (size > maxSize)
  29. {
  30. size = maxSize;
  31. }
  32. byte[] data = context.Memory.ReadBytes(position, size);
  33. Buffer.BlockCopy(data, 0, _storage.Data, (int)writePosition, (int)size);
  34. }
  35. return ResultCode.Success;
  36. }
  37. [Command(11)]
  38. // Read(u64) -> buffer<bytes, 0x22>
  39. public ResultCode Read(ServiceCtx context)
  40. {
  41. // TODO: Error conditions.
  42. long readPosition = context.RequestData.ReadInt64();
  43. (long position, long size) = context.Request.GetBufferType0x22();
  44. byte[] data;
  45. if (_storage.Data.Length > size)
  46. {
  47. data = new byte[size];
  48. Buffer.BlockCopy(_storage.Data, 0, data, 0, (int)size);
  49. }
  50. else
  51. {
  52. data = _storage.Data;
  53. }
  54. context.Memory.WriteBytes(position, data);
  55. return ResultCode.Success;
  56. }
  57. }
  58. }