TitleUpdateWindow.axaml.cs 8.2 KB

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