ファイル

45 行
1.4 KiB
C#

using System.IO;
using System.Security.Cryptography;
using System.Windows;
using YukkuriMovieMaker.Plugin;
using YukkuriMovieMaker.Settings;
using YukkuriMovieMaker.ViewModels;
namespace UndoTree;
internal static class UndoTreeProject
{
public static string? PathOfOnlyWindow()
{
if (Application.Current is not { } application)
return null;
return application.Dispatcher.CheckAccess() ? Read(application) : application.Dispatcher.Invoke(() => Read(application));
}
public static string? HashOf(string path)
{
try
{
using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
return Convert.ToHexString(SHA256.HashData(stream));
}
catch (Exception exception) when (exception is IOException or UnauthorizedAccessException or ArgumentException or NotSupportedException)
{
return null;
}
}
static string? Read(Application application)
{
var indexes = application.Windows.OfType<Window>()
.Select(window => window.DataContext)
.OfType<IMainViewModel>()
.Select(model => model.Index)
.Distinct()
.ToArray();
var states = SettingsBase<YMMSettings>.Default.MainWindowStates;
return indexes is [var index] && index >= 0 && index < states.Count ? states[index].ProjectPath : null;
}
}