2022-07-08 18:47:11 +00:00
|
|
|
using Avalonia.Collections;
|
|
|
|
using Avalonia.Controls;
|
2022-07-28 22:41:34 +00:00
|
|
|
using Avalonia.Threading;
|
2022-07-08 18:47:11 +00:00
|
|
|
using LibHac.Common;
|
|
|
|
using LibHac.Fs;
|
|
|
|
using LibHac.Fs.Fsa;
|
|
|
|
using LibHac.FsSystem;
|
|
|
|
using LibHac.Ns;
|
2022-07-28 22:41:34 +00:00
|
|
|
using LibHac.Tools.FsSystem;
|
|
|
|
using LibHac.Tools.FsSystem.NcaUtils;
|
2022-07-08 18:47:11 +00:00
|
|
|
using Ryujinx.Ava.Common.Locale;
|
2022-12-29 14:24:05 +00:00
|
|
|
using Ryujinx.Ava.UI.Controls;
|
|
|
|
using Ryujinx.Ava.UI.Helpers;
|
|
|
|
using Ryujinx.Ava.UI.Models;
|
2022-07-08 18:47:11 +00:00
|
|
|
using Ryujinx.Common.Configuration;
|
|
|
|
using Ryujinx.Common.Utilities;
|
|
|
|
using Ryujinx.HLE.FileSystem;
|
|
|
|
using Ryujinx.HLE.HOS;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
|
|
|
using Path = System.IO.Path;
|
|
|
|
using SpanHelpers = LibHac.Common.SpanHelpers;
|
|
|
|
|
2022-12-29 14:24:05 +00:00
|
|
|
namespace Ryujinx.Ava.UI.Windows
|
2022-07-08 18:47:11 +00:00
|
|
|
{
|
2022-07-24 17:38:38 +00:00
|
|
|
public partial class TitleUpdateWindow : StyleableWindow
|
2022-07-08 18:47:11 +00:00
|
|
|
{
|
2022-07-28 22:41:34 +00:00
|
|
|
private readonly string _titleUpdateJsonPath;
|
2022-07-08 18:47:11 +00:00
|
|
|
private TitleUpdateMetadata _titleUpdateWindowData;
|
|
|
|
|
2022-11-25 16:55:08 +00:00
|
|
|
private VirtualFileSystem _virtualFileSystem { get; }
|
|
|
|
private AvaloniaList<TitleUpdateModel> _titleUpdates { get; set; }
|
2022-07-08 18:47:11 +00:00
|
|
|
|
2022-11-25 16:55:08 +00:00
|
|
|
private ulong _titleId { get; }
|
|
|
|
private string _titleName { get; }
|
2022-07-08 18:47:11 +00:00
|
|
|
|
|
|
|
public TitleUpdateWindow()
|
|
|
|
{
|
|
|
|
DataContext = this;
|
|
|
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
2023-01-03 18:45:08 +00:00
|
|
|
Title = $"Ryujinx {Program.Version} - {LocaleManager.Instance[LocaleKeys.UpdateWindowTitle]} - {_titleName} ({_titleId:X16})";
|
2022-07-08 18:47:11 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 16:55:08 +00:00
|
|
|
public TitleUpdateWindow(VirtualFileSystem virtualFileSystem, ulong titleId, string titleName)
|
2022-07-08 18:47:11 +00:00
|
|
|
{
|
2022-11-25 16:55:08 +00:00
|
|
|
_virtualFileSystem = virtualFileSystem;
|
|
|
|
_titleUpdates = new AvaloniaList<TitleUpdateModel>();
|
|
|
|
|
|
|
|
_titleId = titleId;
|
|
|
|
_titleName = titleName;
|
2022-07-08 18:47:11 +00:00
|
|
|
|
2022-11-25 16:55:08 +00:00
|
|
|
_titleUpdateJsonPath = Path.Combine(AppDataManager.GamesDirPath, titleId.ToString("x16"), "updates.json");
|
2022-07-08 18:47:11 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2022-07-28 22:41:34 +00:00
|
|
|
_titleUpdateWindowData = JsonHelper.DeserializeFromFile<TitleUpdateMetadata>(_titleUpdateJsonPath);
|
2022-07-08 18:47:11 +00:00
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
2022-07-28 22:41:34 +00:00
|
|
|
_titleUpdateWindowData = new TitleUpdateMetadata
|
|
|
|
{
|
|
|
|
Selected = "",
|
2022-11-25 16:55:08 +00:00
|
|
|
Paths = new List<string>()
|
2022-07-28 22:41:34 +00:00
|
|
|
};
|
2022-07-08 18:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DataContext = this;
|
|
|
|
|
|
|
|
InitializeComponent();
|
2022-07-28 22:41:34 +00:00
|
|
|
|
2023-01-03 18:45:08 +00:00
|
|
|
Title = $"Ryujinx {Program.Version} - {LocaleManager.Instance[LocaleKeys.UpdateWindowTitle]} - {_titleName} ({_titleId:X16})";
|
2022-07-08 18:47:11 +00:00
|
|
|
|
|
|
|
LoadUpdates();
|
2022-11-25 16:55:08 +00:00
|
|
|
PrintHeading();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void PrintHeading()
|
|
|
|
{
|
2023-01-11 04:39:25 +00:00
|
|
|
Heading.Text = string.Format(LocaleManager.Instance[LocaleKeys.GameUpdateWindowHeading], _titleUpdates.Count - 1, _titleName, _titleId.ToString("X16"));
|
2022-07-08 18:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void LoadUpdates()
|
|
|
|
{
|
2022-11-25 16:55:08 +00:00
|
|
|
_titleUpdates.Add(new TitleUpdateModel(default, string.Empty, true));
|
2022-07-08 18:47:11 +00:00
|
|
|
|
|
|
|
foreach (string path in _titleUpdateWindowData.Paths)
|
|
|
|
{
|
|
|
|
AddUpdate(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_titleUpdateWindowData.Selected == "")
|
|
|
|
{
|
2022-11-25 16:55:08 +00:00
|
|
|
_titleUpdates[0].IsEnabled = true;
|
2022-07-08 18:47:11 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-11-25 16:55:08 +00:00
|
|
|
TitleUpdateModel selected = _titleUpdates.FirstOrDefault(x => x.Path == _titleUpdateWindowData.Selected);
|
|
|
|
List<TitleUpdateModel> enabled = _titleUpdates.Where(x => x.IsEnabled).ToList();
|
2022-07-08 18:47:11 +00:00
|
|
|
|
|
|
|
foreach (TitleUpdateModel update in enabled)
|
|
|
|
{
|
|
|
|
update.IsEnabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (selected != null)
|
|
|
|
{
|
|
|
|
selected.IsEnabled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SortUpdates();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void AddUpdate(string path)
|
|
|
|
{
|
2022-11-25 16:55:08 +00:00
|
|
|
if (File.Exists(path) && !_titleUpdates.Any(x => x.Path == path))
|
2022-07-08 18:47:11 +00:00
|
|
|
{
|
2022-11-25 16:55:08 +00:00
|
|
|
using FileStream file = new(path, FileMode.Open, FileAccess.Read);
|
|
|
|
|
|
|
|
try
|
2022-07-08 18:47:11 +00:00
|
|
|
{
|
2022-11-25 16:55:08 +00:00
|
|
|
(Nca patchNca, Nca controlNca) = ApplicationLoader.GetGameUpdateDataFromPartition(_virtualFileSystem, new PartitionFileSystem(file.AsStorage()), _titleId.ToString("x16"), 0);
|
2022-07-08 18:47:11 +00:00
|
|
|
|
2022-11-25 16:55:08 +00:00
|
|
|
if (controlNca != null && patchNca != null)
|
2022-07-08 18:47:11 +00:00
|
|
|
{
|
2022-11-25 16:55:08 +00:00
|
|
|
ApplicationControlProperty controlData = new();
|
2022-07-08 18:47:11 +00:00
|
|
|
|
2022-11-25 16:55:08 +00:00
|
|
|
using UniqueRef<IFile> nacpFile = new();
|
2022-07-08 18:47:11 +00:00
|
|
|
|
2022-11-25 16:55:08 +00:00
|
|
|
controlNca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.None).OpenFile(ref nacpFile.Ref(), "/control.nacp".ToU8Span(), OpenMode.Read).ThrowIfFailure();
|
|
|
|
nacpFile.Get.Read(out _, 0, SpanHelpers.AsByteSpan(ref controlData), ReadOption.None).ThrowIfFailure();
|
2022-11-25 11:41:34 +00:00
|
|
|
|
2022-11-25 16:55:08 +00:00
|
|
|
_titleUpdates.Add(new TitleUpdateModel(controlData, path));
|
2022-11-25 11:41:34 +00:00
|
|
|
|
2022-11-25 16:55:08 +00:00
|
|
|
foreach (var update in _titleUpdates)
|
2022-07-08 18:47:11 +00:00
|
|
|
{
|
2022-11-25 16:55:08 +00:00
|
|
|
update.IsEnabled = false;
|
2022-07-08 18:47:11 +00:00
|
|
|
}
|
2022-11-25 16:55:08 +00:00
|
|
|
|
|
|
|
_titleUpdates.Last().IsEnabled = true;
|
2022-07-08 18:47:11 +00:00
|
|
|
}
|
2022-11-25 16:55:08 +00:00
|
|
|
else
|
2022-07-08 18:47:11 +00:00
|
|
|
{
|
2022-07-11 22:25:33 +00:00
|
|
|
Dispatcher.UIThread.Post(async () =>
|
|
|
|
{
|
2023-01-03 18:45:08 +00:00
|
|
|
await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance[LocaleKeys.DialogUpdateAddUpdateErrorMessage]);
|
2022-07-11 22:25:33 +00:00
|
|
|
});
|
2022-07-08 18:47:11 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-25 16:55:08 +00:00
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
Dispatcher.UIThread.Post(async () =>
|
|
|
|
{
|
2023-01-03 18:45:08 +00:00
|
|
|
await ContentDialogHelper.CreateErrorDialog(string.Format(LocaleManager.Instance[LocaleKeys.DialogDlcLoadNcaErrorMessage], ex.Message, path));
|
2022-11-25 16:55:08 +00:00
|
|
|
});
|
|
|
|
}
|
2022-07-08 18:47:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void RemoveUpdates(bool removeSelectedOnly = false)
|
|
|
|
{
|
|
|
|
if (removeSelectedOnly)
|
|
|
|
{
|
2022-11-25 16:55:08 +00:00
|
|
|
_titleUpdates.RemoveAll(_titleUpdates.Where(x => x.IsEnabled && !x.IsNoUpdate).ToList());
|
2022-07-08 18:47:11 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-11-25 16:55:08 +00:00
|
|
|
_titleUpdates.RemoveAll(_titleUpdates.Where(x => !x.IsNoUpdate).ToList());
|
2022-07-08 18:47:11 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 16:55:08 +00:00
|
|
|
_titleUpdates.FirstOrDefault(x => x.IsNoUpdate).IsEnabled = true;
|
2022-07-08 18:47:11 +00:00
|
|
|
|
|
|
|
SortUpdates();
|
2022-11-25 16:55:08 +00:00
|
|
|
PrintHeading();
|
2022-07-08 18:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void RemoveSelected()
|
|
|
|
{
|
|
|
|
RemoveUpdates(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void RemoveAll()
|
|
|
|
{
|
|
|
|
RemoveUpdates();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async void Add()
|
|
|
|
{
|
2022-11-25 16:55:08 +00:00
|
|
|
OpenFileDialog dialog = new()
|
2022-07-28 22:41:34 +00:00
|
|
|
{
|
2023-01-03 18:45:08 +00:00
|
|
|
Title = LocaleManager.Instance[LocaleKeys.SelectUpdateDialogTitle],
|
2022-07-28 22:41:34 +00:00
|
|
|
AllowMultiple = true
|
|
|
|
};
|
2022-07-08 18:47:11 +00:00
|
|
|
|
2022-07-28 22:41:34 +00:00
|
|
|
dialog.Filters.Add(new FileDialogFilter
|
|
|
|
{
|
|
|
|
Name = "NSP",
|
|
|
|
Extensions = { "nsp" }
|
|
|
|
});
|
2022-07-08 18:47:11 +00:00
|
|
|
|
|
|
|
string[] files = await dialog.ShowAsync(this);
|
|
|
|
|
|
|
|
if (files != null)
|
|
|
|
{
|
|
|
|
foreach (string file in files)
|
|
|
|
{
|
|
|
|
AddUpdate(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SortUpdates();
|
2022-11-25 16:55:08 +00:00
|
|
|
PrintHeading();
|
2022-07-08 18:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void SortUpdates()
|
|
|
|
{
|
2022-11-25 16:55:08 +00:00
|
|
|
var list = _titleUpdates.ToList();
|
2022-07-08 18:47:11 +00:00
|
|
|
|
|
|
|
list.Sort((first, second) =>
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(first.Control.DisplayVersionString.ToString()))
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if (string.IsNullOrEmpty(second.Control.DisplayVersionString.ToString()))
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-07-28 22:41:34 +00:00
|
|
|
return Version.Parse(first.Control.DisplayVersionString.ToString()).CompareTo(Version.Parse(second.Control.DisplayVersionString.ToString())) * -1;
|
2022-07-08 18:47:11 +00:00
|
|
|
});
|
|
|
|
|
2022-11-25 16:55:08 +00:00
|
|
|
_titleUpdates.Clear();
|
|
|
|
_titleUpdates.AddRange(list);
|
2022-07-08 18:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Save()
|
|
|
|
{
|
|
|
|
_titleUpdateWindowData.Paths.Clear();
|
|
|
|
|
|
|
|
_titleUpdateWindowData.Selected = "";
|
|
|
|
|
2022-11-25 16:55:08 +00:00
|
|
|
foreach (TitleUpdateModel update in _titleUpdates)
|
2022-07-08 18:47:11 +00:00
|
|
|
{
|
|
|
|
_titleUpdateWindowData.Paths.Add(update.Path);
|
|
|
|
|
|
|
|
if (update.IsEnabled)
|
|
|
|
{
|
|
|
|
_titleUpdateWindowData.Selected = update.Path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 22:41:34 +00:00
|
|
|
using (FileStream titleUpdateJsonStream = File.Create(_titleUpdateJsonPath, 4096, FileOptions.WriteThrough))
|
2022-07-08 18:47:11 +00:00
|
|
|
{
|
2022-07-28 22:41:34 +00:00
|
|
|
titleUpdateJsonStream.Write(Encoding.UTF8.GetBytes(JsonHelper.Serialize(_titleUpdateWindowData, true)));
|
2022-07-08 18:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Owner is MainWindow window)
|
|
|
|
{
|
|
|
|
window.ViewModel.LoadApplications();
|
|
|
|
}
|
|
|
|
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|