2022-07-08 14:47:11 -04:00
|
|
|
using Avalonia;
|
|
|
|
using Avalonia.Collections;
|
|
|
|
using Avalonia.Media.Imaging;
|
|
|
|
using Avalonia.Threading;
|
|
|
|
using Ryujinx.Ava.Common.Locale;
|
2022-12-29 09:24:05 -05:00
|
|
|
using Ryujinx.Ava.UI.Helpers;
|
|
|
|
using Ryujinx.Ava.UI.Windows;
|
2022-07-08 14:47:11 -04:00
|
|
|
using Ryujinx.Common;
|
|
|
|
using Ryujinx.Common.Configuration;
|
2023-04-23 22:08:31 -04:00
|
|
|
using Ryujinx.Common.Logging;
|
2022-11-23 12:55:26 -05:00
|
|
|
using Ryujinx.Common.Utilities;
|
2023-04-03 06:14:19 -04:00
|
|
|
using Ryujinx.Ui.Common.Models.Amiibo;
|
2022-07-08 14:47:11 -04:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Net.Http;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading.Tasks;
|
2023-04-03 06:14:19 -04:00
|
|
|
using AmiiboJsonSerializerContext = Ryujinx.Ui.Common.Models.Amiibo.AmiiboJsonSerializerContext;
|
2022-07-08 14:47:11 -04:00
|
|
|
|
2022-12-29 09:24:05 -05:00
|
|
|
namespace Ryujinx.Ava.UI.ViewModels
|
2022-07-08 14:47:11 -04:00
|
|
|
{
|
|
|
|
public class AmiiboWindowViewModel : BaseModel, IDisposable
|
|
|
|
{
|
|
|
|
private const string DefaultJson = "{ \"amiibo\": [] }";
|
|
|
|
private const float AmiiboImageSize = 350f;
|
|
|
|
|
|
|
|
private readonly string _amiiboJsonPath;
|
|
|
|
private readonly byte[] _amiiboLogoBytes;
|
|
|
|
private readonly HttpClient _httpClient;
|
|
|
|
private readonly StyleableWindow _owner;
|
2023-03-04 08:43:08 -05:00
|
|
|
|
2022-07-08 14:47:11 -04:00
|
|
|
private Bitmap _amiiboImage;
|
2023-04-03 06:14:19 -04:00
|
|
|
private List<AmiiboApi> _amiiboList;
|
|
|
|
private AvaloniaList<AmiiboApi> _amiibos;
|
2022-07-08 14:47:11 -04:00
|
|
|
private ObservableCollection<string> _amiiboSeries;
|
|
|
|
|
|
|
|
private int _amiiboSelectedIndex;
|
|
|
|
private int _seriesSelectedIndex;
|
|
|
|
private bool _enableScanning;
|
|
|
|
private bool _showAllAmiibo;
|
|
|
|
private bool _useRandomUuid;
|
|
|
|
private string _usage;
|
2023-04-23 22:08:31 -04:00
|
|
|
|
2023-04-03 06:14:19 -04:00
|
|
|
private static readonly AmiiboJsonSerializerContext SerializerContext = new(JsonHelper.GetDefaultSerializerOptions());
|
2022-07-08 14:47:11 -04:00
|
|
|
|
|
|
|
public AmiiboWindowViewModel(StyleableWindow owner, string lastScannedAmiiboId, string titleId)
|
|
|
|
{
|
|
|
|
_owner = owner;
|
2023-04-23 22:08:31 -04:00
|
|
|
|
|
|
|
_httpClient = new HttpClient
|
|
|
|
{
|
|
|
|
Timeout = TimeSpan.FromSeconds(30)
|
|
|
|
};
|
|
|
|
|
2022-07-08 14:47:11 -04:00
|
|
|
LastScannedAmiiboId = lastScannedAmiiboId;
|
|
|
|
TitleId = titleId;
|
|
|
|
|
|
|
|
Directory.CreateDirectory(Path.Join(AppDataManager.BaseDirPath, "system", "amiibo"));
|
|
|
|
|
|
|
|
_amiiboJsonPath = Path.Join(AppDataManager.BaseDirPath, "system", "amiibo", "Amiibo.json");
|
2023-04-03 06:14:19 -04:00
|
|
|
_amiiboList = new List<AmiiboApi>();
|
2022-07-08 14:47:11 -04:00
|
|
|
_amiiboSeries = new ObservableCollection<string>();
|
2023-04-03 06:14:19 -04:00
|
|
|
_amiibos = new AvaloniaList<AmiiboApi>();
|
2022-07-08 14:47:11 -04:00
|
|
|
|
|
|
|
_amiiboLogoBytes = EmbeddedResources.Read("Ryujinx.Ui.Common/Resources/Logo_Amiibo.png");
|
|
|
|
|
|
|
|
_ = LoadContentAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
public AmiiboWindowViewModel() { }
|
|
|
|
|
|
|
|
public string TitleId { get; set; }
|
|
|
|
public string LastScannedAmiiboId { get; set; }
|
|
|
|
|
|
|
|
public UserResult Response { get; private set; }
|
|
|
|
|
|
|
|
public bool UseRandomUuid
|
|
|
|
{
|
|
|
|
get => _useRandomUuid;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_useRandomUuid = value;
|
|
|
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool ShowAllAmiibo
|
|
|
|
{
|
|
|
|
get => _showAllAmiibo;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_showAllAmiibo = value;
|
|
|
|
|
|
|
|
ParseAmiiboData();
|
|
|
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 06:14:19 -04:00
|
|
|
public AvaloniaList<AmiiboApi> AmiiboList
|
2022-07-08 14:47:11 -04:00
|
|
|
{
|
|
|
|
get => _amiibos;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_amiibos = value;
|
|
|
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public ObservableCollection<string> AmiiboSeries
|
|
|
|
{
|
|
|
|
get => _amiiboSeries;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_amiiboSeries = value;
|
|
|
|
OnPropertyChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int SeriesSelectedIndex
|
|
|
|
{
|
|
|
|
get => _seriesSelectedIndex;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_seriesSelectedIndex = value;
|
|
|
|
|
|
|
|
FilterAmiibo();
|
|
|
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int AmiiboSelectedIndex
|
|
|
|
{
|
|
|
|
get => _amiiboSelectedIndex;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_amiiboSelectedIndex = value;
|
|
|
|
|
|
|
|
EnableScanning = _amiiboSelectedIndex >= 0 && _amiiboSelectedIndex < _amiibos.Count;
|
|
|
|
|
|
|
|
SetAmiiboDetails();
|
|
|
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Bitmap AmiiboImage
|
|
|
|
{
|
|
|
|
get => _amiiboImage;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_amiiboImage = value;
|
|
|
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string Usage
|
|
|
|
{
|
|
|
|
get => _usage;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_usage = value;
|
|
|
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool EnableScanning
|
|
|
|
{
|
|
|
|
get => _enableScanning;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_enableScanning = value;
|
|
|
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
_httpClient.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task LoadContentAsync()
|
|
|
|
{
|
|
|
|
string amiiboJsonString = DefaultJson;
|
|
|
|
|
|
|
|
if (File.Exists(_amiiboJsonPath))
|
|
|
|
{
|
2023-04-03 06:14:19 -04:00
|
|
|
amiiboJsonString = await File.ReadAllTextAsync(_amiiboJsonPath);
|
2022-07-08 14:47:11 -04:00
|
|
|
|
2023-04-03 06:14:19 -04:00
|
|
|
if (await NeedsUpdate(JsonHelper.Deserialize(amiiboJsonString, SerializerContext.AmiiboJson).LastUpdated))
|
2022-07-08 14:47:11 -04:00
|
|
|
{
|
|
|
|
amiiboJsonString = await DownloadAmiiboJson();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
amiiboJsonString = await DownloadAmiiboJson();
|
|
|
|
}
|
2023-04-23 22:08:31 -04:00
|
|
|
catch (Exception ex)
|
2022-07-08 14:47:11 -04:00
|
|
|
{
|
2023-04-23 22:08:31 -04:00
|
|
|
Logger.Error?.Print(LogClass.Application, $"Failed to download amiibo data: {ex}");
|
|
|
|
|
2022-07-08 14:47:11 -04:00
|
|
|
ShowInfoDialog();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 06:14:19 -04:00
|
|
|
_amiiboList = JsonHelper.Deserialize(amiiboJsonString, SerializerContext.AmiiboJson).Amiibo;
|
2022-07-08 14:47:11 -04:00
|
|
|
_amiiboList = _amiiboList.OrderBy(amiibo => amiibo.AmiiboSeries).ToList();
|
|
|
|
|
|
|
|
ParseAmiiboData();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ParseAmiiboData()
|
|
|
|
{
|
|
|
|
_amiiboSeries.Clear();
|
|
|
|
_amiibos.Clear();
|
|
|
|
|
|
|
|
for (int i = 0; i < _amiiboList.Count; i++)
|
|
|
|
{
|
|
|
|
if (!_amiiboSeries.Contains(_amiiboList[i].AmiiboSeries))
|
|
|
|
{
|
|
|
|
if (!ShowAllAmiibo)
|
|
|
|
{
|
2023-04-03 06:14:19 -04:00
|
|
|
foreach (AmiiboApiGamesSwitch game in _amiiboList[i].GamesSwitch)
|
2022-07-08 14:47:11 -04:00
|
|
|
{
|
|
|
|
if (game != null)
|
|
|
|
{
|
|
|
|
if (game.GameId.Contains(TitleId))
|
|
|
|
{
|
|
|
|
AmiiboSeries.Add(_amiiboList[i].AmiiboSeries);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AmiiboSeries.Add(_amiiboList[i].AmiiboSeries);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LastScannedAmiiboId != "")
|
|
|
|
{
|
|
|
|
SelectLastScannedAmiibo();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SeriesSelectedIndex = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SelectLastScannedAmiibo()
|
|
|
|
{
|
2023-04-03 06:14:19 -04:00
|
|
|
AmiiboApi scanned = _amiiboList.FirstOrDefault(amiibo => amiibo.GetId() == LastScannedAmiiboId);
|
2022-07-08 14:47:11 -04:00
|
|
|
|
|
|
|
SeriesSelectedIndex = AmiiboSeries.IndexOf(scanned.AmiiboSeries);
|
|
|
|
AmiiboSelectedIndex = AmiiboList.IndexOf(scanned);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void FilterAmiibo()
|
|
|
|
{
|
|
|
|
_amiibos.Clear();
|
|
|
|
|
|
|
|
if (_seriesSelectedIndex < 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-03 06:14:19 -04:00
|
|
|
List<AmiiboApi> amiiboSortedList = _amiiboList
|
2022-07-08 14:47:11 -04:00
|
|
|
.Where(amiibo => amiibo.AmiiboSeries == _amiiboSeries[SeriesSelectedIndex])
|
|
|
|
.OrderBy(amiibo => amiibo.Name).ToList();
|
|
|
|
|
|
|
|
for (int i = 0; i < amiiboSortedList.Count; i++)
|
|
|
|
{
|
|
|
|
if (!_amiibos.Contains(amiiboSortedList[i]))
|
|
|
|
{
|
|
|
|
if (!_showAllAmiibo)
|
|
|
|
{
|
2023-04-03 06:14:19 -04:00
|
|
|
foreach (AmiiboApiGamesSwitch game in amiiboSortedList[i].GamesSwitch)
|
2022-07-08 14:47:11 -04:00
|
|
|
{
|
|
|
|
if (game != null)
|
|
|
|
{
|
|
|
|
if (game.GameId.Contains(TitleId))
|
|
|
|
{
|
|
|
|
_amiibos.Add(amiiboSortedList[i]);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_amiibos.Add(amiiboSortedList[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AmiiboSelectedIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SetAmiiboDetails()
|
|
|
|
{
|
|
|
|
ResetAmiiboPreview();
|
|
|
|
|
|
|
|
Usage = string.Empty;
|
|
|
|
|
|
|
|
if (_amiiboSelectedIndex < 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-03 06:14:19 -04:00
|
|
|
AmiiboApi selected = _amiibos[_amiiboSelectedIndex];
|
2022-07-08 14:47:11 -04:00
|
|
|
|
|
|
|
string imageUrl = _amiiboList.FirstOrDefault(amiibo => amiibo.Equals(selected)).Image;
|
|
|
|
|
|
|
|
string usageString = "";
|
|
|
|
|
|
|
|
for (int i = 0; i < _amiiboList.Count; i++)
|
|
|
|
{
|
|
|
|
if (_amiiboList[i].Equals(selected))
|
|
|
|
{
|
|
|
|
bool writable = false;
|
|
|
|
|
2023-04-03 06:14:19 -04:00
|
|
|
foreach (AmiiboApiGamesSwitch item in _amiiboList[i].GamesSwitch)
|
2022-07-08 14:47:11 -04:00
|
|
|
{
|
|
|
|
if (item.GameId.Contains(TitleId))
|
|
|
|
{
|
2023-04-03 06:14:19 -04:00
|
|
|
foreach (AmiiboApiUsage usageItem in item.AmiiboUsage)
|
2022-07-08 14:47:11 -04:00
|
|
|
{
|
|
|
|
usageString += Environment.NewLine +
|
|
|
|
$"- {usageItem.Usage.Replace("/", Environment.NewLine + "-")}";
|
|
|
|
|
|
|
|
writable = usageItem.Write;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (usageString.Length == 0)
|
|
|
|
{
|
2023-01-03 13:45:08 -05:00
|
|
|
usageString = LocaleManager.Instance[LocaleKeys.Unknown] + ".";
|
2022-07-08 14:47:11 -04:00
|
|
|
}
|
|
|
|
|
2023-01-03 13:45:08 -05:00
|
|
|
Usage = $"{LocaleManager.Instance[LocaleKeys.Usage]} {(writable ? $" ({LocaleManager.Instance[LocaleKeys.Writable]})" : "")} : {usageString}";
|
2022-07-08 14:47:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = UpdateAmiiboPreview(imageUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<bool> NeedsUpdate(DateTime oldLastModified)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
HttpResponseMessage response =
|
|
|
|
await _httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, "https://amiibo.ryujinx.org/"));
|
|
|
|
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
|
|
{
|
2023-05-11 13:10:24 -04:00
|
|
|
return response.Content.Headers.LastModified != new DateTimeOffset(oldLastModified.Ticks - (oldLastModified.Ticks % TimeSpan.TicksPerSecond), TimeSpan.Zero);
|
2022-07-08 14:47:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2023-04-23 22:08:31 -04:00
|
|
|
catch (Exception ex)
|
2022-07-08 14:47:11 -04:00
|
|
|
{
|
2023-04-23 22:08:31 -04:00
|
|
|
Logger.Error?.Print(LogClass.Application, $"Failed to check for amiibo updates: {ex}");
|
|
|
|
|
2022-07-08 14:47:11 -04:00
|
|
|
ShowInfoDialog();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<string> DownloadAmiiboJson()
|
|
|
|
{
|
|
|
|
HttpResponseMessage response = await _httpClient.GetAsync("https://amiibo.ryujinx.org/");
|
|
|
|
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
|
|
{
|
|
|
|
string amiiboJsonString = await response.Content.ReadAsStringAsync();
|
|
|
|
|
2022-07-28 18:41:34 -04:00
|
|
|
using (FileStream amiiboJsonStream = File.Create(_amiiboJsonPath, 4096, FileOptions.WriteThrough))
|
2022-07-08 14:47:11 -04:00
|
|
|
{
|
2022-07-28 18:41:34 -04:00
|
|
|
amiiboJsonStream.Write(Encoding.UTF8.GetBytes(amiiboJsonString));
|
2022-07-08 14:47:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return amiiboJsonString;
|
|
|
|
}
|
|
|
|
|
2023-04-23 22:08:31 -04:00
|
|
|
Logger.Error?.Print(LogClass.Application, $"Failed to download amiibo data. Response status code: {response.StatusCode}");
|
|
|
|
|
2023-01-03 13:45:08 -05:00
|
|
|
await ContentDialogHelper.CreateInfoDialog(LocaleManager.Instance[LocaleKeys.DialogAmiiboApiTitle],
|
|
|
|
LocaleManager.Instance[LocaleKeys.DialogAmiiboApiFailFetchMessage],
|
|
|
|
LocaleManager.Instance[LocaleKeys.InputDialogOk],
|
2022-07-08 14:47:11 -04:00
|
|
|
"",
|
2023-01-03 13:45:08 -05:00
|
|
|
LocaleManager.Instance[LocaleKeys.RyujinxInfo]);
|
2022-07-08 14:47:11 -04:00
|
|
|
|
|
|
|
Close();
|
|
|
|
|
|
|
|
return DefaultJson;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Close()
|
|
|
|
{
|
|
|
|
Dispatcher.UIThread.Post(_owner.Close);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task UpdateAmiiboPreview(string imageUrl)
|
|
|
|
{
|
|
|
|
HttpResponseMessage response = await _httpClient.GetAsync(imageUrl);
|
|
|
|
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
|
|
{
|
|
|
|
byte[] amiiboPreviewBytes = await response.Content.ReadAsByteArrayAsync();
|
|
|
|
using (MemoryStream memoryStream = new(amiiboPreviewBytes))
|
|
|
|
{
|
|
|
|
Bitmap bitmap = new(memoryStream);
|
|
|
|
|
|
|
|
double ratio = Math.Min(AmiiboImageSize / bitmap.Size.Width,
|
|
|
|
AmiiboImageSize / bitmap.Size.Height);
|
|
|
|
|
|
|
|
int resizeHeight = (int)(bitmap.Size.Height * ratio);
|
|
|
|
int resizeWidth = (int)(bitmap.Size.Width * ratio);
|
|
|
|
|
|
|
|
AmiiboImage = bitmap.CreateScaledBitmap(new PixelSize(resizeWidth, resizeHeight));
|
|
|
|
}
|
|
|
|
}
|
2023-04-23 22:08:31 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Logger.Error?.Print(LogClass.Application, $"Failed to get amiibo preview. Response status code: {response.StatusCode}");
|
|
|
|
}
|
2022-07-08 14:47:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private void ResetAmiiboPreview()
|
|
|
|
{
|
|
|
|
using (MemoryStream memoryStream = new(_amiiboLogoBytes))
|
|
|
|
{
|
|
|
|
Bitmap bitmap = new(memoryStream);
|
|
|
|
|
|
|
|
AmiiboImage = bitmap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async void ShowInfoDialog()
|
|
|
|
{
|
2023-01-03 13:45:08 -05:00
|
|
|
await ContentDialogHelper.CreateInfoDialog(LocaleManager.Instance[LocaleKeys.DialogAmiiboApiTitle],
|
|
|
|
LocaleManager.Instance[LocaleKeys.DialogAmiiboApiConnectErrorMessage],
|
|
|
|
LocaleManager.Instance[LocaleKeys.InputDialogOk],
|
2022-07-08 14:47:11 -04:00
|
|
|
"",
|
2023-01-03 13:45:08 -05:00
|
|
|
LocaleManager.Instance[LocaleKeys.RyujinxInfo]);
|
2022-07-08 14:47:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|