Skip to content

Commit

Permalink
SteamTinkerLaunch: Refactor get_tool
Browse files Browse the repository at this point in the history
  • Loading branch information
sonic2kk committed Aug 20, 2024
1 parent 5a0d507 commit adf6e5e
Showing 1 changed file with 73 additions and 69 deletions.
142 changes: 73 additions & 69 deletions pupgui2/resources/ctmods/ctmod_steamtinkerlaunch.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def __init__(self, main_window = None, allow_git=False):
proc_prefix + ['cat', '/etc/lsb-release', '/etc/os-release'],
universal_newlines=True,
stdout=subprocess.PIPE
).stdout.strip().lower()
).stdout.strip().lower()

def get_download_canceled(self):
return self.p_download_canceled
Expand Down Expand Up @@ -240,14 +240,15 @@ def fetch_releases(self, count=100, page=1):

return branches

def get_tool(self, version, install_dir, temp_dir):
def get_tool(self, version: str, install_dir: str, temp_dir: str) -> bool:
"""
Download and install the compatibility tool
Return Type: bool
"""

has_existing_install = False

## Check for and handle any external SteamTinkerLaunch installations
# If there's an existing STL installation that isn't installed by ProtonUp-Qt, ask the user if they still want to install
has_external_install = get_external_steamtinkerlaunch_intall(os.path.join(install_dir, 'SteamTinkerLaunch'))
if os.path.exists(has_external_install):
Expand Down Expand Up @@ -276,8 +277,8 @@ def get_tool(self, version, install_dir, temp_dir):
print('User opted to not continue installing SteamTinkerLaunch. Aborting...')
return False

## Download SteamTinkerLaunch tool
print('Downloading SteamTinkerLaunch...')

data = self.__fetch_github_data(version)
if not data or 'download' not in data:
return False
Expand All @@ -289,6 +290,7 @@ def get_tool(self, version, install_dir, temp_dir):
if not self.__download(url=data['download'], destination=destination):
return False

## Extract SteamTinkerLaunnch
with tarfile.open(destination, "r:gz") as tar:
print('Extracting SteamTinkerLaunch...')
if os.path.exists(constants.STEAM_STL_INSTALL_PATH) and len(os.listdir(constants.STEAM_STL_INSTALL_PATH)) > 0:
Expand All @@ -303,61 +305,62 @@ def get_tool(self, version, install_dir, temp_dir):

tarname = tar.getnames()[0]

# Location of SteamTinkerLaunch script to add to path later
old_stl_path = os.path.join(constants.STEAM_STL_INSTALL_PATH, tarname)
# Location of SteamTinkerLaunch script to add to path later
old_stl_path = os.path.join(constants.STEAM_STL_INSTALL_PATH, tarname)
stl_path = os.path.join(constants.STEAM_STL_INSTALL_PATH, 'prefix')

# Rename folder ~/stl/<tarname> to ~/stl/prefix
os.rename(old_stl_path, stl_path)
os.chdir(stl_path)

# ProtonUp-Qt Flatpak: Run STL on host system
stl_proc_prefix: list[str] = ['flatpak-spawn', '--host'] if constants.IS_FLATPAK else []

## Run any required SteamTinkerLaunch installation steps (e.g. execute script on SteamOS, configure locale, etc)
# If on Steam Deck, run script for initial Steam Deck config
# On Steam Deck, STL is installed to "/home/deck/stl/prefix"
self.__set_download_progress_percent(99.5) # 99.5 installing tool
print('Setting up SteamTinkerLaunch...')
if "steamos" in self.distinfo:
subprocess.run(['chmod', '+x', 'steamtinkerlaunch'])
subprocess.run(stl_proc_prefix + ['./steamtinkerlaunch'])

# Change location of STL script to add to path as this is different on Steam Deck
stl_path = os.path.join(constants.STEAM_STL_INSTALL_PATH, 'prefix')

