Fix WinRT async interop in Windows TTS

This commit is contained in:
2026-05-01 21:06:31 +09:00
parent 96252528b4
commit 53777be675

View File

@@ -25,16 +25,23 @@ function windowsMediaPreamble(): string {
"Add-Type -AssemblyName System.Runtime.WindowsRuntime;", "Add-Type -AssemblyName System.Runtime.WindowsRuntime;",
"$null = [Windows.Media.SpeechSynthesis.SpeechSynthesizer, Windows.Media.SpeechSynthesis, ContentType=WindowsRuntime];", "$null = [Windows.Media.SpeechSynthesis.SpeechSynthesizer, Windows.Media.SpeechSynthesis, ContentType=WindowsRuntime];",
"$null = [Windows.Storage.Streams.DataReader, Windows.Storage.Streams, ContentType=WindowsRuntime];", "$null = [Windows.Storage.Streams.DataReader, Windows.Storage.Streams, ContentType=WindowsRuntime];",
"$null = [Windows.Foundation.AsyncStatus, Windows.Foundation, ContentType=WindowsRuntime];",
"function Await-WinRt($operation) {", "function Await-WinRt($operation) {",
" while ($operation.Status -eq [Windows.Foundation.AsyncStatus]::Started) { Start-Sleep -Milliseconds 10 }", " $interfaceType = $operation.GetType().GetInterfaces() | Where-Object {",
" if ($operation.Status -eq [Windows.Foundation.AsyncStatus]::Completed) { return $operation.GetResults() }", " $_.IsGenericType -and $_.GetGenericTypeDefinition().FullName -eq 'Windows.Foundation.IAsyncOperation`1'",
" if ($operation.Status -eq [Windows.Foundation.AsyncStatus]::Canceled) { throw 'WinRT async operation canceled.' }", " } | Select-Object -First 1;",
" if ($operation.Status -eq [Windows.Foundation.AsyncStatus]::Error) {", " if (-not $interfaceType) { throw 'IAsyncOperation<T> 인터페이스를 찾지 못했습니다.' }",
" if ($operation.ErrorCode) { throw $operation.ErrorCode }", " $resultType = $interfaceType.GetGenericArguments()[0];",
" throw 'WinRT async operation failed.'", " $method = [System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object {",
" }", " $_.Name -eq 'AsTask' -and",
" throw ('Unexpected WinRT async status: ' + [string]$operation.Status)", " $_.IsGenericMethodDefinition -and",
" $_.GetGenericArguments().Count -eq 1 -and",
" $_.GetParameters().Count -eq 1 -and",
" $_.GetParameters()[0].ParameterType.IsGenericType -and",
" $_.GetParameters()[0].ParameterType.GetGenericTypeDefinition().FullName -eq 'Windows.Foundation.IAsyncOperation`1'",
" } | Select-Object -First 1;",
" if (-not $method) { throw 'System.WindowsRuntimeSystemExtensions.AsTask(IAsyncOperation<T>) 를 찾지 못했습니다.' }",
" $task = $method.MakeGenericMethod(@($resultType)).Invoke($null, @($operation));",
" return $task.GetAwaiter().GetResult();",
"}", "}",
].join(" "); ].join(" ");
} }