TitleUpdateWindow.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using Gtk;
  2. using LibHac;
  3. using LibHac.Common;
  4. using LibHac.Fs;
  5. using LibHac.FsSystem;
  6. using LibHac.FsSystem.NcaUtils;
  7. using LibHac.Ns;
  8. using LibHac.Spl;
  9. using Ryujinx.Common.Configuration;
  10. using Ryujinx.HLE.FileSystem;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.IO;
  14. using GUI = Gtk.Builder.ObjectAttribute;
  15. using JsonHelper = Ryujinx.Common.Utilities.JsonHelper;
  16. namespace Ryujinx.Ui
  17. {
  18. public class TitleUpdateWindow : Window
  19. {
  20. private readonly string _titleId;
  21. private readonly VirtualFileSystem _virtualFileSystem;
  22. private TitleUpdateMetadata _titleUpdateWindowData;
  23. private Dictionary<RadioButton, string> _radioButtonToPathDictionary = new Dictionary<RadioButton, string>();
  24. #pragma warning disable CS0649
  25. #pragma warning disable IDE0044
  26. [GUI] Label _baseTitleInfoLabel;
  27. [GUI] Box _availableUpdatesBox;
  28. [GUI] RadioButton _noUpdateRadioButton;
  29. #pragma warning restore CS0649
  30. #pragma warning restore IDE0044
  31. public TitleUpdateWindow(string titleId, string titleName, VirtualFileSystem virtualFileSystem) : this(new Builder("Ryujinx.Ui.TitleUpdateWindow.glade"), titleId, titleName, virtualFileSystem) { }
  32. private TitleUpdateWindow(Builder builder, string titleId, string titleName, VirtualFileSystem virtualFileSystem) : base(builder.GetObject("_titleUpdateWindow").Handle)
  33. {
  34. builder.Autoconnect(this);
  35. _titleId = titleId;
  36. _virtualFileSystem = virtualFileSystem;
  37. try
  38. {
  39. string path = System.IO.Path.Combine(_virtualFileSystem.GetBasePath(), "games", _titleId, "updates.json");
  40. _titleUpdateWindowData = JsonHelper.DeserializeFromFile<TitleUpdateMetadata>(path);
  41. }
  42. catch
  43. {
  44. _titleUpdateWindowData = new TitleUpdateMetadata
  45. {
  46. Selected = "",
  47. Paths = new List<string>()
  48. };
  49. }
  50. _baseTitleInfoLabel.Text = $"Updates Available for {titleName} [{titleId}]";
  51. foreach (string path in _titleUpdateWindowData.Paths)
  52. {
  53. AddUpdate(path);
  54. }
  55. _noUpdateRadioButton.Active = true;
  56. foreach (KeyValuePair<RadioButton, string> keyValuePair in _radioButtonToPathDictionary)
  57. {
  58. if (keyValuePair.Value == _titleUpdateWindowData.Selected)
  59. {
  60. keyValuePair.Key.Active = true;
  61. }
  62. }
  63. }
  64. private void AddUpdate(string path)
  65. {
  66. if (File.Exists(path))
  67. {
  68. using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read))
  69. {
  70. PartitionFileSystem nsp = new PartitionFileSystem(file.AsStorage());
  71. foreach (DirectoryEntryEx ticketEntry in nsp.EnumerateEntries("/", "*.tik"))
  72. {
  73. Result result = nsp.OpenFile(out IFile ticketFile, ticketEntry.FullPath.ToU8Span(), OpenMode.Read);
  74. if (result.IsSuccess())
  75. {
  76. Ticket ticket = new Ticket(ticketFile.AsStream());
  77. _virtualFileSystem.KeySet.ExternalKeySet.Add(new RightsId(ticket.RightsId), new AccessKey(ticket.GetTitleKey(_virtualFileSystem.KeySet)));
  78. }
  79. }
  80. foreach (DirectoryEntryEx fileEntry in nsp.EnumerateEntries("/", "*.nca"))
  81. {
  82. nsp.OpenFile(out IFile ncaFile, fileEntry.FullPath.ToU8Span(), OpenMode.Read).ThrowIfFailure();
  83. Nca nca = new Nca(_virtualFileSystem.KeySet, ncaFile.AsStorage());
  84. if ($"{nca.Header.TitleId.ToString("x16")[..^3]}000" == _titleId)
  85. {
  86. if (nca.Header.ContentType == NcaContentType.Control)
  87. {
  88. ApplicationControlProperty controlData = new ApplicationControlProperty();
  89. nca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.None).OpenFile(out IFile nacpFile, "/control.nacp".ToU8Span(), OpenMode.Read).ThrowIfFailure();
  90. nacpFile.Read(out long _, 0, SpanHelpers.AsByteSpan(ref controlData), ReadOption.None).ThrowIfFailure();
  91. RadioButton radioButton = new RadioButton($"Version {controlData.DisplayVersion.ToString()} - {path}");
  92. radioButton.JoinGroup(_noUpdateRadioButton);
  93. _availableUpdatesBox.Add(radioButton);
  94. _radioButtonToPathDictionary.Add(radioButton, path);
  95. radioButton.Show();
  96. radioButton.Active = true;
  97. }
  98. }
  99. else
  100. {
  101. GtkDialog.CreateErrorDialog("The specified file does not contain an update for the selected title!");
  102. break;
  103. }
  104. }
  105. }
  106. }
  107. }
  108. private void AddButton_Clicked(object sender, EventArgs args)
  109. {
  110. FileChooserDialog fileChooser = new FileChooserDialog("Select update files", this, FileChooserAction.Open, "Cancel", ResponseType.Cancel, "Add", ResponseType.Accept)
  111. {
  112. SelectMultiple = true,
  113. Filter = new FileFilter()
  114. };
  115. fileChooser.SetPosition(WindowPosition.Center);
  116. fileChooser.Filter.AddPattern("*.nsp");
  117. if (fileChooser.Run() == (int)ResponseType.Accept)
  118. {
  119. foreach (string path in fileChooser.Filenames)
  120. {
  121. AddUpdate(path);
  122. }
  123. }
  124. fileChooser.Dispose();
  125. }
  126. private void RemoveButton_Clicked(object sender, EventArgs args)
  127. {
  128. foreach (RadioButton radioButton in _noUpdateRadioButton.Group)
  129. {
  130. if (radioButton.Label != "No Update" && radioButton.Active)
  131. {
  132. _availableUpdatesBox.Remove(radioButton);
  133. _radioButtonToPathDictionary.Remove(radioButton);
  134. radioButton.Dispose();
  135. }
  136. }
  137. }
  138. private void SaveButton_Clicked(object sender, EventArgs args)
  139. {
  140. _titleUpdateWindowData.Paths.Clear();
  141. foreach (string paths in _radioButtonToPathDictionary.Values)
  142. {
  143. _titleUpdateWindowData.Paths.Add(paths);
  144. }
  145. foreach (RadioButton radioButton in _noUpdateRadioButton.Group)
  146. {
  147. if (radioButton.Active)
  148. {
  149. _titleUpdateWindowData.Selected = _radioButtonToPathDictionary.TryGetValue(radioButton, out string updatePath) ? updatePath : "";
  150. }
  151. }
  152. string path = System.IO.Path.Combine(_virtualFileSystem.GetBasePath(), "games", _titleId, "updates.json");
  153. File.WriteAllText(path, JsonHelper.Serialize(_titleUpdateWindowData, true));
  154. MainWindow.UpdateGameTable();
  155. Dispose();
  156. }
  157. private void CancelButton_Clicked(object sender, EventArgs args)
  158. {
  159. Dispose();
  160. }
  161. }
  162. }