TitleUpdateViewModel.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using Avalonia.Collections;
  2. using Avalonia.Controls;
  3. using Avalonia.Controls.ApplicationLifetimes;
  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.Helpers;
  14. using Ryujinx.Ava.UI.Models;
  15. using Ryujinx.Common.Configuration;
  16. using Ryujinx.Common.Logging;
  17. using Ryujinx.Common.Utilities;
  18. using Ryujinx.HLE.FileSystem;
  19. using Ryujinx.HLE.HOS;
  20. using System;
  21. using System.Collections.Generic;
  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.ViewModels
  28. {
  29. public class TitleUpdateViewModel : BaseModel
  30. {
  31. public TitleUpdateMetadata _titleUpdateWindowData;
  32. public readonly string _titleUpdateJsonPath;
  33. private VirtualFileSystem _virtualFileSystem { get; }
  34. private ulong _titleId { get; }
  35. private string _titleName { get; }
  36. private AvaloniaList<TitleUpdateModel> _titleUpdates = new();
  37. private AvaloniaList<object> _views = new();
  38. private object _selectedUpdate;
  39. private static readonly TitleUpdateMetadataJsonSerializerContext SerializerContext = new(JsonHelper.GetDefaultSerializerOptions());
  40. public AvaloniaList<TitleUpdateModel> TitleUpdates
  41. {
  42. get => _titleUpdates;
  43. set
  44. {
  45. _titleUpdates = value;
  46. OnPropertyChanged();
  47. }
  48. }
  49. public AvaloniaList<object> Views
  50. {
  51. get => _views;
  52. set
  53. {
  54. _views = value;
  55. OnPropertyChanged();
  56. }
  57. }
  58. public object SelectedUpdate
  59. {
  60. get => _selectedUpdate;
  61. set
  62. {
  63. _selectedUpdate = value;
  64. OnPropertyChanged();
  65. }
  66. }
  67. public TitleUpdateViewModel(VirtualFileSystem virtualFileSystem, ulong titleId, string titleName)
  68. {
  69. _virtualFileSystem = virtualFileSystem;
  70. _titleId = titleId;
  71. _titleName = titleName;
  72. _titleUpdateJsonPath = Path.Combine(AppDataManager.GamesDirPath, titleId.ToString("x16"), "updates.json");
  73. try
  74. {
  75. _titleUpdateWindowData = JsonHelper.DeserializeFromFile(_titleUpdateJsonPath, SerializerContext.TitleUpdateMetadata);
  76. }
  77. catch
  78. {
  79. Logger.Warning?.Print(LogClass.Application, $"Failed to deserialize title update data for {_titleId} at {_titleUpdateJsonPath}");
  80. _titleUpdateWindowData = new TitleUpdateMetadata
  81. {
  82. Selected = "",
  83. Paths = new List<string>()
  84. };
  85. Save();
  86. }
  87. LoadUpdates();
  88. }
  89. private void LoadUpdates()
  90. {
  91. foreach (string path in _titleUpdateWindowData.Paths)
  92. {
  93. AddUpdate(path);
  94. }
  95. TitleUpdateModel selected = TitleUpdates.FirstOrDefault(x => x.Path == _titleUpdateWindowData.Selected, null);
  96. SelectedUpdate = selected;
  97. // NOTE: Save the list again to remove leftovers.
  98. Save();
  99. SortUpdates();
  100. }
  101. public void SortUpdates()
  102. {
  103. var list = TitleUpdates.ToList();
  104. list.Sort((first, second) =>
  105. {
  106. if (string.IsNullOrEmpty(first.Control.DisplayVersionString.ToString()))
  107. {
  108. return -1;
  109. }
  110. else if (string.IsNullOrEmpty(second.Control.DisplayVersionString.ToString()))
  111. {
  112. return 1;
  113. }
  114. return Version.Parse(first.Control.DisplayVersionString.ToString()).CompareTo(Version.Parse(second.Control.DisplayVersionString.ToString())) * -1;
  115. });
  116. Views.Clear();
  117. Views.Add(new BaseModel());
  118. Views.AddRange(list);
  119. if (SelectedUpdate == null)
  120. {
  121. SelectedUpdate = Views[0];
  122. }
  123. else if (!TitleUpdates.Contains(SelectedUpdate))
  124. {
  125. if (Views.Count > 1)
  126. {
  127. SelectedUpdate = Views[1];
  128. }
  129. else
  130. {
  131. SelectedUpdate = Views[0];
  132. }
  133. }
  134. }
  135. private void AddUpdate(string path)
  136. {
  137. if (File.Exists(path) && TitleUpdates.All(x => x.Path != path))
  138. {
  139. using FileStream file = new(path, FileMode.Open, FileAccess.Read);
  140. try
  141. {
  142. (Nca patchNca, Nca controlNca) = ApplicationLoader.GetGameUpdateDataFromPartition(_virtualFileSystem, new PartitionFileSystem(file.AsStorage()), _titleId.ToString("x16"), 0);
  143. if (controlNca != null && patchNca != null)
  144. {
  145. ApplicationControlProperty controlData = new();
  146. using UniqueRef<IFile> nacpFile = new();
  147. controlNca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.None).OpenFile(ref nacpFile.Ref, "/control.nacp".ToU8Span(), OpenMode.Read).ThrowIfFailure();
  148. nacpFile.Get.Read(out _, 0, SpanHelpers.AsByteSpan(ref controlData), ReadOption.None).ThrowIfFailure();
  149. TitleUpdates.Add(new TitleUpdateModel(controlData, path));
  150. }
  151. else
  152. {
  153. Dispatcher.UIThread.Post(async () =>
  154. {
  155. await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance[LocaleKeys.DialogUpdateAddUpdateErrorMessage]);
  156. });
  157. }
  158. }
  159. catch (Exception ex)
  160. {
  161. Dispatcher.UIThread.Post(async () =>
  162. {
  163. await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance.UpdateAndGetDynamicValue(LocaleKeys.DialogLoadNcaErrorMessage, ex.Message, path));
  164. });
  165. }
  166. }
  167. }
  168. public void RemoveUpdate(TitleUpdateModel update)
  169. {
  170. TitleUpdates.Remove(update);
  171. SortUpdates();
  172. }
  173. public async void Add()
  174. {
  175. OpenFileDialog dialog = new()
  176. {
  177. Title = LocaleManager.Instance[LocaleKeys.SelectUpdateDialogTitle],
  178. AllowMultiple = true
  179. };
  180. dialog.Filters.Add(new FileDialogFilter
  181. {
  182. Name = "NSP",
  183. Extensions = { "nsp" }
  184. });
  185. if (Avalonia.Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
  186. {
  187. string[] files = await dialog.ShowAsync(desktop.MainWindow);
  188. if (files != null)
  189. {
  190. foreach (string file in files)
  191. {
  192. AddUpdate(file);
  193. }
  194. }
  195. }
  196. SortUpdates();
  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 == SelectedUpdate)
  206. {
  207. _titleUpdateWindowData.Selected = update.Path;
  208. }
  209. }
  210. JsonHelper.SerializeToFile(_titleUpdateJsonPath, _titleUpdateWindowData, SerializerContext.TitleUpdateMetadata);
  211. }
  212. }
  213. }