IStorageAccessor.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. namespace Ryujinx.HLE.HOS.Services.Am.AppletAE
  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. long writePosition = context.RequestData.ReadInt64();
  23. if (writePosition > _storage.Data.Length)
  24. {
  25. return ResultCode.OutOfBounds;
  26. }
  27. (long position, long size) = context.Request.GetBufferType0x21();
  28. size = Math.Min(size, _storage.Data.Length - writePosition);
  29. if (size > 0)
  30. {
  31. long maxSize = _storage.Data.Length - writePosition;
  32. if (size > maxSize)
  33. {
  34. size = maxSize;
  35. }
  36. byte[] data = context.Memory.ReadBytes(position, size);
  37. Buffer.BlockCopy(data, 0, _storage.Data, (int)writePosition, (int)size);
  38. }
  39. return ResultCode.Success;
  40. }
  41. [Command(11)]
  42. // Read(u64) -> buffer<bytes, 0x22>
  43. public ResultCode Read(ServiceCtx context)
  44. {
  45. long readPosition = context.RequestData.ReadInt64();
  46. if (readPosition > _storage.Data.Length)
  47. {
  48. return ResultCode.OutOfBounds;
  49. }
  50. (long position, long size) = context.Request.GetBufferType0x22();
  51. size = Math.Min(size, _storage.Data.Length - readPosition);
  52. byte[] data = new byte[size];
  53. Buffer.BlockCopy(_storage.Data, (int)readPosition, data, 0, (int)size);
  54. context.Memory.WriteBytes(position, data);
  55. return ResultCode.Success;
  56. }
  57. }
  58. }