TitleUpdateWindow.axaml.cs 8.5 KB

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