TitleUpdateWindow.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using Gtk;
  2. using LibHac;
  3. using LibHac.Common;
  4. using LibHac.Fs;
  5. using LibHac.FsSystem;
  6. using LibHac.FsSystem.NcaUtils;
  7. using LibHac.Ns;
  8. using LibHac.Spl;
  9. using Ryujinx.Common.Configuration;
  10. using Ryujinx.Common.Logging;
  11. using Ryujinx.HLE.FileSystem;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.IO;
  15. using GUI = Gtk.Builder.ObjectAttribute;
  16. using JsonHelper = Ryujinx.Common.Utilities.JsonHelper;
  17. namespace Ryujinx.Ui
  18. {
  19. public class TitleUpdateWindow : Window
  20. {
  21. private readonly string _titleId;
  22. private readonly VirtualFileSystem _virtualFileSystem;
  23. private TitleUpdateMetadata _titleUpdateWindowData;
  24. private Dictionary<RadioButton, string> _radioButtonToPathDictionary = new Dictionary<RadioButton, string>();
  25. #pragma warning disable CS0649, IDE0044
  26. [GUI] Label _baseTitleInfoLabel;
  27. [GUI] Box _availableUpdatesBox;
  28. [GUI] RadioButton _noUpdateRadioButton;
  29. #pragma warning restore CS0649, IDE0044
  30. public TitleUpdateWindow(string titleId, string titleName, VirtualFileSystem virtualFileSystem) : this(new Builder("Ryujinx.Ui.TitleUpdateWindow.glade"), titleId, titleName, virtualFileSystem) { }
  31. private TitleUpdateWindow(Builder builder, string titleId, string titleName, VirtualFileSystem virtualFileSystem) : base(builder.GetObject("_titleUpdateWindow").Handle)
  32. {
  33. builder.Autoconnect(this);
  34. _titleId = titleId;
  35. _virtualFileSystem = virtualFileSystem;
  36. try
  37. {
  38. string path = System.IO.Path.Combine(_virtualFileSystem.GetBasePath(), "games", _titleId, "updates.json");
  39. _titleUpdateWindowData = JsonHelper.DeserializeFromFile<TitleUpdateMetadata>(path);
  40. }
  41. catch
  42. {
  43. _titleUpdateWindowData = new TitleUpdateMetadata
  44. {
  45. Selected = "",
  46. Paths = new List<string>()
  47. };
  48. }
  49. _baseTitleInfoLabel.Text = $"Updates Available for {titleName} [{titleId}]";
  50. foreach (string path in _titleUpdateWindowData.Paths)
  51. {
  52. AddUpdate(path, false);
  53. }
  54. _noUpdateRadioButton.Active = true;
  55. foreach (KeyValuePair<RadioButton, string> keyValuePair in _radioButtonToPathDictionary)
  56. {
  57. if (keyValuePair.Value == _titleUpdateWindowData.Selected)
  58. {
  59. keyValuePair.Key.Active = true;
  60. }
  61. }
  62. }
  63. private void AddUpdate(string path, bool showErrorDialog = true)
  64. {
  65. if (File.Exists(path))
  66. {
  67. using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read))
  68. {
  69. PartitionFileSystem nsp = new PartitionFileSystem(file.AsStorage());
  70. foreach (DirectoryEntryEx ticketEntry in nsp.EnumerateEntries("/", "*.tik"))
  71. {
  72. Result result = nsp.OpenFile(out IFile ticketFile, ticketEntry.FullPath.ToU8Span(), OpenMode.Read);
  73. if (result.IsSuccess())
  74. {
  75. Ticket ticket = new Ticket(ticketFile.AsStream());
  76. _virtualFileSystem.KeySet.ExternalKeySet.Add(new RightsId(ticket.RightsId), new AccessKey(ticket.GetTitleKey(_virtualFileSystem.KeySet)));
  77. }
  78. }
  79. foreach (DirectoryEntryEx fileEntry in nsp.EnumerateEntries("/", "*.nca"))
  80. {
  81. nsp.OpenFile(out IFile ncaFile, fileEntry.FullPath.ToU8Span(), OpenMode.Read).ThrowIfFailure();
  82. try
  83. {
  84. Nca nca = new Nca(_virtualFileSystem.KeySet, ncaFile.AsStorage());
  85. if ($"{nca.Header.TitleId.ToString("x16")[..^3]}000" == _titleId)
  86. {
  87. if (nca.Header.ContentType == NcaContentType.Control)
  88. {
  89. ApplicationControlProperty controlData = new ApplicationControlProperty();
  90. nca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.None).OpenFile(out IFile nacpFile, "/control.nacp".ToU8Span(), OpenMode.Read).ThrowIfFailure();
  91. nacpFile.Read(out _, 0, SpanHelpers.AsByteSpan(ref controlData), ReadOption.None).ThrowIfFailure();
  92. RadioButton radioButton = new RadioButton($"Version {controlData.DisplayVersion.ToString()} - {path}");
  93. radioButton.JoinGroup(_noUpdateRadioButton);
  94. _availableUpdatesBox.Add(radioButton);
  95. _radioButtonToPathDictionary.Add(radioButton, path);
  96. radioButton.Show();
  97. radioButton.Active = true;
  98. }
  99. }
  100. else
  101. {
  102. GtkDialog.CreateErrorDialog("The specified file does not contain an update for the selected title!");
  103. break;
  104. }
  105. }
  106. catch (InvalidDataException exception)
  107. {
  108. Logger.PrintError(LogClass.Application, $"{exception.Message}. Errored File: {path}");
  109. if (showErrorDialog)
  110. {
  111. GtkDialog.CreateInfoDialog("Ryujinx - Error", "Add Update Failed!", "The NCA header content type check has failed. This is usually because the header key is incorrect or missing.");
  112. }
  113. break;
  114. }
  115. catch (MissingKeyException exception)
  116. {
  117. Logger.PrintError(LogClass.Application, $"Your key set is missing a key with the name: {exception.Name}. Errored File: {path}");
  118. if (showErrorDialog)
  119. {
  120. GtkDialog.CreateInfoDialog("Ryujinx - Error", "Add Update Failed!", $"Your key set is missing a key with the name: {exception.Name}");
  121. }
  122. break;
  123. }
  124. }
  125. }
  126. }
  127. }
  128. private void AddButton_Clicked(object sender, EventArgs args)
  129. {
  130. FileChooserDialog fileChooser = new FileChooserDialog("Select update files", this, FileChooserAction.Open, "Cancel", ResponseType.Cancel, "Add", ResponseType.Accept)
  131. {
  132. SelectMultiple = true,
  133. Filter = new FileFilter()
  134. };
  135. fileChooser.SetPosition(WindowPosition.Center);
  136. fileChooser.Filter.AddPattern("*.nsp");
  137. if (fileChooser.Run() == (int)ResponseType.Accept)
  138. {
  139. foreach (string path in fileChooser.Filenames)
  140. {
  141. AddUpdate(path);
  142. }
  143. }
  144. fileChooser.Dispose();
  145. }
  146. private void RemoveButton_Clicked(object sender, EventArgs args)
  147. {
  148. foreach (RadioButton radioButton in _noUpdateRadioButton.Group)
  149. {
  150. if (radioButton.Label != "No Update" && radioButton.Active)
  151. {
  152. _availableUpdatesBox.Remove(radioButton);
  153. _radioButtonToPathDictionary.Remove(radioButton);
  154. radioButton.Dispose();
  155. }
  156. }
  157. }
  158. private void SaveButton_Clicked(object sender, EventArgs args)
  159. {
  160. _titleUpdateWindowData.Paths.Clear();
  161. foreach (string paths in _radioButtonToPathDictionary.Values)
  162. {
  163. _titleUpdateWindowData.Paths.Add(paths);
  164. }
  165. foreach (RadioButton radioButton in _noUpdateRadioButton.Group)
  166. {
  167. if (radioButton.Active)
  168. {
  169. _titleUpdateWindowData.Selected = _radioButtonToPathDictionary.TryGetValue(radioButton, out string updatePath) ? updatePath : "";
  170. }
  171. }
  172. string path = System.IO.Path.Combine(_virtualFileSystem.GetBasePath(), "games", _titleId, "updates.json");
  173. File.WriteAllText(path, JsonHelper.Serialize(_titleUpdateWindowData, true));
  174. MainWindow.UpdateGameTable();
  175. Dispose();
  176. }
  177. private void CancelButton_Clicked(object sender, EventArgs args)
  178. {
  179. Dispose();
  180. }
  181. }
  182. }