DownloadableContentManagerWindow.axaml.cs 11 KB

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