IStorageAccessor.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 = new byte[size];
  37. context.Memory.Read((ulong)position, data);
  38. Buffer.BlockCopy(data, 0, _storage.Data, (int)writePosition, (int)size);
  39. }
  40. return ResultCode.Success;
  41. }
  42. [Command(11)]
  43. // Read(u64) -> buffer<bytes, 0x22>
  44. public ResultCode Read(ServiceCtx context)
  45. {
  46. long readPosition = context.RequestData.ReadInt64();
  47. if (readPosition > _storage.Data.Length)
  48. {
  49. return ResultCode.OutOfBounds;
  50. }
  51. (long position, long size) = context.Request.GetBufferType0x22();
  52. size = Math.Min(size, _storage.Data.Length - readPosition);
  53. byte[] data = new byte[size];
  54. Buffer.BlockCopy(_storage.Data, (int)readPosition, data, 0, (int)size);
  55. context.Memory.Write((ulong)position, data);
  56. return ResultCode.Success;
  57. }
  58. }
  59. }