Ryujinx/Ryujinx.Ava/UI/ViewModels/TitleUpdateViewModel.cs

250 lines
7.1 KiB
C#
Raw Normal View History

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;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2023-02-09 02:43:53 +00:00
using System.Text;
2023-01-15 20:34:06 +00:00
using Path = System.IO.Path;
2023-02-09 02:43:53 +00:00
using SpanHelpers = LibHac.Common.SpanHelpers;
namespace Ryujinx.Ava.UI.ViewModels;
2023-02-09 02:43:53 +00:00
public class TitleUpdateViewModel : BaseModel
{
2023-02-09 02:43:53 +00:00
public TitleUpdateMetadata _titleUpdateWindowData;
public readonly string _titleUpdateJsonPath;
private VirtualFileSystem _virtualFileSystem { get; }
private ulong _titleId { get; }
private string _titleName { get; }
2023-02-09 02:43:53 +00:00
private AvaloniaList<TitleUpdateModel> _titleUpdates = new();
private AvaloniaList<object> _views = new();
private object _selectedUpdate;
2023-02-09 02:43:53 +00:00
public AvaloniaList<TitleUpdateModel> TitleUpdates
{
get => _titleUpdates;
set
{
2023-02-09 02:43:53 +00:00
_titleUpdates = value;
OnPropertyChanged();
}
2023-02-09 02:43:53 +00:00
}
2023-02-09 02:43:53 +00:00
public AvaloniaList<object> Views
{
get => _views;
set
{
2023-02-09 02:43:53 +00:00
_views = value;
OnPropertyChanged();
}
2023-02-09 02:43:53 +00:00
}
2023-02-09 02:43:53 +00:00
public object SelectedUpdate
{
get => _selectedUpdate;
set
{
2023-02-09 02:43:53 +00:00
_selectedUpdate = value;
OnPropertyChanged();
}
2023-02-09 02:43:53 +00:00
}
2023-02-09 02:43:53 +00:00
public TitleUpdateViewModel(VirtualFileSystem virtualFileSystem, ulong titleId, string titleName)
{
_virtualFileSystem = virtualFileSystem;
2023-02-09 02:43:53 +00:00
_titleId = titleId;
_titleName = titleName;
2023-02-09 02:43:53 +00:00
_titleUpdateJsonPath = Path.Combine(AppDataManager.GamesDirPath, titleId.ToString("x16"), "updates.json");
2023-02-09 02:43:53 +00:00
try
{
_titleUpdateWindowData = JsonHelper.DeserializeFromFile<TitleUpdateMetadata>(_titleUpdateJsonPath);
}
catch
{
Logger.Warning?.Print(LogClass.Application, $"Failed to deserialize title update data for {_titleId} at {_titleUpdateJsonPath}");
2023-02-09 02:43:53 +00:00
_titleUpdateWindowData = new TitleUpdateMetadata
{
Selected = "",
Paths = new List<string>()
};
2023-02-09 02:43:53 +00:00
Save();
}
2023-02-09 02:43:53 +00:00
LoadUpdates();
}
private void LoadUpdates()
{
foreach (string path in _titleUpdateWindowData.Paths)
2023-01-15 20:34:06 +00:00
{
2023-02-09 02:43:53 +00:00
AddUpdate(path);
}
2023-02-09 02:43:53 +00:00
// NOTE: Save the list again to remove leftovers.
Save();
2023-02-09 02:43:53 +00:00
TitleUpdateModel selected = TitleUpdates.FirstOrDefault(x => x.Path == _titleUpdateWindowData.Selected, null);
2023-02-09 02:43:53 +00:00
SelectedUpdate = selected;
2023-02-09 02:43:53 +00:00
SortUpdates();
}
public void SortUpdates()
{
var list = TitleUpdates.ToList();
2023-01-15 20:34:06 +00:00
2023-02-09 02:43:53 +00:00
list.Sort((first, second) =>
{
if (string.IsNullOrEmpty(first.Control.DisplayVersionString.ToString()))
{
2023-02-09 02:43:53 +00:00
return -1;
}
else if (string.IsNullOrEmpty(second.Control.DisplayVersionString.ToString()))
{
return 1;
}
2023-02-09 02:43:53 +00:00
return Version.Parse(first.Control.DisplayVersionString.ToString()).CompareTo(Version.Parse(second.Control.DisplayVersionString.ToString())) * -1;
});
2023-02-09 02:43:53 +00:00
Views.Clear();
Views.Add(new BaseModel());
Views.AddRange(list);
2023-02-09 02:43:53 +00:00
if (SelectedUpdate == null)
{
SelectedUpdate = Views[0];
}
else if (!TitleUpdates.Contains(SelectedUpdate))
{
if (Views.Count > 1)
{
2023-02-09 02:43:53 +00:00
SelectedUpdate = Views[1];
}
2023-02-09 02:43:53 +00:00
else
{
2023-02-09 02:43:53 +00:00
SelectedUpdate = Views[0];
}
}
2023-02-09 02:43:53 +00:00
}
2023-02-09 02:43:53 +00:00
private void AddUpdate(string path)
{
if (File.Exists(path) && TitleUpdates.All(x => x.Path != path))
{
2023-02-09 02:43:53 +00:00
using FileStream file = new(path, FileMode.Open, FileAccess.Read);
try
{
2023-02-09 02:43:53 +00:00
(Nca patchNca, Nca controlNca) = ApplicationLoader.GetGameUpdateDataFromPartition(_virtualFileSystem, new PartitionFileSystem(file.AsStorage()), _titleId.ToString("x16"), 0);
2023-02-09 02:43:53 +00:00
if (controlNca != null && patchNca != null)
{
2023-02-09 02:43:53 +00:00
ApplicationControlProperty controlData = new();
2023-02-09 02:43:53 +00:00
using UniqueRef<IFile> nacpFile = new();
2023-02-09 02:43:53 +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();
2023-02-09 02:43:53 +00:00
TitleUpdates.Add(new TitleUpdateModel(controlData, path));
}
2023-02-09 02:43:53 +00:00
else
{
Dispatcher.UIThread.Post(async () =>
{
2023-02-09 02:43:53 +00:00
await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance[LocaleKeys.DialogUpdateAddUpdateErrorMessage]);
});
}
}
2023-02-09 02:43:53 +00:00
catch (Exception ex)
{
Dispatcher.UIThread.Post(async () =>
{
await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance.UpdateAndGetDynamicValue(LocaleKeys.DialogLoadNcaErrorMessage, ex.Message, path));
});
}
}
2023-02-09 02:43:53 +00:00
}
2023-02-09 02:43:53 +00:00
public void RemoveUpdate(TitleUpdateModel update)
{
TitleUpdates.Remove(update);
2023-02-09 02:43:53 +00:00
SortUpdates();
}
2023-02-09 02:43:53 +00:00
public async void Add()
{
OpenFileDialog dialog = new()
{
2023-02-09 02:43:53 +00:00
Title = LocaleManager.Instance[LocaleKeys.SelectUpdateDialogTitle],
AllowMultiple = true
};
2023-02-09 02:43:53 +00:00
dialog.Filters.Add(new FileDialogFilter
{
Name = "NSP",
Extensions = { "nsp" }
});
2023-01-15 20:34:06 +00:00
2023-02-09 02:43:53 +00:00
if (Avalonia.Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
string[] files = await dialog.ShowAsync(desktop.MainWindow);
2023-01-15 20:34:06 +00:00
2023-02-09 02:43:53 +00:00
if (files != null)
{
foreach (string file in files)
{
2023-02-09 02:43:53 +00:00
AddUpdate(file);
}
}
2023-02-09 02:43:53 +00:00
}
SortUpdates();
}
public void Save()
{
_titleUpdateWindowData.Paths.Clear();
_titleUpdateWindowData.Selected = "";
2023-02-09 02:43:53 +00:00
foreach (TitleUpdateModel update in TitleUpdates)
{
_titleUpdateWindowData.Paths.Add(update.Path);
if (update == SelectedUpdate)
{
_titleUpdateWindowData.Selected = update.Path;
}
}
2023-02-09 02:43:53 +00:00
File.WriteAllBytes(_titleUpdateJsonPath, Encoding.UTF8.GetBytes(JsonHelper.Serialize(_titleUpdateWindowData, true)));
}
}