TitleUpdateWindow.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 System;
  13. using System.Collections.Generic;
  14. using System.IO;
  15. using System.Text;
  16. using GUI = Gtk.Builder.ObjectAttribute;
  17. using JsonHelper = Ryujinx.Common.Utilities.JsonHelper;
  18. namespace Ryujinx.Ui
  19. {
  20. public class TitleUpdateWindow : Window
  21. {
  22. private readonly VirtualFileSystem _virtualFileSystem;
  23. private readonly string _titleId;
  24. private readonly string _updateJsonPath;
  25. private TitleUpdateMetadata _titleUpdateWindowData;
  26. private Dictionary<RadioButton, string> _radioButtonToPathDictionary;
  27. #pragma warning disable CS0649, IDE0044
  28. [GUI] Label _baseTitleInfoLabel;
  29. [GUI] Box _availableUpdatesBox;
  30. [GUI] RadioButton _noUpdateRadioButton;
  31. #pragma warning restore CS0649, IDE0044
  32. public TitleUpdateWindow(string titleId, string titleName, VirtualFileSystem virtualFileSystem) : this(new Builder("Ryujinx.Ui.TitleUpdateWindow.glade"), titleId, titleName, virtualFileSystem) { }
  33. private TitleUpdateWindow(Builder builder, string titleId, string titleName, VirtualFileSystem virtualFileSystem) : base(builder.GetObject("_titleUpdateWindow").Handle)
  34. {
  35. builder.Autoconnect(this);
  36. _titleId = titleId;
  37. _virtualFileSystem = virtualFileSystem;
  38. _updateJsonPath = System.IO.Path.Combine(AppDataManager.GamesDirPath, _titleId, "updates.json");
  39. _radioButtonToPathDictionary = new Dictionary<RadioButton, string>();
  40. try
  41. {
  42. _titleUpdateWindowData = JsonHelper.DeserializeFromFile<TitleUpdateMetadata>(_updateJsonPath);
  43. }
  44. catch
  45. {
  46. _titleUpdateWindowData = new TitleUpdateMetadata
  47. {
  48. Selected = "",
  49. Paths = new List<string>()
  50. };
  51. }
  52. _baseTitleInfoLabel.Text = $"Updates Available for {titleName} [{titleId.ToUpper()}]";
  53. foreach (string path in _titleUpdateWindowData.Paths)
  54. {
  55. AddUpdate(path, false);
  56. }
  57. _noUpdateRadioButton.Active = true;
  58. foreach (KeyValuePair<RadioButton, string> keyValuePair in _radioButtonToPathDictionary)
  59. {
  60. if (keyValuePair.Value == _titleUpdateWindowData.Selected)
  61. {
  62. keyValuePair.Key.Active = true;
  63. }
  64. }
  65. }
  66. private void AddUpdate(string path, bool showErrorDialog = true)
  67. {
  68. if (File.Exists(path))
  69. {
  70. using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read))
  71. {
  72. PartitionFileSystem nsp = new PartitionFileSystem(file.AsStorage());
  73. _virtualFileSystem.ImportTickets(nsp);
  74. foreach (DirectoryEntryEx fileEntry in nsp.EnumerateEntries("/", "*.nca"))
  75. {
  76. nsp.OpenFile(out IFile ncaFile, fileEntry.FullPath.ToU8Span(), OpenMode.Read).ThrowIfFailure();
  77. try
  78. {
  79. Nca nca = new Nca(_virtualFileSystem.KeySet, ncaFile.AsStorage());
  80. if ($"{nca.Header.TitleId.ToString("x16")[..^3]}000" == _titleId)
  81. {
  82. if (nca.Header.ContentType == NcaContentType.Control)
  83. {
  84. ApplicationControlProperty controlData = new ApplicationControlProperty();
  85. nca.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. }
  95. else
  96. {
  97. GtkDialog.CreateErrorDialog("The specified file does not contain an update for the selected title!");
  98. break;
  99. }
  100. }
  101. catch (InvalidDataException exception)
  102. {
  103. Logger.Error?.Print(LogClass.Application, $"{exception.Message}. Errored File: {path}");
  104. if (showErrorDialog)
  105. {
  106. 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.");
  107. }
  108. break;
  109. }
  110. catch (MissingKeyException exception)
  111. {
  112. Logger.Error?.Print(LogClass.Application, $"Your key set is missing a key with the name: {exception.Name}. Errored File: {path}");
  113. if (showErrorDialog)
  114. {
  115. GtkDialog.CreateInfoDialog("Ryujinx - Error", "Add Update Failed!", $"Your key set is missing a key with the name: {exception.Name}");
  116. }
  117. break;
  118. }
  119. }
  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. foreach (RadioButton radioButton in _noUpdateRadioButton.Group)
  144. {
  145. if (radioButton.Label != "No Update" && radioButton.Active)
  146. {
  147. _availableUpdatesBox.Remove(radioButton);
  148. _radioButtonToPathDictionary.Remove(radioButton);
  149. radioButton.Dispose();
  150. }
  151. }
  152. }
  153. private void SaveButton_Clicked(object sender, EventArgs args)
  154. {
  155. _titleUpdateWindowData.Paths.Clear();
  156. foreach (string paths in _radioButtonToPathDictionary.Values)
  157. {
  158. _titleUpdateWindowData.Paths.Add(paths);
  159. }
  160. foreach (RadioButton radioButton in _noUpdateRadioButton.Group)
  161. {
  162. if (radioButton.Active)
  163. {
  164. _titleUpdateWindowData.Selected = _radioButtonToPathDictionary.TryGetValue(radioButton, out string updatePath) ? updatePath : "";
  165. }
  166. }
  167. using (FileStream dlcJsonStream = File.Create(_updateJsonPath, 4096, FileOptions.WriteThrough))
  168. {
  169. dlcJsonStream.Write(Encoding.UTF8.GetBytes(JsonHelper.Serialize(_titleUpdateWindowData, true)));
  170. }
  171. MainWindow.UpdateGameTable();
  172. Dispose();
  173. }
  174. private void CancelButton_Clicked(object sender, EventArgs args)
  175. {
  176. Dispose();
  177. }
  178. }
  179. }