ProcessLoader.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using LibHac.Common;
  2. using LibHac.Fs;
  3. using LibHac.Fs.Fsa;
  4. using LibHac.FsSystem;
  5. using LibHac.Ns;
  6. using LibHac.Tools.Fs;
  7. using LibHac.Tools.FsSystem;
  8. using LibHac.Tools.FsSystem.NcaUtils;
  9. using Ryujinx.Common;
  10. using Ryujinx.Common.Logging;
  11. using Ryujinx.HLE.Loaders.Executables;
  12. using Ryujinx.HLE.Loaders.Processes.Extensions;
  13. using System;
  14. using System.Collections.Concurrent;
  15. using System.IO;
  16. using Path = System.IO.Path;
  17. namespace Ryujinx.HLE.Loaders.Processes
  18. {
  19. public class ProcessLoader
  20. {
  21. private readonly Switch _device;
  22. private readonly ConcurrentDictionary<ulong, ProcessResult> _processesByPid;
  23. private ulong _latestPid;
  24. public ProcessResult ActiveApplication => _processesByPid[_latestPid];
  25. public ProcessLoader(Switch device)
  26. {
  27. _device = device;
  28. _processesByPid = new ConcurrentDictionary<ulong, ProcessResult>();
  29. }
  30. public bool LoadXci(string path, ulong applicationId)
  31. {
  32. FileStream stream = new(path, FileMode.Open, FileAccess.Read);
  33. Xci xci = new(_device.Configuration.VirtualFileSystem.KeySet, stream.AsStorage());
  34. if (!xci.HasPartition(XciPartitionType.Secure))
  35. {
  36. Logger.Error?.Print(LogClass.Loader, "Unable to load XCI: Could not find XCI Secure partition");
  37. return false;
  38. }
  39. (bool success, ProcessResult processResult) = xci.OpenPartition(XciPartitionType.Secure).TryLoad(_device, path, applicationId, out string errorMessage);
  40. if (!success)
  41. {
  42. Logger.Error?.Print(LogClass.Loader, errorMessage, nameof(PartitionFileSystemExtensions.TryLoad));
  43. return false;
  44. }
  45. if (processResult.ProcessId != 0 && _processesByPid.TryAdd(processResult.ProcessId, processResult))
  46. {
  47. if (processResult.Start(_device))
  48. {
  49. _latestPid = processResult.ProcessId;
  50. return true;
  51. }
  52. }
  53. return false;
  54. }
  55. public bool LoadNsp(string path, ulong applicationId)
  56. {
  57. FileStream file = new(path, FileMode.Open, FileAccess.Read);
  58. PartitionFileSystem partitionFileSystem = new();
  59. partitionFileSystem.Initialize(file.AsStorage()).ThrowIfFailure();
  60. (bool success, ProcessResult processResult) = partitionFileSystem.TryLoad(_device, path, applicationId, out string errorMessage);
  61. if (processResult.ProcessId == 0)
  62. {
  63. // This is not a normal NSP, it's actually a ExeFS as a NSP
  64. processResult = partitionFileSystem.Load(_device, new BlitStruct<ApplicationControlProperty>(1), partitionFileSystem.GetNpdm(), 0, true);
  65. }
  66. if (processResult.ProcessId != 0 && _processesByPid.TryAdd(processResult.ProcessId, processResult))
  67. {
  68. if (processResult.Start(_device))
  69. {
  70. _latestPid = processResult.ProcessId;
  71. return true;
  72. }
  73. }
  74. if (!success)
  75. {
  76. Logger.Error?.Print(LogClass.Loader, errorMessage, nameof(PartitionFileSystemExtensions.TryLoad));
  77. }
  78. return false;
  79. }
  80. public bool LoadNca(string path, BlitStruct<ApplicationControlProperty>? customNacpData = null)
  81. {
  82. FileStream file = new(path, FileMode.Open, FileAccess.Read);
  83. Nca nca = new(_device.Configuration.VirtualFileSystem.KeySet, file.AsStorage(false));
  84. ProcessResult processResult = nca.Load(_device, null, null, customNacpData);
  85. if (processResult.ProcessId != 0 && _processesByPid.TryAdd(processResult.ProcessId, processResult))
  86. {
  87. if (processResult.Start(_device))
  88. {
  89. // NOTE: Check if process is SystemApplicationId or ApplicationId
  90. if (processResult.ProgramId > 0x01000000000007FF)
  91. {
  92. _latestPid = processResult.ProcessId;
  93. }
  94. return true;
  95. }
  96. }
  97. return false;
  98. }
  99. public bool LoadUnpackedNca(string exeFsDirPath, string romFsPath = null)
  100. {
  101. ProcessResult processResult = new LocalFileSystem(exeFsDirPath).Load(_device, romFsPath);
  102. if (processResult.ProcessId != 0 && _processesByPid.TryAdd(processResult.ProcessId, processResult))
  103. {
  104. if (processResult.Start(_device))
  105. {
  106. _latestPid = processResult.ProcessId;
  107. return true;
  108. }
  109. }
  110. return false;
  111. }
  112. public bool LoadNxo(string path)
  113. {
  114. var nacpData = new BlitStruct<ApplicationControlProperty>(1);
  115. IFileSystem dummyExeFs = null;
  116. Stream romfsStream = null;
  117. string programName = string.Empty;
  118. ulong programId = 0000000000000000;
  119. // Load executable.
  120. IExecutable executable;
  121. if (Path.GetExtension(path).ToLower() == ".nro")
  122. {
  123. FileStream input = new(path, FileMode.Open);
  124. NroExecutable nro = new(input.AsStorage());
  125. executable = nro;
  126. // Open RomFS if exists.
  127. IStorage romFsStorage = nro.OpenNroAssetSection(LibHac.Tools.Ro.NroAssetType.RomFs, false);
  128. romFsStorage.GetSize(out long romFsSize).ThrowIfFailure();
  129. if (romFsSize != 0)
  130. {
  131. romfsStream = romFsStorage.AsStream();
  132. }
  133. // Load Nacp if exists.
  134. IStorage nacpStorage = nro.OpenNroAssetSection(LibHac.Tools.Ro.NroAssetType.Nacp, false);
  135. nacpStorage.GetSize(out long nacpSize).ThrowIfFailure();
  136. if (nacpSize != 0)
  137. {
  138. nacpStorage.Read(0, nacpData.ByteSpan);
  139. programName = nacpData.Value.Title[(int)_device.System.State.DesiredTitleLanguage].NameString.ToString();
  140. if (string.IsNullOrWhiteSpace(programName))
  141. {
  142. programName = Array.Find(nacpData.Value.Title.ItemsRo.ToArray(), x => x.Name[0] != 0).NameString.ToString();
  143. }
  144. if (nacpData.Value.PresenceGroupId != 0)
  145. {
  146. programId = nacpData.Value.PresenceGroupId;
  147. }
  148. else if (nacpData.Value.SaveDataOwnerId != 0)
  149. {
  150. programId = nacpData.Value.SaveDataOwnerId;
  151. }
  152. else if (nacpData.Value.AddOnContentBaseId != 0)
  153. {
  154. programId = nacpData.Value.AddOnContentBaseId - 0x1000;
  155. }
  156. }
  157. // TODO: Add icon maybe ?
  158. }
  159. else
  160. {
  161. programName = Path.GetFileNameWithoutExtension(path);
  162. executable = new NsoExecutable(new LocalStorage(path, FileAccess.Read), programName);
  163. }
  164. // Explicitly null TitleId to disable the shader cache.
  165. TitleIDs.CurrentApplication = default;
  166. _device.Gpu.HostInitalized.Set();
  167. ProcessResult processResult = ProcessLoaderHelper.LoadNsos(_device,
  168. _device.System.KernelContext,
  169. dummyExeFs.GetNpdm(),
  170. nacpData,
  171. diskCacheEnabled: false,
  172. allowCodeMemoryForJit: true,
  173. programName,
  174. programId,
  175. 0,
  176. null,
  177. executable);
  178. // Make sure the process id is valid.
  179. if (processResult.ProcessId != 0)
  180. {
  181. // Load RomFS.
  182. if (romfsStream != null)
  183. {
  184. _device.Configuration.VirtualFileSystem.SetRomFs(processResult.ProcessId, romfsStream);
  185. }
  186. // Start process.
  187. if (_processesByPid.TryAdd(processResult.ProcessId, processResult))
  188. {
  189. if (processResult.Start(_device))
  190. {
  191. _latestPid = processResult.ProcessId;
  192. return true;
  193. }
  194. }
  195. }
  196. return false;
  197. }
  198. }
  199. }