TitleUpdateWindow.axaml.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. namespace Ryujinx.Ava.Ui.Windows
  28. {
  29. public class TitleUpdateWindow : StyleableWindow
  30. {
  31. private readonly string _updateJsonPath;
  32. private TitleUpdateMetadata _titleUpdateWindowData;
  33. public VirtualFileSystem VirtualFileSystem { get; }
  34. internal AvaloniaList<TitleUpdateModel> TitleUpdates { get; set; }
  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. AttachDebugDevTools();
  43. Title = $"Ryujinx {Program.Version} - " + LocaleManager.Instance["UpdateWindowTitle"];
  44. }
  45. public TitleUpdateWindow(VirtualFileSystem virtualFileSystem, string titleId, string titleName)
  46. {
  47. VirtualFileSystem = virtualFileSystem;
  48. TitleId = titleId;
  49. TitleName = titleName;
  50. _updateJsonPath = Path.Combine(AppDataManager.GamesDirPath, titleId, "updates.json");
  51. try
  52. {
  53. _titleUpdateWindowData = JsonHelper.DeserializeFromFile<TitleUpdateMetadata>(_updateJsonPath);
  54. }
  55. catch
  56. {
  57. _titleUpdateWindowData = new TitleUpdateMetadata {Selected = "", Paths = new List<string>()};
  58. }
  59. DataContext = this;
  60. InitializeComponent();
  61. AttachDebugDevTools();
  62. Title = $"Ryujinx {Program.Version} - " + LocaleManager.Instance["UpdateWindowTitle"];
  63. LoadUpdates();
  64. }
  65. [Conditional("DEBUG")]
  66. private void AttachDebugDevTools()
  67. {
  68. this.AttachDevTools();
  69. }
  70. private void InitializeComponent()
  71. {
  72. TitleUpdates = new AvaloniaList<TitleUpdateModel>();
  73. AvaloniaXamlLoader.Load(this);
  74. }
  75. private void LoadUpdates()
  76. {
  77. TitleUpdates.Add(new TitleUpdateModel(default, string.Empty, true));
  78. foreach (string path in _titleUpdateWindowData.Paths)
  79. {
  80. AddUpdate(path);
  81. }
  82. if (_titleUpdateWindowData.Selected == "")
  83. {
  84. TitleUpdates[0].IsEnabled = true;
  85. }
  86. else
  87. {
  88. TitleUpdateModel selected = TitleUpdates.FirstOrDefault(x => x.Path == _titleUpdateWindowData.Selected);
  89. List<TitleUpdateModel> enabled = TitleUpdates.Where(x => x.IsEnabled).ToList();
  90. foreach (TitleUpdateModel update in enabled)
  91. {
  92. update.IsEnabled = false;
  93. }
  94. if (selected != null)
  95. {
  96. selected.IsEnabled = true;
  97. }
  98. }
  99. SortUpdates();
  100. }
  101. private void AddUpdate(string path)
  102. {
  103. if (File.Exists(path) && !TitleUpdates.Any(x => x.Path == path))
  104. {
  105. using (FileStream file = new(path, FileMode.Open, FileAccess.Read))
  106. {
  107. PartitionFileSystem nsp = new PartitionFileSystem(file.AsStorage());
  108. try
  109. {
  110. (Nca patchNca, Nca controlNca) =
  111. ApplicationLoader.GetGameUpdateDataFromPartition(VirtualFileSystem, nsp, TitleId, 0);
  112. if (controlNca != null && patchNca != null)
  113. {
  114. ApplicationControlProperty controlData = new ApplicationControlProperty();
  115. using var nacpFile = new UniqueRef<IFile>();
  116. controlNca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.None)
  117. .OpenFile(ref nacpFile.Ref(), "/control.nacp".ToU8Span(), OpenMode.Read)
  118. .ThrowIfFailure();
  119. nacpFile.Get.Read(out _, 0, SpanHelpers.AsByteSpan(ref controlData), ReadOption.None)
  120. .ThrowIfFailure();
  121. TitleUpdates.Add(new TitleUpdateModel(controlData, path));
  122. }
  123. else
  124. {
  125. ContentDialogHelper.CreateErrorDialog(this,
  126. LocaleManager.Instance["DialogUpdateAddUpdateErrorMessage"]);
  127. }
  128. }
  129. catch (Exception ex)
  130. {
  131. ContentDialogHelper.CreateErrorDialog(this,
  132. string.Format(LocaleManager.Instance["DialogDlcLoadNcaErrorMessage"], ex.Message, path));
  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. }