DownloadableContentManagerWindow.axaml.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. using Avalonia.Collections;
  2. using Avalonia.Controls;
  3. using Avalonia.Threading;
  4. using LibHac.Common;
  5. using LibHac.Fs;
  6. using LibHac.Fs.Fsa;
  7. using LibHac.FsSystem;
  8. using LibHac.Tools.Fs;
  9. using LibHac.Tools.FsSystem;
  10. using LibHac.Tools.FsSystem.NcaUtils;
  11. using Ryujinx.Ava.Common.Locale;
  12. using Ryujinx.Ava.Ui.Controls;
  13. using Ryujinx.Ava.Ui.Models;
  14. using Ryujinx.Common.Configuration;
  15. using Ryujinx.Common.Utilities;
  16. using Ryujinx.HLE.FileSystem;
  17. using System;
  18. using System.Collections.Generic;
  19. using System.IO;
  20. using System.Linq;
  21. using System.Text;
  22. using System.Threading.Tasks;
  23. using Path = System.IO.Path;
  24. namespace Ryujinx.Ava.Ui.Windows
  25. {
  26. public partial class DownloadableContentManagerWindow : StyleableWindow
  27. {
  28. private readonly List<DownloadableContentContainer> _downloadableContentContainerList;
  29. private readonly string _downloadableContentJsonPath;
  30. public VirtualFileSystem VirtualFileSystem { get; }
  31. public AvaloniaList<DownloadableContentModel> DownloadableContents { get; set; } = new AvaloniaList<DownloadableContentModel>();
  32. public ulong TitleId { get; }
  33. public string TitleName { get; }
  34. public string Heading => string.Format(LocaleManager.Instance["DlcWindowHeading"], TitleName, TitleId.ToString("X16"));
  35. public DownloadableContentManagerWindow()
  36. {
  37. DataContext = this;
  38. InitializeComponent();
  39. Title = $"Ryujinx {Program.Version} - " + LocaleManager.Instance["DlcWindowTitle"];
  40. }
  41. public DownloadableContentManagerWindow(VirtualFileSystem virtualFileSystem, ulong titleId, string titleName)
  42. {
  43. VirtualFileSystem = virtualFileSystem;
  44. TitleId = titleId;
  45. TitleName = titleName;
  46. _downloadableContentJsonPath = Path.Combine(AppDataManager.GamesDirPath, titleId.ToString("x16"), "dlc.json");
  47. try
  48. {
  49. _downloadableContentContainerList = JsonHelper.DeserializeFromFile<List<DownloadableContentContainer>>(_downloadableContentJsonPath);
  50. }
  51. catch
  52. {
  53. _downloadableContentContainerList = new List<DownloadableContentContainer>();
  54. }
  55. DataContext = this;
  56. InitializeComponent();
  57. Title = $"Ryujinx {Program.Version} - " + LocaleManager.Instance["DlcWindowTitle"];
  58. LoadDownloadableContents();
  59. }
  60. private void LoadDownloadableContents()
  61. {
  62. foreach (DownloadableContentContainer downloadableContentContainer in _downloadableContentContainerList)
  63. {
  64. if (File.Exists(downloadableContentContainer.ContainerPath))
  65. {
  66. using FileStream containerFile = File.OpenRead(downloadableContentContainer.ContainerPath);
  67. PartitionFileSystem pfs = new PartitionFileSystem(containerFile.AsStorage());
  68. VirtualFileSystem.ImportTickets(pfs);
  69. foreach (DownloadableContentNca downloadableContentNca in downloadableContentContainer.DownloadableContentNcaList)
  70. {
  71. using var ncaFile = new UniqueRef<IFile>();
  72. pfs.OpenFile(ref ncaFile.Ref(), downloadableContentNca.FullPath.ToU8Span(), OpenMode.Read).ThrowIfFailure();
  73. Nca nca = TryCreateNca(ncaFile.Get.AsStorage(), downloadableContentContainer.ContainerPath);
  74. if (nca != null)
  75. {
  76. DownloadableContents.Add(new DownloadableContentModel(nca.Header.TitleId.ToString("X16"),
  77. downloadableContentContainer.ContainerPath,
  78. downloadableContentNca.FullPath,
  79. downloadableContentNca.Enabled));
  80. }
  81. }
  82. }
  83. }
  84. // NOTE: Save the list again to remove leftovers.
  85. Save();
  86. }
  87. private Nca TryCreateNca(IStorage ncaStorage, string containerPath)
  88. {
  89. try
  90. {
  91. return new Nca(VirtualFileSystem.KeySet, ncaStorage);
  92. }
  93. catch (Exception ex)
  94. {
  95. Dispatcher.UIThread.InvokeAsync(async () =>
  96. {
  97. await ContentDialogHelper.CreateErrorDialog(string.Format(LocaleManager.Instance["DialogDlcLoadNcaErrorMessage"], ex.Message, containerPath));
  98. });
  99. }
  100. return null;
  101. }
  102. private async Task AddDownloadableContent(string path)
  103. {
  104. if (!File.Exists(path) || DownloadableContents.FirstOrDefault(x => x.ContainerPath == path) != null)
  105. {
  106. return;
  107. }
  108. using (FileStream containerFile = File.OpenRead(path))
  109. {
  110. PartitionFileSystem pfs = new PartitionFileSystem(containerFile.AsStorage());
  111. bool containsDownloadableContent = false;
  112. VirtualFileSystem.ImportTickets(pfs);
  113. foreach (DirectoryEntryEx fileEntry in pfs.EnumerateEntries("/", "*.nca"))
  114. {
  115. using var ncaFile = new UniqueRef<IFile>();
  116. pfs.OpenFile(ref ncaFile.Ref(), fileEntry.FullPath.ToU8Span(), OpenMode.Read).ThrowIfFailure();
  117. Nca nca = TryCreateNca(ncaFile.Get.AsStorage(), path);
  118. if (nca == null)
  119. {
  120. continue;
  121. }
  122. if (nca.Header.ContentType == NcaContentType.PublicData)
  123. {
  124. if ((nca.Header.TitleId & 0xFFFFFFFFFFFFE000) != TitleId)
  125. {
  126. break;
  127. }
  128. DownloadableContents.Add(new DownloadableContentModel(nca.Header.TitleId.ToString("X16"), path, fileEntry.FullPath, true));
  129. containsDownloadableContent = true;
  130. }
  131. }
  132. if (!containsDownloadableContent)
  133. {
  134. await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance["DialogDlcNoDlcErrorMessage"]);
  135. }
  136. }
  137. }
  138. private void RemoveDownloadableContents(bool removeSelectedOnly = false)
  139. {
  140. if (removeSelectedOnly)
  141. {
  142. DownloadableContents.RemoveAll(DownloadableContents.Where(x => x.Enabled).ToList());
  143. }
  144. else
  145. {
  146. DownloadableContents.Clear();
  147. }
  148. }
  149. public void RemoveSelected()
  150. {
  151. RemoveDownloadableContents(true);
  152. }
  153. public void RemoveAll()
  154. {
  155. RemoveDownloadableContents();
  156. }
  157. public async void Add()
  158. {
  159. OpenFileDialog dialog = new OpenFileDialog()
  160. {
  161. Title = LocaleManager.Instance["SelectDlcDialogTitle"],
  162. AllowMultiple = true
  163. };
  164. dialog.Filters.Add(new FileDialogFilter
  165. {
  166. Name = "NSP",
  167. Extensions = { "nsp" }
  168. });
  169. string[] files = await dialog.ShowAsync(this);
  170. if (files != null)
  171. {
  172. foreach (string file in files)
  173. {
  174. await AddDownloadableContent(file);
  175. }
  176. }
  177. }
  178. public void Save()
  179. {
  180. _downloadableContentContainerList.Clear();
  181. DownloadableContentContainer container = default;
  182. foreach (DownloadableContentModel downloadableContent in DownloadableContents)
  183. {
  184. if (container.ContainerPath != downloadableContent.ContainerPath)
  185. {
  186. if (!string.IsNullOrWhiteSpace(container.ContainerPath))
  187. {
  188. _downloadableContentContainerList.Add(container);
  189. }
  190. container = new DownloadableContentContainer
  191. {
  192. ContainerPath = downloadableContent.ContainerPath,
  193. DownloadableContentNcaList = new List<DownloadableContentNca>()
  194. };
  195. }
  196. container.DownloadableContentNcaList.Add(new DownloadableContentNca
  197. {
  198. Enabled = downloadableContent.Enabled,
  199. TitleId = Convert.ToUInt64(downloadableContent.TitleId, 16),
  200. FullPath = downloadableContent.FullPath
  201. });
  202. }
  203. if (!string.IsNullOrWhiteSpace(container.ContainerPath))
  204. {
  205. _downloadableContentContainerList.Add(container);
  206. }
  207. using (FileStream downloadableContentJsonStream = File.Create(_downloadableContentJsonPath, 4096, FileOptions.WriteThrough))
  208. {
  209. downloadableContentJsonStream.Write(Encoding.UTF8.GetBytes(JsonHelper.Serialize(_downloadableContentContainerList, true)));
  210. }
  211. }
  212. public void SaveAndClose()
  213. {
  214. Save();
  215. Close();
  216. }
  217. }
  218. }