PartitionFileSystemExtensions.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using LibHac.Common;
  2. using LibHac.Fs;
  3. using LibHac.Fs.Fsa;
  4. using LibHac.FsSystem;
  5. using LibHac.Tools.Fs;
  6. using LibHac.Tools.FsSystem;
  7. using LibHac.Tools.FsSystem.NcaUtils;
  8. using Ryujinx.Common.Configuration;
  9. using Ryujinx.Common.Logging;
  10. using Ryujinx.Common.Utilities;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Globalization;
  14. using System.IO;
  15. namespace Ryujinx.HLE.Loaders.Processes.Extensions
  16. {
  17. public static class PartitionFileSystemExtensions
  18. {
  19. internal static (bool, ProcessResult) TryLoad(this PartitionFileSystem partitionFileSystem, Switch device, string path, out string errorMessage)
  20. {
  21. errorMessage = null;
  22. // Load required NCAs.
  23. Nca mainNca = null;
  24. Nca patchNca = null;
  25. Nca controlNca = null;
  26. try
  27. {
  28. device.Configuration.VirtualFileSystem.ImportTickets(partitionFileSystem);
  29. // TODO: To support multi-games container, this should use CNMT NCA instead.
  30. foreach (DirectoryEntryEx fileEntry in partitionFileSystem.EnumerateEntries("/", "*.nca"))
  31. {
  32. Nca nca = partitionFileSystem.GetNca(device, fileEntry.FullPath);
  33. if (nca.GetProgramIndex() != device.Configuration.UserChannelPersistence.Index)
  34. {
  35. continue;
  36. }
  37. if (nca.IsPatch())
  38. {
  39. patchNca = nca;
  40. }
  41. else if (nca.IsProgram())
  42. {
  43. mainNca = nca;
  44. }
  45. else if (nca.IsControl())
  46. {
  47. controlNca = nca;
  48. }
  49. }
  50. ProcessLoaderHelper.RegisterProgramMapInfo(device, partitionFileSystem).ThrowIfFailure();
  51. }
  52. catch (Exception ex)
  53. {
  54. errorMessage = $"Unable to load: {ex.Message}";
  55. return (false, ProcessResult.Failed);
  56. }
  57. if (mainNca != null)
  58. {
  59. if (mainNca.Header.ContentType != NcaContentType.Program)
  60. {
  61. errorMessage = "Selected NCA file is not a \"Program\" NCA";
  62. return (false, ProcessResult.Failed);
  63. }
  64. // Load Update NCAs.
  65. Nca updatePatchNca = null;
  66. Nca updateControlNca = null;
  67. if (ulong.TryParse(mainNca.Header.TitleId.ToString("x16"), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out ulong titleIdBase))
  68. {
  69. // Clear the program index part.
  70. titleIdBase &= ~0xFUL;
  71. // Load update information if exists.
  72. string titleUpdateMetadataPath = System.IO.Path.Combine(AppDataManager.GamesDirPath, titleIdBase.ToString("x16"), "updates.json");
  73. if (File.Exists(titleUpdateMetadataPath))
  74. {
  75. string updatePath = JsonHelper.DeserializeFromFile<TitleUpdateMetadata>(titleUpdateMetadataPath).Selected;
  76. if (File.Exists(updatePath))
  77. {
  78. PartitionFileSystem updatePartitionFileSystem = new(new FileStream(updatePath, FileMode.Open, FileAccess.Read).AsStorage());
  79. device.Configuration.VirtualFileSystem.ImportTickets(updatePartitionFileSystem);
  80. // TODO: This should use CNMT NCA instead.
  81. foreach (DirectoryEntryEx fileEntry in updatePartitionFileSystem.EnumerateEntries("/", "*.nca"))
  82. {
  83. Nca nca = updatePartitionFileSystem.GetNca(device, fileEntry.FullPath);
  84. if (nca.GetProgramIndex() != device.Configuration.UserChannelPersistence.Index)
  85. {
  86. continue;
  87. }
  88. if ($"{nca.Header.TitleId.ToString("x16")[..^3]}000" != titleIdBase.ToString("x16"))
  89. {
  90. break;
  91. }
  92. if (nca.IsProgram())
  93. {
  94. updatePatchNca = nca;
  95. }
  96. else if (nca.IsControl())
  97. {
  98. updateControlNca = nca;
  99. }
  100. }
  101. }
  102. }
  103. }
  104. if (updatePatchNca != null)
  105. {
  106. patchNca = updatePatchNca;
  107. }
  108. if (updateControlNca != null)
  109. {
  110. controlNca = updateControlNca;
  111. }
  112. // Load contained DownloadableContents.
  113. // TODO: If we want to support multi-processes in future, we shouldn't clear AddOnContent data here.
  114. device.Configuration.ContentManager.ClearAocData();
  115. device.Configuration.ContentManager.AddAocData(partitionFileSystem, path, mainNca.Header.TitleId, device.Configuration.FsIntegrityCheckLevel);
  116. // Load DownloadableContents.
  117. string addOnContentMetadataPath = System.IO.Path.Combine(AppDataManager.GamesDirPath, mainNca.Header.TitleId.ToString("x16"), "dlc.json");
  118. if (File.Exists(addOnContentMetadataPath))
  119. {
  120. List<DownloadableContentContainer> dlcContainerList = JsonHelper.DeserializeFromFile<List<DownloadableContentContainer>>(addOnContentMetadataPath);
  121. foreach (DownloadableContentContainer downloadableContentContainer in dlcContainerList)
  122. {
  123. foreach (DownloadableContentNca downloadableContentNca in downloadableContentContainer.DownloadableContentNcaList)
  124. {
  125. if (File.Exists(downloadableContentContainer.ContainerPath) && downloadableContentNca.Enabled)
  126. {
  127. device.Configuration.ContentManager.AddAocItem(downloadableContentNca.TitleId, downloadableContentContainer.ContainerPath, downloadableContentNca.FullPath);
  128. }
  129. else
  130. {
  131. Logger.Warning?.Print(LogClass.Application, $"Cannot find AddOnContent file {downloadableContentContainer.ContainerPath}. It may have been moved or renamed.");
  132. }
  133. }
  134. }
  135. }
  136. return (true, mainNca.Load(device, patchNca, controlNca));
  137. }
  138. errorMessage = "Unable to load: Could not find Main NCA";
  139. return (false, ProcessResult.Failed);
  140. }
  141. public static Nca GetNca(this IFileSystem fileSystem, Switch device, string path)
  142. {
  143. using var ncaFile = new UniqueRef<IFile>();
  144. fileSystem.OpenFile(ref ncaFile.Ref, path.ToU8Span(), OpenMode.Read).ThrowIfFailure();
  145. return new Nca(device.Configuration.VirtualFileSystem.KeySet, ncaFile.Release().AsStorage());
  146. }
  147. }
  148. }