IStorageAccessor.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using Ryujinx.HLE.OsHle.Ipc;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace Ryujinx.HLE.OsHle.Services.Am
  5. {
  6. class IStorageAccessor : IpcService
  7. {
  8. private Dictionary<int, ServiceProcessRequest> m_Commands;
  9. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
  10. private IStorage Storage;
  11. public IStorageAccessor(IStorage Storage)
  12. {
  13. m_Commands = new Dictionary<int, ServiceProcessRequest>()
  14. {
  15. { 0, GetSize },
  16. { 10, Write },
  17. { 11, Read }
  18. };
  19. this.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. }