2 using System.Collections.Concurrent;
3 using System.Collections.Generic;
4 using System.Diagnostics;
7 using System.Windows.Forms;
8 using Microsoft.VisualBasic.ApplicationServices;
9 using FDK; // for string 拡張
13 class StrokeStyleT : FDK.ApplicationBase
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_ビュアーメッセージキュー );
28 public static void すべての入力デバイスをポーリングする()
30 // hack: 追加の入力デバイスができたら、ここにポーリングコードを追加すること。
31 StrokeStyleT.キーボード入力?.ポーリングする();
32 StrokeStyleT.MIDI入力?.ポーリングする();
35 // get only static property の初期化。
38 // フォルダ変数を真っ先に登録する。(ほかのメンバのコンストラクタでフォルダ変数を利用できるようにするため。)
39 StrokeStyleT.bs_フォルダ = new SST.フォルダ();
40 SST.フォルダ.フォルダ変数を追加する( "Static", StrokeStyleT.フォルダ.StaticFolder );
41 SST.フォルダ.フォルダ変数を追加する( "AppData", StrokeStyleT.フォルダ.AppDataFolder );
42 SST.フォルダ.フォルダ変数を追加する( "User", null );
45 StrokeStyleT.bs_ユーザ管理 = new ユーザ.ユーザ管理();
46 StrokeStyleT.bs_曲ツリー管理 = new 曲.曲ツリー管理();
49 protected override void 初期化する()
51 FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
55 Debug.Assert( null == this.デバイスリソース, "デバイスリソースの作成前であること。" );
57 this.コマンドライン引数を解析する( Environment.GetCommandLineArgs().Skip( 1 ) ); // 最初の要素は exe ファイル名なのでスキップする。
59 this.MainForm.Text = $"StrokeStyle<T> {System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()}";
60 this.設計画面サイズdpx = new SharpDX.Size2F( 1920, 1080 ); // 設計画面サイズdpx(固定)
62 #region " コンフィグ を初期化する。"
64 FDK.Log.Info( "コンフィグを初期化します。" );
65 StrokeStyleT.bs_Config = new 設定.Config();
66 StrokeStyleT.bs_Config.ConfigXmlを読み込む();
69 #region " コンフィグで指定されたウィンドウサイズに変更する。"
71 this.MainForm.ClientSize = new System.Drawing.Size( StrokeStyleT.Config.物理画面サイズpx.Width, StrokeStyleT.Config.物理画面サイズpx.Height );
75 #region " System.Stopwatch が高解像度タイマを使わないならエラー。"
77 if( false == System.Diagnostics.Stopwatch.IsHighResolution )
78 throw new SSTException( "このシステムは、高解像度タイマをサポートしていません。" );
81 #region " MediaFoundation を起動する。"
83 SharpDX.MediaFoundation.MediaManager.Startup();
86 #region " Sleep 精度を上げる。"
88 StrokeStyleT.timeBeginPeriod( 1 );
92 #region " ステージのActionを接続する。"
94 this.曲読込ステージ.読込曲のファイルパスを取得する = () => ( ( StrokeStyleT.曲ツリー管理.現在選択されているノード as SST.曲.MusicNode )?.sstfファイルパス );
95 this.結果ステージ.演奏ステージインスタンスを取得する = () => ( this.演奏ステージ );
96 this.結果ステージ.BGMを終了する = () => { this.演奏ステージ.BGMを解放する(); };
101 FDK.Log.Info( "ユーザ情報を初期化します。" );
102 StrokeStyleT.ユーザ管理.UsersXmlを読み込む();
105 foreach( var ユーザ in StrokeStyleT.ユーザ管理.ユーザリスト )
106 ユーザ.SourcesXmlを読み込む();
109 #region " WASAPI デバイスを初期化する。"
111 StrokeStyleT.bs_Wasapiデバイス = new FDK.メディア.サウンド.WASAPI排他.ExclusiveDevice();
112 StrokeStyleT.bs_Wasapiデバイス.初期化する( 15.0f );
115 #region " キーボード入力 を初期化する。"
117 FDK.Log.Info( "キーボード入力デバイスを初期化します。" );
118 StrokeStyleT.bs_キーボード入力 = new FDK.入力.Keyboard( this.MainForm.Handle );
121 #region " MIDI入力 を初期化する。"
123 FDK.Log.Info( "MIDI入力デバイスを初期化します。" );
124 StrokeStyleT.bs_MIDI入力 = new FDK.入力.MidiIn();
128 this.現在のステージ = this.最初のダミーステージ;
130 //#warning 全画面モード切替えを KeyDown で仮実装。
131 this.MainForm.KeyDown += ( target, arg ) => {
133 // Alt+Enter → 画面モードの切り替え
134 //if( ( arg.KeyCode == System.Windows.Forms.Keys.Return ) && ( arg.Modifiers == Keys.Alt ) )
136 // this.全画面モードとウィンドウモードを切り替える();
137 // arg.Handled = true;
142 FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
144 protected override void 終了する()
146 FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
150 Debug.Assert( null != this.デバイスリソース, "デバイスリソースが解放される前であること。" );
152 #region " ステージを終了し、解放する。"
154 if( ( null != this.現在のステージ ) && ( this.現在のステージ.活性化している ) ) // 念のため
155 this.現在のステージ.非活性化する( this.デバイスリソース );
157 this.最初のダミーステージ = null;
159 this.タイトルステージ = null;
160 this.ログインステージ = null;
167 #region " MIDI入力 を解放する。"
169 FDK.Log.Info( "MIDI入力デバイスを解放します。" );
170 FDK.Utilities.解放する( ref StrokeStyleT.bs_MIDI入力 );
173 #region " キーボード入力 を解放する。"
175 FDK.Log.Info( "キーボード入力デバイスを解放します。" );
176 FDK.Utilities.解放する( ref StrokeStyleT.bs_キーボード入力 );
179 #region " WASAPIデバイスを解放する。"
181 FDK.Log.Info( "WASAPIデバイスを解放します。" );
182 FDK.Utilities.解放する( ref StrokeStyleT.bs_Wasapiデバイス );
185 #region " コンフィグを解放する。"
187 FDK.Log.Info( "コンフィグを解放します。" );
188 StrokeStyleT.bs_Config = null;
191 #region " MediaFoundation を終了する。"
193 SharpDX.MediaFoundation.MediaManager.Shutdown();
198 FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
200 protected override void シーンを描画する()
202 // このメソッドは、GUIスレッドではなく進行描画スレッドから呼び出されるので注意。(FDK.ApplicationBase.進行描画スレッド処理() を参照。)
209 var d3dDevice = (SharpDX.Direct3D11.Device) null;
210 using( var d3dLock = new FDK.同期.AutoD3DDeviceLock( this.デバイスリソース.DXGIDeviceManager, out d3dDevice ) )
212 using( var d3dContext = d3dDevice.ImmediateContext )
214 // 既定のD3Dレンダーターゲットビューを黒でクリアする。
215 d3dContext.ClearRenderTargetView( this.デバイスリソース.D3DRenderTargetView, SharpDX.Color4.Black );
217 // 深度バッファを 1.0f でクリアする。
218 d3dContext.ClearDepthStencilView(
219 this.デバイスリソース.D3DDepthStencilView,
220 SharpDX.Direct3D11.DepthStencilClearFlags.Depth,
227 this.現在のステージ?.進行描画する( this.デバイスリソース );
230 // スワップチェーンを表示する。垂直帰線待ちなどで時間がかかるので、この部分はスレッド排他領域の外に配置すること。
231 this.デバイスリソース.SwapChain.Present(
232 ( StrokeStyleT.Config.垂直帰線待ちを行う ) ? 1 : 0,
233 SharpDX.DXGI.PresentFlags.None );
235 // ステージの状態をチェックし、必要あれば遷移またはアプリを終了する。
236 bool アプリを終了せよ = false;
239 if( null != this.現在のステージ )
241 switch( this.現在のステージ.GetType().Name )
243 case nameof( ステージ.ステージ ):
244 #region " ビュアーモード → AutoPlayerでログインして曲読込ステージへ。"
246 if( StrokeStyleT.ビュアーモードである )
248 this.ログインする( Properties.Resources.AUTOPLAYER );
249 this.曲読込ステージ.読込曲のファイルパスを取得する = () => ( null ); // 今は null 。あとでメッセージキューを見る。
250 this.曲読込ステージ.活性化する( this.デバイスリソース );
251 this.現在のステージ = this.曲読込ステージ;
255 #region " 通常モード → 起動ステージへ。"
259 this.起動ステージ.活性化する( this.デバイスリソース );
260 this.現在のステージ = this.起動ステージ;
266 case nameof( ステージ.起動.起動ステージ ):
267 #region " 終了 → タイトルステージへ。"
269 if( this.起動ステージ.現在のフェーズ == ステージ.起動.起動ステージ.フェーズ.終了 )
271 this.現在のステージ.非活性化する( this.デバイスリソース );
272 this.現在のステージ = this.タイトルステージ;
273 this.現在のステージ.活性化する( this.デバイスリソース );
279 case nameof( ステージ.タイトル.タイトルステージ ):
280 #region " 確定 → ログインステージへ。"
282 if( this.タイトルステージ.現在のフェーズ == ステージ.タイトル.タイトルステージ.フェーズ.確定 )
284 this.現在のステージ.非活性化する( this.デバイスリソース );
285 this.現在のステージ = this.ログインステージ;
286 this.現在のステージ.活性化する( this.デバイスリソース );
290 #region " キャンセル → アプリを終了する。"
292 else if( this.タイトルステージ.現在のフェーズ == ステージ.タイトル.タイトルステージ.フェーズ.キャンセル )
294 this.現在のステージ.非活性化する( this.デバイスリソース );
302 case nameof( ステージ.ログイン.ログインステージ ):
303 #region " 確定 → ログイン処理を行って、選曲ステージへ。"
305 if( this.ログインステージ.現在のフェーズ == ステージ.ログイン.ログインステージ.フェーズ.確定 )
307 var user = StrokeStyleT.ユーザ管理.現在選択されているユーザ;
311 foreach( var path in user.曲の検索元フォルダパスのリスト )
312 SST.曲.曲ツリー管理.フォルダから曲を再帰的に検索して子ノードリストに追加する( user.曲ツリーのルートノード, path );
314 StrokeStyleT.曲ツリー管理.現在の管理対象ツリー = StrokeStyleT.ユーザ管理.現在選択されているユーザ.曲ツリーのルートノード;
317 this.現在のステージ.非活性化する( this.デバイスリソース );
318 this.現在のステージ = this.選曲ステージ;
319 this.現在のステージ.活性化する( this.デバイスリソース );
323 #region " キャンセル → タイトルステージへ。"
325 else if( this.ログインステージ.現在のフェーズ == ステージ.ログイン.ログインステージ.フェーズ.キャンセル )
327 this.現在のステージ.非活性化する( this.デバイスリソース );
328 this.現在のステージ = this.タイトルステージ;
329 this.現在のステージ.活性化する( this.デバイスリソース );
335 case nameof( ステージ.選曲.選曲ステージ ):
336 #region " 曲確定 → 曲読込ステージへ。"
338 if( this.選曲ステージ.現在のフェーズ == ステージ.選曲.選曲ステージ.フェーズ.曲確定 )
340 // 曲ノードが選択されていることを確認。
341 Trace.Assert( null != StrokeStyleT.曲ツリー管理.現在選択されているノード, "[バグあり] 選択曲が null です。" );
342 this.現在のステージ.非活性化する( this.デバイスリソース );
343 this.現在のステージ = this.曲読込ステージ;
344 this.現在のステージ.活性化する( this.デバイスリソース );
348 #region " キャンセル → アプリを終了する。"
350 else if( this.選曲ステージ.現在のフェーズ == ステージ.選曲.選曲ステージ.フェーズ.キャンセル )
352 this.現在のステージ.非活性化する( this.デバイスリソース );
360 case nameof( ステージ.曲読込.曲読込ステージ ):
361 #region " 終了 → 演奏ステージへ。"
362 //--------------------
363 if( this.曲読込ステージ.現在のフェーズ == ステージ.曲読込.曲読込ステージ.フェーズ.終了 )
365 this.現在のステージ.非活性化する( this.デバイスリソース );
366 this.現在のステージ = this.演奏ステージ;
367 this.現在のステージ.活性化する( this.デバイスリソース );
369 //--------------------
373 case nameof( ステージ.演奏.演奏ステージ ):
374 #region " 演奏終了 → 結果ステージへ。"
375 //--------------------
376 if( this.演奏ステージ.現在のフェーズ == ステージ.演奏.演奏ステージ.フェーズ.クリアor失敗 )
378 this.現在のステージ.非活性化する( this.デバイスリソース );
379 this.現在のステージ = this.結果ステージ;
380 this.現在のステージ.活性化する( this.デバイスリソース );
382 //--------------------
384 #region " キャンセル → 選曲ステージへ。"
385 //--------------------
386 if( this.演奏ステージ.現在のフェーズ == ステージ.演奏.演奏ステージ.フェーズ.キャンセル )
388 this.現在のステージ.非活性化する( this.デバイスリソース );
389 this.現在のステージ = this.選曲ステージ;
390 this.現在のステージ.活性化する( this.デバイスリソース );
392 //--------------------
396 case nameof( ステージ.結果.結果ステージ ):
397 #region " 終了 → 選曲ステージへ。"
398 //--------------------
399 if( this.結果ステージ.現在のフェーズ == ステージ.結果.結果ステージ.フェーズ.終了 )
401 this.現在のステージ.非活性化する( this.デバイスリソース );
402 this.現在のステージ = this.選曲ステージ;
403 this.現在のステージ.活性化する( this.デバイスリソース );
405 //--------------------
414 // GUIスレッド上で、ウィンドウを閉じる。
415 this.MainForm.BeginInvoke( new Action( () => { this.MainForm.Close(); } ) );
418 protected override void デバイス依存リソースを解放する()
420 FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
424 Debug.Assert( null != this.デバイスリソース ); // 解放前であること。
425 this.現在のステージ?.デバイス依存リソースを解放する( this.デバイスリソース );
428 FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
430 protected override void デバイス依存リソースを再構築する()
432 FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
436 Debug.Assert( null != this.デバイスリソース ); // 再生成済みであること。
437 this.現在のステージ?.デバイス依存リソースを作成する( this.デバイスリソース );
440 FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
443 /// アプリが二重起動されたときに発生するイベント。
446 /// 後続のインスタンスは起動せず、既存のインスタンスに対してこのイベントが発生する。
447 /// eventArg.CommandLine で、後続のインスタンスのコマンドライン引数を確認することができる。
449 protected override void OnStartupNextInstance( StartupNextInstanceEventArgs eventArgs )
451 FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
455 if( StrokeStyleT.ビュアーモードである )
457 this.コマンドライン引数を解析する( eventArgs.CommandLine );
461 FDK.Log.ERROR( "現在、ビュアーモードではありません。" );
465 FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
468 // 各ステージの、唯一のインスタンス。最終的に null を代入するので、readonlyにはしない。
469 protected SST.ステージ.ステージ 最初のダミーステージ = new ステージ.ステージ();
470 protected SST.ステージ.起動.起動ステージ 起動ステージ = new ステージ.起動.起動ステージ();
471 protected SST.ステージ.タイトル.タイトルステージ タイトルステージ = new ステージ.タイトル.タイトルステージ();
472 protected SST.ステージ.ログイン.ログインステージ ログインステージ = new ステージ.ログイン.ログインステージ();
473 protected SST.ステージ.選曲.選曲ステージ 選曲ステージ = new ステージ.選曲.選曲ステージ();
474 protected SST.ステージ.曲読込.曲読込ステージ 曲読込ステージ = new ステージ.曲読込.曲読込ステージ();
475 protected SST.ステージ.演奏.演奏ステージ 演奏ステージ = new ステージ.演奏.演奏ステージ();
476 protected SST.ステージ.結果.結果ステージ 結果ステージ = new ステージ.結果.結果ステージ();
478 private SST.ステージ.ステージ 現在のステージ = null;
482 private static SST.フォルダ bs_フォルダ = null;
483 private static FDK.入力.Keyboard bs_キーボード入力 = null;
484 private static FDK.入力.MidiIn bs_MIDI入力 = null;
485 private static FDK.メディア.サウンド.WASAPI排他.ExclusiveDevice bs_Wasapiデバイス = null;
486 private static readonly Random bs_乱数 = new Random( DateTime.Now.Millisecond );
487 private static SST.ユーザ.ユーザ管理 bs_ユーザ管理 = null;
488 private static SST.曲.曲ツリー管理 bs_曲ツリー管理 = null;
489 private static SST.設定.Config bs_Config = null;
490 private static readonly ConcurrentQueue<SST.ステージ.演奏.ビュアーメッセージ> bs_ビュアーメッセージキュー = new ConcurrentQueue<ステージ.演奏.ビュアーメッセージ>();
495 /// コマンドライン引数を解析して、ビュアーモードの設定があればそれを返す。
497 /// <param name="args">コマンドライン引数の列挙。exeファイル名は含まない。</param>
498 /// <return>引数を反映したビュアーモード変数。</return>
499 private void コマンドライン引数を解析する( IEnumerable<string> args )
501 FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
505 if( 0 == args.Count() )
507 FDK.Log.Info( "引数は指定されていません。" ); // ビュアーモードではない。
511 StrokeStyleT.ビュアーモードである = true;
516 var msg = new ステージ.演奏.ビュアーメッセージ() {
517 種別 = ステージ.演奏.ビュアーメッセージ.E種別.演奏開始, // 規定値は「演奏開始」
520 ドラム音を発声する = false, // 規定値は false
524 var optionSet = new Mono.Options.OptionSet() {
525 "Usage: StrokeStyleT [File] [OPTIONS]+",
526 " File\t\t\t\tビュアーモードで表示する曲ファイル名です。",
527 { "p=|part=", "ビュアーモードで起動し、指定された小節番号から演奏を開始します。小節番号を省略すると、先頭から再生します。", (int v) => { msg.演奏開始小節番号 = v; } },
528 { "s|stop", "ビュアーモードで演奏中であれば、演奏を停止します。", v => { if ( v != null ) { msg.種別 = ステージ.演奏.ビュアーメッセージ.E種別.演奏停止; } } },
529 { "d|drums", "ビュアーモードで、チップヒット時に内蔵のドラム音を再生します。", v => { if( v != null ) { msg.ドラム音を発声する = true; } } },
533 List<string> ファイルパスs = optionSet.Parse( args );
536 if( msg.種別 == ステージ.演奏.ビュアーメッセージ.E種別.演奏停止 )
538 // (A) 演奏停止(曲ファイルパスは省略可。)
539 FDK.Log.Info( "ビュアーメッセージ: 演奏停止" );
543 // (B) 演奏開始(曲ファイルパスは必須。)
544 if( 0 < ファイルパスs.Count )
546 if( File.Exists( ファイルパスs[ 0 ] ) )
548 msg.曲ファイルパス = ファイルパスs[ 0 ];
552 msg.曲ファイルパス = null; // ファイルが存在しなかったら null 。
553 throw new Mono.Options.OptionException( $"ファイルが存在しません。[{FDK.フォルダ.絶対パスをフォルダ変数付き絶対パスに変換して返す( ファイルパスs[ 0 ] )}]", "File" );
558 throw new Mono.Options.OptionException( "ファイルの指定がありません。", "File" );
561 FDK.Log.Info( "ビュアーメッセージ: 演奏開始" );
562 FDK.Log.Info( $"曲ファイルパス: {msg.曲ファイルパス}" );
563 FDK.Log.Info( $"開始小節番号: {msg.演奏開始小節番号}" );
564 FDK.Log.Info( $"ドラム音: {msg.ドラム音を発声する}" );
568 StrokeStyleT.ビュアーメッセージキュー.Enqueue( msg );
569 FDK.Log.Info( "ビュアーメッセージを送信しました。" );
571 catch( Mono.Options.OptionException e )
573 FDK.Log.ERROR( $"{e.Message}" );
578 FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
582 private void ログインする( string ユーザ名 )
584 StrokeStyleT.ユーザ管理.ユーザを選択する( ユーザ名 );
585 FDK.Log.Info( $"ユーザが選択されました。[{StrokeStyleT.ユーザ管理.現在選択されているユーザ.名前}]" );
588 #region " Win32 API "
590 [ System.Runtime.InteropServices.DllImport( "winmm.dll", EntryPoint = "timeBeginPeriod" )]
591 private static extern uint timeBeginPeriod( uint uMilliseconds );
593 [System.Runtime.InteropServices.DllImport( "winmm.dll", EntryPoint = "timeEndPeriod" )]
594 private static extern uint timeEndPeriod( uint uMilliseconds );