MetaLoaderExtensions.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using LibHac.Common;
  2. using LibHac.Fs;
  3. using LibHac.Fs.Fsa;
  4. using LibHac.Loader;
  5. using LibHac.Util;
  6. using Ryujinx.Common;
  7. using System;
  8. namespace Ryujinx.HLE.Loaders.Processes.Extensions
  9. {
  10. public static class MetaLoaderExtensions
  11. {
  12. public static ulong GetProgramId(this MetaLoader metaLoader)
  13. {
  14. metaLoader.GetNpdm(out var npdm).ThrowIfFailure();
  15. return npdm.Aci.ProgramId.Value;
  16. }
  17. public static string GetProgramName(this MetaLoader metaLoader)
  18. {
  19. metaLoader.GetNpdm(out var npdm).ThrowIfFailure();
  20. return StringUtils.Utf8ZToString(npdm.Meta.ProgramName);
  21. }
  22. public static bool IsProgram64Bit(this MetaLoader metaLoader)
  23. {
  24. metaLoader.GetNpdm(out var npdm).ThrowIfFailure();
  25. return (npdm.Meta.Flags & 1) != 0;
  26. }
  27. public static void LoadDefault(this MetaLoader metaLoader)
  28. {
  29. byte[] npdmBuffer = EmbeddedResources.Read("Ryujinx.HLE/Homebrew.npdm");
  30. metaLoader.Load(npdmBuffer).ThrowIfFailure();
  31. }
  32. public static void LoadFromFile(this MetaLoader metaLoader, IFileSystem fileSystem, string path = "")
  33. {
  34. if (string.IsNullOrEmpty(path))
  35. {
  36. path = ProcessConst.MainNpdmPath;
  37. }
  38. using var npdmFile = new UniqueRef<IFile>();
  39. fileSystem.OpenFile(ref npdmFile.Ref, path.ToU8Span(), OpenMode.Read).ThrowIfFailure();
  40. npdmFile.Get.GetSize(out long fileSize).ThrowIfFailure();
  41. Span<byte> npdmBuffer = new byte[fileSize];
  42. npdmFile.Get.Read(out _, 0, npdmBuffer).ThrowIfFailure();
  43. metaLoader.Load(npdmBuffer).ThrowIfFailure();
  44. }
  45. }
  46. }