2022-12-13 22:33:19 +00:00
|
|
|
|
|
|
|
|
|
using Avalonia.Controls;
|
2022-12-13 01:27:30 +00:00
|
|
|
|
using Ryujinx.Ava.Common.Locale;
|
|
|
|
|
using Ryujinx.Ava.Ui.Windows;
|
|
|
|
|
using Ryujinx.Common.Configuration;
|
|
|
|
|
using Ryujinx.Common.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.IO.Compression;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
using Logger = Ryujinx.Common.Logging.Logger;
|
|
|
|
|
using Path = System.IO.Path;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.Ava.Ui.Controls
|
|
|
|
|
{
|
|
|
|
|
internal class BackupSavedataCommand : ICommand
|
|
|
|
|
{
|
|
|
|
|
public event EventHandler CanExecuteChanged;
|
2022-12-13 22:33:19 +00:00
|
|
|
|
|
2022-12-13 01:27:30 +00:00
|
|
|
|
private readonly IControl parentControl;
|
|
|
|
|
|
|
|
|
|
public BackupSavedataCommand(IControl parentControl)
|
|
|
|
|
{
|
|
|
|
|
this.parentControl = parentControl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool CanExecute(object parameter)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Execute(object parameter)
|
|
|
|
|
{
|
|
|
|
|
SaveUserSaveDirectoryAsZip();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async void SaveUserSaveDirectoryAsZip()
|
|
|
|
|
{
|
2022-12-13 22:33:19 +00:00
|
|
|
|
CreateBackupZip(await GetAndPrepareBackupPath());
|
2022-12-13 01:27:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-13 22:33:19 +00:00
|
|
|
|
private async Task<string> GetAndPrepareBackupPath()
|
2022-12-13 01:27:30 +00:00
|
|
|
|
{
|
2022-12-13 22:33:19 +00:00
|
|
|
|
SaveFileDialog saveFileDialog = new SaveFileDialog()
|
2022-12-13 01:27:30 +00:00
|
|
|
|
{
|
2022-12-13 22:33:19 +00:00
|
|
|
|
Title = LocaleManager.Instance["CreateZipFileDialogTitle"],
|
|
|
|
|
InitialFileName = "Ryujinx_backup.zip",
|
|
|
|
|
Filters = new System.Collections.Generic.List<FileDialogFilter>(new[] { new FileDialogFilter() { Extensions = new System.Collections.Generic.List<string>() { "zip" } } })
|
2022-12-13 01:27:30 +00:00
|
|
|
|
};
|
|
|
|
|
|
2022-12-13 22:33:19 +00:00
|
|
|
|
string zipPath = await saveFileDialog.ShowAsync(parentControl.VisualRoot as MainWindow);
|
|
|
|
|
|
|
|
|
|
if (File.Exists(zipPath))
|
|
|
|
|
{
|
|
|
|
|
File.Delete(zipPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return zipPath;
|
2022-12-13 01:27:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-13 22:33:19 +00:00
|
|
|
|
private void CreateBackupZip(string userBackupPath)
|
2022-12-13 01:27:30 +00:00
|
|
|
|
{
|
2022-12-13 22:33:19 +00:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(userBackupPath) && Directory.Exists(Directory.GetParent(userBackupPath).FullName))
|
2022-12-13 01:27:30 +00:00
|
|
|
|
{
|
|
|
|
|
string saveDir = Path.Combine(AppDataManager.BaseDirPath, AppDataManager.DefaultNandDir, "user", "save");
|
|
|
|
|
|
2022-12-13 22:33:19 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Logger.Info.Value.Print(LogClass.Application, $"Start creating backup...", nameof(BackupSavedataCommand));
|
2022-12-13 01:27:30 +00:00
|
|
|
|
|
2022-12-13 22:33:19 +00:00
|
|
|
|
ZipFile.CreateFromDirectory(saveDir, userBackupPath);
|
2022-12-13 01:27:30 +00:00
|
|
|
|
|
2022-12-13 22:33:19 +00:00
|
|
|
|
Logger.Info.Value.Print(LogClass.Application, $"Backup done. Zip is locate under {userBackupPath}", nameof(BackupSavedataCommand));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
2022-12-13 01:27:30 +00:00
|
|
|
|
{
|
2022-12-13 22:33:19 +00:00
|
|
|
|
Logger.Error.Value.Print(LogClass.Application, $"Could not create backup zip file.", nameof(BackupSavedataCommand));
|
2022-12-13 01:27:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|