2023-01-15 12:11:52 +01:00
|
|
|
using Avalonia.Collections;
|
|
|
|
using Avalonia.Controls;
|
|
|
|
using Avalonia.Controls.ApplicationLifetimes;
|
|
|
|
using Avalonia.Threading;
|
|
|
|
using LibHac.Common;
|
|
|
|
using LibHac.Fs;
|
|
|
|
using LibHac.Fs.Fsa;
|
|
|
|
using LibHac.FsSystem;
|
|
|
|
using LibHac.Ns;
|
|
|
|
using LibHac.Tools.FsSystem;
|
|
|
|
using LibHac.Tools.FsSystem.NcaUtils;
|
|
|
|
using Ryujinx.Ava.Common.Locale;
|
|
|
|
using Ryujinx.Ava.UI.Helpers;
|
|
|
|
using Ryujinx.Ava.UI.Models;
|
|
|
|
using Ryujinx.Common.Configuration;
|
|
|
|
using Ryujinx.Common.Logging;
|
|
|
|
using Ryujinx.Common.Utilities;
|
|
|
|
using Ryujinx.HLE.FileSystem;
|
|
|
|
using Ryujinx.HLE.HOS;
|
2023-03-31 21:16:46 +02:00
|
|
|
using Ryujinx.Ui.App.Common;
|
2023-01-15 12:11:52 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using Path = System.IO.Path;
|
2023-01-22 01:42:55 +01:00
|
|
|
using SpanHelpers = LibHac.Common.SpanHelpers;
|
2023-01-15 12:11:52 +01:00
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
namespace Ryujinx.Ava.UI.ViewModels
|
2023-01-15 12:11:52 +01:00
|
|
|
{
|
2023-04-03 12:14:19 +02:00
|
|
|
public class TitleUpdateViewModel : BaseModel
|
|
|
|
{
|
|
|
|
public TitleUpdateMetadata _titleUpdateWindowData;
|
|
|
|
public readonly string _titleUpdateJsonPath;
|
|
|
|
private VirtualFileSystem _virtualFileSystem { get; }
|
|
|
|
private ulong _titleId { get; }
|
|
|
|
private string _titleName { get; }
|
2023-03-22 00:14:46 +01:00
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
private AvaloniaList<TitleUpdateModel> _titleUpdates = new();
|
|
|
|
private AvaloniaList<object> _views = new();
|
|
|
|
private object _selectedUpdate;
|
2023-03-22 00:14:46 +01:00
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
private static readonly TitleUpdateMetadataJsonSerializerContext SerializerContext = new(JsonHelper.GetDefaultSerializerOptions());
|
|
|
|
|
|
|
|
public AvaloniaList<TitleUpdateModel> TitleUpdates
|
2023-01-15 12:11:52 +01:00
|
|
|
{
|
2023-04-03 12:14:19 +02:00
|
|
|
get => _titleUpdates;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_titleUpdates = value;
|
|
|
|
OnPropertyChanged();
|
|
|
|
}
|
2023-01-15 12:11:52 +01:00
|
|
|
}
|
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
public AvaloniaList<object> Views
|
2023-01-15 12:11:52 +01:00
|
|
|
{
|
2023-04-03 12:14:19 +02:00
|
|
|
get => _views;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_views = value;
|
|
|
|
OnPropertyChanged();
|
|
|
|
}
|
2023-01-15 12:11:52 +01:00
|
|
|
}
|
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
public object SelectedUpdate
|
2023-01-15 12:11:52 +01:00
|
|
|
{
|
2023-04-03 12:14:19 +02:00
|
|
|
get => _selectedUpdate;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_selectedUpdate = value;
|
|
|
|
OnPropertyChanged();
|
|
|
|
}
|
2023-01-15 12:11:52 +01:00
|
|
|
}
|
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
public TitleUpdateViewModel(VirtualFileSystem virtualFileSystem, ulong titleId, string titleName)
|
|
|
|
{
|
|
|
|
_virtualFileSystem = virtualFileSystem;
|
2023-01-15 12:11:52 +01:00
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
_titleId = titleId;
|
|
|
|
_titleName = titleName;
|
2023-01-15 12:11:52 +01:00
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
_titleUpdateJsonPath = Path.Combine(AppDataManager.GamesDirPath, titleId.ToString("x16"), "updates.json");
|
2023-03-22 00:14:46 +01:00
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
try
|
2023-03-21 23:41:19 +01:00
|
|
|
{
|
2023-04-03 12:14:19 +02:00
|
|
|
_titleUpdateWindowData = JsonHelper.DeserializeFromFile(_titleUpdateJsonPath, SerializerContext.TitleUpdateMetadata);
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
Logger.Warning?.Print(LogClass.Application, $"Failed to deserialize title update data for {_titleId} at {_titleUpdateJsonPath}");
|
2023-01-22 01:42:55 +01:00
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
_titleUpdateWindowData = new TitleUpdateMetadata
|
|
|
|
{
|
|
|
|
Selected = "",
|
|
|
|
Paths = new List<string>()
|
|
|
|
};
|
2023-01-15 12:11:52 +01:00
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
Save();
|
|
|
|
}
|
2023-01-15 12:11:52 +01:00
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
LoadUpdates();
|
2023-01-15 12:11:52 +01:00
|
|
|
}
|
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
private void LoadUpdates()
|
|
|
|
{
|
|
|
|
foreach (string path in _titleUpdateWindowData.Paths)
|
|
|
|
{
|
|
|
|
AddUpdate(path);
|
|
|
|
}
|
2023-01-15 12:11:52 +01:00
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
TitleUpdateModel selected = TitleUpdates.FirstOrDefault(x => x.Path == _titleUpdateWindowData.Selected, null);
|
2023-01-15 12:11:52 +01:00
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
SelectedUpdate = selected;
|
2023-02-22 21:58:32 +01:00
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
// NOTE: Save the list again to remove leftovers.
|
|
|
|
Save();
|
|
|
|
SortUpdates();
|
|
|
|
}
|
2023-03-21 23:41:19 +01:00
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
public void SortUpdates()
|
2023-03-22 00:14:46 +01:00
|
|
|
{
|
2023-04-03 12:14:19 +02:00
|
|
|
var list = TitleUpdates.ToList();
|
|
|
|
|
|
|
|
list.Sort((first, second) =>
|
2023-03-22 00:14:46 +01:00
|
|
|
{
|
2023-04-03 12:14:19 +02:00
|
|
|
if (string.IsNullOrEmpty(first.Control.DisplayVersionString.ToString()))
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if (string.IsNullOrEmpty(second.Control.DisplayVersionString.ToString()))
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2023-01-15 12:11:52 +01:00
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
return Version.Parse(first.Control.DisplayVersionString.ToString()).CompareTo(Version.Parse(second.Control.DisplayVersionString.ToString())) * -1;
|
|
|
|
});
|
2023-01-15 12:11:52 +01:00
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
Views.Clear();
|
|
|
|
Views.Add(new BaseModel());
|
|
|
|
Views.AddRange(list);
|
2023-01-15 12:11:52 +01:00
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
if (SelectedUpdate == null)
|
2023-01-15 12:11:52 +01:00
|
|
|
{
|
2023-04-03 12:14:19 +02:00
|
|
|
SelectedUpdate = Views[0];
|
2023-01-15 12:11:52 +01:00
|
|
|
}
|
2023-04-03 12:14:19 +02:00
|
|
|
else if (!TitleUpdates.Contains(SelectedUpdate))
|
2023-01-15 12:11:52 +01:00
|
|
|
{
|
2023-04-03 12:14:19 +02:00
|
|
|
if (Views.Count > 1)
|
|
|
|
{
|
|
|
|
SelectedUpdate = Views[1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SelectedUpdate = Views[0];
|
|
|
|
}
|
2023-01-15 12:11:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
private void AddUpdate(string path)
|
2023-01-15 12:11:52 +01:00
|
|
|
{
|
2023-04-03 12:14:19 +02:00
|
|
|
if (File.Exists(path) && TitleUpdates.All(x => x.Path != path))
|
2023-01-15 12:11:52 +01:00
|
|
|
{
|
2023-04-03 12:14:19 +02:00
|
|
|
using FileStream file = new(path, FileMode.Open, FileAccess.Read);
|
2023-01-15 12:11:52 +01:00
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
try
|
2023-01-15 12:11:52 +01:00
|
|
|
{
|
2023-04-03 12:14:19 +02:00
|
|
|
(Nca patchNca, Nca controlNca) = ApplicationLibrary.GetGameUpdateDataFromPartition(_virtualFileSystem, new PartitionFileSystem(file.AsStorage()), _titleId.ToString("x16"), 0);
|
2023-01-15 12:11:52 +01:00
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
if (controlNca != null && patchNca != null)
|
|
|
|
{
|
|
|
|
ApplicationControlProperty controlData = new();
|
|
|
|
|
|
|
|
using UniqueRef<IFile> nacpFile = new();
|
2023-01-15 12:11:52 +01:00
|
|
|
|
2023-04-03 12:14:19 +02: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();
|
2023-01-15 12:11:52 +01:00
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
TitleUpdates.Add(new TitleUpdateModel(controlData, path));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Dispatcher.UIThread.Post(async () =>
|
|
|
|
{
|
|
|
|
await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance[LocaleKeys.DialogUpdateAddUpdateErrorMessage]);
|
|
|
|
});
|
|
|
|
}
|
2023-01-15 12:11:52 +01:00
|
|
|
}
|
2023-04-03 12:14:19 +02:00
|
|
|
catch (Exception ex)
|
2023-01-15 12:11:52 +01:00
|
|
|
{
|
|
|
|
Dispatcher.UIThread.Post(async () =>
|
|
|
|
{
|
2023-04-03 12:14:19 +02:00
|
|
|
await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance.UpdateAndGetDynamicValue(LocaleKeys.DialogLoadNcaErrorMessage, ex.Message, path));
|
2023-01-15 12:11:52 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
public void RemoveUpdate(TitleUpdateModel update)
|
2023-01-15 12:11:52 +01:00
|
|
|
{
|
2023-04-03 12:14:19 +02:00
|
|
|
TitleUpdates.Remove(update);
|
2023-01-15 12:11:52 +01:00
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
SortUpdates();
|
|
|
|
}
|
2023-03-21 23:41:19 +01:00
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
public async void Add()
|
2023-03-22 00:14:46 +01:00
|
|
|
{
|
2023-04-03 12:14:19 +02:00
|
|
|
OpenFileDialog dialog = new()
|
|
|
|
{
|
|
|
|
Title = LocaleManager.Instance[LocaleKeys.SelectUpdateDialogTitle],
|
|
|
|
AllowMultiple = true
|
|
|
|
};
|
2023-03-21 23:41:19 +01:00
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
dialog.Filters.Add(new FileDialogFilter
|
2023-03-22 00:14:46 +01:00
|
|
|
{
|
2023-04-03 12:14:19 +02:00
|
|
|
Name = "NSP",
|
|
|
|
Extensions = { "nsp" }
|
|
|
|
});
|
|
|
|
|
|
|
|
if (Avalonia.Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
|
|
|
{
|
|
|
|
string[] files = await dialog.ShowAsync(desktop.MainWindow);
|
|
|
|
|
|
|
|
if (files != null)
|
2023-01-15 12:11:52 +01:00
|
|
|
{
|
2023-04-03 12:14:19 +02:00
|
|
|
foreach (string file in files)
|
|
|
|
{
|
|
|
|
AddUpdate(file);
|
|
|
|
}
|
2023-01-15 12:11:52 +01:00
|
|
|
}
|
|
|
|
}
|
2023-03-22 00:14:46 +01:00
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
SortUpdates();
|
|
|
|
}
|
2023-03-22 00:14:46 +01:00
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
public void Save()
|
2023-01-22 01:42:55 +01:00
|
|
|
{
|
2023-04-03 12:14:19 +02:00
|
|
|
_titleUpdateWindowData.Paths.Clear();
|
|
|
|
_titleUpdateWindowData.Selected = "";
|
2023-01-22 01:42:55 +01:00
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
foreach (TitleUpdateModel update in TitleUpdates)
|
2023-01-22 01:42:55 +01:00
|
|
|
{
|
2023-04-03 12:14:19 +02:00
|
|
|
_titleUpdateWindowData.Paths.Add(update.Path);
|
|
|
|
|
|
|
|
if (update == SelectedUpdate)
|
|
|
|
{
|
|
|
|
_titleUpdateWindowData.Selected = update.Path;
|
|
|
|
}
|
2023-01-22 01:42:55 +01:00
|
|
|
}
|
2023-03-22 00:14:46 +01:00
|
|
|
|
2023-04-03 12:14:19 +02:00
|
|
|
JsonHelper.SerializeToFile(_titleUpdateJsonPath, _titleUpdateWindowData, SerializerContext.TitleUpdateMetadata);
|
|
|
|
}
|
2023-01-22 01:42:55 +01:00
|
|
|
}
|
2023-01-15 12:11:52 +01:00
|
|
|
}
|