ファイル
YMM4-ProxyForge/ZeroDiskProxy/Plugin/PluginHost.cs
T
routersys 582e41f313 プロキシキャッシュのメモリ解放通知移譲とストリーム競合回避を含む管理責務対応
MfProxyEncoderでProxyCacheEntry生成時にメモリ解放通知コールバックを渡すよう変更しました。
一時ファイルサイズ取得をFileInfoからFile.OpenHandleとRandomAccess.GetLengthへ変更しました。
ProxyCacheEntryに解放通知用フィールドと追跡バイト数を追加しメモリ返却時に解放を通知するようにしました。 ProxyCacheEntryのIsInMemory判定をディスク未配置条件付きに変更しました。
ProxyCacheEntryのOpenReadでメモリ参照の競合を検知して再試行するループを導入しました。
ディスクパス設定時とメモリ書き出し完了時に遅延解放フラグを立ててTryReturnBuffer()を実行するようにしました。 TryReturnBuffer()をストリーム利用中は解放しない条件に改め必要時のみバッファ返却と解放通知を行うようにしました。
ProxyCacheManagerからMemoryBudget依存と重複したRecordDeallocation(long)呼び出しを削除しました。
PluginHostのProxyCacheManager生成引数を新しいコンストラクタに合わせて整理しました。
2026-03-24 19:41:26 +09:00

153 行
4.4 KiB
C#

using System.IO;
using System.Windows;
using System.Windows.Interop;
using YukkuriMovieMaker.Commons;
using ZeroDiskProxy.Core;
using ZeroDiskProxy.Detection;
using ZeroDiskProxy.Interfaces;
using ZeroDiskProxy.Memory;
using ZeroDiskProxy.Settings;
using ZeroDiskProxy.ViewModels;
using ZeroDiskProxy.Views;
namespace ZeroDiskProxy.Plugin;
internal sealed class PluginHost : IDisposable
{
private static PluginHost? _instance;
private static readonly object Lock = new();
private int _disposed;
private GenerationPopupView? _popupView;
internal static PluginHost? Instance => _instance;
internal MemoryBudget Budget { get; }
internal ProxyCacheManager CacheManager { get; }
internal IExportDetector ExportDetector { get; }
internal string FallbackDirectory { get; }
private PluginHost(
MemoryBudget budget,
IProxyEncoderFactory encoderFactory,
IExportDetector exportDetector,
string fallbackDirectory)
{
Budget = budget;
ExportDetector = exportDetector;
FallbackDirectory = fallbackDirectory;
CacheManager = new ProxyCacheManager(encoderFactory);
CacheManager.ActiveGenerations.CollectionChanged += OnGenerationsChanged;
}
private static PluginHost CreateDefault()
{
var settings = ZeroDiskProxySettings.Default;
var budget = new MemoryBudget(settings.MemoryReserveMb, settings.MaxCacheMemoryMb);
var fallbackDirectory = Path.Combine(AppDirectories.TemporaryDirectory, "ZeroDiskProxyTemp");
IProxyEncoderFactory encoderFactory = new MfProxyEncoderFactory(
budget,
fallbackDirectory,
static () => new EncoderConfig(
ZeroDiskProxySettings.Default.EnableHardwareAcceleration,
ZeroDiskProxySettings.Default.EnableDiskFallback));
IExportDetector exportDetector = new ExportDetector();
return new PluginHost(budget, encoderFactory, exportDetector, fallbackDirectory);
}
internal static PluginHost EnsureInitialized()
{
if (_instance is not null)
return _instance;
lock (Lock)
{
_instance ??= CreateDefault();
}
return _instance;
}
internal static void TearDown()
{
lock (Lock)
{
if (_instance is null)
return;
_instance.Dispose();
_instance = null;
}
}
private void OnGenerationsChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
Application.Current?.Dispatcher.BeginInvoke(() =>
{
if (CacheManager.ActiveGenerations.Count > 0)
EnsurePopupVisible();
else
_popupView?.Hide();
});
}
private void EnsurePopupVisible()
{
if (_popupView is null)
{
var vm = new GenerationPopupViewModel(CacheManager.ActiveGenerations);
_popupView = new GenerationPopupView(vm);
var mainWindow = Application.Current?.MainWindow;
if (mainWindow is { IsLoaded: true })
{
try
{
_popupView.Owner = mainWindow;
}
catch
{
var helper = new WindowInteropHelper(_popupView);
var mainHelper = new WindowInteropHelper(mainWindow);
if (mainHelper.Handle != nint.Zero)
helper.Owner = mainHelper.Handle;
}
}
}
if (!_popupView.IsVisible)
_popupView.Show();
}
public void Dispose()
{
if (Interlocked.Exchange(ref _disposed, 1) != 0)
return;
CacheManager.ActiveGenerations.CollectionChanged -= OnGenerationsChanged;
Application.Current?.Dispatcher.Invoke(() =>
{
_popupView?.ForceClose();
_popupView = null;
});
CacheManager.Dispose();
CleanupFallbackDirectory();
}
private void CleanupFallbackDirectory()
{
try
{
if (!Directory.Exists(FallbackDirectory))
return;
foreach (var file in Directory.GetFiles(FallbackDirectory))
{
try { File.Delete(file); }
catch { }
}
}
catch { }
}
}