Files
MesenCE/UI/ViewModels/SelectStorageFolderViewModel.cs
SourMesen 40335ce735 UI: Update to Avalonia 12, .NET 10 and replace ReactiveUI with MVVM community toolkit (#142)
Also fixes DX11.0 support (for Win7 PCs that aren't fully updated), which stopped working after the recent variable refresh rate changes
2026-07-03 16:04:42 +09:00

115 lines
3.2 KiB
C#

using Avalonia.Metadata;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
using Mesen.Config;
using Mesen.Utilities;
using Mesen.ViewModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
namespace Mesen.ViewModels
{
public partial class SelectStorageFolderViewModel : ViewModelBase
{
[ObservableProperty] public partial bool StoreInUserProfile { get; set; }
[ObservableProperty] public partial bool IsCopying { get; set; }
[ObservableProperty] public partial int CopyProgress { get; set; }
[ObservableProperty] public partial string DestinationFolder { get; private set; } = "";
public SelectStorageFolderViewModel()
{
UpdateDestinationFolder();
}
partial void OnStoreInUserProfileChanged(bool value)
{
UpdateDestinationFolder();
}
private void UpdateDestinationFolder()
{
DestinationFolder = StoreInUserProfile ? ConfigManager.DefaultDocumentsFolder : ConfigManager.DefaultPortableFolder;
}
private void GetFilesToCopy(string source, string target, List<string> sourceFiles, List<string> targetFiles)
{
if(!Directory.Exists(target)) {
Directory.CreateDirectory(target);
}
foreach(string file in Directory.GetFiles(source)) {
string ext = Path.GetExtension(file).ToLower();
if(ext != ".exe" && ext != ".dll" && ext != "") {
sourceFiles.Add(file);
targetFiles.Add(Path.Combine(target, Path.GetFileName(file)));
}
}
foreach(string subdir in Directory.GetDirectories(source)) {
GetFilesToCopy(subdir, Path.Combine(target, Path.GetFileName(subdir)), sourceFiles, targetFiles);
}
}
private Task<bool> CopyFiles(string source, string target)
{
return Task.Run(() => {
IsCopying = true;
try {
List<string> sourceFiles = new();
List<string> targetFiles = new();
GetFilesToCopy(source, target, sourceFiles, targetFiles);
int fileCount = sourceFiles.Count;
int filesCopied = 0;
for(int i = 0; i < sourceFiles.Count; i++) {
try {
File.Copy(sourceFiles[i], targetFiles[i], true);
} catch(Exception ex) {
Dispatcher.UIThread.Post(() => {
MesenMsgBox.ShowException(ex);
});
return false;
}
filesCopied++;
CopyProgress = filesCopied * 100 / fileCount;
}
return true;
} finally {
IsCopying = false;
}
});
}
public async Task<bool> MigrateData()
{
string source = ConfigManager.HomeFolder;
string target = StoreInUserProfile ? ConfigManager.DefaultDocumentsFolder : ConfigManager.DefaultPortableFolder;
if(string.Compare(source.Trim(), target.Trim(), StringComparison.OrdinalIgnoreCase) == 0) {
return false;
}
ConfigManager.Config.Save();
if(await CopyFiles(source, target)) {
try {
if(File.Exists(Path.Combine(source, "settings.backup.json"))) {
File.Delete(Path.Combine(source, "settings.backup.json"));
}
File.Move(Path.Combine(source, "settings.json"), Path.Combine(source, "settings.backup.json"));
} catch(Exception ex) {
Dispatcher.UIThread.Post(() => {
MesenMsgBox.ShowException(ex);
});
}
ConfigManager.ResetHomeFolder();
return true;
} else {
return false;
}
}
}
}