TitleUpdateWindow.cs 7.6 KB

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