TitleUpdateWindow.cs 7.5 KB

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