ApplicationData.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using LibHac.Common;
  2. using LibHac.Fs;
  3. using LibHac.Fs.Fsa;
  4. using LibHac.FsSystem;
  5. using LibHac.Loader;
  6. using LibHac.Ns;
  7. using LibHac.Tools.Fs;
  8. using LibHac.Tools.FsSystem;
  9. using LibHac.Tools.FsSystem.NcaUtils;
  10. using Ryujinx.Ava.Common.Locale;
  11. using Ryujinx.Ava.Utilities.Compat;
  12. using Ryujinx.Common.Logging;
  13. using Ryujinx.HLE.FileSystem;
  14. using Ryujinx.HLE.Loaders.Processes.Extensions;
  15. using System;
  16. using System.IO;
  17. using System.Text.Json.Serialization;
  18. namespace Ryujinx.Ava.Utilities.AppLibrary
  19. {
  20. public class ApplicationData
  21. {
  22. public bool Favorite { get; set; }
  23. public byte[] Icon { get; set; }
  24. public string Name { get; set; } = "Unknown";
  25. private ulong _id;
  26. public ulong Id
  27. {
  28. get => _id;
  29. set
  30. {
  31. _id = value;
  32. PlayabilityStatus = CompatibilityCsv.GetStatus(Id);
  33. }
  34. }
  35. public string Developer { get; set; } = "Unknown";
  36. public string Version { get; set; } = "0";
  37. public bool HasPlayabilityInfo => PlayabilityStatus != null;
  38. public string LocalizedStatus =>
  39. PlayabilityStatus.HasValue
  40. ? LocaleManager.Instance[PlayabilityStatus!.Value]
  41. : string.Empty;
  42. public LocaleKeys? PlayabilityStatus { get; set; }
  43. public int PlayerCount { get; set; }
  44. public int GameCount { get; set; }
  45. public TimeSpan TimePlayed { get; set; }
  46. public DateTime? LastPlayed { get; set; }
  47. public string FileExtension { get; set; }
  48. public long FileSize { get; set; }
  49. public string Path { get; set; }
  50. public BlitStruct<ApplicationControlProperty> ControlHolder { get; set; }
  51. public bool HasControlHolder => ControlHolder.ByteSpan.Length > 0 && !ControlHolder.ByteSpan.IsZeros();
  52. public string TimePlayedString => ValueFormatUtils.FormatTimeSpan(TimePlayed);
  53. public bool HasPlayedPreviously => TimePlayedString != string.Empty;
  54. public string LastPlayedString => ValueFormatUtils.FormatDateTime(LastPlayed)?.Replace(" ", "\n");
  55. public string FileSizeString => ValueFormatUtils.FormatFileSize(FileSize);
  56. [JsonIgnore] public string IdString => Id.ToString("x16");
  57. [JsonIgnore] public ulong IdBase => Id & ~0x1FFFUL;
  58. [JsonIgnore] public string IdBaseString => IdBase.ToString("x16");
  59. public static string GetBuildId(VirtualFileSystem virtualFileSystem, IntegrityCheckLevel checkLevel, string titleFilePath)
  60. {
  61. using FileStream file = new(titleFilePath, FileMode.Open, FileAccess.Read);
  62. Nca mainNca = null;
  63. Nca patchNca = null;
  64. if (!System.IO.Path.Exists(titleFilePath))
  65. {
  66. Logger.Error?.Print(LogClass.Application, $"File \"{titleFilePath}\" does not exist.");
  67. return string.Empty;
  68. }
  69. string extension = System.IO.Path.GetExtension(titleFilePath).ToLower();
  70. if (extension is ".nsp" or ".xci")
  71. {
  72. IFileSystem pfs;
  73. if (extension == ".xci")
  74. {
  75. Xci xci = new(virtualFileSystem.KeySet, file.AsStorage());
  76. pfs = xci.OpenPartition(XciPartitionType.Secure);
  77. }
  78. else
  79. {
  80. PartitionFileSystem pfsTemp = new();
  81. pfsTemp.Initialize(file.AsStorage()).ThrowIfFailure();
  82. pfs = pfsTemp;
  83. }
  84. foreach (DirectoryEntryEx fileEntry in pfs.EnumerateEntries("/", "*.nca"))
  85. {
  86. using UniqueRef<IFile> ncaFile = new();
  87. pfs.OpenFile(ref ncaFile.Ref, fileEntry.FullPath.ToU8Span(), OpenMode.Read).ThrowIfFailure();
  88. Nca nca = new(virtualFileSystem.KeySet, ncaFile.Get.AsStorage());
  89. if (nca.Header.ContentType != NcaContentType.Program)
  90. {
  91. continue;
  92. }
  93. int dataIndex = Nca.GetSectionIndexFromType(NcaSectionType.Data, NcaContentType.Program);
  94. if (nca.Header.GetFsHeader(dataIndex).IsPatchSection())
  95. {
  96. patchNca = nca;
  97. }
  98. else
  99. {
  100. mainNca = nca;
  101. }
  102. }
  103. }
  104. else if (extension == ".nca")
  105. {
  106. mainNca = new Nca(virtualFileSystem.KeySet, file.AsStorage());
  107. }
  108. if (mainNca == null)
  109. {
  110. Logger.Error?.Print(LogClass.Application, "Extraction failure. The main NCA was not present in the selected file");
  111. return string.Empty;
  112. }
  113. (Nca updatePatchNca, _) = mainNca.GetUpdateData(virtualFileSystem, checkLevel, 0, out string _);
  114. if (updatePatchNca != null)
  115. {
  116. patchNca = updatePatchNca;
  117. }
  118. IFileSystem codeFs = null;
  119. if (patchNca == null)
  120. {
  121. if (mainNca.CanOpenSection(NcaSectionType.Code))
  122. {
  123. codeFs = mainNca.OpenFileSystem(NcaSectionType.Code, IntegrityCheckLevel.ErrorOnInvalid);
  124. }
  125. }
  126. else
  127. {
  128. if (patchNca.CanOpenSection(NcaSectionType.Code))
  129. {
  130. codeFs = mainNca.OpenFileSystemWithPatch(patchNca, NcaSectionType.Code, IntegrityCheckLevel.ErrorOnInvalid);
  131. }
  132. }
  133. if (codeFs == null)
  134. {
  135. Logger.Error?.Print(LogClass.Loader, "No ExeFS found in NCA");
  136. return string.Empty;
  137. }
  138. const string MainExeFs = "main";
  139. if (!codeFs.FileExists($"/{MainExeFs}"))
  140. {
  141. Logger.Error?.Print(LogClass.Loader, "No main binary ExeFS found in ExeFS");
  142. return string.Empty;
  143. }
  144. using UniqueRef<IFile> nsoFile = new();
  145. codeFs.OpenFile(ref nsoFile.Ref, $"/{MainExeFs}".ToU8Span(), OpenMode.Read).ThrowIfFailure();
  146. NsoReader reader = new();
  147. reader.Initialize(nsoFile.Release().AsStorage().AsFile(OpenMode.Read)).ThrowIfFailure();
  148. return Convert.ToHexString(reader.Header.ModuleId.ItemsRo.ToArray()).Replace("-", string.Empty).ToUpper()[..16];
  149. }
  150. }
  151. }