FileSystemExtensions.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using LibHac.Common;
  2. using LibHac.Fs;
  3. using LibHac.Fs.Fsa;
  4. using LibHac.Loader;
  5. using LibHac.Ns;
  6. using LibHac.Tools.FsSystem;
  7. using Ryujinx.Common.Configuration;
  8. using Ryujinx.Common.Logging;
  9. using Ryujinx.HLE.Loaders.Executables;
  10. using Ryujinx.Memory;
  11. using System;
  12. using System.Linq;
  13. using static Ryujinx.HLE.HOS.ModLoader;
  14. namespace Ryujinx.HLE.Loaders.Processes.Extensions
  15. {
  16. static class FileSystemExtensions
  17. {
  18. public static MetaLoader GetNpdm(this IFileSystem fileSystem)
  19. {
  20. MetaLoader metaLoader = new();
  21. if (fileSystem == null || !fileSystem.FileExists(ProcessConst.MainNpdmPath))
  22. {
  23. Logger.Warning?.Print(LogClass.Loader, "NPDM file not found, using default values!");
  24. metaLoader.LoadDefault();
  25. }
  26. else
  27. {
  28. metaLoader.LoadFromFile(fileSystem);
  29. }
  30. return metaLoader;
  31. }
  32. public static ProcessResult Load(this IFileSystem exeFs, Switch device, BlitStruct<ApplicationControlProperty> nacpData, MetaLoader metaLoader, byte programIndex, bool isHomebrew = false)
  33. {
  34. ulong programId = metaLoader.GetProgramId();
  35. // Replace the whole ExeFs partition by the modded one.
  36. if (device.Configuration.VirtualFileSystem.ModLoader.ReplaceExefsPartition(programId, ref exeFs))
  37. {
  38. metaLoader = null;
  39. }
  40. // Reload the MetaLoader in case of ExeFs partition replacement.
  41. metaLoader ??= exeFs.GetNpdm();
  42. NsoExecutable[] nsoExecutables = new NsoExecutable[ProcessConst.ExeFsPrefixes.Length];
  43. for (int i = 0; i < nsoExecutables.Length; i++)
  44. {
  45. string name = ProcessConst.ExeFsPrefixes[i];
  46. if (!exeFs.FileExists($"/{name}"))
  47. {
  48. continue; // File doesn't exist, skip.
  49. }
  50. Logger.Info?.Print(LogClass.Loader, $"Loading {name}...");
  51. using var nsoFile = new UniqueRef<IFile>();
  52. exeFs.OpenFile(ref nsoFile.Ref, $"/{name}".ToU8Span(), OpenMode.Read).ThrowIfFailure();
  53. nsoExecutables[i] = new NsoExecutable(nsoFile.Release().AsStorage(), name);
  54. }
  55. // ExeFs file replacements.
  56. ModLoadResult modLoadResult = device.Configuration.VirtualFileSystem.ModLoader.ApplyExefsMods(programId, nsoExecutables);
  57. // Take the Npdm from mods if present.
  58. if (modLoadResult.Npdm != null)
  59. {
  60. metaLoader = modLoadResult.Npdm;
  61. }
  62. // Collect the Nsos, ignoring ones that aren't used.
  63. nsoExecutables = nsoExecutables.Where(x => x != null).ToArray();
  64. // Apply Nsos patches.
  65. device.Configuration.VirtualFileSystem.ModLoader.ApplyNsoPatches(programId, nsoExecutables);
  66. // Don't use PTC if ExeFS files have been replaced.
  67. bool enablePtc = device.System.EnablePtc && !modLoadResult.Modified;
  68. if (!enablePtc)
  69. {
  70. Logger.Warning?.Print(LogClass.Ptc, "Detected unsupported ExeFs modifications. PTC disabled.");
  71. }
  72. string programName = string.Empty;
  73. if (!isHomebrew && programId > 0x010000000000FFFF)
  74. {
  75. programName = nacpData.Value.Title[(int)device.System.State.DesiredTitleLanguage].NameString.ToString();
  76. if (string.IsNullOrWhiteSpace(programName))
  77. {
  78. programName = Array.Find(nacpData.Value.Title.ItemsRo.ToArray(), x => x.Name[0] != 0).NameString.ToString();
  79. }
  80. }
  81. // Initialize GPU.
  82. Graphics.Gpu.GraphicsConfig.TitleId = programId.ToString("X16");
  83. device.Gpu.HostInitalized.Set();
  84. if (!MemoryBlock.SupportsFlags(MemoryAllocationFlags.ViewCompatible))
  85. {
  86. device.Configuration.MemoryManagerMode = MemoryManagerMode.SoftwarePageTable;
  87. }
  88. ProcessResult processResult = ProcessLoaderHelper.LoadNsos(
  89. device,
  90. device.System.KernelContext,
  91. metaLoader,
  92. nacpData,
  93. enablePtc,
  94. true,
  95. programName,
  96. metaLoader.GetProgramId(),
  97. programIndex,
  98. null,
  99. nsoExecutables);
  100. // TODO: This should be stored using ProcessId instead.
  101. device.System.LibHacHorizonManager.ArpIReader.ApplicationId = new LibHac.ApplicationId(metaLoader.GetProgramId());
  102. return processResult;
  103. }
  104. }
  105. }