From 9204f4ed27573517f7204e1024c8b32bef760ae5 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Sat, 28 May 2022 18:13:05 +0200 Subject: read steam names from profiles for better selection --- tf2sgu | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/tf2sgu b/tf2sgu index d4ea122..2d53034 100755 --- a/tf2sgu +++ b/tf2sgu @@ -55,6 +55,7 @@ class Profile: def __init__(self, path): self.path = path + self._name = None def __repr__(self): return f"" @@ -69,6 +70,25 @@ class Profile: """Gives the folder (as Path) for the savegames.""" return self.path / GAME_ID / "local" / "save" + def name(self): + """Gets the personal name of the user.""" + if self._name is not None: + return self._name + + long_id = steam_32_to_64(int(self.user_id)) + loginusers_path = STEAM_PATH / "config" / "loginusers.vdf" + # This should probably be done with a vdf parser, but we want to stay + # away from 3rd party dependencies + with open(loginusers_path, "r") as loginusers: + correct_section = False + for line in loginusers: + if f'"{long_id}"' in line: + correct_section = True + if correct_section and '"PersonaName"' in line: + raw_name = line.split()[1] + self._name = raw_name[1:-1] + return self._name + def saves(self): """Returns a list of all saves available in this profile.""" return read_saves(self.save_dir) @@ -109,6 +129,15 @@ def ask_choice(items, labels=None): print("Invalid choice") +def steam_32_to_64(steam_id): + """Convert a short (32 bit) Steam ID to a long (64 bit) Steam ID.""" + # This is the magic number to convert a 32 bit steam ID (as found in the + # userdata folder) to a 64 bit steam id (as used in the loginusers.vdf). + # For reference, see for example here: + # https://github.com/gorangajic/steam-id-convertor + return steam_id + 76561197960265728 + + def get_steam_profiles(): """Returns steam profiles that have data for Transport Fever 2 assigned.""" profiles = [] @@ -143,7 +172,7 @@ def select_profile(args): return profiles[0] else: labels = [ - f"{profile.user_id}: {len(profile.saves())} savegames" + f"{profile.name()}: {len(profile.saves())} savegames" for profile in profiles ] print("Select your profile:") @@ -161,7 +190,9 @@ def cmd_list_profiles(_args): """List available Steam profiles (with Transport Fever 2 installed).""" profiles = get_steam_profiles() for profile in profiles: - print(profile.user_id, len(profile.saves()), "savegames") + print( + f"{profile.user_id} - {profile.name()} ({len(profile.saves())} savegames)" + ) def cmd_list_locals(args): @@ -376,7 +407,7 @@ def _tui_select_profile(screen, profiles): height, width = screen.getmaxyx() - names = [profile.user_id for profile in profiles] + names = [profile.name() for profile in profiles] view = TuiListView(screen.derwin(height - 5, width, 5, 0), names) view.activate() view.render() -- cgit v1.2.3