# Rename folder ~/stl/<tarname> to ~/stl/prefix
os.rename(old_stl_path, stl_path)
# Change to STL prefix dir on Steam Deck so that the compatibility tool is symlinked correctly
os.chdir(stl_path)

# ProtonUp-Qt Flatpak: Run STL on host system
stl_proc_prefix = ['flatpak-spawn', '--host'] if constants.IS_FLATPAK else []

# If on Steam Deck, run script for initial Steam Deck config
# On Steam Deck, STL is installed to "/home/deck/stl/prefix"
self.__set_download_progress_percent(99.5) # 99.5 installing tool
print('Setting up SteamTinkerLaunch...')
if "steamos" in self.distinfo:
subprocess.run(['chmod', '+x', 'steamtinkerlaunch'])
subprocess.run(stl_proc_prefix + ['./steamtinkerlaunch'])

# Change location of STL script to add to path as this is different on Steam Deck
stl_path = os.path.join(constants.STEAM_STL_INSTALL_PATH, 'prefix')

# Change to STL prefix dir on Steam Deck so that the compatibility tool is symlinked correctly
os.chdir(stl_path)
else:
# Get STL language and default to 'en_US' if the language is not available
# This step should not be necessary on Steam Deck
syslang = locale.getdefaultlocale()[0] or 'en_US'
stl_langs = {
'de_DE': 'german.txt',
'en_GB': 'englishUK.txt',
'en_US': 'english.txt',
'fr_FR': 'french.txt',
'il_IL': 'italian.txt',
'nl_NL': 'dutch.txt',
'pl_PL': 'polish.txt',
'ru_RU': 'russian.txt',
'zh_CN': 'chinese.txt',
}
stl_lang = stl_langs[syslang] if syslang in stl_langs else stl_langs['en_US']
stl_lang_path = os.path.join(constants.STEAM_STL_CONFIG_PATH, 'lang')

# Generate config file structure and copy relevant lang file
os.makedirs(stl_lang_path, exist_ok=True)
if not os.path.isfile(os.path.join(stl_lang_path, 'english.txt')):
shutil.copyfile('lang/english.txt', os.path.join(stl_lang_path, 'english.txt'))
if not os.path.isfile(os.path.join(stl_lang_path, stl_lang)):
shutil.copyfile(f'lang/{stl_lang}', os.path.join(stl_lang_path, stl_lang))
subprocess.run(stl_proc_prefix + ['./steamtinkerlaunch', f'lang={stl_lang.replace(".txt", "")}'])
self.__stl_config_change_language(constants.STEAM_STL_CONFIG_PATH, stl_lang)
else:
# Get STL language and default to 'en_US' if the language is not available
# This step should not be necessary on Steam Deck
syslang: str = locale.getdefaultlocale()[0] or 'en_US'
stl_langs: dict[str, str] = {
'de_DE': 'german.txt',
'en_GB': 'englishUK.txt',
'en_US': 'english.txt',
'fr_FR': 'french.txt',
'il_IL': 'italian.txt',
'nl_NL': 'dutch.txt',
'pl_PL': 'polish.txt',
'ru_RU': 'russian.txt',
'zh_CN': 'chinese.txt',
}
stl_lang = stl_langs[syslang] if syslang in stl_langs else stl_langs['en_US']
stl_lang_path = os.path.join(constants.STEAM_STL_CONFIG_PATH, 'lang')

## Generate config file structure and copy relevant lang file
os.makedirs(stl_lang_path, exist_ok=True)
if not os.path.isfile(os.path.join(stl_lang_path, 'english.txt')):
shutil.copyfile('lang/english.txt', os.path.join(stl_lang_path, 'english.txt'))
if not os.path.isfile(os.path.join(stl_lang_path, stl_lang)):
shutil.copyfile(f'lang/{stl_lang}', os.path.join(stl_lang_path, stl_lang))
subprocess.run(stl_proc_prefix + ['./steamtinkerlaunch', f'lang={stl_lang.replace(".txt", "")}'])
self.__stl_config_change_language(constants.STEAM_STL_CONFIG_PATH, stl_lang)

