OSDN Git Service

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