TitleUpdateWindow.axaml.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 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; }
  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 InitializeComponent()
  72. {
  73. TitleUpdates = new AvaloniaList<TitleUpdateModel>();
  74. AvaloniaXamlLoader.Load(this);
  75. }
  76. private void LoadUpdates()
  77. {
  78. TitleUpdates.Add(new TitleUpdateModel(default, string.Empty, true));
  79. foreach (string path in _titleUpdateWindowData.Paths)
  80. {
  81. AddUpdate(path);
  82. }
  83. if (_titleUpdateWindowData.Selected == "")
  84. {
  85. TitleUpdates[0].IsEnabled = true;
  86. }
  87. else
  88. {
  89. TitleUpdateModel selected = TitleUpdates.FirstOrDefault(x => x.Path == _titleUpdateWindowData.Selected);
  90. List<TitleUpdateModel> enabled = TitleUpdates.Where(x => x.IsEnabled).ToList();
  91. foreach (TitleUpdateModel update in enabled)
  92. {
  93. update.IsEnabled = false;
  94. }
  95. if (selected != null)
  96. {
  97. selected.IsEnabled = true;
  98. }
  99. }
  100. SortUpdates();
  101. }
  102. private void AddUpdate(string path)
  103. {
  104. if (File.Exists(path) && !TitleUpdates.Any(x => x.Path == path))
  105. {
  106. using (FileStream file = new(path, FileMode.Open, FileAccess.Read))
  107. {
  108. PartitionFileSystem nsp = new PartitionFileSystem(file.AsStorage());
  109. try
  110. {
  111. (Nca patchNca, Nca controlNca) =
  112. ApplicationLoader.GetGameUpdateDataFromPartition(VirtualFileSystem, nsp, TitleId, 0);
  113. if (controlNca != null && patchNca != null)
  114. {
  115. ApplicationControlProperty controlData = new ApplicationControlProperty();
  116. using var nacpFile = new UniqueRef<IFile>();
  117. controlNca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.None)
  118. .OpenFile(ref nacpFile.Ref(), "/control.nacp".ToU8Span(), OpenMode.Read)
  119. .ThrowIfFailure();
  120. nacpFile.Get.Read(out _, 0, SpanHelpers.AsByteSpan(ref controlData), ReadOption.None)
  121. .ThrowIfFailure();
  122. TitleUpdates.Add(new TitleUpdateModel(controlData, path));
  123. }
  124. else
  125. {
  126. Dispatcher.UIThread.Post(async () =>
  127. {
  128. await ContentDialogHelper.CreateErrorDialog(this,
  129. LocaleManager.Instance["DialogUpdateAddUpdateErrorMessage"]);
  130. });
  131. }
  132. }
  133. catch (Exception ex)
  134. {
  135. Dispatcher.UIThread.Post(async () =>
  136. {
  137. await ContentDialogHelper.CreateErrorDialog(this,
  138. string.Format(LocaleManager.Instance["DialogDlcLoadNcaErrorMessage"], ex.Message, path));
  139. });
  140. }
  141. }
  142. }
  143. }
  144. private void RemoveUpdates(bool removeSelectedOnly = false)
  145. {
  146. if (removeSelectedOnly)
  147. {
  148. TitleUpdates.RemoveAll(TitleUpdates.Where(x => x.IsEnabled && !x.IsNoUpdate).ToList());
  149. }
  150. else
  151. {
  152. TitleUpdates.RemoveAll(TitleUpdates.Where(x => !x.IsNoUpdate).ToList());
  153. }
  154. TitleUpdates.FirstOrDefault(x => x.IsNoUpdate).IsEnabled = true;
  155. SortUpdates();
  156. }
  157. public void RemoveSelected()
  158. {
  159. RemoveUpdates(true);
  160. }
  161. public void RemoveAll()
  162. {
  163. RemoveUpdates();
  164. }
  165. public async void Add()
  166. {
  167. OpenFileDialog dialog = new OpenFileDialog() { Title = LocaleManager.Instance["SelectUpdateDialogTitle"], AllowMultiple = true };
  168. dialog.Filters.Add(new FileDialogFilter { Name = "NSP", Extensions = { "nsp" } });
  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. }
  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())
  193. .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 dlcJsonStream = File.Create(_updateJsonPath, 4096, FileOptions.WriteThrough))
  211. {
  212. dlcJsonStream.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. }