IStorageAccessor.cs 2.1 KB

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