DlcWindow.cs 11 KB

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