TitleUpdateWindow.cs 7.8 KB

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