From 125fce8e5af860db0acced2b65749869e3b62041 Mon Sep 17 00:00:00 2001 From: R1kaB3rN <100738684+R1kaB3rN@users.noreply.github.com> Date: Fri, 9 Feb 2024 00:49:16 -0800 Subject: [PATCH] Support symlinking unix username to steamuser - Closes https://github.com/Open-Wine-Components/ULWGL-launcher/issues/15 --- gamelauncher.py | 27 +++++++++++- gamelauncher_test.py | 100 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+), 2 deletions(-) diff --git a/gamelauncher.py b/gamelauncher.py index 2dad6528f..e20d6e972 100755 --- a/gamelauncher.py +++ b/gamelauncher.py @@ -49,13 +49,36 @@ def parse_args() -> Namespace: # noqa: D103 def _setup_pfx(path: str) -> None: - """Create a symlink to the WINE prefix and tracked_files file.""" + """Create a symlink to the WINE prefix and tracked_files file. + + Also, create a symlink of steamuser to the Unix username + """ + username: str = Path().home().name + if not (Path(path + "/pfx")).expanduser().is_symlink(): # When creating the symlink, we want it to be in expanded form when passed unexpanded paths - # Example: pfx -> /home/.wine + # Good: pfx -> /home/foo/.wine + # Bad: pfx -> ~/.wine Path(path + "/pfx").expanduser().symlink_to(Path(path).expanduser()) Path(path + "/tracked_files").expanduser().touch() + # Create a symlink of the current user to the steamuser dir for Steam functionality + if Path(path + "/drive_c/users/" + username).expanduser().is_dir(): + # If the prefix exists, rename the dir to steamuser then create a symlink + Path(path + "/drive_c/users/" + username).expanduser().rename( + Path(path + "/drive_c/users/steamuser").expanduser() + ) + Path(path + "/drive_c/users/" + username).expanduser().symlink_to( + Path(path + "/drive_c/users/steamuser").expanduser() + ) + else: + Path(path + "/drive_c/users/steamuser").expanduser().mkdir( + parents=True, exist_ok=True + ) + Path(path + "/drive_c/users/" + username).expanduser().symlink_to( + Path(path + "/drive_c/users/steamuser").expanduser() + ) + def check_env(env: Dict[str, str]) -> Dict[str, str]: """Before executing a game, check for environment variables and set them. diff --git a/gamelauncher_test.py b/gamelauncher_test.py index 3d562438a..5b8ee90ac 100644 --- a/gamelauncher_test.py +++ b/gamelauncher_test.py @@ -1082,6 +1082,106 @@ def test_set_env(self): result = gamelauncher.set_env(self.env, result_args) self.assertTrue(result is self.env, "Expected the same reference") + def test_setup_pfx_symlinks_steam_mv(self): + """Test _setup_pfx for symbolic link to steamuser. + + Expects that symlinks of the user to steamuser are properly created for existing prefixes. + The user dir is renamed to steamuser then a symlink to that is created in the current dir + """ + result = None + pattern = r"^/home/[a-zA-Z]+" + unexpanded_path = re.sub( + pattern, + "~", + Path( + Path(self.test_file).cwd().as_posix() + "/" + self.test_file + ).as_posix(), + ) + # Create a user dir in the prefix + Path( + unexpanded_path + "/drive_c/users/" + Path().home().name + ).expanduser().mkdir(parents=True, exist_ok=True) + + result = gamelauncher._setup_pfx(unexpanded_path) + # Replaces the expanded path to unexpanded + # Example: ~/some/path/to/this/file + self.assertIsNone( + result, + "Expected None when creating symbolic link to WINE prefix and tracked_files file", + ) + self.assertTrue( + Path(self.test_file + "/pfx").is_symlink(), "Expected pfx to be a symlink" + ) + self.assertTrue( + Path(self.test_file + "/tracked_files").is_file(), + "Expected tracked_files to be a file", + ) + self.assertTrue( + Path(self.test_file + "/pfx").is_symlink(), "Expected pfx to be a symlink" + ) + # Check if the symlink is in its unexpanded form + self.assertEqual( + Path(self.test_file + "/pfx").readlink().as_posix(), + Path(unexpanded_path).expanduser().as_posix(), + ) + self.assertTrue( + Path(self.test_file + "/drive_c/users/steamuser").is_dir(), + "Expected steamuser to be created", + ) + self.assertTrue( + Path(unexpanded_path + "/drive_c/users/" + Path().home().name) + .expanduser() + .is_symlink(), + "Expected symlink of username -> steamuser", + ) + + def test_setup_pfx_symlinks_steam(self): + """Test _setup_pfx for symbolic link to steamuser. + + Expects that symlinks of the user to steamuser are properly created for new prefixes + """ + result = None + pattern = r"^/home/[a-zA-Z]+" + unexpanded_path = re.sub( + pattern, + "~", + Path( + Path(self.test_file).cwd().as_posix() + "/" + self.test_file + ).as_posix(), + ) + result = gamelauncher._setup_pfx(unexpanded_path) + # Replaces the expanded path to unexpanded + # Example: ~/some/path/to/this/file + self.assertIsNone( + result, + "Expected None when creating symbolic link to WINE prefix and tracked_files file", + ) + self.assertTrue( + Path(self.test_file + "/pfx").is_symlink(), "Expected pfx to be a symlink" + ) + self.assertTrue( + Path(self.test_file + "/tracked_files").is_file(), + "Expected tracked_files to be a file", + ) + self.assertTrue( + Path(self.test_file + "/pfx").is_symlink(), "Expected pfx to be a symlink" + ) + # Check if the symlink is in its unexpanded form + self.assertEqual( + Path(self.test_file + "/pfx").readlink().as_posix(), + Path(unexpanded_path).expanduser().as_posix(), + ) + self.assertTrue( + Path(self.test_file + "/drive_c/users/steamuser").is_dir(), + "Expected steamuser to be created", + ) + self.assertTrue( + Path(unexpanded_path + "/drive_c/users/" + Path().home().name) + .expanduser() + .is_symlink(), + "Expected symlink of username -> steamuser", + ) + def test_setup_pfx_symlinks(self): """Test _setup_pfx for valid symlinks.