FspSrvIFile.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using ChocolArm64.Memory;
  2. using System;
  3. using System.IO;
  4. namespace Ryujinx.OsHle.Objects
  5. {
  6. class FspSrvIFile : IDisposable
  7. {
  8. public Stream BaseStream { get; private set; }
  9. public FspSrvIFile(Stream BaseStream)
  10. {
  11. this.BaseStream = BaseStream;
  12. }
  13. public static long Read(ServiceCtx Context)
  14. {
  15. FspSrvIFile File = Context.GetObject<FspSrvIFile>();
  16. long Position = Context.Request.ReceiveBuff[0].Position;
  17. long Zero = Context.RequestData.ReadInt64();
  18. long Offset = Context.RequestData.ReadInt64();
  19. long Size = Context.RequestData.ReadInt64();
  20. byte[] Data = new byte[Size];
  21. int ReadSize = File.BaseStream.Read(Data, 0, (int)Size);
  22. AMemoryHelper.WriteBytes(Context.Memory, Position, Data);
  23. //TODO: Use ReadSize, we need to return the size that was REALLY read from the file.
  24. //This is a workaround because we are doing something wrong and the game expects to read
  25. //data from a file that doesn't yet exists -- and breaks if it can't read anything.
  26. Context.ResponseData.Write((long)Size);
  27. return 0;
  28. }
  29. public static long Write(ServiceCtx Context)
  30. {
  31. FspSrvIFile File = Context.GetObject<FspSrvIFile>();
  32. long Position = Context.Request.SendBuff[0].Position;
  33. long Zero = Context.RequestData.ReadInt64();
  34. long Offset = Context.RequestData.ReadInt64();
  35. long Size = Context.RequestData.ReadInt64();
  36. byte[] Data = AMemoryHelper.ReadBytes(Context.Memory, Position, (int)Size);
  37. File.BaseStream.Seek(Offset, SeekOrigin.Begin);
  38. File.BaseStream.Write(Data, 0, (int)Size);
  39. return 0;
  40. }
  41. public void Dispose()
  42. {
  43. Dispose(true);
  44. }
  45. protected virtual void Dispose(bool disposing)
  46. {
  47. if (disposing && BaseStream != null)
  48. {
  49. BaseStream.Dispose();
  50. }
  51. }
  52. }
  53. }