DownloadableContentManagerWindow.axaml.cs 11 KB

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