CaptureManager.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using Ryujinx.Common.Memory;
  2. using Ryujinx.HLE.HOS.Services.Caps.Types;
  3. using SixLabors.ImageSharp;
  4. using SixLabors.ImageSharp.PixelFormats;
  5. using System;
  6. using System.IO;
  7. using System.Runtime.CompilerServices;
  8. using System.Security.Cryptography;
  9. namespace Ryujinx.HLE.HOS.Services.Caps
  10. {
  11. class CaptureManager
  12. {
  13. private string _sdCardPath;
  14. private uint _shimLibraryVersion;
  15. public CaptureManager(Switch device)
  16. {
  17. _sdCardPath = device.FileSystem.GetSdCardPath();
  18. }
  19. public ResultCode SetShimLibraryVersion(ServiceCtx context)
  20. {
  21. ulong shimLibraryVersion = context.RequestData.ReadUInt64();
  22. ulong appletResourceUserId = context.RequestData.ReadUInt64();
  23. // TODO: Service checks if the pid is present in an internal list and returns ResultCode.BlacklistedPid if it is.
  24. // The list contents needs to be determined.
  25. ResultCode resultCode = ResultCode.OutOfRange;
  26. if (shimLibraryVersion != 0)
  27. {
  28. if (_shimLibraryVersion == shimLibraryVersion)
  29. {
  30. resultCode = ResultCode.Success;
  31. }
  32. else if (_shimLibraryVersion != 0)
  33. {
  34. resultCode = ResultCode.ShimLibraryVersionAlreadySet;
  35. }
  36. else if (shimLibraryVersion == 1)
  37. {
  38. resultCode = ResultCode.Success;
  39. _shimLibraryVersion = 1;
  40. }
  41. }
  42. return resultCode;
  43. }
  44. public ResultCode SaveScreenShot(byte[] screenshotData, ulong appletResourceUserId, ulong titleId, out ApplicationAlbumEntry applicationAlbumEntry)
  45. {
  46. applicationAlbumEntry = default;
  47. if (screenshotData.Length == 0)
  48. {
  49. return ResultCode.NullInputBuffer;
  50. }
  51. /*
  52. // NOTE: On our current implementation, appletResourceUserId starts at 0, disable it for now.
  53. if (appletResourceUserId == 0)
  54. {
  55. return ResultCode.InvalidArgument;
  56. }
  57. */
  58. /*
  59. // Doesn't occur in our case.
  60. if (applicationAlbumEntry == null)
  61. {
  62. return ResultCode.NullOutputBuffer;
  63. }
  64. */
  65. if (screenshotData.Length >= 0x384000)
  66. {
  67. DateTime currentDateTime = DateTime.Now;
  68. applicationAlbumEntry = new ApplicationAlbumEntry()
  69. {
  70. Size = (ulong)Unsafe.SizeOf<ApplicationAlbumEntry>(),
  71. TitleId = titleId,
  72. AlbumFileDateTime = new AlbumFileDateTime()
  73. {
  74. Year = (ushort)currentDateTime.Year,
  75. Month = (byte)currentDateTime.Month,
  76. Day = (byte)currentDateTime.Day,
  77. Hour = (byte)currentDateTime.Hour,
  78. Minute = (byte)currentDateTime.Minute,
  79. Second = (byte)currentDateTime.Second,
  80. UniqueId = 0
  81. },
  82. AlbumStorage = AlbumStorage.Sd,
  83. ContentType = ContentType.Screenshot,
  84. Padding = new Array5<byte>(),
  85. Unknown0x1f = 1
  86. };
  87. using (SHA256 sha256Hash = SHA256.Create())
  88. {
  89. // NOTE: The hex hash is a HMAC-SHA256 (first 32 bytes) using a hardcoded secret key over the titleId, we can simulate it by hashing the titleId instead.
  90. string hash = BitConverter.ToString(sha256Hash.ComputeHash(BitConverter.GetBytes(titleId))).Replace("-", "").Remove(0x20);
  91. string folderPath = Path.Combine(_sdCardPath, "Nintendo", "Album", currentDateTime.Year.ToString("00"), currentDateTime.Month.ToString("00"), currentDateTime.Day.ToString("00"));
  92. string filePath = GenerateFilePath(folderPath, applicationAlbumEntry, currentDateTime, hash);
  93. // TODO: Handle that using the FS service implementation and return the right error code instead of throwing exceptions.
  94. Directory.CreateDirectory(folderPath);
  95. while (File.Exists(filePath))
  96. {
  97. applicationAlbumEntry.AlbumFileDateTime.UniqueId++;
  98. filePath = GenerateFilePath(folderPath, applicationAlbumEntry, currentDateTime, hash);
  99. }
  100. // NOTE: The saved JPEG file doesn't have the limitation in the extra EXIF data.
  101. Image.LoadPixelData<Rgba32>(screenshotData, 1280, 720).SaveAsJpegAsync(filePath);
  102. }
  103. return ResultCode.Success;
  104. }
  105. return ResultCode.NullInputBuffer;
  106. }
  107. private string GenerateFilePath(string folderPath, ApplicationAlbumEntry applicationAlbumEntry, DateTime currentDateTime, string hash)
  108. {
  109. string fileName = $"{currentDateTime:yyyyMMddHHmmss}{applicationAlbumEntry.AlbumFileDateTime.UniqueId:00}-{hash}.jpg";
  110. return Path.Combine(folderPath, fileName);
  111. }
  112. }
  113. }