diff --git a/docs/launcher.md b/docs/launcher.md index d4a3d5a0..5b3b4704 100644 --- a/docs/launcher.md +++ b/docs/launcher.md @@ -76,7 +76,9 @@ Customize Launcher (on the home screen) makes the look the player's own: 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. 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. + error or a crash gets a message box, as no window shows it. While the window or a command runs a job in the game + folder (the launcher folder's lock is held and `data/job-running.json` names the job), it does not start the + game, which would be the build that job is replacing, and a box says which job to wait for. The choices are `theme`, `custom_background`, `custom_panel`, `custom_buttons`, `background`, `background_dim`, `animations` and `sounds` in `data/launcher.json`. diff --git a/launcher/jobs.py b/launcher/jobs.py index 96a63341..90bdbf23 100644 --- a/launcher/jobs.py +++ b/launcher/jobs.py @@ -43,6 +43,18 @@ def interrupted(ctx): return left if isinstance(left, dict) and left.get('task') else None +def running_job(ctx): + """The job writing into the game folder as MARKER says it ({'task', 'kind', 'game_dir', 'started'}), or None. The + marker stays. Only worth asking while another process holds the launcher folder's lock: one that a launcher + stopped in its job left behind reads the same (interrupted tells it and removes it).""" + try: + with open(ctx.layout.data / MARKER, encoding='utf-8') as f: + job = json.load(f) + except (OSError, ValueError): + return None + return job if isinstance(job, dict) and job.get('task') else None + + def failure_line(e): """A failure in one line for the log: its kind, its message and what it came from.""" cause = e.__cause__ diff --git a/launcher/ui/app.py b/launcher/ui/app.py index 57bb8e69..8f4b4be4 100644 --- a/launcher/ui/app.py +++ b/launcher/ui/app.py @@ -2867,6 +2867,18 @@ def play_only(ctx): exe = Path(gd) / 'ocarina.exe' if gd else None if not exe or not exe.is_file(): return None + # The window or a command may run a job in the game folder meanwhile (a rebuild, an update's install, the mods' + # build): the game started now would be the build that job is replacing, which then fails to replace it, and + # mods.prepare would unpack into the folder the job reads. Such a job holds the launcher folder's lock and leaves + # jobs.MARKER. The lock is only tried, and let go at once: held, it would keep the window from opening. + try: + locks.FileLock(ctx.layout.instance_lock, 'play from Steam', where=ctx.layout.root).release() + except locks.Busy: + job = jobs.running_job(ctx) + if job: + ctx.log(f'play from Steam: not started: the launcher is busy with {job["task"]}') + tell(f'NX2navi is busy with {job["task"]}. Start the game when it is done.') + return 0 if saves.game_running(gd): ctx.log('play from Steam: the game is already running') return 0 diff --git a/tests/launcher_test.py b/tests/launcher_test.py index 6f1dff4c..cb05662d 100644 --- a/tests/launcher_test.py +++ b/tests/launcher_test.py @@ -5343,9 +5343,10 @@ 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).""" + def play_from_steam(self, code, log_text, lock_held=False): + """play_only (--play, the Steam shortcut) for a game that ends with code, its log then holding log_text, with + the launcher folder's lock held by another process when lock_held; (the context, the stand-in for tell, the + log). self.start_game is the stand-in for winsys.start_game.""" from launcher.ui import app (self.tmp / 'ocarina.exe').write_bytes(b'MZ') log = self.tmp / 'logs' / 'nx2navi.log' @@ -5353,21 +5354,50 @@ class WhilePlayingTest(Tmp): log.write_text(log_text, encoding='utf-8') ctx = mock.MagicMock() ctx.settings = {'game_dir': str(self.tmp)} + ctx.layout = self.layout 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), \ + held = mock.patch('launcher.ui.app.locks.FileLock', side_effect=locks.Busy('the launcher window is using x')) \ + if lock_held else contextlib.nullcontext() + with mock.patch('launcher.ui.app.winsys.start_game', return_value=proc) as self.start_game, \ 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): + mock.patch('launcher.ui.app.tell', side_effect=tell), held: self.assertEqual(app.play_only(ctx), 0) return ctx, told, log + def test_play_from_steam_waits_for_a_job_in_the_game_folder(self): + """The Steam shortcut does not start the game while the window or a command runs a job in the game folder + (another process holds the launcher folder's lock, and the job's marker is there): the game would be the build + the job replaces. A marker that a stopped launcher left, with the lock free, stops nothing, nor does the window + open without a job.""" + try: + from launcher.ui import app # noqa: F401 + except ImportError as e: + self.skipTest(f'no window toolkit: {e}') + marker = self.layout.data / jobs.MARKER + marker.write_text(json.dumps({'task': 'the rebuild of the game', 'kind': 'rebuild'}), encoding='utf-8') + ctx, told, _ = self.play_from_steam(0, '[host] x\n', lock_held=True) + self.start_game.assert_not_called() + self.assertEqual([t[0] for t in told], + ['NX2navi is busy with the rebuild of the game. Start the game when it is done.']) + ctx.log.assert_called_once_with('play from Steam: not started: the launcher is busy with the rebuild of the ' + 'game') + self.assertTrue(marker.is_file()) # the job's marker stays + ctx, told, _ = self.play_from_steam(0, '[host] gfx: window closed; exiting\n') + self.start_game.assert_called_once() # the lock is free: the marker is a leftover + self.assertEqual(told, []) + marker.unlink() + ctx, told, _ = self.play_from_steam(0, '[host] gfx: window closed; exiting\n', lock_held=True) + self.start_game.assert_called_once() # the window is open, with no job + self.assertEqual(told, []) + 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."""