TitleUpdateWindow.axaml.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using Avalonia.Collections;
  2. using Avalonia.Controls;
  3. using Avalonia.Threading;
  4. using LibHac.Common;
  5. using LibHac.Fs;
  6. using LibHac.Fs.Fsa;
  7. using LibHac.FsSystem;
  8. using LibHac.Ns;
  9. using LibHac.Tools.FsSystem;
  10. using LibHac.Tools.FsSystem.NcaUtils;
  11. using Ryujinx.Ava.Common.Locale;
  12. using Ryujinx.Ava.UI.Controls;
  13. using Ryujinx.Ava.UI.Helpers;
  14. using Ryujinx.Ava.UI.Models;
  15. using Ryujinx.Common.Configuration;
  16. using Ryujinx.Common.Utilities;
  17. using Ryujinx.HLE.FileSystem;
  18. using Ryujinx.HLE.HOS;
  19. using System;
  20. using System.Collections.Generic;
  21. using System.IO;
  22. using System.Linq;
  23. using System.Text;
  24. using Path = System.IO.Path;
  25. using SpanHelpers = LibHac.Common.SpanHelpers;
  26. namespace Ryujinx.Ava.UI.Windows
  27. {
  28. public partial class TitleUpdateWindow : StyleableWindow
  29. {
  30. private readonly string _titleUpdateJsonPath;
  31. private TitleUpdateMetadata _titleUpdateWindowData;
  32. private VirtualFileSystem _virtualFileSystem { get; }
  33. private AvaloniaList<TitleUpdateModel> _titleUpdates { get; set; }
  34. private ulong _titleId { get; }
  35. private string _titleName { get; }
  36. public TitleUpdateWindow()
  37. {
  38. DataContext = this;
  39. InitializeComponent();
  40. Title = $"Ryujinx {Program.Version} - {LocaleManager.Instance[LocaleKeys.UpdateWindowTitle]} - {_titleName} ({_titleId:X16})";
  41. }
  42. public TitleUpdateWindow(VirtualFileSystem virtualFileSystem, ulong titleId, string titleName)
  43. {
  44. _virtualFileSystem = virtualFileSystem;
  45. _titleUpdates = new AvaloniaList<TitleUpdateModel>();
  46. _titleId = titleId;
  47. _titleName = titleName;
  48. _titleUpdateJsonPath = Path.Combine(AppDataManager.GamesDirPath, titleId.ToString("x16"), "updates.json");
  49. try
  50. {
  51. _titleUpdateWindowData = JsonHelper.DeserializeFromFile<TitleUpdateMetadata>(_titleUpdateJsonPath);
  52. }
  53. catch
  54. {
  55. _titleUpdateWindowData = new TitleUpdateMetadata
  56. {
  57. Selected = "",
  58. Paths = new List<string>()
  59. };
  60. }
  61. DataContext = this;
  62. InitializeComponent();
  63. Title = $"Ryujinx {Program.Version} - {LocaleManager.Instance[LocaleKeys.UpdateWindowTitle]} - {_titleName} ({_titleId:X16})";
  64. LoadUpdates();
  65. PrintHeading();
  66. }
  67. private void PrintHeading()
  68. {
  69. Heading.Text = string.Format(LocaleManager.Instance[LocaleKeys.GameUpdateWindowHeading], _titleUpdates.Count - 1, _titleName, _titleId.ToString("X16"));
  70. }
  71. private void LoadUpdates()
  72. {
  73. _titleUpdates.Add(new TitleUpdateModel(default, string.Empty, true));
  74. foreach (string path in _titleUpdateWindowData.Paths)
  75. {
  76. AddUpdate(path);
  77. }
  78. if (_titleUpdateWindowData.Selected == "")
  79. {
  80. _titleUpdates[0].IsEnabled = true;
  81. }
  82. else
  83. {
  84. TitleUpdateModel selected = _titleUpdates.FirstOrDefault(x => x.Path == _titleUpdateWindowData.Selected);
  85. List<TitleUpdateModel> enabled = _titleUpdates.Where(x => x.IsEnabled).ToList();
  86. foreach (TitleUpdateModel update in enabled)
  87. {
  88. update.IsEnabled = false;
  89. }
  90. if (selected != null)
  91. {
  92. selected.IsEnabled = true;
  93. }
  94. }
  95. SortUpdates();
  96. }
  97. private void AddUpdate(string path)
  98. {
  99. if (File.Exists(path) && !_titleUpdates.Any(x => x.Path == path))
  100. {
  101. using FileStream file = new(path, FileMode.Open, FileAccess.Read);
  102. try
  103. {
  104. (Nca patchNca, Nca controlNca) = ApplicationLoader.GetGameUpdateDataFromPartition(_virtualFileSystem, new PartitionFileSystem(file.AsStorage()), _titleId.ToString("x16"), 0);
  105. if (controlNca != null && patchNca != null)
  106. {
  107. ApplicationControlProperty controlData = new();
  108. using UniqueRef<IFile> nacpFile = new();
  109. controlNca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.None).OpenFile(ref nacpFile.Ref(), "/control.nacp".ToU8Span(), OpenMode.Read).ThrowIfFailure();
  110. nacpFile.Get.Read(out _, 0, SpanHelpers.AsByteSpan(ref controlData), ReadOption.None).ThrowIfFailure();
  111. _titleUpdates.Add(new TitleUpdateModel(controlData, path));
  112. foreach (var update in _titleUpdates)
  113. {
  114. update.IsEnabled = false;
  115. }
  116. _titleUpdates.Last().IsEnabled = true;
  117. }
  118. else
  119. {
  120. Dispatcher.UIThread.Post(async () =>
  121. {
  122. await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance[LocaleKeys.DialogUpdateAddUpdateErrorMessage]);
  123. });
  124. }
  125. }
  126. catch (Exception ex)
  127. {
  128. Dispatcher.UIThread.Post(async () =>
  129. {
  130. await ContentDialogHelper.CreateErrorDialog(string.Format(LocaleManager.Instance[LocaleKeys.DialogDlcLoadNcaErrorMessage], ex.Message, path));
  131. });
  132. }
  133. }
  134. }
  135. private void RemoveUpdates(bool removeSelectedOnly = false)
  136. {
  137. if (removeSelectedOnly)
  138. {
  139. _titleUpdates.RemoveAll(_titleUpdates.Where(x => x.IsEnabled && !x.IsNoUpdate).ToList());
  140. }
  141. else
  142. {
  143. _titleUpdates.RemoveAll(_titleUpdates.Where(x => !x.IsNoUpdate).ToList());
  144. }
  145. _titleUpdates.FirstOrDefault(x => x.IsNoUpdate).IsEnabled = true;
  146. SortUpdates();
  147. PrintHeading();
  148. }
  149. public void RemoveSelected()
  150. {
  151. RemoveUpdates(true);
  152. }
  153. public void RemoveAll()
  154. {
  155. RemoveUpdates();
  156. }
  157. public async void Add()
  158. {
  159. OpenFileDialog dialog = new()
  160. {
  161. Title = LocaleManager.Instance[LocaleKeys.SelectUpdateDialogTitle],
  162. AllowMultiple = true
  163. };
  164. dialog.Filters.Add(new FileDialogFilter
  165. {
  166. Name = "NSP",
  167. Extensions = { "nsp" }
  168. });
  169. string[] files = await dialog.ShowAsync(this);
  170. if (files != null)
  171. {
  172. foreach (string file in files)
  173. {
  174. AddUpdate(file);
  175. }
  176. }
  177. SortUpdates();
  178. PrintHeading();
  179. }
  180. private void SortUpdates()
  181. {
  182. var list = _titleUpdates.ToList();
  183. list.Sort((first, second) =>
  184. {
  185. if (string.IsNullOrEmpty(first.Control.DisplayVersionString.ToString()))
  186. {
  187. return -1;
  188. }
  189. else if (string.IsNullOrEmpty(second.Control.DisplayVersionString.ToString()))
  190. {
  191. return 1;
  192. }
  193. return Version.Parse(first.Control.DisplayVersionString.ToString()).CompareTo(Version.Parse(second.Control.DisplayVersionString.ToString())) * -1;
  194. });
  195. _titleUpdates.Clear();
  196. _titleUpdates.AddRange(list);
  197. }
  198. public void Save()
  199. {
  200. _titleUpdateWindowData.Paths.Clear();
  201. _titleUpdateWindowData.Selected = "";
  202. foreach (TitleUpdateModel update in _titleUpdates)
  203. {
  204. _titleUpdateWindowData.Paths.Add(update.Path);
  205. if (update.IsEnabled)
  206. {
  207. _titleUpdateWindowData.Selected = update.Path;
  208. }
  209. }
  210. using (FileStream titleUpdateJsonStream = File.Create(_titleUpdateJsonPath, 4096, FileOptions.WriteThrough))
  211. {
  212. titleUpdateJsonStream.Write(Encoding.UTF8.GetBytes(JsonHelper.Serialize(_titleUpdateWindowData, true)));
  213. }
  214. if (Owner is MainWindow window)
  215. {
  216. window.ViewModel.LoadApplications();
  217. }
  218. Close();
  219. }
  220. }
  221. }