ProcessLoader.cs 8.8 KB

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