LCOV - code coverage report
Current view: top level - cmd - play.go Coverage Total Hit
Test: coverage.lcov Lines: 0.0 % 84 0
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package cmd
       2              : 
       3              : import (
       4              :         "context"
       5              :         "fmt"
       6              :         "log"
       7              :         "net/http"
       8              :         "runtime"
       9              : 
      10              :         "github.com/hajimehoshi/ebiten/v2"
      11              :         "github.com/kijimaD/ruins/internal/config"
      12              :         "github.com/kijimaD/ruins/internal/crashreport"
      13              :         "github.com/kijimaD/ruins/internal/logger"
      14              :         "github.com/kijimaD/ruins/internal/maingame"
      15              :         "github.com/kijimaD/ruins/internal/save"
      16              :         "github.com/kijimaD/ruins/internal/steam"
      17              :         "github.com/pkg/profile"
      18              :         "github.com/urfave/cli/v3"
      19              : 
      20              :         _ "net/http/pprof" // pprofのHTTPエンドポイントを登録するためのインポート
      21              : 
      22              :         es "github.com/kijimaD/ruins/internal/engine/states"
      23              :         gs "github.com/kijimaD/ruins/internal/states"
      24              :         w "github.com/kijimaD/ruins/internal/world"
      25              : )
      26              : 
      27              : // CmdPlay はゲームをプレイするコマンド
      28              : var CmdPlay = &cli.Command{
      29              :         Name:        "play",
      30              :         Usage:       "play",
      31              :         Description: "play game",
      32              :         Action:      runPlay,
      33              :         Flags:       []cli.Flag{},
      34              : }
      35              : 
      36              : func runPlay(_ context.Context, _ *cli.Command) error {
      37              :         // 初期化フェーズの panic をここで受ける。RunGame 中のゲーム panic は MainGame の各コールバックが受ける
      38            0 :         defer crashreport.Guard()
      39            0 : 
      40              :         // Steam APIの初期化。steamタグなしではno-op
      41            0 :         if err := steam.Init(); err != nil {
      42            0 :                 return fmt.Errorf("steam initialization failed: %w", err)
      43            0 :         }
      44              : 
      45              :         // ユーザー設定ファイルが無ければデフォルト値で作成する。
      46              :         // 失敗しても Load はデフォルト値で継続できるため、警告のみで処理を進める
      47            0 :         if err := config.EnsureUserConfigFile(); err != nil {
      48            0 :                 logger.New(logger.CategoryLoad).Warn("failed to create user config file", "error", err)
      49            0 :         }
      50              : 
      51              :         // 設定を読み込み
      52            0 :         cfg, err := config.Load()
      53            0 :         if err != nil {
      54            0 :                 return fmt.Errorf("failed to load config: %w", err)
      55            0 :         }
      56              : 
      57              :         // ログ設定を読み込み
      58            0 :         logger.LoadFromConfig(cfg.LogLevel, cfg.LogCategories)
      59            0 : 
      60              :         // デバッグモードの場合は設定を表示
      61            0 :         if cfg.Debug {
      62            0 :                 log.Printf("Configuration loaded:\n%s", cfg.String())
      63            0 :         }
      64              : 
      65              :         // ウィンドウ設定
      66            0 :         ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
      67            0 :         ebiten.SetWindowSize(cfg.User.WindowWidth, cfg.User.WindowHeight)
      68            0 :         ebiten.SetWindowTitle("Coldward")
      69            0 : 
      70              :         // FPS設定
      71            0 :         if cfg.TargetFPS != 60 {
      72            0 :                 ebiten.SetTPS(cfg.TargetFPS)
      73            0 :         }
      74              : 
      75              :         // プロファイラー設定(WASMは除外)
      76            0 :         if runtime.GOOS != "js" && cfg.DebugPProf {
      77            0 :                 var profileOptions []func(*profile.Profile)
      78            0 : 
      79            0 :                 if cfg.ProfileMemory {
      80            0 :                         profileOptions = append(profileOptions, profile.MemProfile)
      81            0 :                 }
      82            0 :                 if cfg.ProfileCPU {
      83            0 :                         profileOptions = append(profileOptions, profile.CPUProfile)
      84            0 :                 }
      85            0 :                 if cfg.ProfileMutex {
      86            0 :                         profileOptions = append(profileOptions, profile.MutexProfile)
      87            0 :                 }
      88            0 :                 if cfg.ProfileTrace {
      89            0 :                         profileOptions = append(profileOptions, profile.TraceProfile)
      90            0 :                 }
      91              : 
      92              :                 // デフォルトでメモリプロファイルを有効化
      93            0 :                 if len(profileOptions) == 0 {
      94            0 :                         profileOptions = append(profileOptions, profile.MemProfile)
      95            0 :                 }
      96              : 
      97            0 :                 profileOptions = append(profileOptions, profile.ProfilePath(cfg.ProfilePath))
      98            0 :                 defer profile.Start(profileOptions...).Stop()
      99            0 : 
     100              :                 // pprofサーバー起動
     101            0 :                 pprofAddr := fmt.Sprintf("localhost:%d", cfg.PProfPort)
     102            0 :                 go func() {
     103            0 :                         log.Printf("pprof server starting on http://%s", pprofAddr)
     104            0 :                         log.Fatal(http.ListenAndServe(pprofAddr, nil))
     105            0 :                 }()
     106              :         }
     107              : 
     108            0 :         world, err := maingame.InitWorld(cfg)
     109            0 :         if err != nil {
     110            0 :                 return err
     111            0 :         }
     112              : 
     113            0 :         initialState, err := initialPlayState(world, cfg)
     114            0 :         if err != nil {
     115            0 :                 return err
     116            0 :         }
     117              : 
     118            0 :         stateMachine, err := es.Init(initialState, world)
     119            0 :         if err != nil {
     120            0 :                 return err
     121            0 :         }
     122              : 
     123            0 :         game, err := maingame.NewMainGame(world, stateMachine)
     124            0 :         if err != nil {
     125            0 :                 return err
     126            0 :         }
     127              : 
     128              :         // クラッシュ諸元へ載せる最上位ステート名の取り出し方を登録する。Save がクラッシュ時に引く。
     129              :         // StateMachine は値型で、ゲームループが動かすのは game が持つ実体。登録前のローカル stateMachine を
     130              :         // 捕捉するとコピーを掴み遷移を追えないので、game を捕捉して落ちた時点の状態を引く
     131            0 :         crashreport.SetStateProvider(func() string {
     132            0 :                 s := game.StateMachine.GetCurrentState()
     133            0 :                 if s == nil {
     134            0 :                         return ""
     135            0 :                 }
     136            0 :                 return fmt.Sprintf("%T", s)
     137              :         })
     138              :         // Linux のタスクバーや Steam Deck は SetWindowTitle でなく X11 の WM_CLASS でアプリを同定する。
     139              :         // 既定のままだと "Ebitengine-Application" と表示される。WM_CLASS の class 側は表示・グルーピングに
     140              :         // 使われるので表示名の Coldward、instance 側は実行体の同定子なので内部名の ruins にする。
     141            0 :         return ebiten.RunGameWithOptions(game, &ebiten.RunGameOptions{
     142            0 :                 X11ClassName:    "Coldward",
     143            0 :                 X11InstanceName: "ruins",
     144            0 :         })
     145              : }
     146              : 
     147              : // initialPlayState は起動時の開始ステートを決める。継続でセーブを読めればその地点から、無いときと読めないときは
     148              : // メインメニューから始める
     149              : func initialPlayState(world w.World, cfg *config.Config) (es.State[w.World], error) {
     150            0 :         if cfg.Continue && cfg.SaveLoadEnabled {
     151            0 :                 saveManager, err := save.NewSerializationManager()
     152            0 :                 if err != nil {
     153            0 :                         logger.New(logger.CategorySave).Error("failed to prepare the save directory", "error", err.Error())
     154            0 :                         return gs.NewMainMenuAfterLoadFailure()
     155            0 :                 }
     156            0 :                 return gs.ContinueOrMainMenu(world, saveManager), nil
     157              :         }
     158            0 :         return &gs.MainMenuState{}, nil
     159              : }
        

Generated by: LCOV version 2.0-1