OSDN Git Service

ビュアーとエディター間の通信を、標準出力から名前付きパイプに変更。
authorくまかみ工房 <kumakamikoubou@gmail.com>
Sat, 22 Oct 2016 09:19:31 +0000 (18:19 +0900)
committerくまかみ工房 <kumakamikoubou@gmail.com>
Sat, 22 Oct 2016 09:19:31 +0000 (18:19 +0900)
SSTFEditor/メインフォーム.cs
StrokeStyleT/StrokeStyleT.cs

index 109391d..39360c5 100644 (file)
@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.Diagnostics;
 using System.Drawing;
 using System.IO;
+using System.IO.Pipes;
 using System.Linq;
 using System.Reflection;
 using System.Text.RegularExpressions;
@@ -765,31 +766,36 @@ namespace SSTFEditor
                                this.最後にプレイヤーに渡した一時ファイル名, 
                                一時ファイルである: true );    // 一時ファイルなので、「最近使ったファイル一覧」には残さない。
 
-                       // プレイヤーを起動する。
-                       string 仮想ドラムオプション = ( 仮想ドラムを使う ) ? @" -d" : @"";
-                       //Process.Start(        普通に起動するバージョン。
-                       //      this.Config.ViewerPath,
-                       //      $"\"{this.最後にプレイヤーに渡した一時ファイル名}\" -p {小節番号.ToString()}{仮想ドラムオプション}" );
-                       var viewerProcess = new Process();
-                       viewerProcess.StartInfo.RedirectStandardOutput = true;  // 標準出力をリダイレクトする。
-                       viewerProcess.StartInfo.UseShellExecute = false;        // リダイレクトするなら false が必須。
-                       viewerProcess.StartInfo.FileName = this.Config.ViewerPath;
-                       viewerProcess.StartInfo.Arguments = $"\"{this.最後にプレイヤーに渡した一時ファイル名}\" -p {小節番号.ToString()}{仮想ドラムオプション}";
-                       viewerProcess.Start();
-
-                       // プレイヤーの標準出力から、デバイス情報を得る。
-                       var sor = viewerProcess.StandardOutput;
-                       string line = null;
-                       while( ( line = sor.ReadLine() ) != null )
-                       {
-                               var match = Regex.Match( line, @"^SoundDevice.Delay=(\d+(\.\d+)?)$" );  // 負数やべき乗は非対応。
-                               if( match.Success )
+                       using( var stream = new NamedPipeServerStream( "SSTFEditor Viewer Device Information" ) )
+                       {
+                               // ビュアーを起動する。
+                               string 仮想ドラムオプション = ( 仮想ドラムを使う ) ? @" -d" : @"";
+                               try
                                {
-                                       var strValue = match.Groups[ 1 ].Value;
-                                       float value = 0f;
-                                       if( float.TryParse( strValue, out value ) )             // 正しく float に変換できる文字列なら、
-                                               this.textBoxサウンド遅延ms.Text = strValue;       // GUI へセットする。
-                                       break;
+                                       Process.Start(
+                                               this.Config.ViewerPath,
+                                               $"\"{this.最後にプレイヤーに渡した一時ファイル名}\" -p {小節番号.ToString()}{仮想ドラムオプション}" );
+                               }
+                               catch
+                               {
+                                       return; // 起動に失敗。
+                               }
+
+                               // デバイス情報を得る。
+                               stream.WaitForConnection();
+
+                               using( var reader = new StreamReader( stream ) )
+                               {
+                                       var msg = reader.ReadLine();
+
+                                       var match = Regex.Match( msg, @"^SoundDevice.Delay=(\d+(\.\d+)?)$" );  // 負数やべき乗には非対応。
+                                       if( match.Success )
+                                       {
+                                               var strValue = match.Groups[ 1 ].Value;
+                                               float value = 0f;
+                                               if( float.TryParse( strValue, out value ) )             // 正しく float に変換できる文字列なら、
+                                                       this.textBoxサウンド遅延ms.Text = strValue;       // GUI へセットする。
+                                       }
                                }
                        }
                }
index afd2ef2..9ae3e54 100644 (file)
@@ -3,6 +3,7 @@ using System.Collections.Concurrent;
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.IO;
+using System.IO.Pipes;
 using System.Linq;
 using System.Windows.Forms;
 using Microsoft.VisualBasic.ApplicationServices;
@@ -112,12 +113,6 @@ namespace SST
                                //----------------
                                StrokeStyleT.bs_Wasapiデバイス = new FDK.メディア.サウンド.WASAPI排他.ExclusiveDevice();
                                StrokeStyleT.bs_Wasapiデバイス.初期化する( 15.0f );
-
-                               if( StrokeStyleT.ビュアーモードである )
-                               {
-                                       // デバイス情報を標準出力へ出力する。
-                                       Console.WriteLine( $"SoundDevice.Delay={StrokeStyleT.bs_Wasapiデバイス.遅延ms}" );
-                               }
                                //----------------
                                #endregion
                                #region " キーボード入力 を初期化する。"
@@ -374,6 +369,10 @@ namespace SST
                                                        if( this.曲読込ステージ.現在のフェーズ == ステージ.曲読込.曲読込ステージ.フェーズ.終了 )
                                                        {
                                                                this.現在のステージ.非活性化する( this.デバイスリソース );
+
+                                                               if( StrokeStyleT.ビュアーモードである )
+                                                                       this.デバイス情報を出力する();
+
                                                                this.現在のステージ = this.演奏ステージ;
                                                                this.現在のステージ.活性化する( this.デバイスリソース );
                                                        }
@@ -614,6 +613,26 @@ namespace SST
                        StrokeStyleT.ユーザ管理.ユーザを選択する( ユーザ名 );
                        FDK.Log.Info( $"ユーザが選択されました。[{StrokeStyleT.ユーザ管理.現在選択されているユーザ.名前}]" );
                }
+               private void デバイス情報を出力する()
+               {
+                       using( var stream = new NamedPipeClientStream( "SSTFEditor Viewer Device Information" ) )
+                       {
+                               try
+                               {
+                                       stream.Connect( 1000 );
+
+                                       using( var writer = new StreamWriter( stream ) )
+                                       {
+                                               writer.WriteLine( $"SoundDevice.Delay={StrokeStyleT.Wasapiデバイス.遅延ms.ToString()}" );
+                                               FDK.Log.Info( "デバイス情報を出力しました。" );
+                                       }
+                               }
+                               catch( TimeoutException )
+                               {
+                                       FDK.Log.WARNING( "SSTFEditor ビュアー用パイプへの接続がタイムアウトしました。SSTFEditor が起動していない可能性があります。" );
+                               }
+                       }
+               }
 
                #region " Win32 API "
                //-----------------