Skip to content

Instantly share code, notes, and snippets.

@Aetopia
Last active January 14, 2023 06:45
Show Gist options
  • Save Aetopia/4f1700e58f76b2a3704148aabc721136 to your computer and use it in GitHub Desktop.
Save Aetopia/4f1700e58f76b2a3704148aabc721136 to your computer and use it in GitHub Desktop.
Steam Related Functions
function Get-SteamPath {
# Get the Steam installation directory.
# MUICache
$muicache = "Registry::HKCR\Local Settings\Software\Microsoft\Windows\Shell\MuiCache"
$protocol = "Registry::HKCR\steam\Shell\Open\Command"
# MUICache
$steam = Split-Path (((Get-Item $MuiCache).Property | Where-Object {$PSItem -Like "*Steam*"} | Where-Object {(Get-ItemPropertyValue $MuiCache -Name $PSItem) -eq "Steam"}).TrimEnd(".FriendlyAppName"))
# Steam Browser Protocol
if ($null -eq $steam) {
$steam = Split-Path (((Get-ItemPropertyValue "$protocol" -Name "(Default)" -ErrorAction SilentlyContinue) -Split "--", 2, "SimpleMatch")[0]).Trim('"')
}
return $steam
}
function Get-SteamLibraryFolders {
# Get all Steam library folders.
$folders = @()
$libs = "$(Get-SteamPath)\config\libraryfolders.vdf"
foreach ($l in (Get-Content "$libs" -Encoding "UTF8")) {
$l = $l.Trim()
if ($l.StartsWith("`"path`"")) {
$folders += ($l -Split "`"path`"", 2, "SimpleMatch")[1].Trim().Trim("`"").Replace("\\", "\")
}
}
return $folders
}
function Get-SteamGameInstallDir ([string]$game) {
# Get the installation directory of a Steam game.
$folders = Get-SteamLibraryFolders
foreach ($f in $folders) {
$dir = "$f\steamapps\common\$game"
if (Test-Path $dir.ToLower()) {
return "$dir"
}
}
}
function Set-SteamGameLaunchOptions ([string]$gameid, [string]$launchopts) {
# Set the launch options of a Steam game.
$steampath = Get-SteamPath
$users = (Get-ChildItem "$steampath\userdata").Name
$tab = "`t" * 5
$game = $false
# Shutdown Steam if its running or else Steam will not read the changes written to "localconfig.vdf".
if ($null -ne (Get-Process "Steam" -ErrorAction SilentlyContinue)) {
Invoke-Expression "& `"$steampath\steam.exe`" -shutdown"
}
:master foreach ($user in $users) {
$p = "$steampath\userdata\$user\config\localconfig.vdf"
$cr = Get-Content "$p" -Encoding "UTF8"
$cw = [System.Collections.ArrayList]$cr
# The file will be split at this line.
$n = $cw.IndexOf("$tab`"$gameid`"") + 1
foreach ($l in $cr) {
$l = $l.TrimEnd()
if ($l.StartsWith($tab)) {
if ($game) {
# Remove any existing launch options.
if ($l.Trim().StartsWith("`"LaunchOptions`"")) {
$cw.RemoveAt($cw.IndexOf($l))
}
}
if ($l.Trim() -eq "`"$gameid`"") { $game = $true }
elseif ($game -and ($l -eq "$tab}")) { break master }
}
}
}
Set-Content "$p" -Value ($cw[0..$n] + @("$tab`t`"LaunchOptions`" `"$launchopts`"") + $cw[($n + 1)..$cw.Count]) -Encoding "UTF8"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment