ファイル
YMM4-ProxyForge/ZeroDiskProxy/Detection/ExportDetector.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

94 行
2.9 KiB
C#

using System.Diagnostics;
using System.Runtime.InteropServices;
using YukkuriMovieMaker.Resources.Localization;
using ZeroDiskProxy.Interfaces;
namespace ZeroDiskProxy.Detection;
internal sealed partial class ExportDetector : IExportDetector
{
[LibraryImport("user32.dll")]
private static partial uint GetWindowThreadProcessId(nint hWnd, out uint lpdwProcessId);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern int GetWindowText(nint hWnd, char[] lpString, int nMaxCount);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumWindows(EnumWindowsProc enumProc, nint lParam);
private delegate bool EnumWindowsProc(nint hWnd, nint lParam);
[ThreadStatic]
private static bool t_found;
[ThreadStatic]
private static uint t_processId;
[ThreadStatic]
private static char[]? t_titleBuffer;
private const int WindowTitleBufferSize = 512;
private const long CacheIntervalMs = 2000L;
private const long StateUnknown = 0L;
private const long StateFalse = 1L;
private const long StateTrue = 2L;
private static readonly EnumWindowsProc s_enumWindowsCallback = EnumWindowsCallback;
private long _packed;
public bool IsExporting()
{
var nowMs = Environment.TickCount64;
var packed = Interlocked.Read(ref _packed);
var state = packed & 3L;
if (state != StateUnknown && (nowMs - (packed >> 2)) < CacheIntervalMs)
return state == StateTrue;
var result = CheckExportWindow();
Interlocked.Exchange(ref _packed, (nowMs << 2) | (result ? StateTrue : StateFalse));
return result;
}
private static bool CheckExportWindow()
{
Debug.Assert(
SynchronizationContext.Current is null || !Thread.CurrentThread.IsThreadPoolThread,
"CheckExportWindow relies on ThreadStatic fields and must not be called from async continuations on thread pool threads.");
try
{
t_processId = (uint)Environment.ProcessId;
t_found = false;
EnumWindows(s_enumWindowsCallback, nint.Zero);
return t_found;
}
catch
{
return false;
}
}
private static bool EnumWindowsCallback(nint hWnd, nint _)
{
GetWindowThreadProcessId(hWnd, out var pid);
if (pid != t_processId)
return true;
t_titleBuffer ??= new char[WindowTitleBufferSize];
var len = GetWindowText(hWnd, t_titleBuffer, t_titleBuffer.Length);
if (len <= 0)
return true;
var title = t_titleBuffer.AsSpan(0, len);
if (title.SequenceEqual(Texts.OutputProgressWindowTitle.AsSpan()) ||
title.SequenceEqual(Texts.VideoExportWindowTitle.AsSpan()))
{
t_found = true;
return false;
}
return true;
}
public void Reset() => Interlocked.Exchange(ref _packed, 0L);
}