OSDN Git Service

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