2025-08-19 00:27:53 -07:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Avalonia.Controls;
|
|
|
|
|
using Avalonia.Interactivity;
|
|
|
|
|
using Avalonia.Platform.Storage;
|
|
|
|
|
using Avalonia.Threading;
|
|
|
|
|
using MercuryConverter.Data;
|
|
|
|
|
using UAssetAPI;
|
|
|
|
|
using UAssetAPI.UnrealTypes;
|
|
|
|
|
|
|
|
|
|
namespace MercuryConverter.UI.Dialogs;
|
|
|
|
|
|
|
|
|
|
public partial class DataScanning : UserControl
|
|
|
|
|
{
|
|
|
|
|
public static DataScanning? Instance { get; private set; }
|
|
|
|
|
public DataScanning()
|
|
|
|
|
{
|
|
|
|
|
Instance = this;
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
|
|
|
|
|
if (!Design.IsDesignMode)
|
|
|
|
|
RunFlow();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RunFlow()
|
|
|
|
|
{
|
|
|
|
|
Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
var path = ""; // TODO: set to current/saved data path
|
|
|
|
|
|
|
|
|
|
// Content selection
|
|
|
|
|
var selectedPath = await BeginDirSelection();
|
|
|
|
|
Console.WriteLine($"selectedPath=[{selectedPath}]");
|
|
|
|
|
|
|
|
|
|
if (selectedPath == "") // cancelled opening folder
|
|
|
|
|
{
|
|
|
|
|
// TODO:
|
|
|
|
|
// return and go to completed mode if scan already completed
|
|
|
|
|
// continue if no scan has been completed
|
|
|
|
|
// break if we already have a path but somehow not scanned
|
|
|
|
|
UISetError("No data folder provided.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!Directory.Exists(selectedPath))
|
|
|
|
|
{
|
|
|
|
|
UISetError("Folder does not exist.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!(File.Exists(Path.Combine(selectedPath, "MusicParameterTable.uasset")) && File.Exists(Path.Combine(selectedPath, "MusicParameterTable.uexp"))))
|
|
|
|
|
{
|
2025-08-19 01:03:06 -07:00
|
|
|
UISetError("Missing MusicParameterTable asset files.\nPlease ensure you've set up your data folder properly!");
|
2025-08-19 00:27:53 -07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
path = selectedPath;
|
|
|
|
|
|
|
|
|
|
UIScanningMode(path);
|
|
|
|
|
Database.SetupNew(path);
|
|
|
|
|
UIScanCompletedMode();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UISelectMode()
|
|
|
|
|
{
|
|
|
|
|
UISetError();
|
|
|
|
|
Dispatcher.UIThread.Post(() =>
|
|
|
|
|
{
|
|
|
|
|
ScanStatus.Text = "select your data folder...";
|
|
|
|
|
ScanPath.IsVisible = false;
|
2025-08-19 01:03:06 -07:00
|
|
|
ScanInfo.IsVisible = false;
|
2025-08-19 00:27:53 -07:00
|
|
|
ButtonGroup.IsVisible = false;
|
|
|
|
|
ProgressAnimation.IsVisible = true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UIScanningMode(string path)
|
|
|
|
|
{
|
|
|
|
|
UISetError();
|
|
|
|
|
Dispatcher.UIThread.Post(() =>
|
|
|
|
|
{
|
|
|
|
|
ScanStatus.Text = "scanning...";
|
|
|
|
|
ScanPath.IsVisible = true;
|
|
|
|
|
ScanPath.Text = path;
|
2025-08-19 01:03:06 -07:00
|
|
|
ScanInfo.IsVisible = false;
|
2025-08-19 00:27:53 -07:00
|
|
|
ButtonGroup.IsVisible = false;
|
|
|
|
|
ProgressAnimation.IsVisible = true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UIScanCompletedMode()
|
|
|
|
|
{
|
|
|
|
|
Dispatcher.UIThread.Post(() =>
|
|
|
|
|
{
|
|
|
|
|
ScanStatus.Text = "scan complete";
|
|
|
|
|
ScanPath.IsVisible = true;
|
|
|
|
|
ScanInfo.IsVisible = true;
|
2025-08-19 01:03:06 -07:00
|
|
|
ScanInfoCountText.Text = Database.Songs.Count.ToString();
|
2025-08-19 00:27:53 -07:00
|
|
|
ButtonGroup.IsVisible = true;
|
|
|
|
|
ProgressAnimation.IsVisible = false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-08-19 01:03:06 -07:00
|
|
|
/// Use only when a scan is no longer running.
|
2025-08-19 00:27:53 -07:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="error"></param>
|
|
|
|
|
private void UISetError(string? error = null)
|
|
|
|
|
{
|
|
|
|
|
Dispatcher.UIThread.Post(() =>
|
|
|
|
|
{
|
|
|
|
|
if (error == null)
|
|
|
|
|
{
|
|
|
|
|
ScanError.IsVisible = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ScanError.IsVisible = true;
|
|
|
|
|
ErrorText.Text = error;
|
|
|
|
|
ProgressAnimation.IsVisible = false;
|
|
|
|
|
ButtonGroup.IsVisible = true;
|
|
|
|
|
ScanStatus.Text = "an error has occurred";
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<string> BeginDirSelection(string? startDir = null)
|
|
|
|
|
{
|
|
|
|
|
IReadOnlyList<IStorageFolder>? dirSelection = null;
|
|
|
|
|
|
|
|
|
|
UISelectMode();
|
|
|
|
|
|
|
|
|
|
await Dispatcher.UIThread.Invoke(async () =>
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(250);
|
|
|
|
|
var tl = TopLevel.GetTopLevel(MainWindow.Instance)!;
|
|
|
|
|
dirSelection = await tl.StorageProvider.OpenFolderPickerAsync
|
|
|
|
|
(
|
|
|
|
|
new FolderPickerOpenOptions
|
|
|
|
|
{
|
|
|
|
|
Title = "Locate Data Folder",
|
|
|
|
|
AllowMultiple = false,
|
|
|
|
|
SuggestedStartLocation = startDir == null ? null : await tl.StorageProvider.TryGetFolderFromPathAsync(startDir),
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (dirSelection!.Count <= 0)
|
|
|
|
|
{
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dirSelection!.First().TryGetLocalPath()!;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CloseHandler(object sender, RoutedEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
Dispatcher.UIThread.Post(() =>
|
|
|
|
|
{
|
|
|
|
|
MainWindow.Instance!.Dialog.IsOpen = false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OpenDataHandler(object sender, RoutedEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
RunFlow();
|
|
|
|
|
}
|
|
|
|
|
}
|