TitleUpdateWindow.cs 8.1 KB

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