OSDN Git Service

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