-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSteamCmd.psm1
133 lines (106 loc) · 4.75 KB
/
SteamCmd.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
Using module .\VdfDeserializer.psm1;
Class SteamCmd
{
hidden [ValidateNotNull()][System.IO.DirectoryInfo]$_SteamcmdFolder;
<#
.SYNOPSIS
Constructor for SteamCmd class
.DESCRIPTION
Creates a new instance of the SteamCmd class.
.PARAMETER SteamcmdFolder
Points to folder where steamcmd.exe is installed
.PARAMETER Force
Allows the class to download and install steamcmd.exe in the SteamcmdFolder, if missing.
.NOTES
The constructor verifies that either steamdcmd.exe is available in the folder.
In case it is not available, it should be allowed to -Force the installation or it will throw an exception.
The constructor itself will never install steamcmd.exe itself.
#>
SteamCmd([System.IO.DirectoryInfo]$SteamcmdFolder, [boolean]$Force)
{
$SteamcmdExe = Join-Path -Path $SteamcmdFolder -ChildPath "steamcmd.exe";
# Remember location if steamcmd.exe exists or can be forced to install
if( (Test-Path $SteamcmdExe -PathType Leaf) -or $Force ) {
$this._SteamcmdFolder = $SteamcmdFolder;
} else {
throw "Could not find " + $SteamcmdExe;
}
}
<#
.SYNOPSIS
Returns the path to steamcmd.exe
.DESCRIPTION
Returns the path to steamcmd.exe. If steamcmd isn't already installed, it will download it from steam
.NOTES
If steamcmd.exe is found, the function returns its the full path.
If not found, it will try to download it into the folder that was specified in the constructor
#>
[System.IO.FileInfo] GetSteamcmdExePath()
{
$SteamcmdExe = Join-Path -Path $this._SteamcmdFolder -ChildPath "steamcmd.exe";
if( Test-Path $SteamcmdExe -PathType Leaf) {
return $SteamcmdExe;
}
if( Test-Path $this._SteamcmdFolder -PathType Container ) {
throw "Could not install steamcmd.exe in " + $this._SteamcmdFolder + ". The specified folder already exists.";
}
$tmpFile = [System.IO.Path]::GetTempPath() + "steamcmd.zip";
Invoke-WebRequest -Uri https://steamcdn-a.akamaihd.net/client/installer/steamcmd.zip -OutFile $tmpFile;
try {
Expand-Archive -Path $tmpFile -DestinationPath $this._SteamcmdFolder.FullName -Force;
}
catch {
Remove-Item -Path $this._SteamcmdFolder.FullName -Confirm:$False;
Throw "Could not unzip " + $tmpFile + " into " + $this._SteamcmdFolder.FullName;
}
finally {
Remove-Item -Path $tmpFile -Confirm:$False;
}
if( Test-Path $SteamcmdExe -PathType Leaf) {
return $SteamcmdExe;
}
throw "Could not find " + $SteamcmdExe;
}
<#
.SYNOPSIS
Retrieves the app_info for the provided application id
.DESCRIPTION
Executes steamcmd.exe +app_info_print and returns the parsed VDF as a Hashtable
#>
[PSCustomObject] GetSteamAppInfo([string] $appId) {
if([string]::IsNullOrWhiteSpace($appId)) {
throw 'Missing value for parameter $appId';
}
$tmpFile = [System.IO.Path]::GetTempFileName();
[string]$steamCmd = $this.GetSteamcmdExePath().FullName + " +login anonymous +app_info_update 1 +app_info_print `"" + $appId + "`" +app_info_print `"" + $appId + "`" +quit > " + $tmpFile;
Invoke-Expression -Command $steamCmd;
$appInfoContent = Get-Content -Path $tmpFile -Raw;
$appInfoContent = $this.CleanAppInfoPrint($appId, $appInfoContent);
Remove-Item -Path $tmpFile;
$vdf = [VdfDeserializer]::new();
$appInfo = $vdf.Deserialize($appInfoContent);
return $appInfo;
}
[string] CleanAppInfoPrint([string] $appId, [string] $rawAppInfo) {
if([string]::IsNullOrWhiteSpace($appId)) {
throw 'Missing value for parameter $appId';
}
if([string]::IsNullOrWhiteSpace($rawAppInfo)) {
throw 'Missing value for parameter `$rawAppInfo';
}
[bool] $doCopy = $false;
[string] $match = 'AppID : ' + $appId;
[string[]]$lines = $rawAppInfo -split "`r`n";
[System.Text.StringBuilder]$sb = [System.Text.StringBuilder]::new();
foreach($line in $lines) {
if($line.StartsWith($match) ){
$doCopy = !$doCopy
} elseif($doCopy) {
$sb.AppendLine($line);
} elseif ($sb.Length -gt 0) {
return $sb.ToString();
}
}
return $null;
}
} ## Class SteamCmd