DlcWindow.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using Gtk;
  2. using LibHac;
  3. using LibHac.Common;
  4. using LibHac.FsSystem.NcaUtils;
  5. using LibHac.Fs;
  6. using LibHac.FsSystem;
  7. using Ryujinx.Common.Configuration;
  8. using Ryujinx.Common.Logging;
  9. using Ryujinx.HLE.FileSystem;
  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
  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(string titleId, string titleName, VirtualFileSystem virtualFileSystem) : this(new Builder("Ryujinx.Ui.DlcWindow.glade"), titleId, titleName, virtualFileSystem) { }
  30. private DlcWindow(Builder builder, string titleId, string titleName, VirtualFileSystem virtualFileSystem) : base(builder.GetObject("_dlcWindow").Handle)
  31. {
  32. builder.Autoconnect(this);
  33. _titleId = titleId;
  34. _virtualFileSystem = virtualFileSystem;
  35. _dlcJsonPath = System.IO.Path.Combine(virtualFileSystem.GetBasePath(), "games", _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(
  46. typeof(bool),
  47. typeof(string),
  48. typeof(string));
  49. CellRendererToggle enableToggle = new CellRendererToggle();
  50. enableToggle.Toggled += (sender, args) =>
  51. {
  52. _dlcTreeView.Model.GetIter(out TreeIter treeIter, new TreePath(args.Path));
  53. bool newValue = !(bool)_dlcTreeView.Model.GetValue(treeIter, 0);
  54. _dlcTreeView.Model.SetValue(treeIter, 0, newValue);
  55. if (_dlcTreeView.Model.IterChildren(out TreeIter childIter, treeIter))
  56. {
  57. do
  58. {
  59. _dlcTreeView.Model.SetValue(childIter, 0, newValue);
  60. }
  61. while (_dlcTreeView.Model.IterNext(ref childIter));
  62. }
  63. };
  64. _dlcTreeView.AppendColumn("Enabled", enableToggle, "active", 0);
  65. _dlcTreeView.AppendColumn("TitleId", new CellRendererText(), "text", 1);
  66. _dlcTreeView.AppendColumn("Path", new CellRendererText(), "text", 2);
  67. foreach (DlcContainer dlcContainer in _dlcContainerList)
  68. {
  69. TreeIter parentIter = ((TreeStore)_dlcTreeView.Model).AppendValues(false, "", dlcContainer.Path);
  70. using FileStream containerFile = File.OpenRead(dlcContainer.Path);
  71. PartitionFileSystem pfs = new PartitionFileSystem(containerFile.AsStorage());
  72. _virtualFileSystem.ImportTickets(pfs);
  73. foreach (DlcNca dlcNca in dlcContainer.DlcNcaList)
  74. {
  75. pfs.OpenFile(out IFile ncaFile, dlcNca.Path.ToU8Span(), OpenMode.Read).ThrowIfFailure();
  76. Nca nca = TryCreateNca(ncaFile.AsStorage(), dlcContainer.Path);
  77. if (nca != null)
  78. {
  79. ((TreeStore)_dlcTreeView.Model).AppendValues(parentIter, dlcNca.Enabled, nca.Header.TitleId.ToString("X16"), dlcNca.Path);
  80. }
  81. }
  82. }
  83. }
  84. private Nca TryCreateNca(IStorage ncaStorage, string containerPath)
  85. {
  86. try
  87. {
  88. return new Nca(_virtualFileSystem.KeySet, ncaStorage);
  89. }
  90. catch (InvalidDataException exception)
  91. {
  92. Logger.PrintError(LogClass.Application, $"{exception.Message}. Errored File: {containerPath}");
  93. GtkDialog.CreateInfoDialog("Ryujinx - Error", "Add DLC Failed!", "The NCA header content type check has failed. This is usually because the header key is incorrect or missing.");
  94. }
  95. catch (MissingKeyException exception)
  96. {
  97. Logger.PrintError(LogClass.Application, $"Your key set is missing a key with the name: {exception.Name}. Errored File: {containerPath}");
  98. GtkDialog.CreateInfoDialog("Ryujinx - Error", "Add DLC Failed!", $"Your key set is missing a key with the name: {exception.Name}");
  99. }
  100. return null;
  101. }
  102. private void AddButton_Clicked(object sender, EventArgs args)
  103. {
  104. FileChooserDialog fileChooser = new FileChooserDialog("Select DLC files", this, FileChooserAction.Open, "Cancel", ResponseType.Cancel, "Add", ResponseType.Accept)
  105. {
  106. SelectMultiple = true,
  107. Filter = new FileFilter()
  108. };
  109. fileChooser.SetPosition(WindowPosition.Center);
  110. fileChooser.Filter.AddPattern("*.nsp");
  111. if (fileChooser.Run() == (int)ResponseType.Accept)
  112. {
  113. foreach (string containerPath in fileChooser.Filenames)
  114. {
  115. if (!File.Exists(containerPath))
  116. {
  117. return;
  118. }
  119. using (FileStream containerFile = File.OpenRead(containerPath))
  120. {
  121. PartitionFileSystem pfs = new PartitionFileSystem(containerFile.AsStorage());
  122. bool containsDlc = false;
  123. _virtualFileSystem.ImportTickets(pfs);
  124. TreeIter? parentIter = null;
  125. foreach (DirectoryEntryEx fileEntry in pfs.EnumerateEntries("/", "*.nca"))
  126. {
  127. pfs.OpenFile(out IFile ncaFile, fileEntry.FullPath.ToU8Span(), OpenMode.Read).ThrowIfFailure();
  128. Nca nca = TryCreateNca(ncaFile.AsStorage(), containerPath);
  129. if (nca == null) continue;
  130. if (nca.Header.ContentType == NcaContentType.PublicData)
  131. {
  132. if ((nca.Header.TitleId & 0xFFFFFFFFFFFFE000).ToString("x16") != _titleId)
  133. {
  134. break;
  135. }
  136. parentIter ??= ((TreeStore)_dlcTreeView.Model).AppendValues(true, "", containerPath);
  137. ((TreeStore)_dlcTreeView.Model).AppendValues(parentIter.Value, true, nca.Header.TitleId.ToString("X16"), fileEntry.FullPath);
  138. containsDlc = true;
  139. }
  140. }
  141. if (!containsDlc)
  142. {
  143. GtkDialog.CreateErrorDialog("The specified file does not contain a DLC for the selected title!");
  144. }
  145. }
  146. }
  147. }
  148. fileChooser.Dispose();
  149. }
  150. private void RemoveButton_Clicked(object sender, EventArgs args)
  151. {
  152. if (_dlcTreeSelection.GetSelected(out ITreeModel treeModel, out TreeIter treeIter))
  153. {
  154. if (_dlcTreeView.Model.IterParent(out TreeIter parentIter, treeIter) && _dlcTreeView.Model.IterNChildren(parentIter) <= 1)
  155. {
  156. ((TreeStore)treeModel).Remove(ref parentIter);
  157. }
  158. else
  159. {
  160. ((TreeStore)treeModel).Remove(ref treeIter);
  161. }
  162. }
  163. }
  164. private void SaveButton_Clicked(object sender, EventArgs args)
  165. {
  166. _dlcContainerList.Clear();
  167. if (_dlcTreeView.Model.GetIterFirst(out TreeIter parentIter))
  168. {
  169. do
  170. {
  171. if (_dlcTreeView.Model.IterChildren(out TreeIter childIter, parentIter))
  172. {
  173. DlcContainer dlcContainer = new DlcContainer
  174. {
  175. Path = (string)_dlcTreeView.Model.GetValue(parentIter, 2),
  176. DlcNcaList = new List<DlcNca>()
  177. };
  178. do
  179. {
  180. dlcContainer.DlcNcaList.Add(new DlcNca
  181. {
  182. Enabled = (bool)_dlcTreeView.Model.GetValue(childIter, 0),
  183. TitleId = Convert.ToUInt64(_dlcTreeView.Model.GetValue(childIter, 1).ToString(), 16),
  184. Path = (string)_dlcTreeView.Model.GetValue(childIter, 2)
  185. });
  186. }
  187. while (_dlcTreeView.Model.IterNext(ref childIter));
  188. _dlcContainerList.Add(dlcContainer);
  189. }
  190. }
  191. while (_dlcTreeView.Model.IterNext(ref parentIter));
  192. }
  193. using (FileStream dlcJsonStream = File.Create(_dlcJsonPath, 4096, FileOptions.WriteThrough))
  194. {
  195. dlcJsonStream.Write(Encoding.UTF8.GetBytes(JsonHelper.Serialize(_dlcContainerList, true)));
  196. }
  197. Dispose();
  198. }
  199. private void CancelButton_Clicked(object sender, EventArgs args)
  200. {
  201. Dispose();
  202. }
  203. }
  204. }