CaptureManager.cs 5.5 KB

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