TitleUpdateWindow.axaml.cs 8.2 KB

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