2020-08-22 19:28:58 +01:00
|
|
|
using System;
|
2025-07-14 13:50:33 +01:00
|
|
|
using System.Collections.Generic;
|
2020-08-22 19:28:58 +01:00
|
|
|
using System.Collections.ObjectModel;
|
2025-07-14 13:50:33 +01:00
|
|
|
using System.Diagnostics;
|
2020-08-22 19:28:58 +01:00
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2025-07-29 21:33:57 +01:00
|
|
|
using System.Threading;
|
2020-08-22 19:28:58 +01:00
|
|
|
using System.Threading.Tasks;
|
2025-07-24 11:11:27 +01:00
|
|
|
using System.Windows.Input;
|
2025-07-14 13:50:33 +01:00
|
|
|
using Avalonia.Controls;
|
|
|
|
|
using Avalonia.Platform.Storage;
|
2020-08-22 19:28:58 +01:00
|
|
|
using Avalonia.Threading;
|
2025-07-24 11:11:27 +01:00
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
using CommunityToolkit.Mvvm.Input;
|
2020-08-22 19:28:58 +01:00
|
|
|
using RomRepoMgr.Core.EventArgs;
|
2025-07-14 13:50:33 +01:00
|
|
|
using RomRepoMgr.Models;
|
2020-08-30 03:00:14 +01:00
|
|
|
using RomRepoMgr.Resources;
|
2025-07-24 21:23:53 +01:00
|
|
|
using Serilog;
|
|
|
|
|
using Serilog.Extensions.Logging;
|
2020-08-22 19:28:58 +01:00
|
|
|
|
2024-11-09 01:37:59 +00:00
|
|
|
namespace RomRepoMgr.ViewModels;
|
|
|
|
|
|
2025-07-24 11:11:27 +01:00
|
|
|
public sealed partial class ImportDatFolderViewModel : ViewModelBase
|
2020-08-22 19:28:58 +01:00
|
|
|
{
|
2025-07-14 13:50:33 +01:00
|
|
|
readonly Stopwatch _stopwatch = new();
|
|
|
|
|
bool _allFilesChecked;
|
2025-07-24 11:11:27 +01:00
|
|
|
[ObservableProperty]
|
|
|
|
|
bool _canClose;
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
bool _canStart;
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
string _category;
|
|
|
|
|
string[] _datFiles;
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
string _folderPath;
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
bool _isImporting;
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
bool _isReady;
|
|
|
|
|
int _listPosition;
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
bool _progressIsIndeterminate;
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
double _progressMaximum;
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
double _progressMinimum;
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
double _progressValue;
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
bool _progressVisible;
|
|
|
|
|
bool _recursiveChecked;
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
string _statusMessage;
|
2025-07-14 13:50:33 +01:00
|
|
|
|
2025-07-09 14:07:26 +01:00
|
|
|
public ImportDatFolderViewModel()
|
|
|
|
|
{
|
2025-07-14 13:50:33 +01:00
|
|
|
CanClose = true;
|
|
|
|
|
IsReady = true;
|
2025-07-24 11:11:27 +01:00
|
|
|
SelectFolderCommand = new AsyncRelayCommand(SelectFolderAsync);
|
|
|
|
|
CloseCommand = new RelayCommand(Close);
|
|
|
|
|
StartCommand = new RelayCommand(Start);
|
2025-07-14 13:50:33 +01:00
|
|
|
}
|
|
|
|
|
|
2025-07-24 11:11:27 +01:00
|
|
|
public ICommand SelectFolderCommand { get; }
|
|
|
|
|
public Window View { get; init; }
|
2020-08-22 19:28:58 +01:00
|
|
|
|
2024-11-09 01:37:59 +00:00
|
|
|
public bool AllFilesChecked
|
|
|
|
|
{
|
|
|
|
|
get => _allFilesChecked;
|
|
|
|
|
set
|
2020-08-22 19:28:58 +01:00
|
|
|
{
|
2025-07-24 11:11:27 +01:00
|
|
|
SetProperty(ref _allFilesChecked, value);
|
2024-11-09 01:37:59 +00:00
|
|
|
RefreshFiles();
|
2020-08-22 19:28:58 +01:00
|
|
|
}
|
2024-11-09 01:37:59 +00:00
|
|
|
}
|
2020-08-22 19:28:58 +01:00
|
|
|
|
2024-11-09 01:37:59 +00:00
|
|
|
public bool RecursiveChecked
|
|
|
|
|
{
|
|
|
|
|
get => _recursiveChecked;
|
|
|
|
|
set
|
2020-08-22 19:28:58 +01:00
|
|
|
{
|
2025-07-24 11:11:27 +01:00
|
|
|
SetProperty(ref _recursiveChecked, value);
|
2024-11-09 01:37:59 +00:00
|
|
|
RefreshFiles();
|
2020-08-22 19:28:58 +01:00
|
|
|
}
|
2024-11-09 01:37:59 +00:00
|
|
|
}
|
2020-08-22 19:28:58 +01:00
|
|
|
|
2025-07-24 11:11:27 +01:00
|
|
|
public ICommand CloseCommand { get; }
|
|
|
|
|
public ICommand StartCommand { get; }
|
2025-07-14 13:50:33 +01:00
|
|
|
public ObservableCollection<DatImporter> Importers { get; } = [];
|
2020-08-22 19:28:58 +01:00
|
|
|
|
2025-07-14 13:50:33 +01:00
|
|
|
void Start()
|
2024-11-09 01:37:59 +00:00
|
|
|
{
|
2025-07-14 13:50:33 +01:00
|
|
|
_listPosition = 0;
|
|
|
|
|
ProgressMinimum = 0;
|
|
|
|
|
ProgressMaximum = _datFiles.Length;
|
|
|
|
|
ProgressValue = 0;
|
|
|
|
|
ProgressIsIndeterminate = false;
|
|
|
|
|
ProgressVisible = true;
|
|
|
|
|
CanClose = false;
|
|
|
|
|
CanStart = false;
|
|
|
|
|
IsReady = false;
|
|
|
|
|
IsImporting = true;
|
|
|
|
|
_stopwatch.Restart();
|
2020-08-22 19:28:58 +01:00
|
|
|
|
2025-07-29 21:33:57 +01:00
|
|
|
_ = Task.Run(Import);
|
2024-11-09 01:37:59 +00:00
|
|
|
}
|
2020-08-22 19:28:58 +01:00
|
|
|
|
2025-07-14 13:50:33 +01:00
|
|
|
void Import()
|
2024-11-09 01:37:59 +00:00
|
|
|
{
|
2025-07-29 21:33:57 +01:00
|
|
|
Parallel.ForEach(_datFiles,
|
|
|
|
|
new ParallelOptions
|
|
|
|
|
{
|
|
|
|
|
MaxDegreeOfParallelism = Environment.ProcessorCount
|
|
|
|
|
},
|
|
|
|
|
datFile =>
|
|
|
|
|
{
|
|
|
|
|
Dispatcher.UIThread.Post(() =>
|
|
|
|
|
{
|
|
|
|
|
StatusMessage = string.Format(Localization.ImportingItem, Path.GetFileName(datFile));
|
|
|
|
|
|
|
|
|
|
ProgressValue = _listPosition;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var model = new DatImporter
|
|
|
|
|
{
|
|
|
|
|
Filename = Path.GetFileName(datFile),
|
|
|
|
|
Minimum = 0,
|
|
|
|
|
Maximum = _datFiles.Length,
|
|
|
|
|
Progress = 0,
|
|
|
|
|
Indeterminate = false
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var worker = new Core.Workers.DatImporter(datFile,
|
|
|
|
|
Category,
|
|
|
|
|
new SerilogLoggerFactory(Log.Logger));
|
|
|
|
|
|
|
|
|
|
worker.ErrorOccurred += model.OnErrorOccurred;
|
|
|
|
|
worker.SetIndeterminateProgress += model.OnSetIndeterminateProgress;
|
|
|
|
|
worker.SetMessage += model.OnSetMessage;
|
|
|
|
|
worker.SetProgress += model.OnSetProgress;
|
|
|
|
|
worker.SetProgressBounds += model.OnSetProgressBounds;
|
|
|
|
|
worker.WorkFinished += model.OnWorkFinished;
|
|
|
|
|
worker.RomSetAdded += RomSetAdded;
|
|
|
|
|
|
|
|
|
|
Dispatcher.UIThread.Post(() => Importers.Add(model));
|
|
|
|
|
|
|
|
|
|
worker.Import();
|
|
|
|
|
|
|
|
|
|
Interlocked.Increment(ref _listPosition);
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-14 13:50:33 +01:00
|
|
|
Dispatcher.UIThread.Post(() =>
|
|
|
|
|
{
|
2025-07-29 21:33:57 +01:00
|
|
|
ProgressVisible = false;
|
|
|
|
|
StatusMessage = Localization.Finished;
|
|
|
|
|
CanClose = true;
|
|
|
|
|
CanStart = false;
|
|
|
|
|
IsReady = true;
|
|
|
|
|
_stopwatch.Stop();
|
2025-07-14 13:50:33 +01:00
|
|
|
});
|
2024-11-09 01:37:59 +00:00
|
|
|
}
|
2020-09-05 01:52:43 +01:00
|
|
|
|
2025-07-14 13:50:33 +01:00
|
|
|
public event EventHandler<RomSetEventArgs> RomSetAdded;
|
2020-08-22 19:28:58 +01:00
|
|
|
|
2025-07-14 13:50:33 +01:00
|
|
|
void Close()
|
2024-11-09 01:37:59 +00:00
|
|
|
{
|
2025-07-14 13:50:33 +01:00
|
|
|
View.Close();
|
2024-11-09 01:37:59 +00:00
|
|
|
}
|
2020-08-22 19:28:58 +01:00
|
|
|
|
2025-07-14 13:50:33 +01:00
|
|
|
async Task SelectFolderAsync()
|
2024-11-09 01:37:59 +00:00
|
|
|
{
|
2025-07-14 13:50:33 +01:00
|
|
|
IReadOnlyList<IStorageFolder> result =
|
|
|
|
|
await View.StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions
|
|
|
|
|
{
|
|
|
|
|
Title = Localization.ImportDatFolderDialogTitle
|
|
|
|
|
});
|
2020-08-22 19:28:58 +01:00
|
|
|
|
2025-07-14 13:50:33 +01:00
|
|
|
if(result.Count < 1) return;
|
2020-08-22 19:28:58 +01:00
|
|
|
|
2025-07-14 13:50:33 +01:00
|
|
|
FolderPath = result[0].TryGetLocalPath() ?? string.Empty;
|
|
|
|
|
RecursiveChecked = true;
|
|
|
|
|
AllFilesChecked = false;
|
|
|
|
|
RefreshFiles();
|
|
|
|
|
}
|
2020-08-22 19:28:58 +01:00
|
|
|
|
2025-07-07 22:17:27 +01:00
|
|
|
void RefreshFiles()
|
2024-11-09 01:37:59 +00:00
|
|
|
{
|
2025-07-07 22:17:27 +01:00
|
|
|
_ = Task.Run(() =>
|
2020-08-22 19:28:58 +01:00
|
|
|
{
|
2025-07-07 22:17:27 +01:00
|
|
|
Dispatcher.UIThread.Post(() =>
|
|
|
|
|
{
|
|
|
|
|
IsReady = false;
|
|
|
|
|
ProgressVisible = true;
|
|
|
|
|
ProgressIsIndeterminate = true;
|
|
|
|
|
StatusMessage = Localization.SearchingForFiles;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if(_allFilesChecked)
|
|
|
|
|
{
|
2025-07-07 22:44:34 +01:00
|
|
|
_datFiles = Directory.GetFiles(FolderPath,
|
|
|
|
|
"*.*",
|
|
|
|
|
_recursiveChecked
|
|
|
|
|
? SearchOption.AllDirectories
|
|
|
|
|
: SearchOption.TopDirectoryOnly)
|
|
|
|
|
.Order()
|
|
|
|
|
.ToArray();
|
2025-07-07 22:17:27 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
string[] dats = Directory.GetFiles(FolderPath,
|
|
|
|
|
"*.dat",
|
|
|
|
|
_recursiveChecked
|
|
|
|
|
? SearchOption.AllDirectories
|
|
|
|
|
: SearchOption.TopDirectoryOnly);
|
|
|
|
|
|
|
|
|
|
string[] xmls = Directory.GetFiles(FolderPath,
|
|
|
|
|
"*.xml",
|
|
|
|
|
_recursiveChecked
|
|
|
|
|
? SearchOption.AllDirectories
|
|
|
|
|
: SearchOption.TopDirectoryOnly);
|
|
|
|
|
|
2025-07-07 22:44:34 +01:00
|
|
|
_datFiles = dats.Concat(xmls).Order().ToArray();
|
2025-07-07 22:17:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Dispatcher.UIThread.Post(() =>
|
|
|
|
|
{
|
|
|
|
|
IsReady = true;
|
|
|
|
|
ProgressVisible = false;
|
|
|
|
|
StatusMessage = string.Format(Localization.FoundFiles, _datFiles.Length);
|
|
|
|
|
CanClose = true;
|
|
|
|
|
CanStart = true;
|
|
|
|
|
});
|
2020-08-22 19:28:58 +01:00
|
|
|
});
|
2025-07-07 22:17:27 +01:00
|
|
|
}
|
2020-08-22 19:28:58 +01:00
|
|
|
}
|