TitleUpdateWindow.cs 8.5 KB

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