ApplicationLoader.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. using ARMeilleure.Translation.PTC;
  2. using LibHac;
  3. using LibHac.Account;
  4. using LibHac.Common;
  5. using LibHac.Fs;
  6. using LibHac.Fs.Fsa;
  7. using LibHac.FsSystem;
  8. using LibHac.FsSystem.NcaUtils;
  9. using LibHac.Ns;
  10. using Ryujinx.Common.Configuration;
  11. using Ryujinx.Common.Logging;
  12. using Ryujinx.HLE.FileSystem;
  13. using Ryujinx.HLE.FileSystem.Content;
  14. using Ryujinx.HLE.HOS.Kernel.Process;
  15. using Ryujinx.HLE.Loaders.Executables;
  16. using Ryujinx.HLE.Loaders.Npdm;
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Globalization;
  20. using System.IO;
  21. using System.Linq;
  22. using System.Reflection;
  23. using static LibHac.Fs.ApplicationSaveDataManagement;
  24. using static Ryujinx.HLE.HOS.ModLoader;
  25. using ApplicationId = LibHac.Ncm.ApplicationId;
  26. namespace Ryujinx.HLE.HOS
  27. {
  28. using JsonHelper = Common.Utilities.JsonHelper;
  29. public class ApplicationLoader
  30. {
  31. // Binaries from exefs are loaded into mem in this order. Do not change.
  32. internal static readonly string[] ExeFsPrefixes =
  33. {
  34. "rtld",
  35. "main",
  36. "subsdk0",
  37. "subsdk1",
  38. "subsdk2",
  39. "subsdk3",
  40. "subsdk4",
  41. "subsdk5",
  42. "subsdk6",
  43. "subsdk7",
  44. "subsdk8",
  45. "subsdk9",
  46. "sdk"
  47. };
  48. private readonly Switch _device;
  49. private readonly ContentManager _contentManager;
  50. private readonly VirtualFileSystem _fileSystem;
  51. private string _titleName;
  52. private string _displayVersion;
  53. private BlitStruct<ApplicationControlProperty> _controlData;
  54. public BlitStruct<ApplicationControlProperty> ControlData => _controlData;
  55. public string TitleName => _titleName;
  56. public string DisplayVersion => _displayVersion;
  57. public ulong TitleId { get; private set; }
  58. public bool TitleIs64Bit { get; private set; }
  59. public string TitleIdText => TitleId.ToString("x16");
  60. public ApplicationLoader(Switch device, VirtualFileSystem fileSystem, ContentManager contentManager)
  61. {
  62. _device = device;
  63. _contentManager = contentManager;
  64. _fileSystem = fileSystem;
  65. _controlData = new BlitStruct<ApplicationControlProperty>(1);
  66. }
  67. public void LoadCart(string exeFsDir, string romFsFile = null)
  68. {
  69. if (romFsFile != null)
  70. {
  71. _fileSystem.LoadRomFs(romFsFile);
  72. }
  73. LocalFileSystem codeFs = new LocalFileSystem(exeFsDir);
  74. Npdm metaData = ReadNpdm(codeFs);
  75. _fileSystem.ModLoader.CollectMods(new[] { TitleId }, _fileSystem.ModLoader.GetModsBasePath());
  76. if (TitleId != 0)
  77. {
  78. EnsureSaveData(new ApplicationId(TitleId));
  79. }
  80. LoadExeFs(codeFs, metaData);
  81. }
  82. public static (Nca main, Nca patch, Nca control) GetGameData(VirtualFileSystem fileSystem, PartitionFileSystem pfs, int programIndex)
  83. {
  84. Nca mainNca = null;
  85. Nca patchNca = null;
  86. Nca controlNca = null;
  87. fileSystem.ImportTickets(pfs);
  88. foreach (DirectoryEntryEx fileEntry in pfs.EnumerateEntries("/", "*.nca"))
  89. {
  90. pfs.OpenFile(out IFile ncaFile, fileEntry.FullPath.ToU8Span(), OpenMode.Read).ThrowIfFailure();
  91. Nca nca = new Nca(fileSystem.KeySet, ncaFile.AsStorage());
  92. int ncaProgramIndex = (int)(nca.Header.TitleId & 0xF);
  93. if (ncaProgramIndex != programIndex)
  94. {
  95. continue;
  96. }
  97. if (nca.Header.ContentType == NcaContentType.Program)
  98. {
  99. int dataIndex = Nca.GetSectionIndexFromType(NcaSectionType.Data, NcaContentType.Program);
  100. if (nca.Header.GetFsHeader(dataIndex).IsPatchSection())
  101. {
  102. patchNca = nca;
  103. }
  104. else
  105. {
  106. mainNca = nca;
  107. }
  108. }
  109. else if (nca.Header.ContentType == NcaContentType.Control)
  110. {
  111. controlNca = nca;
  112. }
  113. }
  114. return (mainNca, patchNca, controlNca);
  115. }
  116. public static (Nca patch, Nca control) GetGameUpdateDataFromPartition(VirtualFileSystem fileSystem, PartitionFileSystem pfs, string titleId, int programIndex)
  117. {
  118. Nca patchNca = null;
  119. Nca controlNca = null;
  120. fileSystem.ImportTickets(pfs);
  121. foreach (DirectoryEntryEx fileEntry in pfs.EnumerateEntries("/", "*.nca"))
  122. {
  123. pfs.OpenFile(out IFile ncaFile, fileEntry.FullPath.ToU8Span(), OpenMode.Read).ThrowIfFailure();
  124. Nca nca = new Nca(fileSystem.KeySet, ncaFile.AsStorage());
  125. int ncaProgramIndex = (int)(nca.Header.TitleId & 0xF);
  126. if (ncaProgramIndex != programIndex)
  127. {
  128. continue;
  129. }
  130. if ($"{nca.Header.TitleId.ToString("x16")[..^3]}000" != titleId)
  131. {
  132. break;
  133. }
  134. if (nca.Header.ContentType == NcaContentType.Program)
  135. {
  136. patchNca = nca;
  137. }
  138. else if (nca.Header.ContentType == NcaContentType.Control)
  139. {
  140. controlNca = nca;
  141. }
  142. }
  143. return (patchNca, controlNca);
  144. }
  145. public static (Nca patch, Nca control) GetGameUpdateData(VirtualFileSystem fileSystem, string titleId, int programIndex, out string updatePath)
  146. {
  147. updatePath = null;
  148. if (ulong.TryParse(titleId, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out ulong titleIdBase))
  149. {
  150. // Clear the program index part.
  151. titleIdBase &= 0xFFFFFFFFFFFFFFF0;
  152. // Load update informations if existing.
  153. string titleUpdateMetadataPath = Path.Combine(AppDataManager.GamesDirPath, titleIdBase.ToString("x16"), "updates.json");
  154. if (File.Exists(titleUpdateMetadataPath))
  155. {
  156. updatePath = JsonHelper.DeserializeFromFile<TitleUpdateMetadata>(titleUpdateMetadataPath).Selected;
  157. if (File.Exists(updatePath))
  158. {
  159. FileStream file = new FileStream(updatePath, FileMode.Open, FileAccess.Read);
  160. PartitionFileSystem nsp = new PartitionFileSystem(file.AsStorage());
  161. return GetGameUpdateDataFromPartition(fileSystem, nsp, titleIdBase.ToString("x16"), programIndex);
  162. }
  163. }
  164. }
  165. return (null, null);
  166. }
  167. public void LoadXci(string xciFile)
  168. {
  169. FileStream file = new FileStream(xciFile, FileMode.Open, FileAccess.Read);
  170. Xci xci = new Xci(_fileSystem.KeySet, file.AsStorage());
  171. if (!xci.HasPartition(XciPartitionType.Secure))
  172. {
  173. Logger.Error?.Print(LogClass.Loader, "Unable to load XCI: Could not find XCI secure partition");
  174. return;
  175. }
  176. PartitionFileSystem securePartition = xci.OpenPartition(XciPartitionType.Secure);
  177. Nca mainNca;
  178. Nca patchNca;
  179. Nca controlNca;
  180. try
  181. {
  182. (mainNca, patchNca, controlNca) = GetGameData(_fileSystem, securePartition, _device.UserChannelPersistence.Index);
  183. }
  184. catch (Exception e)
  185. {
  186. Logger.Error?.Print(LogClass.Loader, $"Unable to load XCI: {e.Message}");
  187. return;
  188. }
  189. if (mainNca == null)
  190. {
  191. Logger.Error?.Print(LogClass.Loader, "Unable to load XCI: Could not find Main NCA");
  192. return;
  193. }
  194. _contentManager.LoadEntries(_device);
  195. _contentManager.ClearAocData();
  196. _contentManager.AddAocData(securePartition, xciFile, mainNca.Header.TitleId);
  197. LoadNca(mainNca, patchNca, controlNca);
  198. }
  199. public void LoadNsp(string nspFile)
  200. {
  201. FileStream file = new FileStream(nspFile, FileMode.Open, FileAccess.Read);
  202. PartitionFileSystem nsp = new PartitionFileSystem(file.AsStorage());
  203. Nca mainNca;
  204. Nca patchNca;
  205. Nca controlNca;
  206. try
  207. {
  208. (mainNca, patchNca, controlNca) = GetGameData(_fileSystem, nsp, _device.UserChannelPersistence.Index);
  209. }
  210. catch (Exception e)
  211. {
  212. Logger.Error?.Print(LogClass.Loader, $"Unable to load NSP: {e.Message}");
  213. return;
  214. }
  215. if (mainNca == null)
  216. {
  217. Logger.Error?.Print(LogClass.Loader, "Unable to load NSP: Could not find Main NCA");
  218. return;
  219. }
  220. if (mainNca != null)
  221. {
  222. _contentManager.ClearAocData();
  223. _contentManager.AddAocData(nsp, nspFile, mainNca.Header.TitleId);
  224. LoadNca(mainNca, patchNca, controlNca);
  225. return;
  226. }
  227. // This is not a normal NSP, it's actually a ExeFS as a NSP
  228. LoadExeFs(nsp);
  229. }
  230. public void LoadNca(string ncaFile)
  231. {
  232. FileStream file = new FileStream(ncaFile, FileMode.Open, FileAccess.Read);
  233. Nca nca = new Nca(_fileSystem.KeySet, file.AsStorage(false));
  234. LoadNca(nca, null, null);
  235. }
  236. private void LoadNca(Nca mainNca, Nca patchNca, Nca controlNca)
  237. {
  238. if (mainNca.Header.ContentType != NcaContentType.Program)
  239. {
  240. Logger.Error?.Print(LogClass.Loader, "Selected NCA is not a \"Program\" NCA");
  241. return;
  242. }
  243. IStorage dataStorage = null;
  244. IFileSystem codeFs = null;
  245. (Nca updatePatchNca, Nca updateControlNca) = GetGameUpdateData(_fileSystem, mainNca.Header.TitleId.ToString("x16"), _device.UserChannelPersistence.Index, out _);
  246. if (updatePatchNca != null)
  247. {
  248. patchNca = updatePatchNca;
  249. }
  250. if (updateControlNca != null)
  251. {
  252. controlNca = updateControlNca;
  253. }
  254. // Load program 0 control NCA as we are going to need it for display version.
  255. (_, Nca updateProgram0ControlNca) = GetGameUpdateData(_fileSystem, mainNca.Header.TitleId.ToString("x16"), 0, out _);
  256. // Load Aoc
  257. string titleAocMetadataPath = Path.Combine(AppDataManager.GamesDirPath, mainNca.Header.TitleId.ToString("x16"), "dlc.json");
  258. if (File.Exists(titleAocMetadataPath))
  259. {
  260. List<DlcContainer> dlcContainerList = JsonHelper.DeserializeFromFile<List<DlcContainer>>(titleAocMetadataPath);
  261. foreach (DlcContainer dlcContainer in dlcContainerList)
  262. {
  263. foreach (DlcNca dlcNca in dlcContainer.DlcNcaList)
  264. {
  265. _contentManager.AddAocItem(dlcNca.TitleId, dlcContainer.Path, dlcNca.Path, dlcNca.Enabled);
  266. }
  267. }
  268. }
  269. if (patchNca == null)
  270. {
  271. if (mainNca.CanOpenSection(NcaSectionType.Data))
  272. {
  273. dataStorage = mainNca.OpenStorage(NcaSectionType.Data, _device.System.FsIntegrityCheckLevel);
  274. }
  275. if (mainNca.CanOpenSection(NcaSectionType.Code))
  276. {
  277. codeFs = mainNca.OpenFileSystem(NcaSectionType.Code, _device.System.FsIntegrityCheckLevel);
  278. }
  279. }
  280. else
  281. {
  282. if (patchNca.CanOpenSection(NcaSectionType.Data))
  283. {
  284. dataStorage = mainNca.OpenStorageWithPatch(patchNca, NcaSectionType.Data, _device.System.FsIntegrityCheckLevel);
  285. }
  286. if (patchNca.CanOpenSection(NcaSectionType.Code))
  287. {
  288. codeFs = mainNca.OpenFileSystemWithPatch(patchNca, NcaSectionType.Code, _device.System.FsIntegrityCheckLevel);
  289. }
  290. }
  291. if (codeFs == null)
  292. {
  293. Logger.Error?.Print(LogClass.Loader, "No ExeFS found in NCA");
  294. return;
  295. }
  296. Npdm metaData = ReadNpdm(codeFs);
  297. _fileSystem.ModLoader.CollectMods(_contentManager.GetAocTitleIds().Prepend(TitleId), _fileSystem.ModLoader.GetModsBasePath());
  298. if (controlNca != null)
  299. {
  300. ReadControlData(_device, controlNca, ref _controlData, ref _titleName, ref _displayVersion);
  301. }
  302. else
  303. {
  304. ControlData.ByteSpan.Clear();
  305. }
  306. // NOTE: Nintendo doesn't guarantee that the display version will be updated on sub programs when updating a multi program application.
  307. // BODY: As such, to avoid PTC cache confusion, we only trust the the program 0 display version when launching a sub program.
  308. if (updateProgram0ControlNca != null && _device.UserChannelPersistence.Index != 0)
  309. {
  310. string dummyTitleName = "";
  311. BlitStruct<ApplicationControlProperty> dummyControl = new BlitStruct<ApplicationControlProperty>(1);
  312. ReadControlData(_device, updateProgram0ControlNca, ref dummyControl, ref dummyTitleName, ref _displayVersion);
  313. }
  314. if (dataStorage == null)
  315. {
  316. Logger.Warning?.Print(LogClass.Loader, "No RomFS found in NCA");
  317. }
  318. else
  319. {
  320. IStorage newStorage = _fileSystem.ModLoader.ApplyRomFsMods(TitleId, dataStorage);
  321. _fileSystem.SetRomFs(newStorage.AsStream(FileAccess.Read));
  322. }
  323. if (TitleId != 0)
  324. {
  325. EnsureSaveData(new ApplicationId(TitleId));
  326. }
  327. LoadExeFs(codeFs, metaData);
  328. Logger.Info?.Print(LogClass.Loader, $"Application Loaded: {TitleName} v{DisplayVersion} [{TitleIdText}] [{(TitleIs64Bit ? "64-bit" : "32-bit")}]");
  329. }
  330. // Sets TitleId, so be sure to call before using it
  331. private Npdm ReadNpdm(IFileSystem fs)
  332. {
  333. Result result = fs.OpenFile(out IFile npdmFile, "/main.npdm".ToU8Span(), OpenMode.Read);
  334. Npdm metaData;
  335. if (ResultFs.PathNotFound.Includes(result))
  336. {
  337. Logger.Warning?.Print(LogClass.Loader, "NPDM file not found, using default values!");
  338. metaData = GetDefaultNpdm();
  339. }
  340. else
  341. {
  342. metaData = new Npdm(npdmFile.AsStream());
  343. }
  344. TitleId = metaData.Aci0.TitleId;
  345. TitleIs64Bit = metaData.Is64Bit;
  346. return metaData;
  347. }
  348. private static void ReadControlData(Switch device, Nca controlNca, ref BlitStruct<ApplicationControlProperty> controlData, ref string titleName, ref string displayVersion)
  349. {
  350. IFileSystem controlFs = controlNca.OpenFileSystem(NcaSectionType.Data, device.System.FsIntegrityCheckLevel);
  351. Result result = controlFs.OpenFile(out IFile controlFile, "/control.nacp".ToU8Span(), OpenMode.Read);
  352. if (result.IsSuccess())
  353. {
  354. result = controlFile.Read(out long bytesRead, 0, controlData.ByteSpan, ReadOption.None);
  355. if (result.IsSuccess() && bytesRead == controlData.ByteSpan.Length)
  356. {
  357. titleName = controlData.Value.Titles[(int)device.System.State.DesiredTitleLanguage].Name.ToString();
  358. if (string.IsNullOrWhiteSpace(titleName))
  359. {
  360. titleName = controlData.Value.Titles.ToArray().FirstOrDefault(x => x.Name[0] != 0).Name.ToString();
  361. }
  362. displayVersion = controlData.Value.DisplayVersion.ToString();
  363. }
  364. }
  365. else
  366. {
  367. controlData.ByteSpan.Clear();
  368. }
  369. }
  370. private void LoadExeFs(IFileSystem codeFs, Npdm metaData = null)
  371. {
  372. if (_fileSystem.ModLoader.ReplaceExefsPartition(TitleId, ref codeFs))
  373. {
  374. metaData = null; //TODO: Check if we should retain old npdm
  375. }
  376. metaData ??= ReadNpdm(codeFs);
  377. NsoExecutable[] nsos = new NsoExecutable[ExeFsPrefixes.Length];
  378. for (int i = 0; i < nsos.Length; i++)
  379. {
  380. string name = ExeFsPrefixes[i];
  381. if (!codeFs.FileExists($"/{name}"))
  382. {
  383. continue; // file doesn't exist, skip
  384. }
  385. Logger.Info?.Print(LogClass.Loader, $"Loading {name}...");
  386. codeFs.OpenFile(out IFile nsoFile, $"/{name}".ToU8Span(), OpenMode.Read).ThrowIfFailure();
  387. nsos[i] = new NsoExecutable(nsoFile.AsStorage(), name);
  388. }
  389. // ExeFs file replacements
  390. ModLoadResult modLoadResult = _fileSystem.ModLoader.ApplyExefsMods(TitleId, nsos);
  391. // collect the nsos, ignoring ones that aren't used
  392. NsoExecutable[] programs = nsos.Where(x => x != null).ToArray();
  393. // take the npdm from mods if present
  394. if (modLoadResult.Npdm != null)
  395. {
  396. metaData = modLoadResult.Npdm;
  397. }
  398. bool hasPatches = _fileSystem.ModLoader.ApplyNsoPatches(TitleId, programs);
  399. _contentManager.LoadEntries(_device);
  400. bool usePtc = _device.System.EnablePtc;
  401. // don't use PTC if exefs files have been replaced
  402. usePtc &= !modLoadResult.Modified;
  403. // don't use PTC if exefs files have been patched
  404. usePtc &= !hasPatches;
  405. if (_device.System.EnablePtc && !usePtc)
  406. {
  407. Logger.Warning?.Print(LogClass.Ptc, $"Detected exefs modifications. PPTC disabled.");
  408. }
  409. Graphics.Gpu.GraphicsConfig.TitleId = TitleIdText;
  410. _device.Gpu.HostInitalized.Set();
  411. Ptc.Initialize(TitleIdText, DisplayVersion, usePtc);
  412. ProgramLoader.LoadNsos(_device.System.KernelContext, out ProcessTamperInfo tamperInfo, metaData, executables: programs);
  413. _fileSystem.ModLoader.LoadCheats(TitleId, tamperInfo, _device.TamperMachine);
  414. }
  415. public void LoadProgram(string filePath)
  416. {
  417. Npdm metaData = GetDefaultNpdm();
  418. bool isNro = Path.GetExtension(filePath).ToLower() == ".nro";
  419. IExecutable executable;
  420. if (isNro)
  421. {
  422. FileStream input = new FileStream(filePath, FileMode.Open);
  423. NroExecutable obj = new NroExecutable(input.AsStorage());
  424. executable = obj;
  425. // homebrew NRO can actually have some data after the actual NRO
  426. if (input.Length > obj.FileSize)
  427. {
  428. input.Position = obj.FileSize;
  429. BinaryReader reader = new BinaryReader(input);
  430. uint asetMagic = reader.ReadUInt32();
  431. if (asetMagic == 0x54455341)
  432. {
  433. uint asetVersion = reader.ReadUInt32();
  434. if (asetVersion == 0)
  435. {
  436. ulong iconOffset = reader.ReadUInt64();
  437. ulong iconSize = reader.ReadUInt64();
  438. ulong nacpOffset = reader.ReadUInt64();
  439. ulong nacpSize = reader.ReadUInt64();
  440. ulong romfsOffset = reader.ReadUInt64();
  441. ulong romfsSize = reader.ReadUInt64();
  442. if (romfsSize != 0)
  443. {
  444. _fileSystem.SetRomFs(new HomebrewRomFsStream(input, obj.FileSize + (long)romfsOffset));
  445. }
  446. if (nacpSize != 0)
  447. {
  448. input.Seek(obj.FileSize + (long)nacpOffset, SeekOrigin.Begin);
  449. reader.Read(ControlData.ByteSpan);
  450. ref ApplicationControlProperty nacp = ref ControlData.Value;
  451. metaData.TitleName = nacp.Titles[(int)_device.System.State.DesiredTitleLanguage].Name.ToString();
  452. if (string.IsNullOrWhiteSpace(metaData.TitleName))
  453. {
  454. metaData.TitleName = nacp.Titles.ToArray().FirstOrDefault(x => x.Name[0] != 0).Name.ToString();
  455. }
  456. if (nacp.PresenceGroupId != 0)
  457. {
  458. metaData.Aci0.TitleId = nacp.PresenceGroupId;
  459. }
  460. else if (nacp.SaveDataOwnerId.Value != 0)
  461. {
  462. metaData.Aci0.TitleId = nacp.SaveDataOwnerId.Value;
  463. }
  464. else if (nacp.AddOnContentBaseId != 0)
  465. {
  466. metaData.Aci0.TitleId = nacp.AddOnContentBaseId - 0x1000;
  467. }
  468. else
  469. {
  470. metaData.Aci0.TitleId = 0000000000000000;
  471. }
  472. }
  473. }
  474. else
  475. {
  476. Logger.Warning?.Print(LogClass.Loader, $"Unsupported ASET header version found \"{asetVersion}\"");
  477. }
  478. }
  479. }
  480. }
  481. else
  482. {
  483. executable = new NsoExecutable(new LocalStorage(filePath, FileAccess.Read), Path.GetFileNameWithoutExtension(filePath));
  484. }
  485. _contentManager.LoadEntries(_device);
  486. _titleName = metaData.TitleName;
  487. TitleId = metaData.Aci0.TitleId;
  488. TitleIs64Bit = metaData.Is64Bit;
  489. // Explicitly null titleid to disable the shader cache
  490. Graphics.Gpu.GraphicsConfig.TitleId = null;
  491. _device.Gpu.HostInitalized.Set();
  492. ProgramLoader.LoadNsos(_device.System.KernelContext, out ProcessTamperInfo tamperInfo, metaData, executables: executable);
  493. _fileSystem.ModLoader.LoadCheats(TitleId, tamperInfo, _device.TamperMachine);
  494. }
  495. private Npdm GetDefaultNpdm()
  496. {
  497. Assembly asm = Assembly.GetCallingAssembly();
  498. using (Stream npdmStream = asm.GetManifestResourceStream("Ryujinx.HLE.Homebrew.npdm"))
  499. {
  500. return new Npdm(npdmStream);
  501. }
  502. }
  503. private Result EnsureSaveData(ApplicationId applicationId)
  504. {
  505. Logger.Info?.Print(LogClass.Application, "Ensuring required savedata exists.");
  506. Uid user = _device.System.AccountManager.LastOpenedUser.UserId.ToLibHacUid();
  507. ref ApplicationControlProperty control = ref ControlData.Value;
  508. if (LibHac.Utilities.IsEmpty(ControlData.ByteSpan))
  509. {
  510. // If the current application doesn't have a loaded control property, create a dummy one
  511. // and set the savedata sizes so a user savedata will be created.
  512. control = ref new BlitStruct<ApplicationControlProperty>(1).Value;
  513. // The set sizes don't actually matter as long as they're non-zero because we use directory savedata.
  514. control.UserAccountSaveDataSize = 0x4000;
  515. control.UserAccountSaveDataJournalSize = 0x4000;
  516. Logger.Warning?.Print(LogClass.Application,
  517. "No control file was found for this game. Using a dummy one instead. This may cause inaccuracies in some games.");
  518. }
  519. FileSystemClient fileSystem = _fileSystem.FsClient;
  520. Result resultCode = fileSystem.EnsureApplicationCacheStorage(out _, applicationId, ref control);
  521. if (resultCode.IsFailure())
  522. {
  523. Logger.Error?.Print(LogClass.Application, $"Error calling EnsureApplicationCacheStorage. Result code {resultCode.ToStringWithName()}");
  524. return resultCode;
  525. }
  526. resultCode = EnsureApplicationSaveData(fileSystem, out _, applicationId, ref control, ref user);
  527. if (resultCode.IsFailure())
  528. {
  529. Logger.Error?.Print(LogClass.Application, $"Error calling EnsureApplicationSaveData. Result code {resultCode.ToStringWithName()}");
  530. }
  531. return resultCode;
  532. }
  533. }
  534. }