TitleUpdateWindow.axaml.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using Avalonia;
  2. using Avalonia.Collections;
  3. using Avalonia.Controls;
  4. using Avalonia.Markup.Xaml;
  5. using LibHac.Common;
  6. using LibHac.Fs;
  7. using LibHac.Fs.Fsa;
  8. using LibHac.FsSystem;
  9. using LibHac.Tools.FsSystem.NcaUtils;
  10. using LibHac.Ns;
  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.Diagnostics;
  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. using LibHac.Tools.FsSystem;
  27. using Avalonia.Threading;
  28. namespace Ryujinx.Ava.Ui.Windows
  29. {
  30. public partial class TitleUpdateWindow : StyleableWindow
  31. {
  32. private readonly string _updateJsonPath;
  33. private TitleUpdateMetadata _titleUpdateWindowData;
  34. public VirtualFileSystem VirtualFileSystem { get; }
  35. internal AvaloniaList<TitleUpdateModel> TitleUpdates { get; set; } = new AvaloniaList<TitleUpdateModel>();
  36. public string TitleId { get; }
  37. public string TitleName { get; }
  38. public string Heading => string.Format(LocaleManager.Instance["GameUpdateWindowHeading"], TitleName, TitleId.ToUpper());
  39. public TitleUpdateWindow()
  40. {
  41. DataContext = this;
  42. InitializeComponent();
  43. AttachDebugDevTools();
  44. Title = $"Ryujinx {Program.Version} - " + LocaleManager.Instance["UpdateWindowTitle"];
  45. }
  46. public TitleUpdateWindow(VirtualFileSystem virtualFileSystem, string titleId, string titleName)
  47. {
  48. VirtualFileSystem = virtualFileSystem;
  49. TitleId = titleId;
  50. TitleName = titleName;
  51. _updateJsonPath = Path.Combine(AppDataManager.GamesDirPath, titleId, "updates.json");
  52. try
  53. {
  54. _titleUpdateWindowData = JsonHelper.DeserializeFromFile<TitleUpdateMetadata>(_updateJsonPath);
  55. }
  56. catch
  57. {
  58. _titleUpdateWindowData = new TitleUpdateMetadata {Selected = "", Paths = new List<string>()};
  59. }
  60. DataContext = this;
  61. InitializeComponent();
  62. AttachDebugDevTools();
  63. Title = $"Ryujinx {Program.Version} - " + LocaleManager.Instance["UpdateWindowTitle"];
  64. LoadUpdates();
  65. }
  66. [Conditional("DEBUG")]
  67. private void AttachDebugDevTools()
  68. {
  69. this.AttachDevTools();
  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. {
  103. PartitionFileSystem nsp = new PartitionFileSystem(file.AsStorage());
  104. try
  105. {
  106. (Nca patchNca, Nca controlNca) =
  107. ApplicationLoader.GetGameUpdateDataFromPartition(VirtualFileSystem, nsp, TitleId, 0);
  108. if (controlNca != null && patchNca != null)
  109. {
  110. ApplicationControlProperty controlData = new ApplicationControlProperty();
  111. using var nacpFile = new UniqueRef<IFile>();
  112. controlNca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.None)
  113. .OpenFile(ref nacpFile.Ref(), "/control.nacp".ToU8Span(), OpenMode.Read)
  114. .ThrowIfFailure();
  115. nacpFile.Get.Read(out _, 0, SpanHelpers.AsByteSpan(ref controlData), ReadOption.None)
  116. .ThrowIfFailure();
  117. TitleUpdates.Add(new TitleUpdateModel(controlData, path));
  118. }
  119. else
  120. {
  121. Dispatcher.UIThread.Post(async () =>
  122. {
  123. await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance["DialogUpdateAddUpdateErrorMessage"]);
  124. });
  125. }
  126. }
  127. catch (Exception ex)
  128. {
  129. Dispatcher.UIThread.Post(async () =>
  130. {
  131. await ContentDialogHelper.CreateErrorDialog(string.Format(LocaleManager.Instance["DialogDlcLoadNcaErrorMessage"], ex.Message, path));
  132. });
  133. }
  134. }
  135. }
  136. }
  137. private void RemoveUpdates(bool removeSelectedOnly = false)
  138. {
  139. if (removeSelectedOnly)
  140. {
  141. TitleUpdates.RemoveAll(TitleUpdates.Where(x => x.IsEnabled && !x.IsNoUpdate).ToList());
  142. }
  143. else
  144. {
  145. TitleUpdates.RemoveAll(TitleUpdates.Where(x => !x.IsNoUpdate).ToList());
  146. }
  147. TitleUpdates.FirstOrDefault(x => x.IsNoUpdate).IsEnabled = true;
  148. SortUpdates();
  149. }
  150. public void RemoveSelected()
  151. {
  152. RemoveUpdates(true);
  153. }
  154. public void RemoveAll()
  155. {
  156. RemoveUpdates();
  157. }
  158. public async void Add()
  159. {
  160. OpenFileDialog dialog = new OpenFileDialog() { Title = LocaleManager.Instance["SelectUpdateDialogTitle"], AllowMultiple = true };
  161. dialog.Filters.Add(new FileDialogFilter { Name = "NSP", Extensions = { "nsp" } });
  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())
  186. .CompareTo(Version.Parse(second.Control.DisplayVersionString.ToString())) * -1;
  187. });
  188. TitleUpdates.Clear();
  189. TitleUpdates.AddRange(list);
  190. }
  191. public void Save()
  192. {
  193. _titleUpdateWindowData.Paths.Clear();
  194. _titleUpdateWindowData.Selected = "";
  195. foreach (TitleUpdateModel update in TitleUpdates)
  196. {
  197. _titleUpdateWindowData.Paths.Add(update.Path);
  198. if (update.IsEnabled)
  199. {
  200. _titleUpdateWindowData.Selected = update.Path;
  201. }
  202. }
  203. using (FileStream dlcJsonStream = File.Create(_updateJsonPath, 4096, FileOptions.WriteThrough))
  204. {
  205. dlcJsonStream.Write(Encoding.UTF8.GetBytes(JsonHelper.Serialize(_titleUpdateWindowData, true)));
  206. }
  207. if (Owner is MainWindow window)
  208. {
  209. window.ViewModel.LoadApplications();
  210. }
  211. Close();
  212. }
  213. }
  214. }