ProcessResult.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using LibHac.Common;
  2. using LibHac.Loader;
  3. using LibHac.Ns;
  4. using Ryujinx.Common.Logging;
  5. using Ryujinx.Cpu;
  6. using Ryujinx.HLE.HOS.SystemState;
  7. using Ryujinx.HLE.Loaders.Processes.Extensions;
  8. using Ryujinx.Horizon.Common;
  9. using System;
  10. namespace Ryujinx.HLE.Loaders.Processes
  11. {
  12. public class ProcessResult
  13. {
  14. public static ProcessResult Failed => new(null, new BlitStruct<ApplicationControlProperty>(1), false, false, null, 0, 0, 0, TitleLanguage.AmericanEnglish);
  15. private readonly byte _mainThreadPriority;
  16. private readonly uint _mainThreadStackSize;
  17. public readonly IDiskCacheLoadState DiskCacheLoadState;
  18. public readonly MetaLoader MetaLoader;
  19. public readonly ApplicationControlProperty ApplicationControlProperties;
  20. public readonly ulong ProcessId;
  21. public readonly string Name;
  22. public readonly string DisplayVersion;
  23. public readonly ulong ProgramId;
  24. public readonly string ProgramIdText;
  25. public readonly bool Is64Bit;
  26. public readonly bool DiskCacheEnabled;
  27. public readonly bool AllowCodeMemoryForJit;
  28. public ProcessResult(
  29. MetaLoader metaLoader,
  30. BlitStruct<ApplicationControlProperty> applicationControlProperties,
  31. bool diskCacheEnabled,
  32. bool allowCodeMemoryForJit,
  33. IDiskCacheLoadState diskCacheLoadState,
  34. ulong pid,
  35. byte mainThreadPriority,
  36. uint mainThreadStackSize,
  37. TitleLanguage titleLanguage)
  38. {
  39. _mainThreadPriority = mainThreadPriority;
  40. _mainThreadStackSize = mainThreadStackSize;
  41. DiskCacheLoadState = diskCacheLoadState;
  42. ProcessId = pid;
  43. MetaLoader = metaLoader;
  44. ApplicationControlProperties = applicationControlProperties.Value;
  45. if (metaLoader is not null)
  46. {
  47. ulong programId = metaLoader.GetProgramId();
  48. Name = ApplicationControlProperties.Title[(int)titleLanguage].NameString.ToString();
  49. if (string.IsNullOrWhiteSpace(Name))
  50. {
  51. Name = Array.Find(ApplicationControlProperties.Title.ItemsRo.ToArray(), x => x.Name[0] != 0).NameString.ToString();
  52. }
  53. DisplayVersion = ApplicationControlProperties.DisplayVersionString.ToString();
  54. ProgramId = programId;
  55. ProgramIdText = $"{programId:x16}";
  56. Is64Bit = metaLoader.IsProgram64Bit();
  57. }
  58. DiskCacheEnabled = diskCacheEnabled;
  59. AllowCodeMemoryForJit = allowCodeMemoryForJit;
  60. }
  61. public bool Start(Switch device)
  62. {
  63. device.Configuration.ContentManager.LoadEntries(device);
  64. Result result = device.System.KernelContext.Processes[ProcessId].Start(_mainThreadPriority, _mainThreadStackSize);
  65. if (result != Result.Success)
  66. {
  67. Logger.Error?.Print(LogClass.Loader, $"Process start returned error \"{result}\".");
  68. return false;
  69. }
  70. bool isFirmware = ProgramId is >= 0x0100000000000819 and <= 0x010000000000081C;
  71. bool isFirmwareApplication = ProgramId <= 0x0100000000007FFF;
  72. string name = !isFirmware
  73. ? (isFirmwareApplication ? "Firmware Application " : string.Empty) + (!string.IsNullOrWhiteSpace(Name) ? Name : "<Unknown Name>")
  74. : "Firmware";
  75. // TODO: LibHac npdm currently doesn't support version field.
  76. string version = !isFirmware
  77. ? (!string.IsNullOrWhiteSpace(DisplayVersion) ? DisplayVersion : "<Unknown Version>")
  78. : device.System.ContentManager.GetCurrentFirmwareVersion()?.VersionString ?? "?";
  79. Logger.Info?.Print(LogClass.Loader, $"Application Loaded: {name} v{version} [{ProgramIdText}] [{(Is64Bit ? "64-bit" : "32-bit")}]");
  80. return true;
  81. }
  82. }
  83. }