ファイル
YMM4-ProxyForge/ZeroDiskProxy/Plugin/ZeroDiskProxyPlugin.cs
T
routersys ff929da7a0 不要なMF相互運用ファイル削除とコード全体のリファクタリング
未使用のMFインターロップファイルを削除しました。
MfProxyEncoderのトランスコード進捗報告処理をTranscodeProgressReporterクラスに抽出しました。
キャンセル登録の型をIAsyncOperationWithProgressからIAsyncActionWithProgressに修正しました。
MfProxyEncoderFactory・MemoryBudget・PooledMemoryStream・CacheEntryViewModel等をプライマリコンストラクタ構文に変換しました。
ProxyCacheManagerのCacheKeyをrecord structに変換し、Equalsのオーバーライドを整理しました。
PooledBufferをmutableなstructに変更し、Interlocked.Exchangeによるスレッドセーフなディスポーズを実装しました。
FallbackStreamにRead(Span<byte>)・Write(ReadOnlySpan<byte>)・WriteAsyncの各オーバーロードを追加しました。
MemoryBudgetのTickCountを32ビットuintにキャストし、ラッピング演算の整合性を修正しました。
GetAllSnapshotsをList<T>ベースに変更し、配列の事前確保とArray.Resizeを除去しました。
ZeroDiskProxyVideoSourceのUpgrade適用ロジックをTryApplyUpgradeメソッドとして抽出しました。
ProxyGenerationItem・ByteFormatter・CacheEntryViewModelの文字列生成をstring.Createと補間文字列に統一しました。
PluginHostからResourceRegistryへの依存を取り除き、関連するDispose呼び出しを削除しました。
CheckExportWindowにDebug.Assertを追加し、スレッドプールの非同期継続から呼ばれた場合の前提条件違反を検出できるようにしました。
ClearAllのforeachでデコンストラクション構文をkeyプロパティの明示的な参照に変更しました。
CreateCoreメソッドをインスタンスメンバーからstaticメソッドに変更しました。
パターンマッチングをis { IsLoaded: true }・is { IsValid: true }形式に統一しました。
2026-03-24 17:09:37 +09:00

174 行
5.4 KiB
C#

using System.Diagnostics;
using System.IO;
using System.Windows;
using YukkuriMovieMaker.Commons;
using YukkuriMovieMaker.Plugin;
using YukkuriMovieMaker.Plugin.FileSource;
using ZeroDiskProxy.Core;
using ZeroDiskProxy.Settings;
namespace ZeroDiskProxy.Plugin;
internal sealed class ZeroDiskProxyPlugin : IVideoFileSourcePlugin
{
public string Name => "ZeroDiskProxy";
public ZeroDiskProxyPlugin()
{
PluginHost.EnsureInitialized();
if (Application.Current is not null)
Application.Current.Exit += OnApplicationExit;
}
public IVideoFileSource? CreateVideoFileSource(IGraphicsDevicesAndContext devices, string filePath)
{
try
{
return CreateCore(devices, filePath);
}
catch (Exception ex)
{
Debug.WriteLine(string.Concat("[ZeroDiskProxy] CreateCore failed for ", filePath, ": ", ex.Message));
return FallbackLoad(devices, filePath);
}
}
private static IVideoFileSource? CreateCore(IGraphicsDevicesAndContext devices, string filePath)
{
var host = PluginHost.Instance;
if (host is null)
return null;
var settings = ZeroDiskProxySettings.Default;
if (!settings.UseProxy && !settings.AutoGenerate)
return null;
if (host.ExportDetector.IsExporting())
return WrapFromOther(devices, filePath);
if (!IsLargeEnough(filePath, settings.MinFileSizeForProxy))
return null;
var proxyScale = settings.Scale / 100f;
if (settings.UseProxy)
{
var cached = host.CacheManager.TryGetProxy(filePath, proxyScale);
if (cached is { IsValid: true })
return LoadFromCache(devices, cached, host.FallbackDirectory);
host.CacheManager.StartProxyGeneration(filePath, proxyScale, settings);
var source = WrapFromOther(devices, filePath);
if (source is null)
return null;
return new ZeroDiskProxyVideoSource(source, devices, () => TryLoadProxy(devices, filePath, proxyScale, host));
}
if (settings.AutoGenerate && !host.CacheManager.ProxyExists(filePath, proxyScale))
host.CacheManager.StartProxyGeneration(filePath, proxyScale, settings);
return null;
}
private static IVideoFileSource? LoadFromCache(
IGraphicsDevicesAndContext devices, ProxyCacheEntry entry, string fallbackDir)
{
try
{
var tempPath = entry.GetOrCreateTempFilePath(fallbackDir);
var source = LoadRaw(devices, tempPath);
if (source is null)
return null;
return entry.Scale < 0.99f
? new ZeroDiskProxyVideoSourceWithScale(source, devices, entry.Scale)
: new ZeroDiskProxyVideoSource(source, devices);
}
catch (Exception ex)
{
Debug.WriteLine(string.Concat("[ZeroDiskProxy] LoadFromCache failed: ", ex.Message));
return null;
}
}
private static IVideoFileSource? WrapFromOther(IGraphicsDevicesAndContext devices, string filePath)
{
var source = LoadRaw(devices, filePath);
return source is not null ? new ZeroDiskProxyVideoSource(source, devices) : null;
}
private static IVideoFileSource? LoadRaw(IGraphicsDevicesAndContext devices, string filePath)
{
foreach (var plugin in PluginLoader.VideoFileSourcePlugins)
{
if (plugin is ZeroDiskProxyPlugin)
continue;
try
{
var s = plugin.CreateVideoFileSource(devices, filePath);
if (s is not null)
return s;
}
catch (Exception ex)
{
Debug.WriteLine(string.Concat("[ZeroDiskProxy] Plugin ", plugin.Name, " failed for ", filePath, ": ", ex.Message));
}
}
return null;
}
private static IVideoFileSource? FallbackLoad(IGraphicsDevicesAndContext devices, string filePath)
{
try
{
return WrapFromOther(devices, filePath);
}
catch (Exception ex)
{
Debug.WriteLine(string.Concat("[ZeroDiskProxy] FallbackLoad failed for ", filePath, ": ", ex.Message));
return null;
}
}
private static IVideoFileSource? TryLoadProxy(
IGraphicsDevicesAndContext devices, string filePath, float proxyScale, PluginHost host)
{
var cached = host.CacheManager.TryGetProxy(filePath, proxyScale);
if (cached is null || !cached.IsValid)
return null;
var source = LoadFromCache(devices, cached, host.FallbackDirectory);
if (source is null)
host.CacheManager.RemoveProxy(filePath, proxyScale);
return source;
}
private static bool IsLargeEnough(string filePath, int minSizeMb)
{
try
{
var info = new FileInfo(filePath);
return info.Exists && info.Length >= (long)minSizeMb * 1048576L;
}
catch
{
return false;
}
}
private static void OnApplicationExit(object? sender, ExitEventArgs e)
{
try
{
if (ZeroDiskProxySettings.Default.ClearCacheOnExit)
PluginHost.Instance?.CacheManager.ClearAll();
PluginHost.TearDown();
}
catch { }
}
}