diff --git a/launcher/steamlib.py b/launcher/steamlib.py index c18e0b95..b5bde296 100644 --- a/launcher/steamlib.py +++ b/launcher/steamlib.py @@ -197,6 +197,21 @@ def find(steam_folder, signed=signed_by_valve): return None +def wanted(game_dir): + """True when co-op is on in the game's settings, read as runtime/cfg.c reads them: NX2NAVI_COOP from the + environment the game inherits first, else nx2navi.ini's coop; empty is the default, off, and any other value but 0 + is on (cfg_bool). The launcher puts the library beside the game (ensure) only then: with co-op off the game loads + none, and a Play should not wait for a search of the Steam library. A file that cannot be read counts as off.""" + value = os.environ.get('NX2NAVI_COOP') + if value is None: + from . import gameini + try: + value = gameini.get(game_dir, 'coop') + except OSError: + return False + return value not in ('', '0') + + _none = set() # game folders for which this run of the launcher found no library: not searched again at each Play diff --git a/launcher/ui/app.py b/launcher/ui/app.py index a8a0c984..2781f1ad 100644 --- a/launcher/ui/app.py +++ b/launcher/ui/app.py @@ -256,6 +256,7 @@ class App: self.dump_search = None self.uninstall_game = False self.game_proc = None + self.game_starting = False # Play waits for co-op's Steam library file on a thread of its own (start_game) self.cursor_frames, self.cursor_k = [], 0 self.last_second = 0 self.gamepad = winsys.Gamepad() @@ -1872,7 +1873,7 @@ class App: self.message = 'The game is not built yet.' self.render() return - if self.game_proc and self.game_proc.poll() is None: + if (self.game_proc and self.game_proc.poll() is None) or self.game_starting: return if saves.game_running(gd): # a copy this launcher did not start (another launcher, a shortcut, a test run) self.message = 'The game is already running.' @@ -1891,7 +1892,34 @@ class App: return if waiting: self.ctx.log(f'play: without the code of {", ".join(waiting)}, whose build did not finish') - steamlib.ensure(gd, self.ctx.layout.app / 'bin', log=self.ctx.log) # co-op's Steam library file, beside the game + self.start_game(gd, exe) + + def start_game(self, gd, exe): + """Starts the game. With co-op on in its settings, co-op's Steam library file goes beside it first + (steamlib.ensure), on a thread of its own: without the file that is a search of the player's Steam libraries + and a signature check, seconds in which the window would stand still. The game starts from the window's + thread once the file is there or known to be missing (launch).""" + if not steamlib.wanted(gd): + self.launch(exe) + return + import threading + self.game_starting = True + self.message = 'Starting the game…' + self.render() + kit_bin = self.ctx.layout.app / 'bin' + + def work(): + try: + steamlib.ensure(gd, kit_bin, log=self.ctx.log) + finally: + self.root.after(0, lambda: self.launch(exe)) + threading.Thread(target=work, daemon=True).start() + + def launch(self, exe): + if self.game_starting: # start_game's message goes with the wait + self.game_starting = False + self.message = '' + self.render() try: self.game_proc = winsys.start_game(exe) except OSError as e: @@ -2748,7 +2776,8 @@ 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 - steamlib.ensure(gd, ctx.layout.app / 'bin', log=ctx.log) # co-op's Steam library file, beside the game + if steamlib.wanted(gd): # co-op's Steam library file, beside the game; no window waits here + steamlib.ensure(gd, ctx.layout.app / 'bin', log=ctx.log) try: proc = winsys.start_game(exe) except OSError as e: diff --git a/tests/launcher_test.py b/tests/launcher_test.py index 9f256b6e..d8a630e2 100644 --- a/tests/launcher_test.py +++ b/tests/launcher_test.py @@ -5087,6 +5087,67 @@ class SteamLibTest(Tmp): self.assertEqual(len(lines), 1) self.assertFalse((empty / steamlib.NAME).exists()) + def test_the_library_is_wanted_only_with_co_op_on(self): + """As runtime/cfg.c reads co-op: NX2NAVI_COOP first, else the game's nx2navi.ini, off by default.""" + game = self.tmp / 'game' + game.mkdir() + with mock.patch.dict(os.environ): + os.environ.pop('NX2NAVI_COOP', None) + self.assertFalse(steamlib.wanted(game)) # no settings file: the default, off + (game / 'nx2navi.ini').write_text('[coop]\ncoop = 0\n', encoding='utf-8') + self.assertFalse(steamlib.wanted(game)) + (game / 'nx2navi.ini').write_text('[coop]\ncoop = 1\ncoop =\n', encoding='utf-8') + self.assertTrue(steamlib.wanted(game)) # an empty line leaves the earlier value + os.environ['NX2NAVI_COOP'] = '0' + self.assertFalse(steamlib.wanted(game)) # the environment wins, as in the game + os.environ['NX2NAVI_COOP'] = '1' + self.assertTrue(steamlib.wanted(self.tmp / 'no game')) + + def test_play_waits_for_the_library_only_with_co_op_on_and_not_on_the_windows_thread(self): + game = self.tmp / 'game' + game.mkdir() + (game / 'ocarina.exe').write_bytes(b'MZ') + App, a = mock_app(self, game) + a.game_proc, a.game_starting = None, False + a.mods_before_play.return_value = [] + a.start_game = lambda gd, exe: App.start_game(a, gd, exe) + a.launch = lambda exe: App.launch(a, exe) + searched = [] + done = threading.Event() + + def ensure(*args, **kwargs): + searched.append(threading.current_thread()) + done.set() + return 'none' + with mock.patch.dict(os.environ), mock.patch('launcher.ui.app.steamlib.ensure', side_effect=ensure), \ + mock.patch('launcher.ui.app.winsys.start_game') as start, \ + mock.patch('launcher.ui.app.saves.game_running', return_value=False): + os.environ.pop('NX2NAVI_COOP', None) + App.play(a) # co-op off: no search, the game starts + self.assertEqual(searched, []) + start.assert_called_once_with(game / 'ocarina.exe') + a.game_proc = None + start.reset_mock() + a.root.after.reset_mock() + gameini.put(game, 'coop', 'coop', '1') + App.play(a) # co-op on: the search runs on its own thread + self.assertTrue(done.wait(10)) + self.assertNotEqual(searched, [threading.current_thread()]) + self.assertTrue(a.game_starting) + App.play(a) # a second Play while it waits does nothing + self.assertEqual(len(searched), 1) + for _ in range(1000): # the thread hands the start to the window's + if a.root.after.call_args_list: + break + time.sleep(0.01) + start.assert_not_called() + delay, then = a.root.after.call_args_list[0][0] + self.assertEqual(delay, 0) + then() + start.assert_called_once_with(game / 'ocarina.exe') + self.assertFalse(a.game_starting) + self.assertEqual(a.message, '') + class SteamTest(Tmp): """launcher/steam.py on made-up Steam files: the binary shortcuts file read and written back unchanged, our