Skip to content

Commit

Permalink
Replace game info dict with class 'SteamApp'
Browse files Browse the repository at this point in the history
Breaks previous api
  • Loading branch information
DavidoTek committed Apr 14, 2022
1 parent 1568321 commit 7f15485
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 57 deletions.
13 changes: 13 additions & 0 deletions pupgui2/datastructures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class SteamApp:
app_id = -1
libraryfolder_id = -1
game_name = ''
compat_tool = ''
app_type = ''
deck_compatibility = 0

def get_app_id_str(self):
return str(self.app_id)

def get_libraryfolder_id_str(self):
return str(self.libraryfolder_id)
6 changes: 3 additions & 3 deletions pupgui2/pupgui2.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from util import apply_dark_theme, create_compatibilitytools_folder
from util import install_directory, available_install_directories, get_install_location_from_directory_name
from util import list_installed_ctools, remove_ctool, sort_compatibility_tool_names
from steamutil import get_steam_games_using_compat_tool
from steamutil import get_steam_game_list
from util import print_system_information
from constants import APP_NAME, APP_VERSION, TEMP_DIR
import ctloader
Expand Down Expand Up @@ -150,7 +150,7 @@ def update_ui(self):
for ver in ctools:
# Launcher specific
if install_loc.get('launcher') == 'steam' and 'vdf_dir' in install_loc:
games = get_steam_games_using_compat_tool(ver.split(' - ')[0], install_loc.get('vdf_dir'))
games = get_steam_game_list(install_loc.get('vdf_dir'), ver.split(' - ')[0])
if len(games) == 0:
ver += ' - ' + self.tr('unused')

Expand Down Expand Up @@ -225,7 +225,7 @@ def btn_remove_selcted_clicked(self):
for item in self.ui.listInstalledVersions.selectedItems():
ver = item.text()
if install_loc.get('launcher') == 'steam' and 'vdf_dir' in install_loc:
games_using_tools += len(get_steam_games_using_compat_tool(ver.split(' - ')[0], install_loc.get('vdf_dir')))
games_using_tools += len(get_steam_game_list(install_loc.get('vdf_dir'), ver.split(' - ')[0]))
vers_to_remove.append(ver)

if games_using_tools > 0:
Expand Down
7 changes: 3 additions & 4 deletions pupgui2/pupgui2ctinfodialog.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
from util import open_webbrowser_thread
from steamutil import get_steam_games_using_compat_tool, get_steam_game_names_by_ids
from steamutil import get_steam_game_list, get_steam_game_names_by_ids
from constants import STEAM_APP_PAGE_URL

from PySide6.QtWidgets import *
Expand Down Expand Up @@ -59,13 +59,12 @@ def setup_ui(self):

def update_game_list(self):
if self.install_loc.get('launcher') == 'steam' and 'vdf_dir' in self.install_loc:
self.games = get_steam_games_using_compat_tool(self.ctool, self.install_loc.get('vdf_dir'))
self.games = get_steam_game_list(self.install_loc.get('vdf_dir'), self.ctool)
self.ui.txtNumGamesUsingTool.setText(str(len(self.games)))

self.ui.listGames.clear()
game_names = get_steam_game_names_by_ids(self.install_dir, self.games)
for game in self.games:
self.ui.listGames.addItem(str(game) + ': ' + str(game_names.get(int(game))))
self.ui.listGames.addItem(game.get_app_id_str() + ': ' + game.game_name)

self.batch_update_complete.emit(True)

Expand Down
18 changes: 9 additions & 9 deletions pupgui2/pupgui2gamelistdialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,28 @@ def update_game_list(self):
game_id_table_lables = []
i = 0
for game in games:
self.ui.tableGames.setCellWidget(i, 0, QLabel(game.get('game_name')))
self.ui.tableGames.setCellWidget(i, 0, QLabel(game.game_name))
combo = QComboBox()
combo.addItem('-')
combo.addItems(ctools)
if game.get('compat_tool') not in ctools:
combo.addItem(game.get('compat_tool'))
if game.get('compat_tool') is None:
if game.compat_tool not in ctools:
combo.addItem(game.compat_tool)
if game.compat_tool is None:
combo.setCurrentText('-')
else:
combo.setCurrentText(game.get('compat_tool'))
combo.currentTextChanged.connect(lambda text,id=game.get('id'): self.update_ctool(text, id))
combo.setCurrentText(game.compat_tool)
combo.currentTextChanged.connect(lambda text,game=game: self.update_ctool(text, game))
self.ui.tableGames.setCellWidget(i, 1, combo)
game_id_table_lables.append(game.get('id'))
game_id_table_lables.append(game.app_id)
i += 1
self.ui.tableGames.setVerticalHeaderLabels(game_id_table_lables)

def btn_close_clicked(self):
self.ui.close()

def update_ctool(self, ctool_name: str, game_id: str = '0'):
def update_ctool(self, ctool_name: str, game):
if ctool_name == '-':
ctool_name = None
install_loc = get_install_location_from_directory_name(self.install_dir)
steam_update_ctool(int(game_id), ctool_name, steam_config_folder=install_loc.get('vdf_dir'))
steam_update_ctool(game, ctool_name, steam_config_folder=install_loc.get('vdf_dir'))
self.game_property_changed.emit(True)
76 changes: 35 additions & 41 deletions pupgui2/steamutil.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,14 @@
import os
import vdf
from steam.utils.appcache import parse_appinfo


