OSDN Git Service

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