DownloadableContentManagerWindow.axaml.cs 11 KB

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