diff --git a/docs/launcher.md b/docs/launcher.md index 7c36f563..d4a3d5a0 100644 --- a/docs/launcher.md +++ b/docs/launcher.md @@ -75,7 +75,8 @@ Customize Launcher (on the home screen) makes the look the player's own: Steam reads that file only when it starts and writes it back when it closes, so a running Steam is asked to close first and started again after. The shortcut runs the launcher's `pythonw.exe` on `data/steam_play.pyw`, which starts the launcher of `app/current.txt` with `--play`: the game starts as Play starts it, and the process waits - for it, so Steam counts the game as running while it is. + for it, so Steam counts the game as running while it is. Its end goes into the logs as Play's does, and a fatal + error or a crash gets a message box, as no window shows it. The choices are `theme`, `custom_background`, `custom_panel`, `custom_buttons`, `background`, `background_dim`, `animations` and `sounds` in `data/launcher.json`. diff --git a/launcher/ui/app.py b/launcher/ui/app.py index ca772511..57bb8e69 100644 --- a/launcher/ui/app.py +++ b/launcher/ui/app.py @@ -241,13 +241,13 @@ def shown_fatal(fatal): def game_end(game_dir, code, started=None, restarted=False): - """What the launcher says about an end of the game (App.watch_game): a GameEnd of the line for launcher.log, the - sentence for the player ('' for none), the text note_exit_in_log adds to the game's log ('' for none), and alarm: - whether the end is worth a message box where no window shows the sentence (a FATAL line, a code of FATAL_CODES or - a Windows status; not 0, nor 1, which Task Manager's End task gives). code is the exit code of the run's last - process, or None when it is not known, which happens only for a copy the game's own Restart started that could not - be followed (GameRun). started is time.time() before the game was started, restarted whether it restarted itself - on the way.""" + """What the launcher says about an end of the game (App.watch_game, play_only): a GameEnd of the line for + launcher.log, the sentence for the player ('' for none), the text note_exit_in_log adds to the game's log ('' for + none), and alarm: whether the end is worth a message box where no window shows the sentence (a FATAL line, a code + of FATAL_CODES or a Windows status; not 0, nor 1, which Task Manager's End task gives). code is the exit code of + the run's last process, or None when it is not known, which happens only for a copy the game's own Restart started + that could not be followed (GameRun). started is time.time() before the game was started, restarted whether it + restarted itself on the way.""" how = ' after restarting itself' if restarted else '' if code not in (0, None): code &= 0xFFFFFFFF @@ -2880,16 +2880,24 @@ def play_only(ctx): if waiting: # the window builds the mods' code in, then starts the game (App.start with --play) ctx.log(f'play from Steam: the game is built again first for the code of {", ".join(waiting)}') return None + started = time.time() try: - proc = winsys.start_game(exe) + run = GameRun(winsys.start_game(exe), gd, started) except OSError as e: ctx.log(f'play from Steam: {exe}: {e}') return None ctx.log(f'play from Steam: {exe}') time.sleep(2) - while proc.poll() is None or saves.game_running(gd): + while run.running(): time.sleep(1) - ctx.log(f'play from Steam: the game ended (code {proc.returncode})') + # The end is told as Play's is (App.watch_game), with no window: a box for an end worth one. The logs come first, + # since the box waits for OK and Steam may end this process meanwhile. + end = run.end() + ctx.log(f'play from Steam: {end.log}') + if end.note: + note_exit_in_log(gd, end.note) + if end.alarm: + tell(end.message) return 0 diff --git a/tests/launcher_test.py b/tests/launcher_test.py index e9cfae56..f87e2bce 100644 --- a/tests/launcher_test.py +++ b/tests/launcher_test.py @@ -5293,6 +5293,57 @@ class WhilePlayingTest(Tmp): a.ctx.log.assert_called_once_with('the game ended with code 0') self.assertNotIn('[launcher]', log.read_text(encoding='utf-8')) + def play_from_steam(self, code, log_text): + """play_only (--play, the Steam shortcut) for a game that ends with code, its log then holding log_text; (the + context, the stand-in for tell, the log).""" + from launcher.ui import app + (self.tmp / 'ocarina.exe').write_bytes(b'MZ') + log = self.tmp / 'logs' / 'nx2navi.log' + log.parent.mkdir(exist_ok=True) + log.write_text(log_text, encoding='utf-8') + ctx = mock.MagicMock() + ctx.settings = {'game_dir': str(self.tmp)} + proc = mock.MagicMock(returncode=code, pid=5) + proc.poll.return_value = code + told = [] + + def tell(text): # what the logs hold when the box comes, which waits for OK + told.append((text, log.read_text(encoding='utf-8'), list(ctx.log.call_args_list))) + with mock.patch('launcher.ui.app.winsys.start_game', return_value=proc), \ + mock.patch('launcher.ui.app.saves.game_running', return_value=False), \ + mock.patch('launcher.ui.app.mods.prepare', return_value=[]), \ + mock.patch('launcher.ui.app.build.can_rebuild', return_value=False), \ + mock.patch('launcher.ui.app.time.sleep'), \ + mock.patch('launcher.ui.app.tell', side_effect=tell): + self.assertEqual(app.play_only(ctx), 0) + return ctx, told, log + + def test_an_end_of_a_game_started_from_steam_is_told(self): + """The Steam shortcut's game ends as Play's does: launcher.log and the game's log say how, and an end worth it + (a FATAL line, 70, 71 or 72, a Windows status) gets a message box, as no window shows it.""" + try: + from launcher.ui import app # noqa: F401 + except ImportError as e: + self.skipTest(f'no window toolkit: {e}') + ctx, told, log = self.play_from_steam(70, '[host] x\nFATAL: fs: another copy of the game is running\n') + self.assertEqual(len(told), 1) + text, game_log, calls = told[0] + self.assertEqual(text, 'The game ended with a fatal error (code 70): fs: another copy of the game is running. ' + 'Its log is logs\\nx2navi.log in the game folder.') + self.assertIn('[launcher] the game ended with code 70', game_log) + self.assertIn(mock.call('play from Steam: the game ended with code 70; its log says: FATAL: fs: another copy ' + 'of the game is running'), calls) + _, told, _ = self.play_from_steam(0xC0000005, '[host] x\n') + self.assertEqual([t[0] for t in told], ['The game ended with code 0xC0000005 (access violation). Its log is ' + 'logs\\nx2navi.log in the game folder.']) + ctx, told, log = self.play_from_steam(0, '[host] x\n[host] gfx: window closed; exiting\n') + self.assertEqual(told, []) + ctx.log.assert_called_with('play from Steam: the game ended with code 0') + ctx, told, log = self.play_from_steam(1, '[host] perf: the last line\n') # Task Manager's End task + self.assertEqual(told, []) + ctx.log.assert_called_with('play from Steam: the game ended with code 1') + self.assertIn('[launcher] the game ended with code 1 at', log.read_text(encoding='utf-8')) + def test_a_fatal_exit_shows_the_games_fatal_line(self): """Exit codes 70 and 71 follow a FATAL line in the game's log: the home screen and launcher.log name it.""" try: