DownloadableContentManagerViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. using Avalonia.Collections;
  2. using Avalonia.Controls;
  3. using Avalonia.Controls.ApplicationLifetimes;
  4. using Avalonia.Threading;
  5. using DynamicData;
  6. using LibHac.Common;
  7. using LibHac.Fs;
  8. using LibHac.Fs.Fsa;
  9. using LibHac.FsSystem;
  10. using LibHac.Tools.Fs;
  11. using LibHac.Tools.FsSystem;
  12. using LibHac.Tools.FsSystem.NcaUtils;
  13. using Ryujinx.Ava.Common.Locale;
  14. using Ryujinx.Ava.UI.Helpers;
  15. using Ryujinx.Ava.UI.Models;
  16. using Ryujinx.Common.Configuration;
  17. using Ryujinx.Common.Logging;
  18. using Ryujinx.Common.Utilities;
  19. using Ryujinx.HLE.FileSystem;
  20. using System;
  21. using System.Collections.Generic;
  22. using System.IO;
  23. using System.Linq;
  24. using System.Threading.Tasks;
  25. using Path = System.IO.Path;
  26. namespace Ryujinx.Ava.UI.ViewModels
  27. {
  28. public class DownloadableContentManagerViewModel : BaseModel
  29. {
  30. private readonly List<DownloadableContentContainer> _downloadableContentContainerList;
  31. private readonly string _downloadableContentJsonPath;
  32. private VirtualFileSystem _virtualFileSystem;
  33. private AvaloniaList<DownloadableContentModel> _downloadableContents = new();
  34. private AvaloniaList<DownloadableContentModel> _views = new();
  35. private AvaloniaList<DownloadableContentModel> _selectedDownloadableContents = new();
  36. private string _search;
  37. private ulong _titleId;
  38. private string _titleName;
  39. private static readonly DownloadableContentJsonSerializerContext SerializerContext = new(JsonHelper.GetDefaultSerializerOptions());
  40. public AvaloniaList<DownloadableContentModel> DownloadableContents
  41. {
  42. get => _downloadableContents;
  43. set
  44. {
  45. _downloadableContents = value;
  46. OnPropertyChanged();
  47. OnPropertyChanged(nameof(UpdateCount));
  48. Sort();
  49. }
  50. }
  51. public AvaloniaList<DownloadableContentModel> Views
  52. {
  53. get => _views;
  54. set
  55. {
  56. _views = value;
  57. OnPropertyChanged();
  58. }
  59. }
  60. public AvaloniaList<DownloadableContentModel> SelectedDownloadableContents
  61. {
  62. get => _selectedDownloadableContents;
  63. set
  64. {
  65. _selectedDownloadableContents = value;
  66. OnPropertyChanged();
  67. }
  68. }
  69. public string Search
  70. {
  71. get => _search;
  72. set
  73. {
  74. _search = value;
  75. OnPropertyChanged();
  76. Sort();
  77. }
  78. }
  79. public string UpdateCount
  80. {
  81. get => string.Format(LocaleManager.Instance[LocaleKeys.DlcWindowHeading], DownloadableContents.Count);
  82. }
  83. public DownloadableContentManagerViewModel(VirtualFileSystem virtualFileSystem, ulong titleId, string titleName)
  84. {
  85. _virtualFileSystem = virtualFileSystem;
  86. _titleId = titleId;
  87. _titleName = titleName;
  88. _downloadableContentJsonPath = Path.Combine(AppDataManager.GamesDirPath, titleId.ToString("x16"), "dlc.json");
  89. try
  90. {
  91. _downloadableContentContainerList = JsonHelper.DeserializeFromFile(_downloadableContentJsonPath, SerializerContext.ListDownloadableContentContainer);
  92. }
  93. catch
  94. {
  95. Logger.Error?.Print(LogClass.Configuration, "Downloadable Content JSON failed to deserialize.");
  96. _downloadableContentContainerList = new List<DownloadableContentContainer>();
  97. }
  98. LoadDownloadableContents();
  99. }
  100. private void LoadDownloadableContents()
  101. {
  102. foreach (DownloadableContentContainer downloadableContentContainer in _downloadableContentContainerList)
  103. {
  104. if (File.Exists(downloadableContentContainer.ContainerPath))
  105. {
  106. using FileStream containerFile = File.OpenRead(downloadableContentContainer.ContainerPath);
  107. PartitionFileSystem partitionFileSystem = new(containerFile.AsStorage());
  108. _virtualFileSystem.ImportTickets(partitionFileSystem);
  109. foreach (DownloadableContentNca downloadableContentNca in downloadableContentContainer.DownloadableContentNcaList)
  110. {
  111. using UniqueRef<IFile> ncaFile = new();
  112. partitionFileSystem.OpenFile(ref ncaFile.Ref, downloadableContentNca.FullPath.ToU8Span(), OpenMode.Read).ThrowIfFailure();
  113. Nca nca = TryOpenNca(ncaFile.Get.AsStorage(), downloadableContentContainer.ContainerPath);
  114. if (nca != null)
  115. {
  116. var content = new DownloadableContentModel(nca.Header.TitleId.ToString("X16"),
  117. downloadableContentContainer.ContainerPath,
  118. downloadableContentNca.FullPath,
  119. downloadableContentNca.Enabled);
  120. DownloadableContents.Add(content);
  121. if (content.Enabled)
  122. {
  123. SelectedDownloadableContents.Add(content);
  124. }
  125. OnPropertyChanged(nameof(UpdateCount));
  126. }
  127. }
  128. }
  129. }
  130. // NOTE: Save the list again to remove leftovers.
  131. Save();
  132. Sort();
  133. }
  134. public void Sort()
  135. {
  136. DownloadableContents.AsObservableChangeSet()
  137. .Filter(Filter)
  138. .Bind(out var view).AsObservableList();
  139. _views.Clear();
  140. _views.AddRange(view);
  141. OnPropertyChanged(nameof(Views));
  142. }
  143. private bool Filter(object arg)
  144. {
  145. if (arg is DownloadableContentModel content)
  146. {
  147. return string.IsNullOrWhiteSpace(_search) || content.FileName.ToLower().Contains(_search.ToLower()) || content.TitleId.ToLower().Contains(_search.ToLower());
  148. }
  149. return false;
  150. }
  151. private Nca TryOpenNca(IStorage ncaStorage, string containerPath)
  152. {
  153. try
  154. {
  155. return new Nca(_virtualFileSystem.KeySet, ncaStorage);
  156. }
  157. catch (Exception ex)
  158. {
  159. Dispatcher.UIThread.InvokeAsync(async () =>
  160. {
  161. await ContentDialogHelper.CreateErrorDialog(string.Format(LocaleManager.Instance[LocaleKeys.DialogLoadNcaErrorMessage], ex.Message, containerPath));
  162. });
  163. }
  164. return null;
  165. }
  166. public async void Add()
  167. {
  168. OpenFileDialog dialog = new OpenFileDialog()
  169. {
  170. Title = LocaleManager.Instance[LocaleKeys.SelectDlcDialogTitle],
  171. AllowMultiple = true
  172. };
  173. dialog.Filters.Add(new FileDialogFilter
  174. {
  175. Name = "NSP",
  176. Extensions = { "nsp" }
  177. });
  178. if (Avalonia.Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
  179. {
  180. string[] files = await dialog.ShowAsync(desktop.MainWindow);
  181. if (files != null)
  182. {
  183. foreach (string file in files)
  184. {
  185. await AddDownloadableContent(file);
  186. }
  187. }
  188. }
  189. }
  190. private async Task AddDownloadableContent(string path)
  191. {
  192. if (!File.Exists(path) || DownloadableContents.FirstOrDefault(x => x.ContainerPath == path) != null)
  193. {
  194. return;
  195. }
  196. using FileStream containerFile = File.OpenRead(path);
  197. PartitionFileSystem partitionFileSystem = new(containerFile.AsStorage());
  198. bool containsDownloadableContent = false;
  199. _virtualFileSystem.ImportTickets(partitionFileSystem);
  200. foreach (DirectoryEntryEx fileEntry in partitionFileSystem.EnumerateEntries("/", "*.nca"))
  201. {
  202. using var ncaFile = new UniqueRef<IFile>();
  203. partitionFileSystem.OpenFile(ref ncaFile.Ref, fileEntry.FullPath.ToU8Span(), OpenMode.Read).ThrowIfFailure();
  204. Nca nca = TryOpenNca(ncaFile.Get.AsStorage(), path);
  205. if (nca == null)
  206. {
  207. continue;
  208. }
  209. if (nca.Header.ContentType == NcaContentType.PublicData)
  210. {
  211. if ((nca.Header.TitleId & 0xFFFFFFFFFFFFE000) != _titleId)
  212. {
  213. break;
  214. }
  215. var content = new DownloadableContentModel(nca.Header.TitleId.ToString("X16"), path, fileEntry.FullPath, true);
  216. DownloadableContents.Add(content);
  217. SelectedDownloadableContents.Add(content);
  218. OnPropertyChanged(nameof(UpdateCount));
  219. Sort();
  220. containsDownloadableContent = true;
  221. }
  222. }
  223. if (!containsDownloadableContent)
  224. {
  225. await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance[LocaleKeys.DialogDlcNoDlcErrorMessage]);
  226. }
  227. }
  228. public void Remove(DownloadableContentModel model)
  229. {
  230. DownloadableContents.Remove(model);
  231. OnPropertyChanged(nameof(UpdateCount));
  232. Sort();
  233. }
  234. public void RemoveAll()
  235. {
  236. DownloadableContents.Clear();
  237. OnPropertyChanged(nameof(UpdateCount));
  238. Sort();
  239. }
  240. public void EnableAll()
  241. {
  242. SelectedDownloadableContents = new(DownloadableContents);
  243. }
  244. public void DisableAll()
  245. {
  246. SelectedDownloadableContents.Clear();
  247. }
  248. public void Save()
  249. {
  250. _downloadableContentContainerList.Clear();
  251. DownloadableContentContainer container = default;
  252. foreach (DownloadableContentModel downloadableContent in DownloadableContents)
  253. {
  254. if (container.ContainerPath != downloadableContent.ContainerPath)
  255. {
  256. if (!string.IsNullOrWhiteSpace(container.ContainerPath))
  257. {
  258. _downloadableContentContainerList.Add(container);
  259. }
  260. container = new DownloadableContentContainer
  261. {
  262. ContainerPath = downloadableContent.ContainerPath,
  263. DownloadableContentNcaList = new List<DownloadableContentNca>()
  264. };
  265. }
  266. container.DownloadableContentNcaList.Add(new DownloadableContentNca
  267. {
  268. Enabled = downloadableContent.Enabled,
  269. TitleId = Convert.ToUInt64(downloadableContent.TitleId, 16),
  270. FullPath = downloadableContent.FullPath
  271. });
  272. }
  273. if (!string.IsNullOrWhiteSpace(container.ContainerPath))
  274. {
  275. _downloadableContentContainerList.Add(container);
  276. }
  277. JsonHelper.SerializeToFile(_downloadableContentJsonPath, _downloadableContentContainerList, SerializerContext.ListDownloadableContentContainer);
  278. }
  279. }
  280. }