def get_steam_games_using_compat_tool(ver, steam_config_folder):
"""
Get all games using a specified compatibility tool from Steam config.vdf
'ver' should be the same as the internal name specified in compatibilitytool.vdf (Matches folder name in most cases)
Return Type: str[]
"""
config_vdf_file = os.path.join(os.path.expanduser(steam_config_folder), 'config.vdf')
tools = []

try:
d = vdf.load(open(config_vdf_file))
c = d.get('InstallConfigStore').get('Software').get('Valve').get('Steam').get('CompatToolMapping')

for t in c:
x = c.get(str(t))
if x is None:
continue
if x.get('name') == ver:
tools.append(t)
except Exception as e:
print('Error: Could not get list of Steam games using compat tool "' + str(ver) + '":', e)
tools = ['-1'] # don't return empty list else compat tool would be listed as unused

return tools
from datastructures import SteamApp


def get_steam_app_list(steam_config_folder):
"""
Returns a list of installed Steam apps and optionally game names and the compatibility tool they are using
steam_config_folder = e.g. '~/.steam/root/config'
Return Type: dict[]
Content: 'id', 'libraryfolder_id'
Optional-Content: 'game_name', 'compat_tool', 'type'
Return Type: SteamApp[]
"""
libraryfolders_vdf_file = os.path.join(os.path.expanduser(steam_config_folder), 'libraryfolders.vdf')
config_vdf_file = os.path.join(os.path.expanduser(steam_config_folder), 'config.vdf')
Expand All @@ -50,45 +23,64 @@ def get_steam_app_list(steam_config_folder):
if 'apps' not in v.get('libraryfolders').get(fid):
continue
for appid in v.get('libraryfolders').get(fid).get('apps'):
app = {'id': str(appid), 'libraryfolder_id': str(fid)}
app = SteamApp()
app.app_id = int(appid)
app.libraryfolder_id = fid
if appid in c:
app['compat_tool'] = c.get(appid).get('name')
app.compat_tool = c.get(appid).get('name')
apps.append(app)
app_ids_str.append(str(appid))

game_names = get_steam_game_names_by_ids(steam_config_folder, app_ids_str)
for a in apps:
if int(a.get('id', -1)) in game_names:
a['game_name'] = game_names.get(int(a.get('id', -1)))
if a.app_id in game_names:
a.game_name = game_names.get(a.app_id)
# Check if game or tool
if 'steamworks' not in a['game_name'].lower() and 'proton' not in a['game_name'].lower():
a['type'] = 'game'
if 'steamworks' not in a.game_name.lower() and 'proton' not in a.game_name.lower():
a.app_type = 'game'
except Exception as e:
print('Error: Could not get a list of all Steam apps:', e)

return apps


def get_steam_game_list(steam_config_folder):
def get_steam_game_list(steam_config_folder, compat_tool=''):
"""
Returns a list of installed Steam games and which compatibility tools they are using.
Return Type: dict[]
Content: 'id', 'compat_tool', 'game_name'
Specify compat_tool to only return games using the specified tool.
Return Type: SteamApp[]
"""
games = []
apps = get_steam_app_list(steam_config_folder)

for app in apps:
if app.get('type') == 'game':
if app.app_type == 'game':
if compat_tool != '':
if app.compat_tool != compat_tool:
continue
games.append(app)

return games


def get_steam_ctool_list(steam_config_folder, only_proton=False):
"""
Returns a list of installed Steam compatibility tools (official tools).
Return Type: SteamApp[]
"""
ctools = []
apps = get_steam_app_list(steam_config_folder)

# TODO

return ctools


def get_steam_game_names_by_ids(steam_config_folder, ids_str=[]):
"""
Get steam game names by ids
Return Type: dict[]
Return Type: dict
Maps game id to game name
"""
appinfo_file = os.path.join(os.path.expanduser(steam_config_folder), '../appcache/appinfo.vdf')
appinfo_file = os.path.realpath(appinfo_file)
Expand All @@ -113,7 +105,7 @@ def get_steam_game_names_by_ids(steam_config_folder, ids_str=[]):
return names


def steam_update_ctool(game_id=0, new_ctool=None, steam_config_folder=''):
def steam_update_ctool(game:SteamApp, new_ctool=None, steam_config_folder=''):
"""
Change compatibility tool for 'game_id' to 'new_ctool' in Steam config vdf
Return Type: bool
Expand All @@ -122,6 +114,8 @@ def steam_update_ctool(game_id=0, new_ctool=None, steam_config_folder=''):
if not os.path.exists(config_vdf_file):
return False

game_id = game.app_id

try:
d = vdf.load(open(config_vdf_file))
c = d.get('InstallConfigStore').get('Software').get('Valve').get('Steam').get('CompatToolMapping')
Expand Down

0 comments on commit 7f15485

Please sign in to comment.