DlcWindow.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using Gtk;
  2. using LibHac.Common;
  3. using LibHac.Fs;
  4. using LibHac.Fs.Fsa;
  5. using LibHac.FsSystem;
  6. using LibHac.Tools.Fs;
  7. using LibHac.Tools.FsSystem;
  8. using LibHac.Tools.FsSystem.NcaUtils;
  9. using Ryujinx.Common.Configuration;
  10. using Ryujinx.Common.Utilities;
  11. using Ryujinx.HLE.FileSystem;
  12. using Ryujinx.Ui.Widgets;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.IO;
  16. using GUI = Gtk.Builder.ObjectAttribute;
  17. namespace Ryujinx.Ui.Windows
  18. {
  19. public class DlcWindow : Window
  20. {
  21. private readonly VirtualFileSystem _virtualFileSystem;
  22. private readonly string _titleId;
  23. private readonly string _dlcJsonPath;
  24. private readonly List<DownloadableContentContainer> _dlcContainerList;
  25. private static readonly DownloadableContentJsonSerializerContext SerializerContext = new(JsonHelper.GetDefaultSerializerOptions());
  26. #pragma warning disable CS0649, IDE0044
  27. [GUI] Label _baseTitleInfoLabel;
  28. [GUI] TreeView _dlcTreeView;
  29. [GUI] TreeSelection _dlcTreeSelection;
  30. #pragma warning restore CS0649, IDE0044
  31. public DlcWindow(VirtualFileSystem virtualFileSystem, string titleId, string titleName) : this(new Builder("Ryujinx.Ui.Windows.DlcWindow.glade"), virtualFileSystem, titleId, titleName) { }
  32. private DlcWindow(Builder builder, VirtualFileSystem virtualFileSystem, string titleId, string titleName) : base(builder.GetRawOwnedObject("_dlcWindow"))
  33. {
  34. builder.Autoconnect(this);
  35. _titleId = titleId;
  36. _virtualFileSystem = virtualFileSystem;
  37. _dlcJsonPath = System.IO.Path.Combine(AppDataManager.GamesDirPath, _titleId, "dlc.json");
  38. _baseTitleInfoLabel.Text = $"DLC Available for {titleName} [{titleId.ToUpper()}]";
  39. try
  40. {
  41. _dlcContainerList = JsonHelper.DeserializeFromFile(_dlcJsonPath, SerializerContext.ListDownloadableContentContainer);
  42. }
  43. catch
  44. {
  45. _dlcContainerList = new List<DownloadableContentContainer>();
  46. }
  47. _dlcTreeView.Model = new TreeStore(typeof(bool), typeof(string), typeof(string));
  48. CellRendererToggle enableToggle = new CellRendererToggle();
  49. enableToggle.Toggled += (sender, args) =>
  50. {
  51. _dlcTreeView.Model.GetIter(out TreeIter treeIter, new TreePath(args.Path));
  52. bool newValue = !(bool)_dlcTreeView.Model.GetValue(treeIter, 0);
  53. _dlcTreeView.Model.SetValue(treeIter, 0, newValue);
  54. if (_dlcTreeView.Model.IterChildren(out TreeIter childIter, treeIter))
  55. {
  56. do
  57. {
  58. _dlcTreeView.Model.SetValue(childIter, 0, newValue);
  59. }
  60. while (_dlcTreeView.Model.IterNext(ref childIter));
  61. }
  62. };
  63. _dlcTreeView.AppendColumn("Enabled", enableToggle, "active", 0);
  64. _dlcTreeView.AppendColumn("TitleId", new CellRendererText(), "text", 1);
  65. _dlcTreeView.AppendColumn("Path", new CellRendererText(), "text", 2);
  66. foreach (DownloadableContentContainer dlcContainer in _dlcContainerList)
  67. {
  68. if (File.Exists(dlcContainer.ContainerPath))
  69. {
  70. // The parent tree item has its own "enabled" check box, but it's the actual
  71. // nca entries that store the enabled / disabled state. A bit of a UI inconsistency.
  72. // Maybe a tri-state check box would be better, but for now we check the parent
  73. // "enabled" box if all child NCAs are enabled. Usually fine since each nsp has only one nca.
  74. bool areAllContentPacksEnabled = dlcContainer.DownloadableContentNcaList.TrueForAll((nca) => nca.Enabled);
  75. TreeIter parentIter = ((TreeStore)_dlcTreeView.Model).AppendValues(areAllContentPacksEnabled, "", dlcContainer.ContainerPath);
  76. using FileStream containerFile = File.OpenRead(dlcContainer.ContainerPath);
  77. PartitionFileSystem pfs = new PartitionFileSystem(containerFile.AsStorage());
  78. _virtualFileSystem.ImportTickets(pfs);
  79. foreach (DownloadableContentNca dlcNca in dlcContainer.DownloadableContentNcaList)
  80. {
  81. using var ncaFile = new UniqueRef<IFile>();
  82. pfs.OpenFile(ref ncaFile.Ref, dlcNca.FullPath.ToU8Span(), OpenMode.Read).ThrowIfFailure();
  83. Nca nca = TryCreateNca(ncaFile.Get.AsStorage(), dlcContainer.ContainerPath);
  84. if (nca != null)
  85. {
  86. ((TreeStore)_dlcTreeView.Model).AppendValues(parentIter, dlcNca.Enabled, nca.Header.TitleId.ToString("X16"), dlcNca.FullPath);
  87. }
  88. }
  89. }
  90. else
  91. {
  92. // DLC file moved or renamed. Allow the user to remove it without crashing the whole dialog.
  93. TreeIter parentIter = ((TreeStore)_dlcTreeView.Model).AppendValues(false, "", $"(MISSING) {dlcContainer.ContainerPath}");
  94. }
  95. }
  96. }
  97. private Nca TryCreateNca(IStorage ncaStorage, string containerPath)
  98. {
  99. try
  100. {
  101. return new Nca(_virtualFileSystem.KeySet, ncaStorage);
  102. }
  103. catch (Exception exception)
  104. {
  105. GtkDialog.CreateErrorDialog($"{exception.Message}. Errored File: {containerPath}");
  106. }
  107. return null;
  108. }
  109. private void AddButton_Clicked(object sender, EventArgs args)
  110. {
  111. FileChooserNative fileChooser = new FileChooserNative("Select DLC files", this, FileChooserAction.Open, "Add", "Cancel")
  112. {
  113. SelectMultiple = true
  114. };
  115. FileFilter filter = new FileFilter()
  116. {
  117. Name = "Switch Game DLCs"
  118. };
  119. filter.AddPattern("*.nsp");
  120. fileChooser.AddFilter(filter);
  121. if (fileChooser.Run() == (int)ResponseType.Accept)
  122. {
  123. foreach (string containerPath in fileChooser.Filenames)
  124. {
  125. if (!File.Exists(containerPath))
  126. {
  127. return;
  128. }
  129. using (FileStream containerFile = File.OpenRead(containerPath))
  130. {
  131. PartitionFileSystem pfs = new PartitionFileSystem(containerFile.AsStorage());
  132. bool containsDlc = false;
  133. _virtualFileSystem.ImportTickets(pfs);
  134. TreeIter? parentIter = null;
  135. foreach (DirectoryEntryEx fileEntry in pfs.EnumerateEntries("/", "*.nca"))
  136. {
  137. using var ncaFile = new UniqueRef<IFile>();
  138. pfs.OpenFile(ref ncaFile.Ref, fileEntry.FullPath.ToU8Span(), OpenMode.Read).ThrowIfFailure();
  139. Nca nca = TryCreateNca(ncaFile.Get.AsStorage(), containerPath);
  140. if (nca == null) continue;
  141. if (nca.Header.ContentType == NcaContentType.PublicData)
  142. {
  143. if ((nca.Header.TitleId & 0xFFFFFFFFFFFFE000).ToString("x16") != _titleId)
  144. {
  145. break;
  146. }
  147. parentIter ??= ((TreeStore)_dlcTreeView.Model).AppendValues(true, "", containerPath);
  148. ((TreeStore)_dlcTreeView.Model).AppendValues(parentIter.Value, true, nca.Header.TitleId.ToString("X16"), fileEntry.FullPath);
  149. containsDlc = true;
  150. }
  151. }
  152. if (!containsDlc)
  153. {
  154. GtkDialog.CreateErrorDialog("The specified file does not contain DLC for the selected title!");
  155. }
  156. }
  157. }
  158. }
  159. fileChooser.Dispose();
  160. }
  161. private void RemoveButton_Clicked(object sender, EventArgs args)
  162. {
  163. if (_dlcTreeSelection.GetSelected(out ITreeModel treeModel, out TreeIter treeIter))
  164. {
  165. if (_dlcTreeView.Model.IterParent(out TreeIter parentIter, treeIter) && _dlcTreeView.Model.IterNChildren(parentIter) <= 1)
  166. {
  167. ((TreeStore)treeModel).Remove(ref parentIter);
  168. }
  169. else
  170. {
  171. ((TreeStore)treeModel).Remove(ref treeIter);
  172. }
  173. }
  174. }
  175. private void RemoveAllButton_Clicked(object sender, EventArgs args)
  176. {
  177. List<TreeIter> toRemove = new List<TreeIter>();
  178. if (_dlcTreeView.Model.GetIterFirst(out TreeIter iter))
  179. {
  180. do
  181. {
  182. toRemove.Add(iter);
  183. }
  184. while (_dlcTreeView.Model.IterNext(ref iter));
  185. }
  186. foreach (TreeIter i in toRemove)
  187. {
  188. TreeIter j = i;
  189. ((TreeStore)_dlcTreeView.Model).Remove(ref j);
  190. }
  191. }
  192. private void SaveButton_Clicked(object sender, EventArgs args)
  193. {
  194. _dlcContainerList.Clear();
  195. if (_dlcTreeView.Model.GetIterFirst(out TreeIter parentIter))
  196. {
  197. do
  198. {
  199. if (_dlcTreeView.Model.IterChildren(out TreeIter childIter, parentIter))
  200. {
  201. DownloadableContentContainer dlcContainer = new DownloadableContentContainer
  202. {
  203. ContainerPath = (string)_dlcTreeView.Model.GetValue(parentIter, 2),
  204. DownloadableContentNcaList = new List<DownloadableContentNca>()
  205. };
  206. do
  207. {
  208. dlcContainer.DownloadableContentNcaList.Add(new DownloadableContentNca
  209. {
  210. Enabled = (bool)_dlcTreeView.Model.GetValue(childIter, 0),
  211. TitleId = Convert.ToUInt64(_dlcTreeView.Model.GetValue(childIter, 1).ToString(), 16),
  212. FullPath = (string)_dlcTreeView.Model.GetValue(childIter, 2)
  213. });
  214. }
  215. while (_dlcTreeView.Model.IterNext(ref childIter));
  216. _dlcContainerList.Add(dlcContainer);
  217. }
  218. }
  219. while (_dlcTreeView.Model.IterNext(ref parentIter));
  220. }
  221. JsonHelper.SerializeToFile(_dlcJsonPath, _dlcContainerList, SerializerContext.ListDownloadableContentContainer);
  222. Dispose();
  223. }
  224. private void CancelButton_Clicked(object sender, EventArgs args)
  225. {
  226. Dispose();
  227. }
  228. }
  229. }