aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/Loaders/Processes/Extensions
diff options
context:
space:
mode:
authorTSR Berry <20988865+TSRBerry@users.noreply.github.com>2023-04-08 01:22:00 +0200
committerMary <thog@protonmail.com>2023-04-27 23:51:14 +0200
commitcee712105850ac3385cd0091a923438167433f9f (patch)
tree4a5274b21d8b7f938c0d0ce18736d3f2993b11b1 /Ryujinx.HLE/Loaders/Processes/Extensions
parentcd124bda587ef09668a971fa1cac1c3f0cfc9f21 (diff)
Move solution and projects to src
Diffstat (limited to 'Ryujinx.HLE/Loaders/Processes/Extensions')
-rw-r--r--Ryujinx.HLE/Loaders/Processes/Extensions/FileSystemExtensions.cs133
-rw-r--r--Ryujinx.HLE/Loaders/Processes/Extensions/LocalFileSystemExtensions.cs39
-rw-r--r--Ryujinx.HLE/Loaders/Processes/Extensions/MetaLoaderExtensions.cs61
-rw-r--r--Ryujinx.HLE/Loaders/Processes/Extensions/NcaExtensions.cs175
-rw-r--r--Ryujinx.HLE/Loaders/Processes/Extensions/PartitionFileSystemExtensions.cs180
5 files changed, 0 insertions, 588 deletions
diff --git a/Ryujinx.HLE/Loaders/Processes/Extensions/FileSystemExtensions.cs b/Ryujinx.HLE/Loaders/Processes/Extensions/FileSystemExtensions.cs
deleted file mode 100644
index 58759ddb..00000000
--- a/Ryujinx.HLE/Loaders/Processes/Extensions/FileSystemExtensions.cs
+++ /dev/null
@@ -1,133 +0,0 @@
-using LibHac.Common;
-using LibHac.Fs;
-using LibHac.Fs.Fsa;
-using LibHac.Loader;
-using LibHac.Ns;
-using LibHac.Tools.FsSystem;
-using Ryujinx.Common.Configuration;
-using Ryujinx.Common.Logging;
-using Ryujinx.HLE.Loaders.Executables;
-using Ryujinx.Memory;
-using System.Linq;
-using static Ryujinx.HLE.HOS.ModLoader;
-
-namespace Ryujinx.HLE.Loaders.Processes.Extensions
-{
- static class FileSystemExtensions
- {
- public static MetaLoader GetNpdm(this IFileSystem fileSystem)
- {
- MetaLoader metaLoader = new();
-
- if (fileSystem == null || !fileSystem.FileExists(ProcessConst.MainNpdmPath))
- {
- Logger.Warning?.Print(LogClass.Loader, "NPDM file not found, using default values!");
-
- metaLoader.LoadDefault();
- }
- else
- {
- metaLoader.LoadFromFile(fileSystem);
- }
-
- return metaLoader;
- }
-
- public static ProcessResult Load(this IFileSystem exeFs, Switch device, BlitStruct<ApplicationControlProperty> nacpData, MetaLoader metaLoader, bool isHomebrew = false)
- {
- ulong programId = metaLoader.GetProgramId();
-
- // Replace the whole ExeFs partition by the modded one.
- if (device.Configuration.VirtualFileSystem.ModLoader.ReplaceExefsPartition(programId, ref exeFs))
- {
- metaLoader = null;
- }
-
- // Reload the MetaLoader in case of ExeFs partition replacement.
- metaLoader ??= exeFs.GetNpdm();
-
- NsoExecutable[] nsoExecutables = new NsoExecutable[ProcessConst.ExeFsPrefixes.Length];
-
- for (int i = 0; i < nsoExecutables.Length; i++)
- {
- string name = ProcessConst.ExeFsPrefixes[i];
-
- if (!exeFs.FileExists($"/{name}"))
- {
- continue; // File doesn't exist, skip.
- }
-
- Logger.Info?.Print(LogClass.Loader, $"Loading {name}...");
-
- using var nsoFile = new UniqueRef<IFile>();
-
- exeFs.OpenFile(ref nsoFile.Ref, $"/{name}".ToU8Span(), OpenMode.Read).ThrowIfFailure();
-
- nsoExecutables[i] = new NsoExecutable(nsoFile.Release().AsStorage(), name);
- }
-
- // ExeFs file replacements.
- ModLoadResult modLoadResult = device.Configuration.VirtualFileSystem.ModLoader.ApplyExefsMods(programId, nsoExecutables);
-
- // Take the Npdm from mods if present.
- if (modLoadResult.Npdm != null)
- {
- metaLoader = modLoadResult.Npdm;
- }
-
- // Collect the Nsos, ignoring ones that aren't used.
- nsoExecutables = nsoExecutables.Where(x => x != null).ToArray();
-
- // Apply Nsos patches.
- device.Configuration.VirtualFileSystem.ModLoader.ApplyNsoPatches(programId, nsoExecutables);
-
- // Don't use PTC if ExeFS files have been replaced.
- bool enablePtc = device.System.EnablePtc && !modLoadResult.Modified;
- if (!enablePtc)
- {
- Logger.Warning?.Print(LogClass.Ptc, $"Detected unsupported ExeFs modifications. PTC disabled.");
- }
-
- // We allow it for nx-hbloader because it can be used to launch homebrew.
- bool allowCodeMemoryForJit = programId == 0x010000000000100DUL || isHomebrew;
-
- string programName = "";
-
- if (!isHomebrew && programId > 0x010000000000FFFF)
- {
- programName = nacpData.Value.Title[(int)device.System.State.DesiredTitleLanguage].NameString.ToString();
-
- if (string.IsNullOrWhiteSpace(programName))
- {
- programName = nacpData.Value.Title.ItemsRo.ToArray().FirstOrDefault(x => x.Name[0] != 0).NameString.ToString();
- }
- }
-
- // Initialize GPU.
- Graphics.Gpu.GraphicsConfig.TitleId = $"{programId:x16}";
- device.Gpu.HostInitalized.Set();
-
- if (!MemoryBlock.SupportsFlags(MemoryAllocationFlags.ViewCompatible))
- {
- device.Configuration.MemoryManagerMode = MemoryManagerMode.SoftwarePageTable;
- }
-
- ProcessResult processResult = ProcessLoaderHelper.LoadNsos(
- device,
- device.System.KernelContext,
- metaLoader,
- nacpData.Value,
- enablePtc,
- allowCodeMemoryForJit,
- programName,
- metaLoader.GetProgramId(),
- null,
- nsoExecutables);
-
- // TODO: This should be stored using ProcessId instead.
- device.System.LibHacHorizonManager.ArpIReader.ApplicationId = new LibHac.ApplicationId(metaLoader.GetProgramId());
-
- return processResult;
- }
- }
-} \ No newline at end of file
diff --git a/Ryujinx.HLE/Loaders/Processes/Extensions/LocalFileSystemExtensions.cs b/Ryujinx.HLE/Loaders/Processes/Extensions/LocalFileSystemExtensions.cs
deleted file mode 100644
index 28d90785..00000000
--- a/Ryujinx.HLE/Loaders/Processes/Extensions/LocalFileSystemExtensions.cs
+++ /dev/null
@@ -1,39 +0,0 @@
-using LibHac.Common;
-using LibHac.FsSystem;
-using LibHac.Loader;
-using LibHac.Ns;
-using Ryujinx.HLE.Loaders.Processes.Extensions;
-using ApplicationId = LibHac.Ncm.ApplicationId;
-
-namespace Ryujinx.HLE.Loaders.Processes
-{
- static class LocalFileSystemExtensions
- {
- public static ProcessResult Load(this LocalFileSystem exeFs, Switch device, string romFsPath = "")
- {
- MetaLoader metaLoader = exeFs.GetNpdm();
- var nacpData = new BlitStruct<ApplicationControlProperty>(1);
- ulong programId = metaLoader.GetProgramId();
-
- device.Configuration.VirtualFileSystem.ModLoader.CollectMods(
- new[] { programId },
- device.Configuration.VirtualFileSystem.ModLoader.GetModsBasePath(),
- device.Configuration.VirtualFileSystem.ModLoader.GetSdModsBasePath());
-
- if (programId != 0)
- {
- ProcessLoaderHelper.EnsureSaveData(device, new ApplicationId(programId), nacpData);
- }
-
- ProcessResult processResult = exeFs.Load(device, nacpData, metaLoader);
-
- // Load RomFS.
- if (!string.IsNullOrEmpty(romFsPath))
- {
- device.Configuration.VirtualFileSystem.LoadRomFs(processResult.ProcessId, romFsPath);
- }
-
- return processResult;
- }
- }
-}
diff --git a/Ryujinx.HLE/Loaders/Processes/Extensions/MetaLoaderExtensions.cs b/Ryujinx.HLE/Loaders/Processes/Extensions/MetaLoaderExtensions.cs
deleted file mode 100644
index c639ee52..00000000
--- a/Ryujinx.HLE/Loaders/Processes/Extensions/MetaLoaderExtensions.cs
+++ /dev/null
@@ -1,61 +0,0 @@
-using LibHac.Common;
-using LibHac.Fs;
-using LibHac.Fs.Fsa;
-using LibHac.Loader;
-using LibHac.Util;
-using Ryujinx.Common;
-using System;
-
-namespace Ryujinx.HLE.Loaders.Processes.Extensions
-{
- public static class MetaLoaderExtensions
- {
- public static ulong GetProgramId(this MetaLoader metaLoader)
- {
- metaLoader.GetNpdm(out var npdm).ThrowIfFailure();
-
- return npdm.Aci.ProgramId.Value;
- }
-
- public static string GetProgramName(this MetaLoader metaLoader)
- {
- metaLoader.GetNpdm(out var npdm).ThrowIfFailure();
-
- return StringUtils.Utf8ZToString(npdm.Meta.ProgramName);
- }
-
- public static bool IsProgram64Bit(this MetaLoader metaLoader)
- {
- metaLoader.GetNpdm(out var npdm).ThrowIfFailure();
-
- return (npdm.Meta.Flags & 1) != 0;
- }
-
- public static void LoadDefault(this MetaLoader metaLoader)
- {
- byte[] npdmBuffer = EmbeddedResources.Read("Ryujinx.HLE/Homebrew.npdm");
-
- metaLoader.Load(npdmBuffer).ThrowIfFailure();
- }
-
- public static void LoadFromFile(this MetaLoader metaLoader, IFileSystem fileSystem, string path = "")
- {
- if (string.IsNullOrEmpty(path))
- {
- path = ProcessConst.MainNpdmPath;
- }
-
- using var npdmFile = new UniqueRef<IFile>();
-
- fileSystem.OpenFile(ref npdmFile.Ref, path.ToU8Span(), OpenMode.Read).ThrowIfFailure();
-
- npdmFile.Get.GetSize(out long fileSize).ThrowIfFailure();
-
- Span<byte> npdmBuffer = new byte[fileSize];
-
- npdmFile.Get.Read(out _, 0, npdmBuffer).ThrowIfFailure();
-
- metaLoader.Load(npdmBuffer).ThrowIfFailure();
- }
- }
-} \ No newline at end of file
diff --git a/Ryujinx.HLE/Loaders/Processes/Extensions/NcaExtensions.cs b/Ryujinx.HLE/Loaders/Processes/Extensions/NcaExtensions.cs
deleted file mode 100644
index 473f374d..00000000
--- a/Ryujinx.HLE/Loaders/Processes/Extensions/NcaExtensions.cs
+++ /dev/null
@@ -1,175 +0,0 @@
-using LibHac;
-using LibHac.Common;
-using LibHac.Fs;
-using LibHac.Fs.Fsa;
-using LibHac.Loader;
-using LibHac.Ncm;
-using LibHac.Ns;
-using LibHac.Tools.FsSystem;
-using LibHac.Tools.FsSystem.NcaUtils;
-using Ryujinx.Common.Logging;
-using System.IO;
-using System.Linq;
-using ApplicationId = LibHac.Ncm.ApplicationId;
-
-namespace Ryujinx.HLE.Loaders.Processes.Extensions
-{
- static class NcaExtensions
- {
- public static ProcessResult Load(this Nca nca, Switch device, Nca patchNca, Nca controlNca)
- {
- // Extract RomFs and ExeFs from NCA.
- IStorage romFs = nca.GetRomFs(device, patchNca);
- IFileSystem exeFs = nca.GetExeFs(device, patchNca);
-
- if (exeFs == null)
- {
- Logger.Error?.Print(LogClass.Loader, "No ExeFS found in NCA");
-
- return ProcessResult.Failed;
- }
-
- // Load Npdm file.
- MetaLoader metaLoader = exeFs.GetNpdm();
-
- // Collecting mods related to AocTitleIds and ProgramId.
- device.Configuration.VirtualFileSystem.ModLoader.CollectMods(
- device.Configuration.ContentManager.GetAocTitleIds().Prepend(metaLoader.GetProgramId()),
- device.Configuration.VirtualFileSystem.ModLoader.GetModsBasePath(),
- device.Configuration.VirtualFileSystem.ModLoader.GetSdModsBasePath());
-
- // Load Nacp file.
- var nacpData = new BlitStruct<ApplicationControlProperty>(1);
-
- if (controlNca != null)
- {
- nacpData = controlNca.GetNacp(device);
- }
-
- /* TODO: Rework this since it's wrong and doesn't work as it takes the DisplayVersion from a "potential" inexistant update.
-
- // Load program 0 control NCA as we are going to need it for display version.
- (_, Nca updateProgram0ControlNca) = GetGameUpdateData(_device.Configuration.VirtualFileSystem, mainNca.Header.TitleId.ToString("x16"), 0, out _);
-
- // NOTE: Nintendo doesn't guarantee that the display version will be updated on sub programs when updating a multi program application.
- // As such, to avoid PTC cache confusion, we only trust the program 0 display version when launching a sub program.
- if (updateProgram0ControlNca != null && _device.Configuration.UserChannelPersistence.Index != 0)
- {
- nacpData.Value.DisplayVersion = updateProgram0ControlNca.GetNacp(_device).Value.DisplayVersion;
- }
-
- */
-
- ProcessResult processResult = exeFs.Load(device, nacpData, metaLoader);
-
- // Load RomFS.
- if (romFs == null)
- {
- Logger.Warning?.Print(LogClass.Loader, "No RomFS found in NCA");
- }
- else
- {
- romFs = device.Configuration.VirtualFileSystem.ModLoader.ApplyRomFsMods(processResult.ProgramId, romFs);
-
- device.Configuration.VirtualFileSystem.SetRomFs(processResult.ProcessId, romFs.AsStream(FileAccess.Read));
- }
-
- // Don't create save data for system programs.
- if (processResult.ProgramId != 0 && (processResult.ProgramId < SystemProgramId.Start.Value || processResult.ProgramId > SystemAppletId.End.Value))
- {
- // Multi-program applications can technically use any program ID for the main program, but in practice they always use 0 in the low nibble.
- // We'll know if this changes in the future because applications will get errors when trying to mount the correct save.
- ProcessLoaderHelper.EnsureSaveData(device, new ApplicationId(processResult.ProgramId & ~0xFul), nacpData);
- }
-
- return processResult;
- }
-
- public static int GetProgramIndex(this Nca nca)
- {
- return (int)(nca.Header.TitleId & 0xF);
- }
-
- public static bool IsProgram(this Nca nca)
- {
- return nca.Header.ContentType == NcaContentType.Program;
- }
-
- public static bool IsPatch(this Nca nca)
- {
- int dataIndex = Nca.GetSectionIndexFromType(NcaSectionType.Data, NcaContentType.Program);
-
- return nca.IsProgram() && nca.SectionExists(NcaSectionType.Data) && nca.Header.GetFsHeader(dataIndex).IsPatchSection();
- }
-
- public static bool IsControl(this Nca nca)
- {
- return nca.Header.ContentType == NcaContentType.Control;
- }
-
- public static IFileSystem GetExeFs(this Nca nca, Switch device, Nca patchNca = null)
- {
- IFileSystem exeFs = null;
-
- if (patchNca == null)
- {
- if (nca.CanOpenSection(NcaSectionType.Code))
- {
- exeFs = nca.OpenFileSystem(NcaSectionType.Code, device.System.FsIntegrityCheckLevel);
- }
- }
- else
- {
- if (patchNca.CanOpenSection(NcaSectionType.Code))
- {
- exeFs = nca.OpenFileSystemWithPatch(patchNca, NcaSectionType.Code, device.System.FsIntegrityCheckLevel);
- }
- }
-
- return exeFs;
- }
-
- public static IStorage GetRomFs(this Nca nca, Switch device, Nca patchNca = null)
- {
- IStorage romFs = null;
-
- if (patchNca == null)
- {
- if (nca.CanOpenSection(NcaSectionType.Data))
- {
- romFs = nca.OpenStorage(NcaSectionType.Data, device.System.FsIntegrityCheckLevel);
- }
- }
- else
- {
- if (patchNca.CanOpenSection(NcaSectionType.Data))
- {
- romFs = nca.OpenStorageWithPatch(patchNca, NcaSectionType.Data, device.System.FsIntegrityCheckLevel);
- }
- }
-
- return romFs;
- }
-
- public static BlitStruct<ApplicationControlProperty> GetNacp(this Nca controlNca, Switch device)
- {
- var nacpData = new BlitStruct<ApplicationControlProperty>(1);
-
- using var controlFile = new UniqueRef<IFile>();
-
- Result result = controlNca.OpenFileSystem(NcaSectionType.Data, device.System.FsIntegrityCheckLevel)
- .OpenFile(ref controlFile.Ref, "/control.nacp".ToU8Span(), OpenMode.Read);
-
- if (result.IsSuccess())
- {
- result = controlFile.Get.Read(out long bytesRead, 0, nacpData.ByteSpan, ReadOption.None);
- }
- else
- {
- nacpData.ByteSpan.Clear();
- }
-
- return nacpData;
- }
- }
-} \ No newline at end of file
diff --git a/Ryujinx.HLE/Loaders/Processes/Extensions/PartitionFileSystemExtensions.cs b/Ryujinx.HLE/Loaders/Processes/Extensions/PartitionFileSystemExtensions.cs
deleted file mode 100644
index e93802ae..00000000
--- a/Ryujinx.HLE/Loaders/Processes/Extensions/PartitionFileSystemExtensions.cs
+++ /dev/null
@@ -1,180 +0,0 @@
-using LibHac.Common;
-using LibHac.Fs;
-using LibHac.Fs.Fsa;
-using LibHac.FsSystem;
-using LibHac.Tools.Fs;
-using LibHac.Tools.FsSystem;
-using LibHac.Tools.FsSystem.NcaUtils;
-using Ryujinx.Common.Configuration;
-using Ryujinx.Common.Logging;
-using Ryujinx.Common.Utilities;
-using System;
-using System.Collections.Generic;
-using System.Globalization;
-using System.IO;
-
-namespace Ryujinx.HLE.Loaders.Processes.Extensions
-{
- public static class PartitionFileSystemExtensions
- {
- private static readonly DownloadableContentJsonSerializerContext ContentSerializerContext = new(JsonHelper.GetDefaultSerializerOptions());
- private static readonly TitleUpdateMetadataJsonSerializerContext TitleSerializerContext = new(JsonHelper.GetDefaultSerializerOptions());
-
- internal static (bool, ProcessResult) TryLoad(this PartitionFileSystem partitionFileSystem, Switch device, string path, out string errorMessage)
- {
- errorMessage = null;
-
- // Load required NCAs.
- Nca mainNca = null;
- Nca patchNca = null;
- Nca controlNca = null;
-
- try
- {
- device.Configuration.VirtualFileSystem.ImportTickets(partitionFileSystem);
-
- // TODO: To support multi-games container, this should use CNMT NCA instead.
- foreach (DirectoryEntryEx fileEntry in partitionFileSystem.EnumerateEntries("/", "*.nca"))
- {
- Nca nca = partitionFileSystem.GetNca(device, fileEntry.FullPath);
-
- if (nca.GetProgramIndex() != device.Configuration.UserChannelPersistence.Index)
- {
- continue;
- }
-
- if (nca.IsPatch())
- {
- patchNca = nca;
- }
- else if (nca.IsProgram())
- {
- mainNca = nca;
- }
- else if (nca.IsControl())
- {
- controlNca = nca;
- }
- }
-
- ProcessLoaderHelper.RegisterProgramMapInfo(device, partitionFileSystem).ThrowIfFailure();
- }
- catch (Exception ex)
- {
- errorMessage = $"Unable to load: {ex.Message}";
-
- return (false, ProcessResult.Failed);
- }
-
- if (mainNca != null)
- {
- if (mainNca.Header.ContentType != NcaContentType.Program)
- {
- errorMessage = "Selected NCA file is not a \"Program\" NCA";
-
- return (false, ProcessResult.Failed);
- }
-
- // Load Update NCAs.
- Nca updatePatchNca = null;
- Nca updateControlNca = null;
-
- if (ulong.TryParse(mainNca.Header.TitleId.ToString("x16"), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out ulong titleIdBase))
- {
- // Clear the program index part.
- titleIdBase &= ~0xFUL;
-
- // Load update information if exists.
- string titleUpdateMetadataPath = System.IO.Path.Combine(AppDataManager.GamesDirPath, titleIdBase.ToString("x16"), "updates.json");
- if (File.Exists(titleUpdateMetadataPath))
- {
- string updatePath = JsonHelper.DeserializeFromFile(titleUpdateMetadataPath, TitleSerializerContext.TitleUpdateMetadata).Selected;
- if (File.Exists(updatePath))
- {
- PartitionFileSystem updatePartitionFileSystem = new(new FileStream(updatePath, FileMode.Open, FileAccess.Read).AsStorage());
-
- device.Configuration.VirtualFileSystem.ImportTickets(updatePartitionFileSystem);
-
- // TODO: This should use CNMT NCA instead.
- foreach (DirectoryEntryEx fileEntry in updatePartitionFileSystem.EnumerateEntries("/", "*.nca"))
- {
- Nca nca = updatePartitionFileSystem.GetNca(device, fileEntry.FullPath);
-
- if (nca.GetProgramIndex() != device.Configuration.UserChannelPersistence.Index)
- {
- continue;
- }
-
- if ($"{nca.Header.TitleId.ToString("x16")[..^3]}000" != titleIdBase.ToString("x16"))
- {
- break;
- }
-
- if (nca.IsProgram())
- {
- updatePatchNca = nca;
- }
- else if (nca.IsControl())
- {
- updateControlNca = nca;
- }
- }
- }
- }
- }
-
- if (updatePatchNca != null)
- {
- patchNca = updatePatchNca;
- }
-
- if (updateControlNca != null)
- {
- controlNca = updateControlNca;
- }
-
- // Load contained DownloadableContents.
- // TODO: If we want to support multi-processes in future, we shouldn't clear AddOnContent data here.
- device.Configuration.ContentManager.ClearAocData();
- device.Configuration.ContentManager.AddAocData(partitionFileSystem, path, mainNca.Header.TitleId, device.Configuration.FsIntegrityCheckLevel);
-
- // Load DownloadableContents.
- string addOnContentMetadataPath = System.IO.Path.Combine(AppDataManager.GamesDirPath, mainNca.Header.TitleId.ToString("x16"), "dlc.json");
- if (File.Exists(addOnContentMetadataPath))
- {
- List<DownloadableContentContainer> dlcContainerList = JsonHelper.DeserializeFromFile(addOnContentMetadataPath, ContentSerializerContext.ListDownloadableContentContainer);
-
- foreach (DownloadableContentContainer downloadableContentContainer in dlcContainerList)
- {
- foreach (DownloadableContentNca downloadableContentNca in downloadableContentContainer.DownloadableContentNcaList)
- {
- if (File.Exists(downloadableContentContainer.ContainerPath) && downloadableContentNca.Enabled)
- {
- device.Configuration.ContentManager.AddAocItem(downloadableContentNca.TitleId, downloadableContentContainer.ContainerPath, downloadableContentNca.FullPath);
- }
- else
- {
- Logger.Warning?.Print(LogClass.Application, $"Cannot find AddOnContent file {downloadableContentContainer.ContainerPath}. It may have been moved or renamed.");
- }
- }
- }
- }
-
- return (true, mainNca.Load(device, patchNca, controlNca));
- }
-
- errorMessage = "Unable to load: Could not find Main NCA";
-
- return (false, ProcessResult.Failed);
- }
-
- public static Nca GetNca(this IFileSystem fileSystem, Switch device, string path)
- {
- using var ncaFile = new UniqueRef<IFile>();
-
- fileSystem.OpenFile(ref ncaFile.Ref, path.ToU8Span(), OpenMode.Read).ThrowIfFailure();
-
- return new Nca(device.Configuration.VirtualFileSystem.KeySet, ncaFile.Release().AsStorage());
- }
- }
-} \ No newline at end of file