TitleUpdateWindow.cs 7.9 KB

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