IStorageAccessor.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. [CommandHipc(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. [CommandHipc(10)]
  19. // Write(u64, buffer<bytes, 0x21>)
  20. public ResultCode Write(ServiceCtx context)
  21. {
  22. if (_storage.IsReadOnly)
  23. {
  24. return ResultCode.ObjectInvalid;
  25. }
  26. ulong writePosition = context.RequestData.ReadUInt64();
  27. if (writePosition > (ulong)_storage.Data.Length)
  28. {
  29. return ResultCode.OutOfBounds;
  30. }
  31. (ulong position, ulong size) = context.Request.GetBufferType0x21();
  32. size = Math.Min(size, (ulong)_storage.Data.Length - writePosition);
  33. if (size > 0)
  34. {
  35. ulong maxSize = (ulong)_storage.Data.Length - writePosition;
  36. if (size > maxSize)
  37. {
  38. size = maxSize;
  39. }
  40. byte[] data = new byte[size];
  41. context.Memory.Read(position, data);
  42. Buffer.BlockCopy(data, 0, _storage.Data, (int)writePosition, (int)size);
  43. }
  44. return ResultCode.Success;
  45. }
  46. [CommandHipc(11)]
  47. // Read(u64) -> buffer<bytes, 0x22>
  48. public ResultCode Read(ServiceCtx context)
  49. {
  50. ulong readPosition = context.RequestData.ReadUInt64();
  51. if (readPosition > (ulong)_storage.Data.Length)
  52. {
  53. return ResultCode.OutOfBounds;
  54. }
  55. (ulong position, ulong size) = context.Request.GetBufferType0x22();
  56. size = Math.Min(size, (ulong)_storage.Data.Length - readPosition);
  57. byte[] data = new byte[size];
  58. Buffer.BlockCopy(_storage.Data, (int)readPosition, data, 0, (int)size);
  59. context.Memory.Write(position, data);
  60. return ResultCode.Success;
  61. }
  62. }
  63. }