OSDN Git Service

垂直帰線同期を行う場合、Present 前に Flush するように修正。
[strokestylet/CsWin10Desktop3.git] / StrokeStyleT / StrokeStyleT.cs
1 using System;
2 using System.Collections.Concurrent;
3 using System.Collections.Generic;
4 using System.Diagnostics;
5 using System.IO;
6 using System.IO.Pipes;
7 using System.Linq;
8 using System.Windows.Forms;
9 using Microsoft.VisualBasic.ApplicationServices;
10 using FDK;      // for string 拡張
11
12 namespace SST
13 {
14         class StrokeStyleT : FDK.ApplicationBase
15         {
16                 // グローバルリソース (static) 
17                 public static SST.フォルダ フォルダ => ( StrokeStyleT.bs_フォルダ );
18                 public static FDK.入力.Keyboard キーボード入力 => ( StrokeStyleT.bs_キーボード入力 );
19                 public static FDK.入力.MidiIn MIDI入力 => ( StrokeStyleT.bs_MIDI入力 );
20                 public static FDK.メディア.サウンド.WASAPI排他.ExclusiveDevice Wasapiデバイス => ( StrokeStyleT.bs_Wasapiデバイス );
21                 public static Random 乱数 => ( StrokeStyleT.bs_乱数 );
22                 public static SST.ユーザ.ユーザ管理 ユーザ管理 => ( StrokeStyleT.bs_ユーザ管理 );
23                 public static SST.曲.曲ツリー管理 曲ツリー管理 => ( StrokeStyleT.bs_曲ツリー管理 );
24                 public static SSTFormat.スコア 演奏スコア { get; set; } = null;
25                 public static SST.設定.Config Config => ( StrokeStyleT.bs_Config );
26                 public static bool ビュアーモードである { get; set; } = false;
27                 public static ConcurrentQueue<SST.ステージ.演奏.ビュアーメッセージ> ビュアーメッセージキュー => ( StrokeStyleT.bs_ビュアーメッセージキュー );
28
29                 public static void すべての入力デバイスをポーリングする()
30                 {
31                         // hack: 追加の入力デバイスができたら、ここにポーリングコードを追加すること。
32                         StrokeStyleT.キーボード入力?.ポーリングする();
33                         StrokeStyleT.MIDI入力?.ポーリングする();
34                 }
35
36                 // get only static property の初期化。
37                 static StrokeStyleT()
38                 {
39                         // フォルダ変数を真っ先に登録する。(ほかのメンバのコンストラクタでフォルダ変数を利用できるようにするため。)
40                         StrokeStyleT.bs_フォルダ = new SST.フォルダ();
41                         SST.フォルダ.フォルダ変数を追加する( "Static", StrokeStyleT.フォルダ.StaticFolder );
42                         SST.フォルダ.フォルダ変数を追加する( "AppData", StrokeStyleT.フォルダ.AppDataFolder );
43                         SST.フォルダ.フォルダ変数を追加する( "User", null );
44
45                         // その他の static の生成。
46                         StrokeStyleT.bs_ユーザ管理 = new ユーザ.ユーザ管理();
47                         StrokeStyleT.bs_曲ツリー管理 = new 曲.曲ツリー管理();
48                 }
49
50                 protected override void 初期化する()
51                 {
52                         FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
53
54                         lock( this.スレッド間同期 )
55                         {
56                                 Debug.Assert( null == this.デバイスリソース, "デバイスリソースの作成前であること。" );
57
58                                 this.コマンドライン引数を解析する( Environment.GetCommandLineArgs().Skip( 1 ) );   // 最初の要素は exe ファイル名なのでスキップする。
59
60                                 this.MainForm.Text = $"StrokeStyle<T> {System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()}";
61                                 if( StrokeStyleT.ビュアーモードである )
62                                         this.MainForm.Text += " (Viewer)";
63                                 this.設計画面サイズdpx = new SharpDX.Size2F( 1920, 1080 );     // 設計画面サイズdpx(固定)
64
65                                 #region " コンフィグ を初期化する。"
66                                 //----------------
67                                 FDK.Log.Info( "コンフィグを初期化します。" );
68                                 StrokeStyleT.bs_Config = new 設定.Config();
69                                 StrokeStyleT.bs_Config.ConfigXmlを読み込む();
70                                 //----------------
71                                 #endregion
72                                 #region " コンフィグで指定されたウィンドウサイズに変更する。"
73                                 //----------------
74                                 this.MainForm.ClientSize = new System.Drawing.Size( StrokeStyleT.Config.物理画面サイズpx.Width, StrokeStyleT.Config.物理画面サイズpx.Height );
75                                 //----------------
76                                 #endregion
77
78                                 #region " System.Stopwatch が高解像度タイマを使わないならエラー。"
79                                 //-----------------
80                                 if( false == System.Diagnostics.Stopwatch.IsHighResolution )
81                                         throw new SSTException( "このシステムは、高解像度タイマをサポートしていません。" );
82                                 //-----------------
83                                 #endregion
84                                 #region " MediaFoundation を起動する。"
85                                 //-----------------
86                                 SharpDX.MediaFoundation.MediaManager.Startup();
87                                 //-----------------
88                                 #endregion
89                                 #region " Sleep 精度を上げる。"
90                                 //-----------------
91                                 StrokeStyleT.timeBeginPeriod( 1 );
92                                 //-----------------
93                                 #endregion
94
95                                 #region " ステージを生成する。"
96                                 //----------------
97                                 this.最初のダミーステージ = new ステージ.ステージ();
98                                 this.起動ステージ = new ステージ.起動.起動ステージ();
99                                 this.タイトルステージ = new ステージ.タイトル.タイトルステージ();
100                                 this.ログインステージ = new ステージ.ログイン.ログインステージ();
101                                 this.選曲ステージ = new ステージ.選曲.選曲ステージ();
102                                 this.曲読込ステージ = new ステージ.曲読込.曲読込ステージ();
103                                 this.演奏ステージ = new ステージ.演奏.演奏ステージ();
104                                 this.結果ステージ = new ステージ.結果.結果ステージ();
105                                 //----------------
106                                 #endregion
107                                 #region " ステージのActionを接続する。"
108                                 //----------------
109                                 this.曲読込ステージ.読込曲のファイルパスを取得する = () => ( ( StrokeStyleT.曲ツリー管理.現在選択されているノード as SST.曲.MusicNode )?.sstfファイルパス );
110                                 this.結果ステージ.演奏ステージインスタンスを取得する = () => ( this.演奏ステージ );
111                                 this.結果ステージ.BGMを終了する = () => { this.演奏ステージ.BGMを解放する(); };
112                                 //----------------
113                                 #endregion
114                                 #region " ユーザを初期化する。"
115                                 //-----------------
116                                 FDK.Log.Info( "ユーザ情報を初期化します。" );
117                                 StrokeStyleT.ユーザ管理.UsersXmlを読み込む();
118
119                                 // ユーザ別の初期化。
120                                 foreach( var ユーザ in StrokeStyleT.ユーザ管理.ユーザリスト )
121                                         ユーザ.SourcesXmlを読み込む();
122                                 //-----------------
123                                 #endregion
124                                 #region " WASAPI デバイスを初期化する。"
125                                 //----------------
126                                 StrokeStyleT.bs_Wasapiデバイス = new FDK.メディア.サウンド.WASAPI排他.ExclusiveDevice();
127                                 StrokeStyleT.bs_Wasapiデバイス.初期化する( 15.0f );
128                                 //----------------
129                                 #endregion
130                                 #region " キーボード入力 を初期化する。"
131                                 //-----------------
132                                 FDK.Log.Info( "キーボード入力デバイスを初期化します。" );
133                                 StrokeStyleT.bs_キーボード入力 = new FDK.入力.Keyboard( this.MainForm.Handle );
134                                 //-----------------
135                                 #endregion
136                                 #region " MIDI入力 を初期化する。"
137                                 //-----------------
138                                 FDK.Log.Info( "MIDI入力デバイスを初期化します。" );
139                                 StrokeStyleT.bs_MIDI入力 = new FDK.入力.MidiIn();
140                                 //-----------------
141                                 #endregion
142
143                                 FDK.Log.Info( "最初のダミーステージを開始します。" );
144                                 this.現在のステージ = this.最初のダミーステージ;
145
146                                 //#warning 全画面モード切替えを KeyDown で仮実装。
147                                 this.MainForm.KeyDown += ( target, arg ) => {
148
149                                         // Alt+Enter → 画面モードの切り替え
150                                         if( ( arg.KeyCode == System.Windows.Forms.Keys.Return ) && ( arg.Modifiers == Keys.Alt ) )
151                                         {
152                                                 this.全画面モードとウィンドウモードを切り替える();
153                                                 arg.Handled = true;
154                                         }
155                                 };
156                         }
157
158                         FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
159                 }
160                 protected override void 終了する()
161                 {
162                         FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
163
164                         lock( this.スレッド間同期 )
165                         {
166                                 Debug.Assert( null != this.デバイスリソース, "デバイスリソースが解放される前であること。" );
167
168                                 #region " ステージを終了し、解放する。"
169                                 //----------------
170                                 if( ( null != this.現在のステージ ) && ( this.現在のステージ.活性化している ) )            // 念のため
171                                         this.現在のステージ.非活性化する( this.デバイスリソース );
172
173                                 this.最初のダミーステージ = null;
174                                 this.起動ステージ = null;
175                                 this.タイトルステージ = null;
176                                 this.ログインステージ = null;
177                                 this.選曲ステージ = null;
178                                 this.曲読込ステージ = null;
179                                 this.演奏ステージ = null;
180                                 this.結果ステージ = null;
181                                 //----------------
182                                 #endregion
183                                 #region " MIDI入力 を解放する。"
184                                 //-----------------
185                                 FDK.Log.Info( "MIDI入力デバイスを解放します。" );
186                                 FDK.Utilities.解放する( ref StrokeStyleT.bs_MIDI入力 );
187                                 //-----------------
188                                 #endregion
189                                 #region " キーボード入力 を解放する。"
190                                 //-----------------
191                                 FDK.Log.Info( "キーボード入力デバイスを解放します。" );
192                                 FDK.Utilities.解放する( ref StrokeStyleT.bs_キーボード入力 );
193                                 //-----------------
194                                 #endregion
195                                 #region " WASAPIデバイスを解放する。"
196                                 //----------------
197                                 FDK.Log.Info( "WASAPIデバイスを解放します。" );
198                                 FDK.Utilities.解放する( ref StrokeStyleT.bs_Wasapiデバイス );
199                                 //----------------
200                                 #endregion
201                                 #region " コンフィグを解放する。"
202                                 //----------------
203                                 FDK.Log.Info( "コンフィグを解放します。" );
204                                 StrokeStyleT.bs_Config.ConfigXmlを保存する();
205                                 StrokeStyleT.bs_Config = null;
206                                 //----------------
207                                 #endregion
208                                 #region " MediaFoundation を終了する。"
209                                 //-----------------
210                                 SharpDX.MediaFoundation.MediaManager.Shutdown();
211                                 //-----------------
212                                 #endregion
213                         }
214
215                         FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
216                 }
217                 protected override void シーンを描画する()
218                 {
219                         // このメソッドは、GUIスレッドではなく進行描画スレッドから呼び出されるので注意。(FDK.ApplicationBase.進行描画スレッド処理() を参照。)
220
221                         var swapChain = (SharpDX.DXGI.SwapChain1) null;
222
223                         // 現在のステージを進行描画する。
224                         lock( this.スレッド間同期 )
225                         {
226                                 #region " 描画の準備を行う。"
227                                 //----------------
228                                 var d3dDevice = (SharpDX.Direct3D11.Device) null;
229                                 using( var d3dLock = new FDK.同期.AutoD3DDeviceLock( this.デバイスリソース.DXGIDeviceManager, out d3dDevice ) )
230                                 using( d3dDevice )
231                                 using( var d3dContext = d3dDevice.ImmediateContext )
232                                 {
233                                         // 既定のD3Dレンダーターゲットビューを黒でクリアする。
234                                         d3dContext.ClearRenderTargetView( this.デバイスリソース.D3DRenderTargetView, SharpDX.Color4.Black );
235
236                                         // 深度バッファを 1.0f でクリアする。
237                                         d3dContext.ClearDepthStencilView(
238                                                 this.デバイスリソース.D3DDepthStencilView,
239                                                 SharpDX.Direct3D11.DepthStencilClearFlags.Depth,
240                                                 depth: 1.0f,
241                                                 stencil: 0 );
242                                 }
243                                 //----------------
244                                 #endregion
245
246                                 this.現在のステージ?.進行描画する( this.デバイスリソース );
247
248                                 if( StrokeStyleT.Config.垂直帰線待ちを行う )
249                                 {
250                                         #region " Flush する。"
251                                         //----------------
252                                         d3dDevice = (SharpDX.Direct3D11.Device) null;
253                                         using( var d3dLock = new FDK.同期.AutoD3DDeviceLock( this.デバイスリソース.DXGIDeviceManager, out d3dDevice ) )
254                                         using( d3dDevice )
255                                         using( var d3dContext = d3dDevice.ImmediateContext )
256                                         {
257                                                 // We recommend that you use Flush when the CPU waits for an arbitrary amount of time
258                                                 // ( such as when you call the Sleep function). 
259                                                 // https://msdn.microsoft.com/ja-jp/library/windows/desktop/ff476425(v=vs.85).aspx
260                                                 d3dContext.Flush();
261                                         }
262                                         //----------------
263                                         #endregion
264                                 }
265
266                                 swapChain = this.デバイスリソース.SwapChain1;
267                         }
268
269                         // スワップチェーンを表示する。垂直帰線待ちなどで時間がかかるので、この部分はスレッド排他領域の外に配置すること。
270                         swapChain.Present(
271                                 ( StrokeStyleT.Config.垂直帰線待ちを行う ) ? 1 : 0,
272                                 SharpDX.DXGI.PresentFlags.None );
273
274                         // ステージの状態をチェックし、必要あれば遷移またはアプリを終了する。
275                         bool アプリを終了せよ = false;
276                         lock( this.スレッド間同期)
277                         {
278                                 if( null != this.現在のステージ )
279                                 {
280                                         switch( this.現在のステージ.GetType().Name )
281                                         {
282                                                 case nameof( ステージ.ステージ ):
283                                                         #region " ビュアーモード → AutoPlayerでログインして曲読込ステージへ。"
284                                                         //----------------
285                                                         if( StrokeStyleT.ビュアーモードである )
286                                                         {
287                                                                 FDK.Log.Info( "ビュアーモード: AutoPlayer ユーザでログインします。" );
288                                                                 this.ログインする( Properties.Resources.AUTOPLAYER );
289
290                                                                 this.曲読込ステージ.読込曲のファイルパスを取得する = () => ( null );      // 今は null 。あとでメッセージキューを見る。
291                                                                 this.曲読込ステージ.活性化する( this.デバイスリソース );
292                                                                 this.現在のステージ = this.曲読込ステージ;
293                                                         }
294                                                         //----------------
295                                                         #endregion
296                                                         #region " 通常モード → 起動ステージへ。"
297                                                         //----------------
298                                                         else
299                                                         {
300                                                                 this.起動ステージ.活性化する( this.デバイスリソース );
301                                                                 this.現在のステージ = this.起動ステージ;
302                                                         }
303                                                         //----------------
304                                                         #endregion
305                                                         break;
306
307                                                 case nameof( ステージ.起動.起動ステージ ):
308                                                         #region " 終了 → タイトルステージへ。"
309                                                         //---------------
310                                                         if( this.起動ステージ.現在のフェーズ == ステージ.起動.起動ステージ.フェーズ.終了 )
311                                                         {
312                                                                 this.現在のステージ.非活性化する( this.デバイスリソース );
313                                                                 this.現在のステージ = this.タイトルステージ;
314                                                                 this.現在のステージ.活性化する( this.デバイスリソース );
315                                                         }
316                                                         //---------------
317                                                         #endregion
318                                                         break;
319
320                                                 case nameof( ステージ.タイトル.タイトルステージ ):
321                                                         #region " 確定 → ログインステージへ。"
322                                                         //---------------
323                                                         if( this.タイトルステージ.現在のフェーズ == ステージ.タイトル.タイトルステージ.フェーズ.確定 )
324                                                         {
325                                                                 this.現在のステージ.非活性化する( this.デバイスリソース );
326                                                                 this.現在のステージ = this.ログインステージ;
327                                                                 this.現在のステージ.活性化する( this.デバイスリソース );
328                                                         }
329                                                         //---------------
330                                                         #endregion
331                                                         #region " キャンセル → アプリを終了する。"
332                                                         //---------------
333                                                         else if( this.タイトルステージ.現在のフェーズ == ステージ.タイトル.タイトルステージ.フェーズ.キャンセル )
334                                                         {
335                                                                 this.現在のステージ.非活性化する( this.デバイスリソース );
336                                                                 this.現在のステージ = null;
337                                                                 アプリを終了せよ = true;
338                                                         }
339                                                         //---------------
340                                                         #endregion
341                                                         break;
342
343                                                 case nameof( ステージ.ログイン.ログインステージ ):
344                                                         #region " 確定 → ログイン処理を行って、選曲ステージへ。"
345                                                         //---------------
346                                                         if( this.ログインステージ.現在のフェーズ == ステージ.ログイン.ログインステージ.フェーズ.確定 )
347                                                         {
348                                                                 var user = StrokeStyleT.ユーザ管理.現在選択されているユーザ;
349
350                                                                 if( null != user )
351                                                                 {
352                                                                         foreach( var path in user.曲の検索元フォルダパスのリスト )
353                                                                                 SST.曲.曲ツリー管理.フォルダから曲を再帰的に検索して子ノードリストに追加する( user.曲ツリーのルートノード, path );
354
355                                                                         StrokeStyleT.曲ツリー管理.現在の管理対象ツリー = StrokeStyleT.ユーザ管理.現在選択されているユーザ.曲ツリーのルートノード;
356                                                                 }
357
358                                                                 this.現在のステージ.非活性化する( this.デバイスリソース );
359                                                                 this.現在のステージ = this.選曲ステージ;
360                                                                 this.現在のステージ.活性化する( this.デバイスリソース );
361                                                         }
362                                                         //---------------
363                                                         #endregion
364                                                         #region " キャンセル → タイトルステージへ。"
365                                                         //---------------
366                                                         else if( this.ログインステージ.現在のフェーズ == ステージ.ログイン.ログインステージ.フェーズ.キャンセル )
367                                                         {
368                                                                 this.現在のステージ.非活性化する( this.デバイスリソース );
369                                                                 this.現在のステージ = this.タイトルステージ;
370                                                                 this.現在のステージ.活性化する( this.デバイスリソース );
371                                                         }
372                                                         //---------------
373                                                         #endregion
374                                                         break;
375
376                                                 case nameof( ステージ.選曲.選曲ステージ ):
377                                                         #region " 曲確定 → 曲読込ステージへ。"
378                                                         //---------------
379                                                         if( this.選曲ステージ.現在のフェーズ == ステージ.選曲.選曲ステージ.フェーズ.曲確定 )
380                                                         {
381                                                                 // 曲ノードが選択されていることを確認。
382                                                                 Trace.Assert( null != StrokeStyleT.曲ツリー管理.現在選択されているノード, "[バグあり] 選択曲が null です。" );
383                                                                 this.現在のステージ.非活性化する( this.デバイスリソース );
384                                                                 this.現在のステージ = this.曲読込ステージ;
385                                                                 this.現在のステージ.活性化する( this.デバイスリソース );
386                                                         }
387                                                         //---------------
388                                                         #endregion
389                                                         #region " キャンセル → アプリを終了する。"
390                                                         //---------------
391                                                         else if( this.選曲ステージ.現在のフェーズ == ステージ.選曲.選曲ステージ.フェーズ.キャンセル )
392                                                         {
393                                                                 this.現在のステージ.非活性化する( this.デバイスリソース );
394                                                                 this.現在のステージ = null;
395                                                                 アプリを終了せよ = true;
396                                                         }
397                                                         //---------------
398                                                         #endregion
399                                                         break;
400
401                                                 case nameof( ステージ.曲読込.曲読込ステージ ):
402                                                         #region " 終了 → 演奏ステージへ。"
403                                                         //--------------------
404                                                         if( this.曲読込ステージ.現在のフェーズ == ステージ.曲読込.曲読込ステージ.フェーズ.終了 )
405                                                         {
406                                                                 this.現在のステージ.非活性化する( this.デバイスリソース );
407
408                                                                 if( StrokeStyleT.ビュアーモードである )
409                                                                         this.デバイス情報を出力する();
410
411                                                                 this.現在のステージ = this.演奏ステージ;
412                                                                 this.現在のステージ.活性化する( this.デバイスリソース );
413                                                         }
414                                                         //--------------------
415                                                         #endregion
416                                                         break;
417
418                                                 case nameof( ステージ.演奏.演奏ステージ ):
419                                                         #region " 演奏終了 → 結果ステージへ。"
420                                                         //--------------------
421                                                         if( this.演奏ステージ.現在のフェーズ.Value == ステージ.演奏.演奏ステージ.フェーズ.クリアor失敗 )
422                                                         {
423                                                                 this.現在のステージ.非活性化する( this.デバイスリソース );
424                                                                 this.現在のステージ = this.結果ステージ;
425                                                                 this.現在のステージ.活性化する( this.デバイスリソース );
426                                                         }
427                                                         //--------------------
428                                                         #endregion
429                                                         #region " キャンセル → 選曲ステージへ。"
430                                                         //--------------------
431                                                         if( this.演奏ステージ.現在のフェーズ.Value == ステージ.演奏.演奏ステージ.フェーズ.キャンセル )
432                                                         {
433                                                                 this.現在のステージ.非活性化する( this.デバイスリソース );
434                                                                 this.現在のステージ = this.選曲ステージ;
435                                                                 this.現在のステージ.活性化する( this.デバイスリソース );
436                                                         }
437                                                         //--------------------
438                                                         #endregion
439                                                         #region " ビュアーメッセージ受信 → 曲読込ステージへ。"
440                                                         //--------------------
441                                                         if( this.演奏ステージ.現在のフェーズ.Value == ステージ.演奏.演奏ステージ.フェーズ.ビュアーメッセージ待機中 )
442                                                         {
443                                                                 var msg = (SST.ステージ.演奏.ビュアーメッセージ) null;
444                                                                 if( ( 0 < StrokeStyleT.ビュアーメッセージキュー.Count ) &&
445                                                                         ( StrokeStyleT.ビュアーメッセージキュー.TryDequeue( out msg ) ) )
446                                                                 {
447                                                                         FDK.Log.Info( "ビュアーメッセージを受信しました。" );
448                                                                         this.曲読込ステージ.読込曲のファイルパスを取得する = () => { return msg.曲ファイルパス; };
449
450                                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
451                                                                         this.演奏ステージ.BGMを解放する();
452                                                                         this.現在のステージ = this.曲読込ステージ;
453                                                                         this.現在のステージ.活性化する( this.デバイスリソース );
454                                                                 }
455                                                         }
456                                                         //--------------------
457                                                         #endregion
458                                                         break;
459
460                                                 case nameof( ステージ.結果.結果ステージ ):
461                                                         #region " 終了 → 選曲ステージへ。"
462                                                         //--------------------
463                                                         if( this.結果ステージ.現在のフェーズ == ステージ.結果.結果ステージ.フェーズ.終了 )
464                                                         {
465                                                                 this.現在のステージ.非活性化する( this.デバイスリソース );
466                                                                 this.現在のステージ = this.選曲ステージ;
467                                                                 this.現在のステージ.活性化する( this.デバイスリソース );
468                                                         }
469                                                         //--------------------
470                                                         #endregion
471                                                         break;
472                                         }
473                                 }
474                         }
475
476                         if( アプリを終了せよ )
477                         {
478                                 // GUIスレッド上で、ウィンドウを閉じる。
479                                 this.MainForm.BeginInvoke( new Action( () => { this.MainForm.Close(); } ) );
480                         }
481                 }
482                 protected override void デバイス依存リソースを解放する()
483                 {
484                         FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
485
486                         lock( this.スレッド間同期 )
487                         {
488                                 Debug.Assert( null != this.デバイスリソース );  // 解放前であること。
489                                 this.現在のステージ?.デバイス依存リソースを解放する( this.デバイスリソース );
490                         }
491
492                         FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
493                 }
494                 protected override void デバイス依存リソースを再構築する()
495                 {
496                         FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
497
498                         lock( this.スレッド間同期 )
499                         {
500                                 Debug.Assert( null != this.デバイスリソース );  // 再生成済みであること。
501                                 this.現在のステージ?.デバイス依存リソースを作成する( this.デバイスリソース );
502                         }
503
504                         FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
505                 }
506                 /// <summary>
507                 /// アプリが二重起動されたときに発生するイベント。
508                 /// </summary>
509                 /// <remarks>
510                 /// 後続のインスタンスは起動せず、既存のインスタンスに対してこのイベントが発生する。
511                 /// eventArg.CommandLine で、後続のインスタンスのコマンドライン引数を確認することができる。
512                 /// </remarks>
513                 protected override void OnStartupNextInstance( StartupNextInstanceEventArgs eventArgs )
514                 {
515                         FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
516
517                         lock( this.スレッド間同期 )
518                         {
519                                 if( StrokeStyleT.ビュアーモードである )
520                                 {
521                                         this.コマンドライン引数を解析する( eventArgs.CommandLine );
522                                 }
523                                 else
524                                 {
525                                         FDK.Log.ERROR( "現在、ビュアーモードではありません。" );
526                                 }
527                         }
528
529                         FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
530                 }
531
532                 // 各ステージの、唯一のインスタンス。Config 生成後に生成するので、readonlyにはしない。
533                 protected SST.ステージ.ステージ 最初のダミーステージ = null;
534                 protected SST.ステージ.起動.起動ステージ 起動ステージ = null;
535                 protected SST.ステージ.タイトル.タイトルステージ タイトルステージ = null;
536                 protected SST.ステージ.ログイン.ログインステージ ログインステージ = null;
537                 protected SST.ステージ.選曲.選曲ステージ 選曲ステージ = null;
538                 protected SST.ステージ.曲読込.曲読込ステージ 曲読込ステージ = null;
539                 protected SST.ステージ.演奏.演奏ステージ 演奏ステージ = null;
540                 protected SST.ステージ.結果.結果ステージ 結果ステージ = null;
541
542                 private SST.ステージ.ステージ 現在のステージ = null;
543
544                 #region " バックストア。"
545                 //----------------
546                 private static SST.フォルダ bs_フォルダ = null;
547                 private static FDK.入力.Keyboard bs_キーボード入力 = null;
548                 private static FDK.入力.MidiIn bs_MIDI入力 = null;
549                 private static FDK.メディア.サウンド.WASAPI排他.ExclusiveDevice bs_Wasapiデバイス = null;
550                 private static readonly Random bs_乱数 = new Random( DateTime.Now.Millisecond );
551                 private static SST.ユーザ.ユーザ管理 bs_ユーザ管理 = null;
552                 private static SST.曲.曲ツリー管理 bs_曲ツリー管理 = null;
553                 private static SST.設定.Config bs_Config = null;
554                 private static readonly ConcurrentQueue<SST.ステージ.演奏.ビュアーメッセージ> bs_ビュアーメッセージキュー = new ConcurrentQueue<ステージ.演奏.ビュアーメッセージ>();
555                 //----------------
556                 #endregion
557
558                 /// <summary>
559                 /// コマンドライン引数を解析して、ビュアーモードの設定があればそれを返す。
560                 /// </summary>
561                 /// <param name="args">コマンドライン引数の列挙。exeファイル名は含まない。</param>
562                 /// <return>引数を反映したビュアーモード変数。</return>
563                 private void コマンドライン引数を解析する( IEnumerable<string> args )
564                 {
565                         FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
566
567                         try
568                         {
569                                 if( 0 == args.Count() )
570                                 {
571                                         FDK.Log.Info( "引数は指定されていません。" );    // ビュアーモードではない。
572                                         return;
573                                 }
574
575                                 StrokeStyleT.ビュアーモードである = true;
576
577                                 try
578                                 {
579                                         // 新しいメッセージを生成する。
580                                         var msg = new ステージ.演奏.ビュアーメッセージ() {
581                                                 種別 = ステージ.演奏.ビュアーメッセージ.E種別.演奏開始,          // 規定値は「演奏開始」
582                                                 曲ファイルパス = null,
583                                                 演奏開始小節番号 = 0,
584                                                 ドラム音を発声する = false,    // 規定値は false
585                                         };
586
587                                         // オプションを定義する。
588                                         var optionSet = new Mono.Options.OptionSet() {
589                                                 "Usage: StrokeStyleT [File] [OPTIONS]+",
590                                                 "  File\t\t\t\tビュアーモードで表示する曲ファイル名です。",
591                                                 { "p=|part=", "ビュアーモードで起動し、指定された小節番号から演奏を開始します。小節番号を省略すると、先頭から再生します。", (int v) => { msg.演奏開始小節番号 = v; }  },
592                                                 { "s|stop", "ビュアーモードで演奏中であれば、演奏を停止します。", v => { if ( v != null ) { msg.種別 = ステージ.演奏.ビュアーメッセージ.E種別.演奏停止; } } },
593                                                 { "d|drums", "ビュアーモードで、チップヒット時に内蔵のドラム音を再生します。", v => { if( v != null ) { msg.ドラム音を発声する = true; } } },
594                                         };
595
596                                         // オプションを解析する。
597                                         List<string> ファイルパスs = optionSet.Parse( args );
598
599                                         // 解析結果。
600                                         if( msg.種別 == ステージ.演奏.ビュアーメッセージ.E種別.演奏停止 )
601                                         {
602                                                 // (A) 演奏停止(曲ファイルパスは省略可。)
603                                                 FDK.Log.Info( "ビュアーメッセージ: 演奏停止" );
604                                         }
605                                         else
606                                         {
607                                                 // (B) 演奏開始(曲ファイルパスは必須。)
608                                                 if( 0 < ファイルパスs.Count )
609                                                 {
610                                                         if( File.Exists( ファイルパスs[ 0 ] ) )
611                                                         {
612                                                                 msg.曲ファイルパス = ファイルパスs[ 0 ];
613                                                         }
614                                                         else
615                                                         {
616                                                                 msg.曲ファイルパス = null;    // ファイルが存在しなかったら null 。
617                                                                 throw new Mono.Options.OptionException( $"ファイルが存在しません。[{FDK.フォルダ.絶対パスをフォルダ変数付き絶対パスに変換して返す( ファイルパスs[ 0 ] )}]", "File" );
618                                                         }
619                                                 }
620                                                 else
621                                                 {
622                                                         throw new Mono.Options.OptionException( "ファイルの指定がありません。", "File" );
623                                                 }
624
625                                                 FDK.Log.Info( "ビュアーメッセージ: 演奏開始" );
626                                                 FDK.Log.Info( $"曲ファイルパス: {msg.曲ファイルパス}" );
627                                                 FDK.Log.Info( $"開始小節番号: {msg.演奏開始小節番号}" );
628                                                 FDK.Log.Info( $"ドラム音: {msg.ドラム音を発声する}" );
629                                         }
630
631                                         // キューへ格納。
632                                         StrokeStyleT.ビュアーメッセージキュー.Enqueue( msg );
633                                         FDK.Log.Info( "ビュアーメッセージを送信しました。" );
634                                 }
635                                 catch( Mono.Options.OptionException e )
636                                 {
637                                         FDK.Log.ERROR( $"{e.Message}" );
638                                 }
639                         }
640                         finally
641                         {
642                                 FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
643                         }
644                 }
645
646                 private void ログインする( string ユーザ名 )
647                 {
648                         StrokeStyleT.ユーザ管理.ユーザを選択する( ユーザ名 );
649                         FDK.Log.Info( $"ユーザが選択されました。[{StrokeStyleT.ユーザ管理.現在選択されているユーザ.名前}]" );
650                 }
651                 private void デバイス情報を出力する()
652                 {
653                         using( var stream = new NamedPipeClientStream( "SSTFEditor Viewer Device Information" ) )
654                         {
655                                 try
656                                 {
657                                         stream.Connect( 1000 );
658
659                                         using( var writer = new StreamWriter( stream ) )
660                                         {
661                                                 writer.WriteLine( $"SoundDevice.Delay={StrokeStyleT.Wasapiデバイス.遅延ms.ToString()}" );
662                                                 FDK.Log.Info( "デバイス情報を出力しました。" );
663                                         }
664                                 }
665                                 catch( TimeoutException )
666                                 {
667                                         FDK.Log.WARNING( "SSTFEditor ビュアー用パイプへの接続がタイムアウトしました。SSTFEditor が起動していない可能性があります。" );
668                                 }
669                         }
670                 }
671
672                 #region " Win32 API "
673                 //-----------------
674                 [ System.Runtime.InteropServices.DllImport( "winmm.dll", EntryPoint = "timeBeginPeriod" )]
675                 private static extern uint timeBeginPeriod( uint uMilliseconds );
676
677                 [System.Runtime.InteropServices.DllImport( "winmm.dll", EntryPoint = "timeEndPeriod" )]
678                 private static extern uint timeEndPeriod( uint uMilliseconds );
679                 //-----------------
680                 #endregion
681         }
682 }