OSDN Git Service

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