TitleUpdateWindow.cs 7.7 KB

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