TitleUpdateWindow.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. _virtualFileSystem.ImportTickets(nsp);
  71. foreach (DirectoryEntryEx fileEntry in nsp.EnumerateEntries("/", "*.nca"))
  72. {
  73. nsp.OpenFile(out IFile ncaFile, fileEntry.FullPath.ToU8Span(), OpenMode.Read).ThrowIfFailure();
  74. try
  75. {
  76. Nca nca = new Nca(_virtualFileSystem.KeySet, ncaFile.AsStorage());
  77. if ($"{nca.Header.TitleId.ToString("x16")[..^3]}000" == _titleId)
  78. {
  79. if (nca.Header.ContentType == NcaContentType.Control)
  80. {
  81. ApplicationControlProperty controlData = new ApplicationControlProperty();
  82. nca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.None).OpenFile(out IFile nacpFile, "/control.nacp".ToU8Span(), OpenMode.Read).ThrowIfFailure();
  83. nacpFile.Read(out _, 0, SpanHelpers.AsByteSpan(ref controlData), ReadOption.None).ThrowIfFailure();
  84. RadioButton radioButton = new RadioButton($"Version {controlData.DisplayVersion.ToString()} - {path}");
  85. radioButton.JoinGroup(_noUpdateRadioButton);
  86. _availableUpdatesBox.Add(radioButton);
  87. _radioButtonToPathDictionary.Add(radioButton, path);
  88. radioButton.Show();
  89. radioButton.Active = true;
  90. }
  91. }
  92. else
  93. {
  94. GtkDialog.CreateErrorDialog("The specified file does not contain an update for the selected title!");
  95. break;
  96. }
  97. }
  98. catch (InvalidDataException exception)
  99. {
  100. Logger.PrintError(LogClass.Application, $"{exception.Message}. Errored File: {path}");
  101. if (showErrorDialog)
  102. {
  103. 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.");
  104. }
  105. break;
  106. }
  107. catch (MissingKeyException exception)
  108. {
  109. Logger.PrintError(LogClass.Application, $"Your key set is missing a key with the name: {exception.Name}. Errored File: {path}");
  110. if (showErrorDialog)
  111. {
  112. GtkDialog.CreateInfoDialog("Ryujinx - Error", "Add Update Failed!", $"Your key set is missing a key with the name: {exception.Name}");
  113. }
  114. break;
  115. }
  116. }
  117. }
  118. }
  119. }
  120. private void AddButton_Clicked(object sender, EventArgs args)
  121. {
  122. FileChooserDialog fileChooser = new FileChooserDialog("Select update files", this, FileChooserAction.Open, "Cancel", ResponseType.Cancel, "Add", ResponseType.Accept)
  123. {
  124. SelectMultiple = true,
  125. Filter = new FileFilter()
  126. };
  127. fileChooser.SetPosition(WindowPosition.Center);
  128. fileChooser.Filter.AddPattern("*.nsp");
  129. if (fileChooser.Run() == (int)ResponseType.Accept)
  130. {
  131. foreach (string path in fileChooser.Filenames)
  132. {
  133. AddUpdate(path);
  134. }
  135. }
  136. fileChooser.Dispose();
  137. }
  138. private void RemoveButton_Clicked(object sender, EventArgs args)
  139. {
  140. foreach (RadioButton radioButton in _noUpdateRadioButton.Group)
  141. {
  142. if (radioButton.Label != "No Update" && radioButton.Active)
  143. {
  144. _availableUpdatesBox.Remove(radioButton);
  145. _radioButtonToPathDictionary.Remove(radioButton);
  146. radioButton.Dispose();
  147. }
  148. }
  149. }
  150. private void SaveButton_Clicked(object sender, EventArgs args)
  151. {
  152. _titleUpdateWindowData.Paths.Clear();
  153. foreach (string paths in _radioButtonToPathDictionary.Values)
  154. {
  155. _titleUpdateWindowData.Paths.Add(paths);
  156. }
  157. foreach (RadioButton radioButton in _noUpdateRadioButton.Group)
  158. {
  159. if (radioButton.Active)
  160. {
  161. _titleUpdateWindowData.Selected = _radioButtonToPathDictionary.TryGetValue(radioButton, out string updatePath) ? updatePath : "";
  162. }
  163. }
  164. string path = System.IO.Path.Combine(_virtualFileSystem.GetBasePath(), "games", _titleId, "updates.json");
  165. File.WriteAllText(path, JsonHelper.Serialize(_titleUpdateWindowData, true));
  166. MainWindow.UpdateGameTable();
  167. Dispose();
  168. }
  169. private void CancelButton_Clicked(object sender, EventArgs args)
  170. {
  171. Dispose();
  172. }
  173. }
  174. }