# Add SteamTinkerLaunch to all available shell paths (native Linux)
# Dialog warning - Only warn on new installs or overwritten manual installs
# For background see this issue: https://github.com/DavidoTek/ProtonUp-Qt/issues/127
should_show_shellmod_dialog = has_external_install or not has_existing_install
should_show_shellmod_dialog = bool(has_external_install or not has_existing_install)
should_add_path = True

# Checkbox is only shown to users who have ProtonUp-Qt Advanced mode enalbed
Expand All @@ -378,7 +381,7 @@ def get_tool(self, version, install_dir, temp_dir):
should_add_path = False # Shouldn't matter since installation will end here, but setting for completeness
remove_steamtinkerlaunch(remove_config=False, ctmod_object=self) # shouldn't need compat_folder arg - (compat_folder=os.path.join(install_dir, 'SteamTinkerLaunch'))
self.__set_download_progress_percent(-2)
return
return False
elif not shellmod_msgbox_result.is_checked and shellmod_msgbox_result.button_clicked == MsgBoxResult.BUTTON_OK:
# Continue installation but skip adding to PATH
print('User asked not to add SteamTinkerLaunch to shell paths, skipping...')
Expand All @@ -400,31 +403,32 @@ def get_tool(self, version, install_dir, temp_dir):
for shell_file in present_shell_files:
with open(shell_file, 'r+') as mfile:
stl_already_in_path = constants.STEAM_STL_INSTALL_PATH in [line for line in mfile.readlines()]
if not stl_already_in_path:
# Add Fish user path, preserving any existing paths
if 'fish' in mfile.name:
mfile.seek(0)
curr_fish_user_paths = get_fish_user_paths(mfile)
curr_fish_user_paths.insert(0, stl_path)
updated_fish_user_paths = '\\x1e'.join(curr_fish_user_paths)
pup_stl_path_line = f'SETUVAR fish_user_paths:{updated_fish_user_paths}'

mfile.seek(0)
new_fish_contents = ''.join([line for line in mfile.readlines() if 'fish_user_paths:' not in line])
mfile.seek(0)
mfile.write(new_fish_contents)

mfile.write(f'\n{pup_stl_path_date}\n{pup_stl_path_line}\n')
if stl_already_in_path:
continue

# Add Fish user path, preserving any existing paths
if 'fish' in mfile.name:
mfile.seek(0)
curr_fish_user_paths = get_fish_user_paths(mfile)
curr_fish_user_paths.insert(0, stl_path)
updated_fish_user_paths = '\\x1e'.join(curr_fish_user_paths)
pup_stl_path_line = f'SETUVAR fish_user_paths:{updated_fish_user_paths}'

mfile.seek(0)
new_fish_contents = ''.join([line for line in mfile.readlines() if 'fish_user_paths:' not in line])
mfile.seek(0)
mfile.write(new_fish_contents)

mfile.write(f'\n{pup_stl_path_date}\n{pup_stl_path_line}\n')

# Install Compatibility Tool (Proton games)
print('Adding SteamTinkerLaunch as a compatibility tool...')
subprocess.run(stl_proc_prefix + ['./steamtinkerlaunch', 'compat', 'add'])

os.chdir(constants.HOME_DIR)

protondir = os.path.join(install_dir, 'SteamTinkerLaunch')

# We can't use the version arg to this method because we need to list the PROGVERS stored by the SteamTinkerLaunch script
protondir: str = os.path.join(install_dir, 'SteamTinkerLaunch')
if os.path.exists(protondir):
# Get PROGVERS from STL script
stl_filename = 'steamtinkerlaunch'
Expand Down

0 comments on commit adf6e5e

Please sign in to comment.